Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Oct 31, 2023
1 parent c19dd4a commit a1fbb31
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 23 deletions.
27 changes: 11 additions & 16 deletions examples/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,22 +739,17 @@ if (showUI) {
// --------------- [ make a GUI for each CSS variable that affects GUI ] -----------------
const obj = {};

const folder = gui.addFolder('Style');
for (const {vars} of varNamesBySelector) {
for (const {key, rule} of vars) {
const value = rule.style.getPropertyValue(key).trim();
if (looksLikeCSSColor(value)) {
obj[key] = cssStringToHexColor(value);
controllersByKey[key] = folder.addColor(obj, key).onChange(updateMuiguiCSSStyles);
} else if (!value.startsWith('var')){
obj[key] = value;
controllersByKey[key] = folder.add(obj, key).onChange(v => {
const fn = fns[key];
if (fn) {
fn(v);
}
updateMuiguiCSSStyles();
});
for (const {name: key, rule} of varNames) {
const value = rule.style.getPropertyValue(key).trim();
if (value.startsWith('#')) {
obj[key] = cssStringToHexColor(value);
controllersByKey[key] = folder.addColor(obj, key).onChange(updateStyles);
} else if (!value.startsWith('var')){
obj[key] = value;
controllersByKey[key] = folder.add(obj, key).onChange(v => {
const fn = fns[key];
if (fn) {
fn(v);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "muigui",
"version": "0.0.6",
"version": "0.0.7",
"description": "A Simple GUI",
"main": "muigui.js",
"module": "src/muigui.js",
Expand Down
36 changes: 36 additions & 0 deletions src/libs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,39 @@ export const makeRangeOptions = ({from, to, step}) => {
};
};

// TODO: remove an use one in conversions. Move makeRangeConverters there?
export const identity = {
to: v => v,
from: v => [true, v],
};
export function makeMinMaxPair(gui, properties, minPropName, maxPropName, options) {
const { converters: { from } = identity } = options;
const { min, max } = options;
const guiMinRange = options.minRange || 0;
const valueMinRange = from(guiMinRange)[1];
console.log('guiMinRange', guiMinRange);
console.log('valueMinRange', valueMinRange);
return [
gui
.add(properties, minPropName, {
...options,
min,
max: max - guiMinRange,
})
.listen()
.onChange(v => {
properties[maxPropName] = Math.min(max, Math.max(v + valueMinRange, properties[maxPropName]));
}),
gui
.add(properties, maxPropName, {
...options,
min: min + guiMinRange,
max,
})
.listen()
.onChange(v => {
properties[minPropName] = Math.max(min, Math.min(v - valueMinRange, properties[minPropName]));
}),
];
}

2 changes: 2 additions & 0 deletions src/muigui.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
mapRange,
makeRangeConverters,
makeRangeOptions,
makeMinMaxPair,
} from './libs/utils.js';
import {
converters
Expand Down Expand Up @@ -95,6 +96,7 @@ export class GUI extends GUIFolder {
static mapRange = mapRange;
static makeRangeConverters = makeRangeConverters;
static makeRangeOptions = makeRangeOptions;
static makeMinMaxPair = makeMinMaxPair;
#localStyleSheet = new CSSStyleSheet();

constructor(options = {}) {
Expand Down
4 changes: 2 additions & 2 deletions src/styles/muigui.css.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default `
.muigui-colors {
.muigui {
--bg-color: #ddd;
--color: #222;
--value-color: #145 ;
Expand All @@ -24,7 +24,7 @@ export default `
}
@media (prefers-color-scheme: dark) {
.muigui-colors {
.muigui {
--bg-color: #222222;
--color: #dddddd;
--value-color: #43e5f7;
Expand Down
14 changes: 10 additions & 4 deletions src/views/RangeView.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ export default class RangeView extends EditView {
type: 'range',
onInput: () => {
this.#skipUpdate = true;
const [valid, v] = this.#from(parseFloat(this.domElement.value));
const {min, max, step} = this.#options;
const v = parseFloat(this.domElement.value);
const newV = clamp(stepify(v, v => v, step), min, max);
const [valid, validV] = this.#from(newV);
if (valid) {
setter.setValue(v);
setter.setValue(validV);
}
},
onChange: () => {
this.#skipUpdate = true;
const [valid, v] = this.#from(parseFloat(this.domElement.value));
const {min, max, step} = this.#options;
const v = parseFloat(this.domElement.value);
const newV = clamp(stepify(v, v => v, step), min, max);
const [valid, validV] = this.#from(newV);
if (valid) {
setter.setFinalValue(v);
setter.setFinalValue(validV);
}
},
onWheel: e => {
Expand Down

0 comments on commit a1fbb31

Please sign in to comment.