diff --git a/index.css b/index.css index 501c99ee..ad7a8ee9 100644 --- a/index.css +++ b/index.css @@ -1545,14 +1545,19 @@ div.states > .icon { vertical-align: middle; } -div.states > .model { - width: 7em; -} - div.states > .icon > * { pointer-events: none; } +div.states > .resourceCategory, +div.states > .resourceModel { + cursor: pointer; + width: 7em; + overflow: hidden; + vertical-align: middle; + white-space: nowrap; +} + #diplomacyBodySection > div { cursor: pointer; } diff --git a/modules/ui/resources-editor.js b/modules/ui/resources-editor.js index 6d65094f..fdbfc1d1 100644 --- a/modules/ui/resources-editor.js +++ b/modules/ui/resources-editor.js @@ -22,18 +22,29 @@ function editResources() { document.getElementById("resourcesPercentage").addEventListener("click", togglePercentageMode); document.getElementById("resourcesExport").addEventListener("click", downloadResourcesData); + body.addEventListener("click", function(ev) { + const el = ev.target, cl = el.classList, line = el.parentNode, i = +line.dataset.id; + const resource = Resources.get(+line.dataset.id); + if (cl.contains("resourceCategory")) return changeCategory(resource, line, el); + if (cl.contains("resourceModel")) return changeModel(resource, line, el); + }); + + body.addEventListener("change", function(ev) { + const el = ev.target, cl = el.classList, line = el.parentNode; + const resource = Resources.get(+line.dataset.id); + if (cl.contains("resourceName")) return changeName(resource, el.value, line); + }); + // add line for each resource function resourcesEditorAddLines() { + const addTitle = (string, max) => string.length < max ? "" : `title="${string}"`; let lines = ""; - const categories = [...new Set(pack.resources.map(r => r.category))].sort(); - const categoryOptions = category => categories.map(c => ``).join(""); - - const models = [...new Set(pack.resources.map(r => r.model))].sort(); - const modelOptions = model => models.map(m => ``).join(""); // // {i: 33, name: "Saltpeter", icon: "resource-saltpeter", color: "#e6e3e3", value: 8, chance: 2, model: "habitability", bonus: {artillery: 3}} for (const r of pack.resources) { const stroke = Resources.getStroke(r.color); + const model = r.model.replaceAll("_", " "); + lines += `
@@ -42,8 +53,8 @@ function editResources() { - - +
${r.category}
+
${model}
@@ -68,6 +79,106 @@ function editResources() { $("#resourcesEditor").dialog({width: fitContent()}); } + function changeName(resource, name, line) { + resource.name = line.dataset.name = name; + } + + function changeCategory(resource, line, el) { + const categories = [...new Set(pack.resources.map(r => r.category))].sort(); + const categoryOptions = category => categories.map(c => ``).join(""); + + alertMessage.innerHTML = ` +
+
Select category:
+ +
+ +
+
Custom category:
+ +
+ `; + + $("#alert").dialog({resizable: false, title: "Change category", + buttons: { + Cancel: function() {$(this).dialog("close");}, + Apply: function() {applyChanges(); $(this).dialog("close");} + } + }); + + function applyChanges() { + const custom = document.getElementById("resouceCategoryAdd").value; + const select = document.getElementById("resouceCategorySelect").value; + const category = custom ? capitalize(custom) : select; + resource.category = line.dataset.category = el.innerHTML = category; + } + } + + function changeModel(resource, line, el) { + const defaultModels = Resources.defaultModels; + const model = line.dataset.model; + const modelOptions = Object.keys(defaultModels).map(m => ``).join(""); + const wikiURL = "https://github.com/Azgaar/Fantasy-Map-Generator/wiki/Resources:-spread-functions"; + + alertMessage.innerHTML = ` +
+ Predefined models +
+
Name:
+ +
+ +
+
Function:
+
${defaultModels[model]}
+
+
+ +
+ Custom model +
+
Name:
+ +
+ +
+
Function:
+ +
+
+ +
+ `; + + $("#alert").dialog({resizable: false, title: "Change spread model", + buttons: { + Help: () => openURL(wikiURL), + Cancel: function() {$(this).dialog("close");}, + Apply: function() {applyChanges(this);} + } + }); + + function applyChanges(dialog) { + const customName = document.getElementById("resouceModelCustomName").value; + const customFn = document.getElementById("resouceModelCustomFunction").value; + + const message = document.getElementById("resourceModelMessage"); + if (customName && !customFn) return message.innerHTML = "Error. Custom model function is required"; + if (!customName && customFn) return message.innerHTML = "Error. Custom model name is required"; + message.innerHTML = ""; + + if (customName && customFn) { + resource.model = line.dataset.model = el.innerHTML = customName; + resource.custom = customFn; + return; + } + + const model = document.getElementById("resouceModelSelect").value; + resource.model = line.dataset.model = el.innerHTML = model; + $(dialog).dialog("close"); + } + } + function resourceChangeColor() { const circle = this.querySelector("circle"); const resource = Resources.get(+this.parentNode.dataset.id);