mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
feat(display): render the style parameters from the display store
VisualSettings publishes the active style's parameter rows to the display store, and the Display panel renders them with the shared row component; a moved slider goes back through the store's setStyleParameter action. The readout snaps a value to its step as the browser does, half-steps up and an off-grid maximum down. A preset that resets the shown style's uniforms republishes its rows, input for another style's parameters is ignored, and a stopped owner takes its rows with it unless a newer owner has published since. createStyleParameters is deprecated and builds its rows the same way.
This commit is contained in:
1 parent
3a750c9021
commit
c3c69a91cd
14 files changed
+316
-80
No files matched your search
+69
-25
@@ -13,9 +13,9 @@ const formatParam = (value, digits) =>
|
||||
/**
|
||||
* The generated rows: a label, the kit's slider and its value in the UI
|
||||
* language. A render function, so the portable input package needs no
|
||||
* single-file-component compiler.
|
||||
* single-file-component compiler. The Display panel renders it too.
|
||||
*/
|
||||
const StyleParameterRows = {
|
||||
export const StyleParameterRows = {
|
||||
props: {
|
||||
rows: { type: Array, required: true },
|
||||
onInput: { type: Function, required: true },
|
||||
@@ -39,6 +39,68 @@ const StyleParameterRows = {
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* A value as a range input holds it: clamped to its bounds and snapped to
|
||||
* its step from the minimum, as the browser snaps the slider.
|
||||
* @param {number} value
|
||||
* @param {{min: number, max: number, step: string}} row
|
||||
*/
|
||||
export function snapToStep(value, { min, max, step }) {
|
||||
const size = Number(step);
|
||||
const decimals = (String(step).split('.')[1] || '').length;
|
||||
// Rounded to nine places first, so a value halfway between two steps
|
||||
// (1.15 from 1 by 0.1) rounds up as the browser does, not down through
|
||||
// floating point error.
|
||||
const count = (span) => Number((span / size).toFixed(9));
|
||||
// A maximum off the step grid holds the last step below it, as the browser
|
||||
// does.
|
||||
const steps = Math.min(
|
||||
Math.floor(count(max - min)),
|
||||
Math.max(0, Math.round(count(Number(value) - min))),
|
||||
);
|
||||
return Number((min + steps * size).toFixed(decimals));
|
||||
}
|
||||
|
||||
/**
|
||||
* The parameter rows of a style's uniforms: each one's label, bounds, step,
|
||||
* value and the value's text in the UI language.
|
||||
* @param {object} uniforms The style's uniform metadata.
|
||||
* @param {(name: string) => number} readValue A uniform's current value.
|
||||
*/
|
||||
export function styleParameterRows(uniforms, readValue) {
|
||||
return Object.entries(uniforms).map(([name, metadata]) =>
|
||||
withValue(
|
||||
{
|
||||
name,
|
||||
label: t(metadata.label),
|
||||
min: metadata.min,
|
||||
max: metadata.max,
|
||||
step: metadata.max <= 1 ? '0.01' : '0.1',
|
||||
digits: metadata.max <= 1 ? 2 : 1,
|
||||
},
|
||||
readValue(name),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The rows with one parameter's value changed.
|
||||
* @param {object[]} rows From {@link styleParameterRows}.
|
||||
* @param {string} name
|
||||
* @param {number} value
|
||||
*/
|
||||
export function withParameterValue(rows, name, value) {
|
||||
return rows.map((row) => (row.name === name ? withValue(row, value) : row));
|
||||
}
|
||||
|
||||
function withValue(row, value) {
|
||||
return {
|
||||
...row,
|
||||
value,
|
||||
text: formatParam(snapToStep(value, row), row.digits),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Own generated parameter rows and their input handling. Renders
|
||||
* StyleParameterRows into `container` and patches it synchronously.
|
||||
@@ -47,6 +109,9 @@ const StyleParameterRows = {
|
||||
* @param {object} options
|
||||
* @param {HTMLElement} options.container Container owned by this component.
|
||||
* @returns {{render: Function, clear: Function, destroy: Function}}
|
||||
* @deprecated The Display panel renders the rows VisualSettings publishes to
|
||||
* the display store (styleParameterRows); this stays exported from
|
||||
* `./ui/input` for hosts that imported it.
|
||||
*/
|
||||
export function createStyleParameters({ container }) {
|
||||
let rows = [];
|
||||
@@ -69,36 +134,15 @@ export function createStyleParameters({ container }) {
|
||||
if (destroyed) return;
|
||||
clear();
|
||||
const current = generation;
|
||||
rows = Object.entries(uniforms).map(([name, metadata]) => ({
|
||||
name,
|
||||
label: t(metadata.label),
|
||||
min: metadata.min,
|
||||
max: metadata.max,
|
||||
step: metadata.max <= 1 ? '0.01' : '0.1',
|
||||
digits: metadata.max <= 1 ? 2 : 1,
|
||||
value: readValue(name),
|
||||
text: '',
|
||||
}));
|
||||
rows = styleParameterRows(uniforms, readValue);
|
||||
onInput = (name, value) => {
|
||||
if (destroyed || current !== generation) return;
|
||||
writeValue(name, value);
|
||||
rows = rows.map((row) =>
|
||||
row.name === name
|
||||
? { ...row, value, text: formatParam(value, row.digits) }
|
||||
: row,
|
||||
);
|
||||
rows = withParameterValue(rows, name, value);
|
||||
paint();
|
||||
onChange();
|
||||
};
|
||||
paint();
|
||||
// The readout shows the value the slider holds once the browser has
|
||||
// clamped and snapped it to its step.
|
||||
const sliders = container.querySelectorAll('input.param-slider');
|
||||
rows = rows.map((row, index) => ({
|
||||
...row,
|
||||
text: formatParam(parseFloat(sliders[index].value), row.digits),
|
||||
}));
|
||||
paint();
|
||||
},
|
||||
clear,
|
||||
destroy() {
|
||||
|
||||
Reference in new issue
Block a user