mirror of
https://github.com/dataarts/dat.gui.git
synced 2024-12-12 04:08:27 +00:00
Implement .add( controller ) & its use for custom Controllers.
See example of a custom controller in example.html.
This commit is contained in:
parent
743a16b398
commit
36e1991c94
53
example.html
53
example.html
@ -1,8 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script type="text/javascript" src="build/dat.gui.js"></script>
|
||||
<script type="text/javascript">
|
||||
@ -26,7 +28,11 @@
|
||||
color0: "#ffae23", // CSS string
|
||||
color1: [0, 128, 255], // RGB array
|
||||
color2: [0, 128, 255, 0.3], // RGB with alpha
|
||||
color3: { h: 350, s: 0.9, v: 0.3 } // Hue, saturation, value
|
||||
color3: {
|
||||
h: 350,
|
||||
s: 0.9,
|
||||
v: 0.3
|
||||
} // Hue, saturation, value
|
||||
};
|
||||
|
||||
var gui = new dat.gui.GUI();
|
||||
@ -44,7 +50,11 @@
|
||||
gui.add(obj, 'type', ['one', 'two', 'three']);
|
||||
|
||||
// Choose from named values
|
||||
gui.add(obj, 'speed', { Stopped: 0, Slow: 0.1, Fast: 5 } );
|
||||
gui.add(obj, 'speed', {
|
||||
Stopped: 0,
|
||||
Slow: 0.1,
|
||||
Fast: 5
|
||||
});
|
||||
|
||||
var f1 = gui.addFolder('Colors');
|
||||
f1.addColor(obj, 'color0');
|
||||
@ -58,6 +68,45 @@
|
||||
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>
|
@ -1131,12 +1131,17 @@ function recallSavedValue(gui, controller) {
|
||||
}
|
||||
|
||||
function add(gui, object, property, params) {
|
||||
let controller;
|
||||
|
||||
if (object instanceof Controller) {
|
||||
controller = object;
|
||||
params = property || { };
|
||||
} else {
|
||||
|
||||
if (object[property] === undefined) {
|
||||
throw new Error(`Object "${object}" has no property "${property}"`);
|
||||
}
|
||||
|
||||
let controller;
|
||||
|
||||
if (params.color) {
|
||||
controller = new ColorController(object, property);
|
||||
} else {
|
||||
@ -1144,6 +1149,8 @@ function add(gui, object, property, params) {
|
||||
controller = ControllerFactory.apply(gui, factoryArgs);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (params.before instanceof Controller) {
|
||||
params.before = params.before.__li;
|
||||
}
|
||||
@ -1165,6 +1172,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 {
|
||||
dom.addClass(li, typeof controller.getValue());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user