浏览代码

Add basic saving/loading

tags/v0.1.0
Fen Dweller 5 年前
父节点
当前提交
16106081d6
共有 2 个文件被更改,包括 38 次插入2 次删除
  1. +3
    -1
      macrovision.html
  2. +35
    -1
      macrovision.js

+ 3
- 1
macrovision.html 查看文件

@@ -30,7 +30,9 @@
<button id="menu-order-height">Order by height</button>
</span>
<span class="menubar-group">
<button id="menu-export">Export Scene</button>
<button id="menu-export">Export</button>
<button id="menu-save">Save</button>
<button id="menu-load">Load</button>
</span>
<span class="menubar-group" id="spawners">



+ 35
- 1
macrovision.js 查看文件

@@ -836,7 +836,15 @@ document.addEventListener("DOMContentLoaded", () => {
});

document.querySelector("#menu-export").addEventListener("click", e => {
exportScene();
copyScene();
});

document.querySelector("#menu-save").addEventListener("click", e => {
saveScene();
});

document.querySelector("#menu-load").addEventListener("click", e => {
loadScene();
});
});

@@ -984,6 +992,26 @@ function setWorldHeight(oldHeight, newHeight) {
updateSizes();
}

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

function saveScene() {
try {
const string = JSON.stringify(exportScene());
localStorage.setItem("macrovision-save", string);
} 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.")
console.error(err);
}
}

function exportScene() {
const results = {};

@@ -1006,6 +1034,12 @@ function exportScene() {
unit: unit
}

return results;
}

function copyScene() {
const results = exportScene();

navigator.clipboard.writeText(JSON.stringify(results))

alert("Scene copied to clipboard. Paste text into the page to load the scene.");


正在加载...
取消
保存