Update elementsManager.js

This commit is contained in:
GGodPL 2023-08-03 12:24:45 +02:00 committed by GitHub
parent 63e722baa3
commit 7d229e094c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 83 additions and 19 deletions

View File

@ -257,7 +257,8 @@ if (enabledMods.includes("mods/betterMenuScreens.js")) {
// ugly way of doing it but probably works // ugly way of doing it but probably works
const checkType = (key, value) => { const checkType = (key, value) => {
if (key == "behavior" && (typeof value == "function" || value instanceof Array)) return true; if (key == "behavior" && (typeof value == "function" || (value instanceof Array && value.filter(e => e instanceof Array && e.filter(s => typeof s == "string").length == e.length).length == value.length))) return true;
else if (key == "behavior") return false;
if (["darkText", "hidden", "insulate", "noMix", "isFood", "forceAutoGen", "customColor", "ignoreAir", "excludeRandom", "burning", "flipX", "flipY", "flippableX", "flippableY"].includes(key) && typeof value != "boolean") return false; if (["darkText", "hidden", "insulate", "noMix", "isFood", "forceAutoGen", "customColor", "ignoreAir", "excludeRandom", "burning", "flipX", "flipY", "flippableX", "flippableY"].includes(key) && typeof value != "boolean") return false;
if (["name", "category", "desc", "alias", "seed", "baby", "state", "stateHigh", "stateHighName", "stateHighColor", "stateLow", "stateLowNmae", "stateLowColor"].includes(key) && typeof value != "string") return false; if (["name", "category", "desc", "alias", "seed", "baby", "state", "stateHigh", "stateHighName", "stateHighColor", "stateLow", "stateLowNmae", "stateLowColor"].includes(key) && typeof value != "string") return false;
if (["id", "burn", "burnTime", "stateHighColorMultiplier", "stateLowColorMutliplier", "temp", "tempHigh", "extraTempHigh", "tempLow", "extraTempLow"].includes(key) && typeof value != "number") return false; if (["id", "burn", "burnTime", "stateHighColorMultiplier", "stateLowColorMutliplier", "temp", "tempHigh", "extraTempHigh", "tempLow", "extraTempLow"].includes(key) && typeof value != "number") return false;
@ -272,7 +273,7 @@ if (enabledMods.includes("mods/betterMenuScreens.js")) {
const newElements = Storage.get("elements", []); const newElements = Storage.get("elements", []);
for (const element of newElements) { for (const element of newElements) {
const element_ = element; const element_ = element;
element_["behavior"] = behaviors[element_["behavior"]]; if (Object.keys(behaviors).includes(element_["behavior"])) element_["behavior"] = behaviors[element_["behavior"]];
elements[element.name] = {}; elements[element.name] = {};
// elements[element.name] = element_; // elements[element.name] = element_;
for (const key of Object.keys(element_)) { for (const key of Object.keys(element_)) {
@ -294,23 +295,50 @@ if (enabledMods.includes("mods/betterMenuScreens.js")) {
} }
} }
const applyChange = (property, value) => { const saveChanges = () => {
const element = Storage.get("currentElement"); const element = Storage.get("currentElement");
if (element && elements[element]) const changes = Storage.get("tempChanges", []);
elements[element][property] = value; if (Storage.get("elements", []).find(a => a.name == element)) {
let changes = Storage.get("changes", []); const elements_ = Storage.get("elements", []);
let a; for (const change of changes) {
if (a = changes.find(c => c.element == element)) { elements_.find(a => a.name == element)[change.property] = change.value;
a.changes[property] = value; }
Storage.set("elements", elements_);
} else { } else {
let c = {}; const permChanges = Storage.get("changes", []);
c[property] = value; for (const change of changes) {
changes.push({ let a;
element, if (a = permChanges.find(c => c.element == element)) {
changes: c a.changes[change.property] = change.value;
}) } else {
let c = {};
c[change.property] = change.value;
permChanges.push({
element,
changes: c
})
}
}
Storage.set("changes", permChanges);
}
}
const applyChange = (property, value) => {
// if (element && elements[element])
// elements[element][property] = value;
const element = Storage.get("currentElement");
const changes = Storage.get("tempChanges", []);
changes.push({property, value});
Storage.set("tempChanges", changes);
const nullish = {
string: "",
boolean: false,
number: 0,
array: [],
}
if (elements[element][property] == value || value == nullish[value instanceof Array ? "array" : typeof value]) {
Storage.filter("tempChanges", e => e.property != property);
} }
Storage.set("changes", changes);
} }
const elementsManagerLoader = () => { const elementsManagerLoader = () => {
@ -503,16 +531,23 @@ if (enabledMods.includes("mods/betterMenuScreens.js")) {
dropdown.onchange = (ev) => { dropdown.onchange = (ev) => {
if (ev.target.value == "CUSTOM") { if (ev.target.value == "CUSTOM") {
document.getElementById(id + "/textInput").style.display = ""; document.getElementById(id + "/textInput").style.display = "";
} else if (ev.target.value == "NONE") {
applyChange(prop.name, null);
} else { } else {
document.getElementById(id + "/textInput").style.display = "none"; document.getElementById(id + "/textInput").style.display = "none";
applyChange(prop.name, ev.target.value); applyChange(prop.name, ev.target.value);
} }
} }
const noneOption = document.createElement("option");
noneOption.value = "NONE";
noneOption.id = id + "/option/none"
noneOption.innerText = "None";
dropdown.appendChild(noneOption);
const el = createInput("text", false, id + "/textInput"); const el = createInput("text", false, id + "/textInput");
el.style.display = "none"; el.style.display = "none";
el.onchange = (ev) => { el.onchange = (ev) => {
if (document.getElementById(id).value == "CUSTOM") { if (document.getElementById(id).value == "CUSTOM") {
applyChange(prop.name, ev.target.value.split(";")); applyChange(prop.name, ev.target.value.split(";").map(e => e.split(",")));
} else { } else {
ev.target.style.display = "none"; ev.target.style.display = "none";
} }
@ -533,6 +568,17 @@ if (enabledMods.includes("mods/betterMenuScreens.js")) {
nodes.push(category); nodes.push(category);
} }
const saveButton = span("Save Changes");
saveButton.className = "createButton";
saveButton.onclick = () => {
saveChanges();
Storage.remove("tempChanges");
closeMenu();
alert("Changes successfully applied");
}
nodes.push(br(), saveButton)
new MenuScreen() new MenuScreen()
.setTitle("Element Manager") .setTitle("Element Manager")
.setCloseButtonText("<") .setCloseButtonText("<")
@ -612,7 +658,7 @@ if (enabledMods.includes("mods/betterMenuScreens.js")) {
el.onchange = (ev) => { el.onchange = (ev) => {
if (document.getElementById(id).value == "CUSTOM") { if (document.getElementById(id).value == "CUSTOM") {
const elementData = Storage.get("newElement", {}); const elementData = Storage.get("newElement", {});
elementData[prop.name] = ev.target.value.split(";"); elementData[prop.name] = ev.target.value.split(";").map(e => e.split(","));
Storage.set("newElement", elementData); Storage.set("newElement", elementData);
} else { } else {
ev.target.style.display = "none"; ev.target.style.display = "none";
@ -885,6 +931,7 @@ if (enabledMods.includes("mods/betterMenuScreens.js")) {
show: false, show: false,
loader: elementManagerLoader, loader: elementManagerLoader,
preOpen: () => { preOpen: () => {
Storage.remove("tempChanges");
const currentElement = Storage.get("currentElement"); const currentElement = Storage.get("currentElement");
if (!currentElement) return closeMenu(); if (!currentElement) return closeMenu();
const element = elements[currentElement]; const element = elements[currentElement];
@ -936,6 +983,10 @@ if (enabledMods.includes("mods/betterMenuScreens.js")) {
} }
} else if (prop.name == "name") { } else if (prop.name == "name") {
el.setAttribute("value", currentElement); el.setAttribute("value", currentElement);
} else if (prop.name == "behavior") {
console.log(element[prop.name], element);
document.getElementById(id + "/option/none").selected = true;
document.getElementById(id + "/textInput").style.display = "none";
} else { } else {
const default_ = { const default_ = {
string: "none", string: "none",
@ -953,8 +1004,21 @@ if (enabledMods.includes("mods/betterMenuScreens.js")) {
} }
} }
}, },
close: () => {
if (!Storage.get("tempChanges") || !Storage.get("tempChanges", []).length || confirm("Are you sure you want to close the menu without saving the changes?")) {
const menuParent = document.getElementById("elementManagerParent");
menuParent.style.display = "none";
Storage.remove("tempChanges");
Storage.remove("noClose");
} else {
Storage.set("noClose", true);
}
},
onClose: () => { onClose: () => {
openMenu("elementsManager"); if (!Storage.get("noClose")) {
showingMenu = false;
openMenu("elementsManager", true);
}
} }
} }