From 5ce1dc3010142bc1c028a8375a45e1592be04a4c Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Thu, 13 Feb 2020 21:54:15 -0500 Subject: [PATCH] Add export/import. Remove generic person silhouette --- macrovision.html | 3 ++ macrovision.js | 86 +++++++++++++++++++++++++++++++++++++++++++ presets/characters.js | 6 +-- 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/macrovision.html b/macrovision.html index f892b4c6..1829010c 100644 --- a/macrovision.html +++ b/macrovision.html @@ -29,6 +29,9 @@ + + + diff --git a/macrovision.js b/macrovision.js index deb1d525..19b9f913 100644 --- a/macrovision.js +++ b/macrovision.js @@ -210,6 +210,7 @@ function drawScale() { function makeEntity(name, author, views) { const entityTemplate = { name: name, + identifier: name, author: author, scale: 1, views: views, @@ -814,6 +815,29 @@ document.addEventListener("DOMContentLoaded", () => { altHeld = false; } }); + + document.addEventListener("paste", e => { + try { + const data = JSON.parse(e.clipboardData.getData("text")); + console.log(data) + if (data.entities === undefined) { + return; + } + if (data.world === undefined) { + return; + } + + importScene(data); + } catch(err) { + console.error(err); + + // probably wasn't valid data + } + }); + + document.querySelector("#menu-export").addEventListener("click", e => { + exportScene(); + }); }); function prepareEntities() { @@ -959,3 +983,65 @@ function setWorldHeight(oldHeight, newHeight) { updateSizes(); } + +function exportScene() { + const results = {}; + + results.entities = []; + + Object.entries(entities).forEach(([key, entity]) => { + const element = document.querySelector("#entity-" + key); + results.entities.push({ + name: entity.identifier, + scale: entity.scale, + view: entity.view, + x: element.dataset.x, + y: element.dataset.y + }); + }); + + const unit = document.querySelector("#options-height-unit").value; + results.world = { + height: config.height.toNumber(unit), + unit: unit + } + + navigator.clipboard.writeText(JSON.stringify(results)) + + alert("Scene copied to clipboard. Paste text into the page to load the scene."); +} + +// TODO - don't just search through every single entity +// probably just have a way to do lookups directly + +function findEntity(name) { + const groups = Object.values(availableEntities); + for (let i = 0; i < groups.length; i++) { + const entityGroup = groups[i]; + for (let j = 0; j < entityGroup.length; j++) { + const candidate = entityGroup[j]; + if (candidate.name === name) { + return candidate; + } + } + } + + + return null; +} + +function importScene(data) { + removeAllEntities(); + + data.entities.forEach(entityInfo => { + console.log(findEntity(entityInfo.name)) + const entity = findEntity(entityInfo.name).constructor(); + entity.scale = entityInfo.scale + displayEntity(entity, entityInfo.view, entityInfo.x, entityInfo.y); + }); + + config.height = math.unit(data.world.height, data.world.unit); + document.querySelector("#options-height-unit").value = data.world.unit; + + updateSizes(); +} \ No newline at end of file diff --git a/presets/characters.js b/presets/characters.js index 1c6d6f37..05f17711 100644 --- a/presets/characters.js +++ b/presets/characters.js @@ -1195,7 +1195,7 @@ function makeMan() { } }; - return makeEntity("Man", "Fen", views); + return makeEntity("Normal Man", "Fen", views); } characterMakers["North"] = () => { @@ -2447,10 +2447,6 @@ function makeCharacters() { name: "Sefer", constructor: makeSefer }); - results.push({ - name: "Normal man", - constructor: makeMan - }); Object.entries(characterMakers).forEach(([key, value]) => { results.push({