Skip to content

Commit

Permalink
Merge "Implement .add( controller ) & its use for custom Controllers"…
Browse files Browse the repository at this point in the history
… from dataarts#243.
  • Loading branch information
superkelvint committed Mar 29, 2022
1 parent 36ca84e commit 09cc818
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
38 changes: 38 additions & 0 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,44 @@
var f3 = f2.addFolder('Nested Folder');
f3.add(obj, 'growthSpeed');

// alternative StringController add
gui.add(new dat.controllers.StringController(obj, 'message'));

// a custom controller
class KnobController extends dat.controllers.Controller {
constructor(object, property, a, b) {
super(object, property);
const _this = this;
this.__input = document.createElement('input');
this.__input.setAttribute('type', 'number');
this.__input.style.width = '40%';
this.updateDisplay();
this.domElement.appendChild(this.__input);
var button = document.createElement('input');
button.setAttribute('type', 'button');
button.value = 'Set ' + property;
button.style.width = '50%';
button.onclick = function(e) {
object[property] = a + b;
_this.updateDisplay();
};
this.domElement.appendChild(button);
}
updateDisplay() {
this.__input.value = this.getValue();
}
}

const api = {

color: '#ffffff',
value: 0.5,

};

gui.add(new KnobController(api, 'value', 0.5, 25), {
liClass: 'knobby'
});
</script>
</body>
</html>
30 changes: 19 additions & 11 deletions src/dat/gui/GUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1128,19 +1128,23 @@ function recallSavedValue(gui, controller) {
}

function add(gui, object, property, params) {
if (object[property] === undefined) {
throw new Error(`Object "${object}" has no property "${property}"`);
}

let controller;

if (params.color) {
controller = new ColorController(object, property);
} else if (params.image) {
controller = new ImageController(object, property, params.factoryArgs[0]);
} else {
const factoryArgs = [object, property].concat(params.factoryArgs);
controller = ControllerFactory.apply(gui, factoryArgs);
if (object instanceof Controller) {
controller = object;
params = property || { };
} else {

if (object[property] === undefined) {
throw new Error(`Object "${object}" has no property "${property}"`);
}

if (params.color) {
controller = new ColorController(object, property);
} else {
const factoryArgs = [object, property].concat(params.factoryArgs);
controller = ControllerFactory.apply(gui, factoryArgs);
}
}

if (params.before instanceof Controller) {
Expand All @@ -1164,6 +1168,10 @@ function add(gui, object, property, params) {
dom.addClass(li, GUI.CLASS_CONTROLLER_ROW);
if (controller instanceof ColorController) {
dom.addClass(li, 'color');
} else if ( params.liClass ) {
dom.addClass(li, params.liClass);
} else if ( controller.liClass ) {
dom.addClass(li, controller.liClass);
} else if (controller instanceof ImageController) {
dom.addClass(li, 'image');
} else {
Expand Down

0 comments on commit 09cc818

Please sign in to comment.