Skip to content

Commit ffecc20

Browse files
committed
API
API where custom controllers can be used more similarly to the builtin controller types. dataarts/dat.gui#232 (comment)
1 parent e2bda55 commit ffecc20

File tree

1 file changed

+63
-17
lines changed

1 file changed

+63
-17
lines changed

Diff for: index.js

+63-17
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,81 @@ import { GUI, controllers } from '../../dat.gui';
1919
*
2020
* @extends dat.controllers.CustomController
2121
*
22+
* @param {Object} object The object to be manipulated
23+
* @param {string} property The name of the property to be manipulated
2224
* @param {number} a
2325
* @param {number} b
2426
*/
2527
export class KnobController extends controllers.CustomController {
26-
constructor( a, b ) {
27-
super(function (controller) {
28+
constructor( object, property, a, b ) {
29+
super( object, property );
2830

29-
var button = document.createElement('span');
30-
button.innerHTML = 'Knob Controller';
31-
button.title = 'Please press knob';
32-
button.style.cursor = 'pointer';
33-
button.style.margin = '0px 2px';
34-
button.onclick = function (value) {
31+
// ... set up options if needed
3532

36-
alert('Knob Controller ' + ( knobController.a + knobController.b ));
33+
const _this = this;
3734

38-
}
39-
controller.domElement.appendChild(button);
35+
//input element
36+
this.__input = document.createElement( 'input' );
37+
this.__input.setAttribute( 'type', 'number' );
38+
this.__input.style.width = '40%';
39+
this.updateDisplay();
40+
this.domElement.appendChild( this.__input );
4041

41-
});
42-
this.a = a;
43-
this.b = b;
44-
var knobController = this;
42+
//button element
43+
var button = document.createElement( 'input' );
44+
button.setAttribute( 'type', 'button' );
45+
button.value = 'Set ' + property;
46+
button.style.width = '50%';
47+
button.onclick = function ( e ) {
48+
object[property] = a + b;
49+
_this.updateDisplay();
50+
}
51+
this.domElement.appendChild( button );
52+
}
4553

54+
updateDisplay() {
55+
this.__input.value = this.getValue();
4656
}
4757
}
4858

59+
/**
60+
* @class Example of subtype of CustomController class.
61+
* Periodically changes the selected 3D object.
62+
* Adds NumberControllerSlider controller into PlayController for changing of the rate of changing of 3D obects per second.
63+
*
64+
* @extends dat.controllers.CustomController
65+
*
66+
* @param {Function} init Returns an object with elements for adding into "property-name" class element.
67+
*/
4968
export class PlayController extends controllers.CustomController {
50-
constructor( init ) {
51-
super( init );
69+
constructor( init ) {
70+
super( {
71+
72+
playRate: 1,//Default play rate is 1 changes per second
73+
property: init,
74+
75+
}, 'playRate', 1, 25, 1 );
76+
// this.property = init();
77+
if ( this.property === undefined )
78+
console.error( 'init() returns ' + this.property );
79+
}
80+
}
81+
82+
/**
83+
* @class Example of subtype of CustomController class.
84+
* Selects previous or next 3D object
85+
*
86+
* @extends dat.controllers.CustomController
87+
*
88+
* @param {Function} init Returns an object with elements for adding into "property-name" class element.
89+
*/
90+
export class PrevAndNextController extends controllers.CustomController {
91+
constructor( init ) {
92+
super( {
93+
property: init,
94+
} );
95+
// this.property = init();
96+
if ( this.property === undefined )
97+
console.error( ' init() returns ' + this.property );
5298
}
5399
}

0 commit comments

Comments
 (0)