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,6 +1,7 @@
var BooleanController = function() {
this.type = "boolean";
Controller.apply(this, arguments);
Controller.apply(this, arguments);
var _this = this;
var input = document.createElement('input');

View File

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

View File

@ -27,11 +27,17 @@ Controller.prototype.listen = function() {
this.parent.listenTo(this);
}
Controller.prototype.unlisten = function() {
this.parent.unlistenTo(this); // <--- hasn't been implemented yet
}
Controller.prototype.setValue = function(n) {
this.object[this.propertyName] = n;
if (this.changeFunction != null) {
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();
return this;
}

View File

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

View File

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

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

1538
demo/prettify.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -8,9 +8,6 @@
left: 100%;
margin-left: -300px;
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);
-moz-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-x: hidden;
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 {

117
gui.js
View File

@ -7,6 +7,35 @@ var GUI = function() {
var autoListen = true;
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;
var createListenInterval = function() {
@ -15,7 +44,6 @@ var GUI = function() {
}, this.autoListenIntervalTime);
}
this.__defineSetter__("autoListen", function(v) {
autoListen = v;
if (!autoListen) {
@ -55,13 +83,38 @@ var GUI = function() {
this.autoListen = true;
this.add = function() {
var handlerTypes = {
"number": NumberController,
"string": StringController,
"boolean": BooleanController,
"function": FunctionController
};
// We need to call GUI.start() before .add()
if (!started) {
error("Make sure to call GUI.start() in the window.onload function");
return;
var alreadyControlled = function(object, propertyName) {
for (var i in controllers) {
if (controllers[i].object == object &&
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 propertyName = arguments[1];
@ -81,7 +134,7 @@ var GUI = function() {
}
var type = typeof value;
var handler = addHandlers[type];
var handler = handlerTypes[type];
// Do we know how to deal with this data type?
if (handler == undefined) {
@ -89,7 +142,7 @@ var GUI = function() {
return;
}
var args = [_this];
var args = [_this]; // Set first arg (parent) to this
for (var j = 0; j < arguments.length; j++) {
args.push(arguments[j]);
}
@ -145,60 +198,18 @@ var GUI = function() {
// 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() {
if (open) {
this.hide();
} else {
this.show();
}
open ? this.hide() : this.show();
};
this.show = function() {
this.domElement.style.marginTop = 0+"px";
controllerContainer.style.height = '300px';
toggleButton.innerHTML = "Hide Controls";
open = true;
}
this.hide = function() {
this.domElement.style.marginTop = -domElementMarginTop+"px";
controllerContainer.style.height = '0px';
toggleButton.innerHTML = "Show Controls";
open = false;
}

View File

@ -1,68 +1,58 @@
<!doctype html>
<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" />
<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="controllers/slider.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.function.js"></script>
<!-- <script type="text/javascript" src="gui.min.js"></script> -->
<script type="text/javascript" src="demo/improvedNoise.js"></script>
<script type="text/javascript" src="demo/demo.js"></script>
<script type="text/javascript">
var fizzyText;
var gui;
window.onload = function() {
<link href="gui.css" media="screen" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="gui.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.boolean.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>
// prettyPrint();
fizzyText = new FizzyText("gui-dat");
<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">
gui = new GUI();
gui.start();
window.onload = function() {
// Text field
gui.add(fizzyText, "message").listen();
prettyPrint();
// 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);
var fizzyText = new FizzyText("gui-dat");
// Sliders with min, max and increment.
gui.add(fizzyText, "noiseStrength", 10, 100, 5);
var gui = new GUI();
document.body.appendChild( gui.domElement );
// Boolean checkbox
gui.add(fizzyText, "displayOutline").onChange(function(v) {
alert(v);
});
// Text field
gui.add(fizzyText, "message");
// Fires a function called "explode"
gui.add(fizzyText, "explode")
.name('Explode!'); // Specify a custom name.
// 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);
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20996084-1']);
_gaq.push(['_trackPageview']);
// Boolean checkbox
gui.add(fizzyText, "displayOutline");
// Fires a function called "explode"
gui.add(fizzyText, "explode").name("Explode!"); // Specify a custom name.
};
(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>
</head>
<body>
<div id="container">
<div id="container">
<div id = "helvetica-demo"></div>
<div id = "notifier"></div>
<h1><a href = "http://twitter.com/guidat"><img src = "demo/assets/profile.png" border = "0" alt = "GUI-DAT flag" /></a></h1>
@ -76,7 +66,7 @@
</ul>
<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;
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 gui = new GUI();
gui.start();
document.body.appendChild( gui.domElement );
// Text field
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;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);
// Boolean checkbox
@ -148,6 +138,7 @@ setInterval(function() {
<footer>
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>
</div>
</div>
</body>
</html>