Преглед изворни кода

Allow expression input

This affects the world height, the entity scale, and all view numeric inputs.
The fields are no longer of type 'number' so that text can be entered.
master
Fen Dweller пре 4 година
родитељ
комит
b17e7366c6
2 измењених фајлова са 52 додато и 12 уклоњено
  1. +1
    -1
      macrovision.html
  2. +51
    -11
      macrovision.js

+ 1
- 1
macrovision.html Прегледај датотеку

@@ -95,7 +95,7 @@
World height
</div>
<div class="options-row">
<input class="options-field-numeric" type="number" id="options-height-value" min="1" value="10" step="any">
<input class="options-field-numeric" id="options-height-value" value="10">
<select class="options-field-unit" id="options-height-unit">
</select>
</div>


+ 51
- 11
macrovision.js Прегледај датотеку

@@ -1335,7 +1335,18 @@ function configEntityOptions(entity, view) {
scaleInput.id = "options-entity-scale";

scaleInput.addEventListener("change", e => {
entity.scale = e.target.value == 0 ? 1 : e.target.value;
try {
const newScale = e.target.value == 0 ? 1 : math.evaluate(e.target.value);
if (typeof(newScale) !== "number") {
toast("Invalid input: scale can't have any units!")
return;
}
entity.scale = newScale;
} catch {
toast("Invalid input: could not parse " + e.target.value)
}


entity.dirty = true;
if (config.autoFit) {
fitWorld();
@@ -1350,9 +1361,6 @@ function configEntityOptions(entity, view) {
e.stopPropagation();
})

scaleInput.setAttribute("min", 1);
scaleInput.setAttribute("type", "number");
scaleInput.setAttribute("step", "any");
setNumericInput(scaleInput, entity.scale);

scaleRow.appendChild(scaleInput);
@@ -1464,9 +1472,6 @@ function configViewOptions(entity, view) {
const input = document.createElement("input");
input.classList.add("options-field-numeric");
input.id = "options-view-" + key + "-input";
input.setAttribute("type", "number");
input.setAttribute("step", "any");
input.setAttribute("min", 1);

const select = document.createElement("select");
select.classList.add("options-field-unit");
@@ -1488,7 +1493,26 @@ function configViewOptions(entity, view) {


input.addEventListener("change", e => {
const value = input.value == 0 ? 1 : input.value;
const raw_value = input.value == 0 ? 1 : input.value;
let value
try {
value = math.evaluate(raw_value).toNumber(select.value);
} catch {
try {
value = math.evaluate(input.value)
if (typeof(value) !== "number") {
toast("Invalid input: " + value.format() + " can't convert to " + select.value)
value = undefined
}
} catch {
toast("Invalid input: could not parse: " + input.value)
value = undefined
}
}
if (value === undefined) {
return;
}
input.value = value
entity.views[view][key] = math.unit(value, select.value);
entity.dirty = true;
if (config.autoFit) {
@@ -4031,13 +4055,29 @@ function fitEntities(targetEntities, manual = false, factor = 1.1) {
setWorldHeight(config.height, math.multiply(height, factor));
}

// TODO why am I doing this
function updateWorldHeight() {
const unit = document.querySelector("#options-height-unit").value;
const value = Math.max(0.000000001, document.querySelector("#options-height-value").value);
const rawValue = document.querySelector("#options-height-value").value;

var value
try {
value = math.evaluate(rawValue)
if (typeof(value) !== "number") {
try {
value = value.toNumber(unit)
} catch {
toast("Invalid input: " + rawValue + " can't be converted to " + unit)
}
}
} catch {
toast("Invalid input: could not parse " + rawValue)
return;
}

const newHeight = Math.max(0.000000001, value);
const oldHeight = config.height;

setWorldHeight(oldHeight, math.unit(value, unit));
setWorldHeight(oldHeight, math.unit(newHeight, unit));
}

function setWorldHeight(oldHeight, newHeight) {


Loading…
Откажи
Сачувај