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 67557292bb
commit eb410d3fda
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,15 +20,9 @@ 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,15 +1,18 @@
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.
this.growthSpeed = 0.5; // how fast do particles change size? this.growthSpeed = 0.5; // how fast do particles change size?
this.maxSize = 3.2; // how big can they get? this.maxSize = 3.2; // how big can they get?
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
// change the message variable, we can call some more functions. // change the message variable, we can call some more functions.
@ -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) {
@ -216,4 +221,4 @@ function FizzyText(message) {
return v; return v;
} }
} }

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

@ -2,8 +2,8 @@
<head> <head>
<title>gui-dat</title> <title>gui-dat</title>
<link rel="icon" type="image/png" href="demo/assets/favicon.png" /> <link rel="icon" type="image/png" href="demo/assets/favicon.png" />
<link href="demo/demo.css" media="screen" rel="stylesheet" type="text/css" /> <link href="demo/demo.css" media="screen" rel="stylesheet" type="text/css" />
<!-- <script type="text/javascript" src="gui.min.js"></script> --> <!-- <script type="text/javascript" src="gui.min.js"></script> -->
@ -17,37 +17,42 @@
<script type="text/javascript" src="controllers/controller.string.js"></script> <script type="text/javascript" src="controllers/controller.string.js"></script>
<script type="text/javascript" src="gui.js"></script> <script type="text/javascript" src="gui.js"></script>
<script type="text/javascript" src="demo/improvedNoise.js"></script> <script type="text/javascript" src="demo/improvedNoise.js"></script>
<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() {
prettyPrint(); window.onload = function() {
var fizzyText = new FizzyText("gui-dat"); prettyPrint();
var gui = new GUI(); var fizzyText = new FizzyText("gui-dat");
document.body.appendChild( gui.domElement );
// Text field var gui = new GUI();
gui.add(fizzyText, "message"); document.body.appendChild( gui.domElement );
// Sliders with min and max // Text field
gui.add(fizzyText, "maxSize", 0.5, 7); gui.add(fizzyText, "message");
gui.add(fizzyText, "growthSpeed", 0.01, 1);
gui.add(fizzyText, "speed", 0.1, 2);
// Sliders with min, max and increment. // Sliders with min and max
gui.add(fizzyText, "noiseStrength", 10, 100, 5); gui.add(fizzyText, "maxSize", 0.5, 7);
gui.add(fizzyText, "growthSpeed", 0.01, 1);
gui.add(fizzyText, "speed", 0.1, 2);
// Boolean checkbox // Sliders with min, max and increment.
gui.add(fizzyText, "displayOutline"); gui.add(fizzyText, "noiseStrength", 10, 100, 5);
// Fires a function called "explode" // Boolean checkbox
gui.add(fizzyText, "explode").setName('Explode!'); // Specify a custom name. gui.add(fizzyText, "displayOutline");
// Watches a property
gui.add(fizzyText, "framesRendered").setWatched();
// Fires a function called "explode"
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.
}; };