Browse Source

Allow for editing of attributes; save and load custom attributes

master
Fen Dweller 3 years ago
parent
commit
d3c6657766
2 changed files with 73 additions and 9 deletions
  1. +12
    -0
      macrovision.css
  2. +61
    -9
      macrovision.js

+ 12
- 0
macrovision.css View File

@@ -1120,4 +1120,16 @@ body.screenshot-mode .scroll-button {
.submit-button > i { .submit-button > i {
margin: 2px; margin: 2px;
text-align: center; text-align: center;
}

.attribute-label {
display: flex;
text-align: center;
user-select: none;
-webkit-user-select: none;
margin-top: 12px;
margin-bottom: 4px;
}

.attribute-edit-button {
} }

+ 61
- 9
macrovision.js View File

@@ -2331,6 +2331,7 @@ function configViewOptions(entity, view) {
if (val.editing) { if (val.editing) {
const name = document.createElement("input"); const name = document.createElement("input");
name.placeholder = "Enter name..."; name.placeholder = "Enter name...";
name.value = val.name;
holder.appendChild(name); holder.appendChild(name);


holder.addEventListener("keydown", (e) => { holder.addEventListener("keydown", (e) => {
@@ -2339,6 +2340,7 @@ function configViewOptions(entity, view) {


const input = document.createElement("input"); const input = document.createElement("input");
input.placeholder = "Enter measurement..."; input.placeholder = "Enter measurement...";
input.value = val.text;
holder.appendChild(input); holder.appendChild(input);


input.addEventListener("keydown", (e) => { input.addEventListener("keydown", (e) => {
@@ -2374,6 +2376,7 @@ function configViewOptions(entity, view) {
power: power, power: power,
type: unitType, type: unitType,
base: baseValue, base: baseValue,
custom: true
}; };


// since we might have changed unit types, we should // since we might have changed unit types, we should
@@ -2381,15 +2384,33 @@ function configViewOptions(entity, view) {
entity.currentView.units[key] = undefined; entity.currentView.units[key] = undefined;


defineAttributeGetters(entity.views[view]); defineAttributeGetters(entity.views[view]);

configViewOptions(entity, view); configViewOptions(entity, view);
updateSizes();
}); });
} else { } else {
const label = document.createElement("div"); const label = document.createElement("div");
label.classList.add("options-label");
label.classList.add("attribute-label");
label.innerText = val.name; label.innerText = val.name;


holder.appendChild(label); holder.appendChild(label);
const editButton = document.createElement("button");
editButton.classList.add("attribute-edit-button");
const editButtonIcon = document.createElement("i");
editButtonIcon.classList.add("fas");
editButtonIcon.classList.add("fa-edit");

editButton.addEventListener("click", e => {
entity.currentView.attributes[key] = {
name: val.name,
text: entity.currentView[key],
editing: true
}
configViewOptions(entity, view);
});

editButton.appendChild(editButtonIcon);
label.appendChild(editButton);


const row = document.createElement("div"); const row = document.createElement("div");
row.classList.add("options-row"); row.classList.add("options-row");
@@ -2506,8 +2527,9 @@ function configViewOptions(entity, view) {


customButton.addEventListener("click", e => { customButton.addEventListener("click", e => {
entity.currentView.attributes["custom" + (Object.keys(entity.currentView.attributes).length + 1)] = { entity.currentView.attributes["custom" + (Object.keys(entity.currentView.attributes).length + 1)] = {
name: "Custom",
editing: true
name: "",
text: "",
editing: true,
} }
configViewOptions(entity, view); configViewOptions(entity, view);
}); });
@@ -4934,7 +4956,7 @@ document.addEventListener("DOMContentLoaded", () => {
scenes["Empty"](); scenes["Empty"]();
} else { } else {
try { try {
const data = JSON.parse(b64DecodeUnicode(param));
const data = JSON.parse(b64DecodeUnicode(param), math.reviver);
if (data.entities === undefined) { if (data.entities === undefined) {
return; return;
} }
@@ -5935,7 +5957,7 @@ function loadScene(name = "default") {
} }
try { try {
const data = JSON.parse( const data = JSON.parse(
localStorage.getItem("macrovision-save-" + name)
localStorage.getItem("macrovision-save-" + name), math.reviver
); );
if (data === null) { if (data === null) {
console.error("Couldn't load " + name); console.error("Couldn't load " + name);
@@ -5987,7 +6009,7 @@ function exportScene() {
.filter(([key, entity]) => entity.ephemeral !== true) .filter(([key, entity]) => entity.ephemeral !== true)
.forEach(([key, entity]) => { .forEach(([key, entity]) => {
const element = document.querySelector("#entity-" + key); const element = document.querySelector("#entity-" + key);
results.entities.push({
const entityData = {
name: entity.identifier, name: entity.identifier,
customName: entity.name, customName: entity.name,
scale: entity.scale, scale: entity.scale,
@@ -5999,7 +6021,22 @@ function exportScene() {
y: element.dataset.y, y: element.dataset.y,
priority: entity.priority, priority: entity.priority,
brightness: entity.brightness, brightness: entity.brightness,
};

entityData.views = {};

Object.entries(entity.views).forEach(([viewId, viewData]) => {
Object.entries(viewData.attributes).forEach(([attrId, attrData]) => {
if (attrData.custom) {
if (entityData.views[viewId] === undefined) {
entityData.views[viewId] = {};
}
entityData.views[viewId][attrId] = attrData;
}
});
}); });

results.entities.push(entityData);
}); });


const unit = document.querySelector("#options-height-unit").value; const unit = document.querySelector("#options-height-unit").value;
@@ -6076,7 +6113,7 @@ function pasteScene() {
navigator.clipboard navigator.clipboard
.readText() .readText()
.then((text) => { .then((text) => {
const data = JSON.parse(text);
const data = JSON.parse(text, math.reviver);
if (data.entities === undefined) { if (data.entities === undefined) {
return; return;
} }
@@ -6086,7 +6123,7 @@ function pasteScene() {


importScene(data); importScene(data);
}) })
.catch((err) => alert(err));
.catch((err) => { toast("Something went wrong when importing: " + err), console.error(err) });
} catch (err) { } catch (err) {
console.error(err); console.error(err);


@@ -6154,6 +6191,11 @@ const migrationDefs = [
entity.flipped = false; entity.flipped = false;
}); });
} }
/*
Migration: 5 -> 6

Entities can now have custom attributes
*/
]; ];


function migrateScene(data) { function migrateScene(data) {
@@ -6184,6 +6226,16 @@ function importScene(data) {
entity.priority = entityInfo.priority; entity.priority = entityInfo.priority;
entity.brightness = entityInfo.brightness; entity.brightness = entityInfo.brightness;
entity.form = entityInfo.form; entity.form = entityInfo.form;

Object.entries(entityInfo.views).forEach(([viewId, viewData]) => {
if (entityInfo.views[viewId] !== undefined) {
Object.entries(entityInfo.views[viewId]).forEach(([attrId, attrData]) => {
entity.views[viewId].attributes[attrId] = attrData;
});
}
});

Object.keys(entityInfo.views).forEach(key => defineAttributeGetters(entity.views[key]));
displayEntity(entity, entityInfo.view, entityInfo.x, entityInfo.y); displayEntity(entity, entityInfo.view, entityInfo.x, entityInfo.y);
}); });




Loading…
Cancel
Save