Merged my changes with doob's -- exact same external functionality, some internal differences

This commit is contained in:
George Michael Brower 2011-01-28 21:28:12 -07:00
commit 19bc3b1eac
13 changed files with 1757 additions and 188 deletions

31
README
View File

@ -1,31 +0,0 @@
var controllableObject =
{
numberProperty: 20,
anotherNumberProperty: 0,
textProperty: "a string",
booleanProperty: false,
functionProperty: function() {
alert("I am a function!");
}
};
window.onload = function() {
GUI.start();
// Creates a number box
GUI.add(controllableObject, "numberProperty");
// Creates a slider (min, max)
GUI.add(controllableObject, "anotherNumberProperty", -100, 100);
// Creates a text field
GUI.add(controllableObject, "textProperty");
// Creates a checkbox
GUI.add(controllableObject, "booleanProperty");
// Creates a button
GUI.add(controllableObject, "functionProperty");
}

44
README.md Normal file
View File

@ -0,0 +1,44 @@
# gui-dat
**gui-dat** is a lightweight controller library for JavaScript. It allows you to easily manipulate variables and fire functions on the fly.
## Basic Usage
<script type="text/javascript" src="demo/demo.js"></script>
<script type="text/javascript">
window.onload = function() {
var fizzyText = new FizzyText("gui-dat");
GUI.start();
// Text field
GUI.add(fizzyText, "message");
// 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);
// Sliders with min, max and increment
GUI.add(fizzyText, "noiseStrength", 10, 100, 5);
// Boolean checkbox
GUI.add(fizzyText, "displayOutline");
// Fires a function called "explode"
GUI.add(fizzyText, "explode").name("Explode!"); // Specify a custom name.
};
</script>
+ ui-dat will infer the type of the property you're trying to add (based on its initial value) and create the corresponding control.
+ The properties must be public, i.e. defined by `this.prop = value`.
Monitor variable changes <i>outside</i> of the GUI
--------------------------------------------------
Let's say you have a variable that changes by itself from time to time. If you'd like the GUI to reflect those changes, use the listen() method
GUI.add(obj, "propName").listen();
## Fire a function when someone uses a control
GUI.add(obj, "propName").onChange(function(n) {
alert("You changed me to " + n);
});
Initiated by [George Michael Brower](http://georgemichaelbrower.com/) and [Jono Brandel](http://jonobr1.com/) of the Data Arts Team, Google Creative Lab.

View File

@ -1,4 +1,5 @@
var BooleanController = function() { var BooleanController = function() {
this.type = "boolean"; this.type = "boolean";
Controller.apply(this, arguments); Controller.apply(this, arguments);

View File

@ -1,9 +1,9 @@
var FunctionController = function() { var FunctionController = function() {
this.type = "function"; this.type = "function";
var _this = this; var that = this;
Controller.apply(this, arguments); Controller.apply(this, arguments);
this.domElement.addEventListener('click', function() { this.domElement.addEventListener('click', function() {
_this.object[_this.propertyName].call(_this.object); that.object[that.propertyName].call(that.object);
}, false); }, false);
this.domElement.style.cursor = "pointer"; this.domElement.style.cursor = "pointer";
this.propertyNameElement.style.cursor = "pointer"; this.propertyNameElement.style.cursor = "pointer";

View File

@ -27,11 +27,17 @@ Controller.prototype.listen = function() {
this.parent.listenTo(this); this.parent.listenTo(this);
} }
Controller.prototype.unlisten = function() {
this.parent.unlistenTo(this); // <--- hasn't been implemented yet
}
Controller.prototype.setValue = function(n) { Controller.prototype.setValue = function(n) {
this.object[this.propertyName] = n; this.object[this.propertyName] = n;
if (this.changeFunction != null) { if (this.changeFunction != null) {
this.changeFunction.call(this, n); this.changeFunction.call(this, n);
} }
// Whenever you call setValue, the display will be updated automatically.
// This reduces some clutter in subclasses. We can also use this method for listen().
this.updateDisplay(); this.updateDisplay();
return this; return this;
} }

View File

@ -85,6 +85,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.
GUI.makeUnselectable(_this.parent.domElement); GUI.makeUnselectable(_this.parent.domElement);
GUI.makeUnselectable(numberField); GUI.makeUnselectable(numberField);
@ -120,7 +121,6 @@ var NumberController = function() {
numberField.value = roundToDecimal(_this.getValue(), 4); numberField.value = roundToDecimal(_this.getValue(), 4);
if (slider) slider.value = _this.getValue(); if (slider) slider.value = _this.getValue();
} }
}; };
NumberController.prototype = new Controller(); NumberController.prototype = new Controller();

View File

@ -1,6 +1,7 @@
var StringController = function() { var StringController = function() {
this.type = "string"; this.type = "string";
var _this = this; var _this = this;
Controller.apply(this, arguments); Controller.apply(this, arguments);
@ -10,6 +11,7 @@ var StringController = function() {
input.setAttribute('value', initialValue); input.setAttribute('value', initialValue);
input.setAttribute('spellcheck', 'false'); input.setAttribute('spellcheck', 'false');
this.domElement.addEventListener('mouseup', function() { this.domElement.addEventListener('mouseup', function() {
input.focus(); input.focus();
input.select(); input.select();
@ -24,6 +26,8 @@ var StringController = function() {
} }
this.domElement.appendChild(input); this.domElement.appendChild(input);
}; };
StringController.prototype = new Controller(); StringController.prototype = new Controller();
StringController.prototype.constructor = StringController; StringController.prototype.constructor = StringController;

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) {

1538
demo/prettify.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -8,9 +8,6 @@
left: 100%; left: 100%;
margin-left: -300px; margin-left: -300px;
background-color: #fff; background-color: #fff;
-moz-transition: margin-top .2s ease-out;
-webkit-transition: margin-top .2s ease-out;
transition: margin-top .2s ease-out;
-webkit-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3); -webkit-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3); -moz-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3);
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3); box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3);
@ -26,6 +23,9 @@
overflow-y: auto; overflow-y: auto;
overflow-x: hidden; overflow-x: hidden;
background-color: rgba(0,0,0,0.1); background-color: rgba(0,0,0,0.1);
-moz-transition: height .2s ease-out;
-webkit-transition: height .2s ease-out;
transition: height .2s ease-out;
} }
#guidat-toggle { #guidat-toggle {

117
gui.js
View File

@ -7,6 +7,35 @@ var GUI = function() {
var autoListen = true; var autoListen = true;
var listenInterval; var listenInterval;
var _this = this, open = false,
controllers = [], controllersWatched = [];
this.domElement = document.createElement('div');
this.domElement.setAttribute('id', 'guidat');
controllerContainer = document.createElement('div');
controllerContainer.setAttribute('id', 'guidat-controllers');
// @doob
// I think this is way more elegant than the negative margin.
// Only wish we didn't have to see the scrollbar on its way open.
// Any thoughts?
controllerContainer.style.height = '0px';
toggleButton = document.createElement('a');
toggleButton.setAttribute('id', 'guidat-toggle');
toggleButton.setAttribute('href', '#');
toggleButton.innerHTML = "Show Controls";
toggleButton.addEventListener('click', function(e) {
_this.toggle();
e.preventDefault();
}, false);
this.domElement.appendChild(controllerContainer);
this.domElement.appendChild(toggleButton);
this.autoListenIntervalTime = 1000/60; this.autoListenIntervalTime = 1000/60;
var createListenInterval = function() { var createListenInterval = function() {
@ -15,7 +44,6 @@ var GUI = function() {
}, this.autoListenIntervalTime); }, this.autoListenIntervalTime);
} }
this.__defineSetter__("autoListen", function(v) { this.__defineSetter__("autoListen", function(v) {
autoListen = v; autoListen = v;
if (!autoListen) { if (!autoListen) {
@ -55,13 +83,38 @@ var GUI = function() {
this.autoListen = true; this.autoListen = true;
this.add = function() { var handlerTypes = {
"number": NumberController,
"string": StringController,
"boolean": BooleanController,
"function": FunctionController
};
// We need to call GUI.start() before .add() var alreadyControlled = function(object, propertyName) {
if (!started) { for (var i in controllers) {
error("Make sure to call GUI.start() in the window.onload function"); if (controllers[i].object == object &&
return; controllers[i].propertyName == propertyName) {
return true;
} }
}
return false;
};
var error = function(str) {
if (typeof console.log == 'function') {
console.error("[GUI ERROR] " + str);
}
};
var construct = function(constructor, args) {
function F() {
return constructor.apply(this, args);
}
F.prototype = constructor.prototype;
return new F();
};
this.add = function() {
var object = arguments[0]; var object = arguments[0];
var propertyName = arguments[1]; var propertyName = arguments[1];
@ -81,7 +134,7 @@ var GUI = function() {
} }
var type = typeof value; var type = typeof value;
var handler = addHandlers[type]; var handler = handlerTypes[type];
// Do we know how to deal with this data type? // Do we know how to deal with this data type?
if (handler == undefined) { if (handler == undefined) {
@ -89,7 +142,7 @@ var GUI = function() {
return; return;
} }
var args = [_this]; var args = [_this]; // Set first arg (parent) to this
for (var j = 0; j < arguments.length; j++) { for (var j = 0; j < arguments.length; j++) {
args.push(arguments[j]); args.push(arguments[j]);
} }
@ -145,60 +198,18 @@ var GUI = function() {
// GUI ... GUI // GUI ... GUI
this.domElement = null;
var controllerContainer;
var started = false;
var open = false;
// TODO: obtain this dynamically?
var domElementMarginTop = 300;
this.start = function() {
this.domElement = document.createElement('div');
this.domElement.setAttribute('id', 'guidat');
controllerContainer = document.createElement('div');
controllerContainer.setAttribute('id', 'guidat-controllers');
toggleButton = document.createElement('a');
toggleButton.setAttribute('id', 'guidat-toggle');
toggleButton.setAttribute('href', '#');
toggleButton.innerHTML = "Show Controls";
toggleButton.addEventListener('click', function(e) {
_this.toggle();
e.preventDefault();
}, false);
this.domElement.appendChild(controllerContainer);
this.domElement.appendChild(toggleButton);
this.domElement.style.marginTop = -domElementMarginTop+"px";
document.body.appendChild(this.domElement);
started = true;
};
this.toggle = function() { this.toggle = function() {
open ? this.hide() : this.show();
if (open) {
this.hide();
} else {
this.show();
}
}; };
this.show = function() { this.show = function() {
this.domElement.style.marginTop = 0+"px"; controllerContainer.style.height = '300px';
toggleButton.innerHTML = "Hide Controls"; toggleButton.innerHTML = "Hide Controls";
open = true; open = true;
} }
this.hide = function() { this.hide = function() {
this.domElement.style.marginTop = -domElementMarginTop+"px"; controllerContainer.style.height = '0px';
toggleButton.innerHTML = "Show Controls"; toggleButton.innerHTML = "Show Controls";
open = false; open = false;
} }

View File

@ -1,35 +1,37 @@
<!doctype html> <!doctype html>
<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> -->
<link href="gui.css" media="screen" rel="stylesheet" type="text/css" /> <link href="gui.css" media="screen" rel="stylesheet" type="text/css" />
<!-- <script type="text/javascript" src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js"></script>-->
<script type="text/javascript" src="gui.js"></script> <script type="text/javascript" src="gui.js"></script>
<script type="text/javascript" src="controllers/slider.js"></script> <script type="text/javascript" src="controllers/slider.js"></script>
<script type="text/javascript" src="controllers/controller.js"></script> <script type="text/javascript" src="controllers/controller.js"></script>
<script type="text/javascript" src="controllers/controller.number.js"></script>
<script type="text/javascript" src="controllers/controller.string.js"></script>
<script type="text/javascript" src="controllers/controller.boolean.js"></script> <script type="text/javascript" src="controllers/controller.boolean.js"></script>
<script type="text/javascript" src="controllers/controller.function.js"></script> <script type="text/javascript" src="controllers/controller.function.js"></script>
<script type="text/javascript" src="controllers/controller.number.js"></script>
<script type="text/javascript" src="controllers/controller.string.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/demo.js"></script> <script type="text/javascript" src="demo/demo.js"></script>
<script type="text/javascript"> <script type="text/javascript">
var fizzyText;
var gui;
window.onload = function() { window.onload = function() {
// prettyPrint(); prettyPrint();
fizzyText = new FizzyText("gui-dat"); var fizzyText = new FizzyText("gui-dat");
gui = new GUI(); var gui = new GUI();
gui.start(); document.body.appendChild( gui.domElement );
// Text field // Text field
gui.add(fizzyText, "message").listen(); gui.add(fizzyText, "message");
// Sliders with min and max // Sliders with min and max
gui.add(fizzyText, "maxSize", 0.5, 7); gui.add(fizzyText, "maxSize", 0.5, 7);
@ -40,25 +42,13 @@
gui.add(fizzyText, "noiseStrength", 10, 100, 5); gui.add(fizzyText, "noiseStrength", 10, 100, 5);
// Boolean checkbox // Boolean checkbox
gui.add(fizzyText, "displayOutline").onChange(function(v) { gui.add(fizzyText, "displayOutline");
alert(v);
});
// Fires a function called "explode" // Fires a function called "explode"
gui.add(fizzyText, "explode") gui.add(fizzyText, "explode").name("Explode!"); // Specify a custom name.
.name('Explode!'); // Specify a custom name.
}; };
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20996084-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script> </script>
</head> </head>
<body> <body>
@ -76,7 +66,7 @@
</ul> </ul>
<h2>Basic Usage</h2> <h2>Basic Usage</h2>
<pre class="prettyprint">&lt;script type=&quot;text/javascript&quot; src=&quot;demo/demo.js&quot;&gt;&lt;/script&gt; <pre id="demo-pre" class="prettyprint">&lt;script type=&quot;text/javascript&quot; src=&quot;demo/demo.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt; &lt;script type=&quot;text/javascript&quot;&gt;
window.onload = function() { window.onload = function() {
@ -84,7 +74,7 @@ window.onload = function() {
var fizzyText = new <a href="demo/demo.js">FizzyText</a>(&quot;gui-dat&quot;); var fizzyText = new <a href="demo/demo.js">FizzyText</a>(&quot;gui-dat&quot;);
var gui = new GUI(); var gui = new GUI();
gui.start(); document.body.appendChild( gui.domElement );
// Text field // Text field
gui.add(fizzyText, &quot;message&quot;); gui.add(fizzyText, &quot;message&quot;);
@ -94,7 +84,7 @@ window.onload = function() {
gui.add(fizzyText, &quot;growthSpeed&quot;, 0.01, 1); gui.add(fizzyText, &quot;growthSpeed&quot;, 0.01, 1);
gui.add(fizzyText, &quot;speed&quot;, 0.1, 2); gui.add(fizzyText, &quot;speed&quot;, 0.1, 2);
// Sliders with min, max and increment // Sliders with min, max and increment.
gui.add(fizzyText, &quot;noiseStrength&quot;, 10, 100, 5); gui.add(fizzyText, &quot;noiseStrength&quot;, 10, 100, 5);
// Boolean checkbox // Boolean checkbox
@ -149,5 +139,6 @@ setInterval(function() {
Initiated by <a href="http://georgemichaelbrower.com/">George Michael Brower</a> and <a href="http://jonobr1.com/">Jono Brandel</a> of the Data Arts Team, Google Creative Lab. Initiated by <a href="http://georgemichaelbrower.com/">George Michael Brower</a> and <a href="http://jonobr1.com/">Jono Brandel</a> of the Data Arts Team, Google Creative Lab.
</footer> </footer>
</div> </div>
</div>
</body> </body>
</html> </html>