Hacked in a Controllers Watcher.

It would be nice if the controllers had the last value cached so setValue will execute only if _value != value.
Added a setWatched example in the demo.
This commit is contained in:
Mr.doob 2011-01-29 01:23:20 +00:00
parent 402093179a
commit d770dbb0c2
5 changed files with 65 additions and 45 deletions

View File

@ -9,6 +9,15 @@ var Controller = function() {
return this; return this;
} }
this.setWatched = function() {
this.parent.watchController(this);
return this;
}
this.getValue = function() {
return this.object[this.propertyName];
}
this.setValue = function(n) { this.setValue = function(n) {
this.object[this.propertyName] = n; this.object[this.propertyName] = n;
if (onChange != null) { if (onChange != null) {
@ -17,8 +26,8 @@ var Controller = function() {
return this; return this;
} }
this.getValue = function() { this.watchValue = function() {
return this.object[this.propertyName]; this.updateValue(this.object[this.propertyName]);
} }
this.onChange = function(fnc) { this.onChange = function(fnc) {

View File

@ -20,14 +20,8 @@ var NumberController = function() {
var step = arguments[4]; var step = arguments[4];
if (!step) { if (!step) {
if (min != undefined && max != undefined) { step = min != undefined && max != undefined ? (max-min)*0.01: 1;
step = (max-min)*0.01;
} else {
step = 1;
} }
}
console.log("step " + step);
var numberField = document.createElement('input'); var numberField = document.createElement('input');
numberField.setAttribute('id', this.propertyName); numberField.setAttribute('id', this.propertyName);
@ -94,7 +88,7 @@ var NumberController = function() {
// We don't want to be highlighting this field as we scroll. // We don't want to be highlighting this field as we scroll.
// Or any other fields in this gui for that matter ... // Or any other fields in this gui for that matter ...
// TODO: Make makeUselectable go through each element and child element. // TODO: Make makeUselectable go through each element and child element.
_this.makeUnselectable(GUI.domElement); _this.makeUnselectable(_this.parent.domElement);
_this.makeUnselectable(numberField); _this.makeUnselectable(numberField);
py = y; py = y;

View File

@ -1,5 +1,7 @@
function FizzyText(message) { function FizzyText(message) {
var that = this;
// These are the variables that we manipulate with gui-dat. // These are the variables that we manipulate with gui-dat.
// Notice they're all defined with "this". That makes them public. // Notice they're all defined with "this". That makes them public.
// Otherwise, gui-dat can't see them. // Otherwise, gui-dat can't see them.
@ -9,6 +11,7 @@ function FizzyText(message) {
this.noiseStrength = 10; // how turbulent is the flow? this.noiseStrength = 10; // how turbulent is the flow?
this.speed = 0.4; // how fast do particles move? this.speed = 0.4; // how fast do particles move?
this.displayOutline = false; // should we draw the message as a stroke? this.displayOutline = false; // should we draw the message as a stroke?
this.framesRendered = 0;
// __defineGetter__ and __defineSetter__ makes JavaScript believe that // __defineGetter__ and __defineSetter__ makes JavaScript believe that
// we've defined a variable 'this.message'. This way, whenever we // we've defined a variable 'this.message'. This way, whenever we
@ -98,6 +101,8 @@ function FizzyText(message) {
// Called once per frame, updates the animation. // Called once per frame, updates the animation.
var render = function () { var render = function () {
that.framesRendered ++;
g.clearRect(0, 0, width, height); g.clearRect(0, 0, width, height);
if (_this.displayOutline) { if (_this.displayOutline) {

21
gui.js
View File

@ -22,6 +22,13 @@ var GUI = function () {
this.domElement.appendChild(controllerContainer); this.domElement.appendChild(controllerContainer);
this.domElement.appendChild(toggleButton); this.domElement.appendChild(toggleButton);
// Controllers Watcher
setInterval( function() {
for (var c in controllersWatched) {
controllersWatched[c].watchValue();
}
}, 1000 / 60 );
var handlerTypes = { var handlerTypes = {
"number": NumberController, "number": NumberController,
"string": StringController, "string": StringController,
@ -96,16 +103,16 @@ var GUI = function () {
return controllerObject; return controllerObject;
};
this.watchController = function(c) {
controllersWatched.push(c);
} }
this.toggle = function() { this.toggle = function() {
open ? this.hide() : this.show();
if (open) {
this.hide();
} else {
this.show();
}
}; };
this.show = function() { this.show = function() {

View File

@ -21,6 +21,7 @@
<script type="text/javascript" src="demo/prettify.js"></script> <script type="text/javascript" src="demo/prettify.js"></script>
<script type="text/javascript" src="demo/demo.js"></script> <script type="text/javascript" src="demo/demo.js"></script>
<script type="text/javascript"> <script type="text/javascript">
window.onload = function() { window.onload = function() {
prettyPrint(); prettyPrint();
@ -44,10 +45,14 @@
// Boolean checkbox // Boolean checkbox
gui.add(fizzyText, "displayOutline"); gui.add(fizzyText, "displayOutline");
// Watches a property
gui.add(fizzyText, "framesRendered").setWatched();
// Fires a function called "explode" // Fires a function called "explode"
gui.add(fizzyText, "explode").setName('Explode!'); // Specify a custom name. gui.add(fizzyText, "explode").setName("Explode!"); // Specify a custom name.
}; };
</script> </script>
</head> </head>
<body> <body>
@ -89,7 +94,7 @@ window.onload = function() {
gui.add(fizzyText, &quot;displayOutline&quot;); gui.add(fizzyText, &quot;displayOutline&quot;);
// Fires a function called &quot;explode&quot; // Fires a function called &quot;explode&quot;
gui.add(fizzyText, &quot;explode&quot;).setName(&#039;Explode!&#039;); // Specify a custom name. gui.add(fizzyText, &quot;explode&quot;).setName(&quot;Explode!&quot;); // Specify a custom name.
}; };