Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save and load map #102

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ <h1>7 Days to Die Map Renderer 1.0</h1>
<div id="controller">
<p><input id="files" type="file" multiple webkitdirectory /></p>
<p>
<button data-show-dialog-for="save_prompt">Save</button>
<button data-show-dialog-for="load_prompt">Load</button>
<button id="clear_map" title="Clear all world files">Clear</button>
<button id="download">Download</button>
</p>
Expand Down Expand Up @@ -290,5 +292,32 @@ <h3>Bundled Worlds</h3>
<p>The rest of files are ignored.</p>
</div>
</div>

<dialog id="save_prompt">
<h2>Save Map</h2>
<form method="dialog">
<p>
<label>Map name: <input id="save_map_name" type="text" minlength="1" /></label>
</p>
<div>
<button id="save_map" type="submit" name="save" value="">Save</button>
<button type="submit" value="">Cancel</button>
</div>
</form>
</dialog>

<dialog id="load_prompt">
<h2>Load Map</h2>
<form method="dialog">
<p>
Choose a map:
<select id="load_map_name">
<option value="" selected>Choose</option>
<option value="aaa">aaa</option>
</select>
</p>
<div><button type="submit" value="">Cancel</button></div>
</form>
</dialog>
</body>
</html>
49 changes: 49 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ function main() {
dialogButtons.init();
rememberValue.init();
minMaxInputs.init();
initSaveDialog();
initLoadDialog();

component("download").addEventListener("click", () => {
const mapName = component("map_name", HTMLInputElement).value || "7dtd-map";
Expand All @@ -37,6 +39,14 @@ function main() {
updateMapRightMargin();
window.addEventListener("resize", updateMapRightMargin);

const loadMapName = component("load_map_name", HTMLSelectElement);
loadMapName.addEventListener("change", () => {
if (loadMapName.value === "") return;
const dialog = loadMapName.closest("dialog");
if (!(dialog instanceof HTMLDialogElement)) throw Error(`Unexpected state`);
dialog.close(loadMapName.value);
});

const loadingHandler = new LoadingHandler({
indicator: component("loading_indicator"),
disableTargets() {
Expand Down Expand Up @@ -136,6 +146,45 @@ function main() {
fileHandler.initialize().catch(printError);
}

function initSaveDialog() {
const mapName = component("map_name", HTMLInputElement);
const saveMapName = component("save_map_name", HTMLInputElement);
const saveMap = component("save_map", HTMLButtonElement);
const saveDialog = component("save_prompt", HTMLDialogElement);

// Bind map name
mapName.addEventListener("input", () => {
if (saveMapName.value !== mapName.value) {
saveMapName.value = mapName.value;
saveMapName.dispatchEvent(new Event("input", { bubbles: true }));
}
saveMap.value = mapName.value;
});
saveMapName.addEventListener("input", () => {
if (mapName.value !== saveMapName.value) {
mapName.value = saveMapName.value;
mapName.dispatchEvent(new Event("input", { bubbles: true }));
}
saveMap.value = saveMapName.value;
});

saveDialog.addEventListener("close", () => {
console.debug("Save dialog closed", saveDialog.returnValue);
});
}

function initLoadDialog() {
const loadMapName = component("load_map_name", HTMLSelectElement);
const loadDialog = component("load_prompt", HTMLDialogElement);
loadMapName.addEventListener("change", () => {
loadDialog.close(loadMapName.value);
});
loadDialog.addEventListener("close", () => {
loadMapName.value = "";
console.debug("Load dialog closed", loadDialog.returnValue);
});
}

function prefabLi(prefab: HighlightedPrefab) {
const li = document.createElement("li");
li.innerHTML = [
Expand Down