Merge branch 'master' into gh-pages

This commit is contained in:
jonobr1 2011-01-28 20:32:28 -07:00
commit 2bab9a0699
8 changed files with 1798 additions and 254 deletions

View File

@ -2,53 +2,64 @@ var Controller = function() {
var onChange = null; var onChange = null;
this.setName = function(n) { this.parent = null;
this.propertyNameElement.innerHTML = n;
return this; this.name = function(n) {
} this.propertyNameElement.innerHTML = n;
return this;
this.setValue = function(n) { }
this.object[this.propertyName] = n;
if (onChange != null) { this.listen = function() {
onChange.call(this, n); this.parent.watchController(this);
} return this;
return this; }
}
this.getValue = function() {
this.getValue = function() { return this.object[this.propertyName];
return this.object[this.propertyName]; }
}
this.setValue = function(n) {
this.onChange = function(fnc) { this.object[this.propertyName] = n;
onChange = fnc; if (onChange != null) {
return this; onChange.call(this, n);
} }
return this;
}
this.watchValue = function() {
this.updateValue(this.object[this.propertyName]);
}
this.onChange = function(fnc) {
onChange = fnc;
return this;
}
this.makeUnselectable = function(elem) { this.makeUnselectable = function(elem) {
elem.onselectstart = function() { return false; }; elem.onselectstart = function() { return false; };
elem.style.MozUserSelect = "none"; elem.style.MozUserSelect = "none";
elem.style.KhtmlUserSelect = "none"; elem.style.KhtmlUserSelect = "none";
elem.unselectable = "on"; elem.unselectable = "on";
} }
this.makeSelectable = function(elem) { this.makeSelectable = function(elem) {
elem.onselectstart = function() { }; elem.onselectstart = function() { };
elem.style.MozUserSelect = "auto"; elem.style.MozUserSelect = "auto";
elem.style.KhtmlUserSelect = "auto"; elem.style.KhtmlUserSelect = "auto";
elem.unselectable = "off"; elem.unselectable = "off";
} }
this.domElement = document.createElement('div');
this.domElement.setAttribute('class', 'guidat-controller ' + this.type);
this.object = arguments[0]; this.domElement = document.createElement('div');
this.propertyName = arguments[1]; this.domElement.setAttribute('class', 'guidat-controller ' + this.type);
this.propertyNameElement = document.createElement('span'); this.object = arguments[0];
this.propertyNameElement.setAttribute('class', 'guidat-propertyname'); this.propertyName = arguments[1];
this.setName(this.propertyName);
this.domElement.appendChild(this.propertyNameElement); this.propertyNameElement = document.createElement('span');
this.propertyNameElement.setAttribute('class', 'guidat-propertyname');
this.makeUnselectable(this.domElement); this.name(this.propertyName);
this.domElement.appendChild(this.propertyNameElement);
};
this.makeUnselectable(this.domElement);
};

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);
@ -70,7 +64,7 @@ var NumberController = function() {
document.addEventListener('mouseup', function(e) { document.addEventListener('mouseup', function(e) {
document.removeEventListener('mousemove', dragNumberField, false); document.removeEventListener('mousemove', dragNumberField, false);
_this.makeSelectable(GUI.domElement); _this.makeSelectable(_this.parent.domElement);
_this.makeSelectable(numberField); _this.makeSelectable(numberField);
if (clickedNumberField && !draggedNumberField) { if (clickedNumberField && !draggedNumberField) {
numberField.focus(); numberField.focus();
@ -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;
@ -129,4 +123,4 @@ var NumberController = function() {
}; };
NumberController.prototype = new Controller(); NumberController.prototype = new Controller();
NumberController.prototype.constructor = NumberController; NumberController.prototype.constructor = NumberController;

View File

@ -1,27 +1,33 @@
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);
var input = document.createElement('input'); var input = document.createElement('input');
var initialValue = this.getValue(); var initialValue = this.getValue();
input.setAttribute('value', initialValue); input.setAttribute('value', initialValue);
input.setAttribute('spellcheck', 'false'); input.setAttribute('spellcheck', 'false');
this.domElement.addEventListener('mouseup', function() {
input.focus(); this.domElement.addEventListener('mouseup', function() {
input.select(); input.focus();
}, false); input.select();
}, false);
input.addEventListener('keyup', function() {
_this.setValue(input.value); input.addEventListener('keyup', function() {
}, false); _this.setValue(input.value);
}, false);
this.domElement.appendChild(input);
this.domElement.appendChild(input);
this.updateValue = function(val) {
input.setAttribute('value', val);
}
}; };
StringController.prototype = new Controller(); StringController.prototype = new Controller();
StringController.prototype.constructor = StringController; StringController.prototype.constructor = StringController;

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;
} }
} }

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 {
@ -155,4 +155,4 @@ width: 148px;
.guidat-slider-fg { .guidat-slider-fg {
background-color: #00aeff; background-color: #00aeff;
height: 20px; height: 20px;
} }

212
gui.js
View File

@ -1,66 +1,41 @@
var GUI = new function() { var GUI = function () {
var _this = this; var _this = this, open = false,
controllers = [], controllersWatched = [];
var controllers = [];
this.add = function() { this.domElement = document.createElement('div');
this.domElement.setAttribute('id', 'guidat');
// We need to call GUI.start() before .add()
if (!started) { controllerContainer = document.createElement('div');
error("Make sure to call GUI.start() in the window.onload function"); controllerContainer.setAttribute('id', 'guidat-controllers');
return; 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);
// Controllers Watcher
setInterval( function() {
for (var c in controllersWatched) {
controllersWatched[c].watchValue();
} }
}, 1000 / 60 );
var object = arguments[0];
var propertyName = arguments[1]; var handlerTypes = {
// Have we already added this?
if (alreadyControlled(object, propertyName)) {
error("Controller for \"" + propertyName+"\" already added.");
return;
}
var value = object[propertyName];
// Does this value exist? Is it accessible?
if (value == undefined) {
error(object + " either has no property \""+propertyName+"\", or the property is inaccessible.");
return;
}
var type = typeof value;
var handler = addHandlers[type];
// Do we know how to deal with this data type?
if (handler == undefined) {
error("Cannot create controller for data type \""+type+"\"");
return;
}
var controllerObject = construct(handler, arguments);
// Were we able to make the controller?
if (!controllerObject) {
error("Error creating controller for \""+propertyName+"\".");
return;
}
// Success.
controllerContainer.appendChild(controllerObject.domElement);
controllers.push(controllerObject);
return controllerObject;
}
var addHandlers = {
"number": NumberController, "number": NumberController,
"string": StringController, "string": StringController,
"boolean": BooleanController, "boolean": BooleanController,
"function": FunctionController "function": FunctionController
}; };
var alreadyControlled = function(object, propertyName) { var alreadyControlled = function(object, propertyName) {
for (var i in controllers) { for (var i in controllers) {
if (controllers[i].object == object && if (controllers[i].object == object &&
@ -70,81 +45,86 @@ var GUI = new function() {
} }
return false; return false;
}; };
var error = function(str) { var error = function(str) {
if (typeof console.log == 'function') { if (typeof console.log == 'function') {
console.error("[GUI ERROR] " + str); console.error("[GUI ERROR] " + str);
} }
}; };
var construct = function(constructor, args) { var construct = function(constructor, args) {
function F() { function F() {
return constructor.apply(this, args); return constructor.apply(this, args);
}
F.prototype = constructor.prototype;
return new F();
};
// 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();
} }
F.prototype = constructor.prototype;
return new F();
}; };
this.add = function() {
var object = arguments[0];
var propertyName = arguments[1];
// Have we already added this?
if (alreadyControlled(object, propertyName)) {
error("Controller for \"" + propertyName+"\" already added.");
return;
}
var value = object[propertyName];
// Does this value exist? Is it accessible?
if (value == undefined) {
error(object + " either has no property \""+propertyName+"\", or the property is inaccessible.");
return;
}
var type = typeof value;
var handler = handlerTypes[type];
// Do we know how to deal with this data type?
if (handler == undefined) {
error("Cannot create controller for data type \""+type+"\"");
return;
}
var controllerObject = construct( handler, arguments);
controllerObject.parent = _this;
// Were we able to make the controller?
if (!controllerObject) {
error("Error creating controller for \""+propertyName+"\".");
return;
}
// Success.
controllerContainer.appendChild(controllerObject.domElement);
controllers.push(controllerObject);
return controllerObject;
};
this.watchController = function(c) {
controllersWatched.push(c);
}
this.toggle = function() {
open ? this.hide() : 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,104 +1,114 @@
<!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 href="demo/demo.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.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">
window.onload = function() {
prettyPrint(); <link rel="icon" type="image/png" href="demo/assets/favicon.png" />
<link href="demo/demo.css" media="screen" rel="stylesheet" type="text/css" />
var fizzyText = new FizzyText("gui-dat"); <!-- <script type="text/javascript" src="gui.min.js"></script> -->
GUI.start(); <link href="gui.css" media="screen" rel="stylesheet" type="text/css" />
// Text field <script type="text/javascript" src="controllers/slider.js"></script>
GUI.add(fizzyText, "message"); <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>
<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() {
prettyPrint();
var fizzyText = new FizzyText("gui-dat");
var gui = new GUI();
document.body.appendChild( gui.domElement );
// 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");
// Watches a property
gui.add(fizzyText, "framesRendered").listen();
// Fires a function called "explode"
gui.add(fizzyText, "explode").setName("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);
// Boolean checkbox
GUI.add(fizzyText, "displayOutline");
// Fires a function called "explode"
GUI.add(fizzyText, "explode")
.setName('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>
<div id="container">
<div id = "helvetica-demo"></div> <div id = "helvetica-demo"></div>
<div id = "notifier"></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> <h1><a href = "http://twitter.com/guidat"><img src = "demo/assets/profile.png" border = "0" alt = "GUI-DAT flag" /></a></h1>
<p> <p>
<strong>gui-dat</strong> is a lightweight controller library for JavaScript. It allows you to easily manipulate variables and fire functions on the fly. <strong>gui-dat</strong> is a lightweight controller library for JavaScript. It allows you to easily manipulate variables and fire functions on the fly.
</p> </p>
<ul> <ul>
<li><a href="https://github.com/jonobr1/GUI-DAT/raw/master/gui.min.js"><strong>Download the minified source</strong></a> <small>[9.8kb]</small></li> <li><a href="https://github.com/jonobr1/GUI-DAT/raw/master/gui.min.js"><strong>Download the minified source</strong></a> <small>[9.8kb]</small></li>
<li><a href="http://github.com/jonobr1/GUI-DAT">Contribute on GitHub!</a></li> <li><a href="http://github.com/jonobr1/GUI-DAT">Contribute on GitHub!</a></li>
</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() {
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;);
GUI.start(); var gui = new GUI();
document.body.appendChild( gui.domElement );
// Text field // Text field
GUI.add(fizzyText, &quot;message&quot;); gui.add(fizzyText, &quot;message&quot;);
// Sliders with min and max // Sliders with min and max
GUI.add(fizzyText, &quot;maxSize&quot;, 0.5, 7); gui.add(fizzyText, &quot;maxSize&quot;, 0.5, 7);
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
GUI.add(fizzyText, &quot;displayOutline&quot;); gui.add(fizzyText, &quot;displayOutline&quot;);
// Watches a property
gui.add(fizzyText, "framesRendered").listen();
// Fires a function called &quot;explode&quot; // Fires a function called &quot;explode&quot;
GUI.add(fizzyText, &quot;explode&quot;).setName(&quot;Explode!&quot;); // Specify a custom name. gui.add(fizzyText, &quot;explode&quot;).setName(&quot;Explode!&quot;); // Specify a custom name.
}; };
&lt;/script&gt;</pre> &lt;/script&gt;</pre>
<ul id="desc"> <ul id="desc">
<li><strong>gui-dat</strong> will infer the type of the property you're trying to add<br/>(based on its initial value) and create the corresponding control.</li> <li><strong>gui-dat</strong> will infer the type of the property you're trying to add<br/>(based on its initial value) and create the corresponding control.</li>
<li>The properties must be public, i.e. defined by <code><strong>this</strong>.prop = value</code>.</li> <li>The properties must be public, i.e. defined by <code><strong>this</strong>.prop = value</code>.</li>
</ul> </ul>
<!-- <hr/> <hr/>
<h2>Monitor variable changes <em>outside</em> of the GUI</h2> <h2>Monitor variable changes <em>outside</em> of the GUI</h2>
<p>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 <code>listen()</code> method.</p> <p>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 <code>listen()</code> method.</p>
<pre class="prettyprint">GUI.add(obj, "propName").listen();</pre> <pre class="prettyprint">GUI.add(obj, "propName").listen();</pre>
@ -106,11 +116,11 @@ window.onload = function() {
<h2>Fire a function when someone uses a control</h2> <h2>Fire a function when someone uses a control</h2>
<pre class="prettyprint">GUI.add(obj, "propName").onChange(function(n) { <pre class="prettyprint">GUI.add(obj, "propName").onChange(function(n) {
alert("You changed me to " + n); alert("You changed me to " + n);
});</pre> --> });</pre>
<footer> <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. 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>
</body> </body>
</html> </html>