From 8915ad49fb14d0050e357642437c0200b8f1ba60 Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Wed, 22 Apr 2020 17:14:56 -0400 Subject: [PATCH] Add basic custom entity support You can paste an image into the window to get a temporary entity. --- macrovision.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/macrovision.js b/macrovision.js index 77801769..63e1ce41 100644 --- a/macrovision.js +++ b/macrovision.js @@ -1845,11 +1845,77 @@ document.addEventListener("DOMContentLoaded", () => { document.querySelector("#menu-load-autosave").addEventListener("click", e => { loadScene("autosave"); }); + + document.addEventListener("paste", e => { + + let index = 0; + let item = null; + let found = false; + + for (; index < e.clipboardData.items.length; index++) { + item = e.clipboardData.items[index]; + if (item.type == "image/png") { + found = true; + break; + } + } + + if (!found) { + return; + } + + console.log(item) + console.log(item.type) + + let url = null; + + const file = item.getAsFile(); + + file.arrayBuffer().then(buf => { + arr = new Uint8Array(buf); + blob = new Blob([arr], {type: file.type }); + url = window.URL.createObjectURL(blob) + makeCustomEntity(url); + }); + + + }); clearEntityOptions(); clearViewOptions(); clearAttribution(); }); +function makeCustomEntity(url) { + const maker = createEntityMaker( + { + name: "Custom Entity" + }, + { + custom: { + attributes: { + height: { + name: "Height", + power: 1, + type: "length", + base: math.unit(6, "feet") + } + }, + image: { + source: url + }, + name: "Image", + info: {}, + rename: false + } + }, + [] + ); + + const entity = maker.constructor(); + + entity.ephemeral = true; + displayEntity(entity, "custom", 0.5, 0.5, true, true); +} function prepareEntities() { availableEntities["buildings"] = makeBuildings(); availableEntities["characters"] = makeCharacters(); @@ -2082,7 +2148,7 @@ function exportScene() { results.entities = []; - Object.entries(entities).forEach(([key, entity]) => { + Object.entries(entities).filter(([key, entity]) => entity.ephemeral !== true).forEach(([key, entity]) => { const element = document.querySelector("#entity-" + key); results.entities.push({ name: entity.identifier,