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;
}
this.setWatched = function() {
this.parent.watchController(this);
return this;
}
this.getValue = function() {
return this.object[this.propertyName];
}
this.setValue = function(n) {
this.object[this.propertyName] = n;
if (onChange != null) {
@ -17,8 +26,8 @@ var Controller = function() {
return this;
}
this.getValue = function() {
return this.object[this.propertyName];
this.watchValue = function() {
this.updateValue(this.object[this.propertyName]);
}
this.onChange = function(fnc) {

View File

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

View File

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

21
gui.js
View File

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

View File

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