Bladeren bron

Autosave the scene when closing and reopen it when loading

tags/v0.1.0
Fen Dweller 6 jaren geleden
bovenliggende
commit
8b57813bca
1 gewijzigde bestanden met toevoegingen van 25 en 6 verwijderingen
  1. +25
    -6
      macrovision.js

+ 25
- 6
macrovision.js Bestand weergeven

@@ -1176,6 +1176,7 @@ document.addEventListener("DOMContentLoaded", () => {
prepareMenu(); prepareMenu();
prepareEntities(); prepareEntities();


window.addEventListener("unload", () => saveScene("autosave"));
document.querySelector("#options-selected-entity").addEventListener("input", e => { document.querySelector("#options-selected-entity").addEventListener("input", e => {
if (e.target.value == "none") { if (e.target.value == "none") {
deselect() deselect()
@@ -1353,8 +1354,14 @@ document.addEventListener("DOMContentLoaded", () => {


param = new URL(window.location.href).searchParams.get("scene"); param = new URL(window.location.href).searchParams.get("scene");


if (param === null)
scenes["Default"]();
if (param === null) {
if (loadScene("autosave")) {
deleteScene("autosave");
} else {
scenes["Default"]();
}
}
else { else {
try { try {
const data = JSON.parse(b64DecodeUnicode(param)); const data = JSON.parse(b64DecodeUnicode(param));
@@ -1739,26 +1746,38 @@ function setWorldHeight(oldHeight, newHeight) {
updateSizes(); updateSizes();
} }


function loadScene() {
function loadScene(name="default") {
try { try {
const data = JSON.parse(localStorage.getItem("macrovision-save"));
const data = JSON.parse(localStorage.getItem("macrovision-save-" + name));
if (data === null) {
return false;
}
importScene(data); importScene(data);
return true;
} catch (err) { } catch (err) {
alert("Something went wrong while loading (maybe you didn't have anything saved. Check the F12 console for the error.") alert("Something went wrong while loading (maybe you didn't have anything saved. Check the F12 console for the error.")
console.error(err); console.error(err);
return false;
} }
} }


function saveScene() {
function saveScene(name="default") {
try { try {
const string = JSON.stringify(exportScene()); const string = JSON.stringify(exportScene());
localStorage.setItem("macrovision-save", string);
localStorage.setItem("macrovision-save-" + name, string);
} catch (err) { } catch (err) {
alert("Something went wrong while saving (maybe I don't have localStorage permissions, or exporting failed). Check the F12 console for the error.") alert("Something went wrong while saving (maybe I don't have localStorage permissions, or exporting failed). Check the F12 console for the error.")
console.error(err); console.error(err);
} }
} }


function deleteScene(name="default") {
try {
localStorage.removeItem("macrovision-save-" + name)
} catch(err) {
console.error(err);
}
}
function exportScene() { function exportScene() {
const results = {}; const results = {};




Laden…
Annuleren
Opslaan