dat.gui/gui.js

131 lines
3.0 KiB
JavaScript
Raw Normal View History

var GUI = function () {
2011-01-23 23:43:02 +00:00
var _this = this, open = false,
controllers = [], controllersWatched = [];
2011-01-23 23:43:02 +00:00
this.domElement = document.createElement('div');
this.domElement.setAttribute('id', 'guidat');
controllerContainer = document.createElement('div');
controllerContainer.setAttribute('id', 'guidat-controllers');
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 handlerTypes = {
"number": NumberController,
"string": StringController,
"boolean": BooleanController,
"function": FunctionController
};
var alreadyControlled = function(object, propertyName) {
for (var i in controllers) {
if (controllers[i].object == object &&
controllers[i].propertyName == propertyName) {
return true;
}
2011-01-23 22:35:19 +00:00
}
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() {
2011-01-23 21:39:07 +00:00
var object = arguments[0];
2011-01-23 22:35:19 +00:00
var propertyName = arguments[1];
2011-01-23 22:57:12 +00:00
// Have we already added this?
if (alreadyControlled(object, propertyName)) {
2011-01-26 01:55:59 +00:00
error("Controller for \"" + propertyName+"\" already added.");
return;
2011-01-23 22:57:12 +00:00
}
2011-01-23 22:35:19 +00:00
var value = object[propertyName];
2011-01-23 22:35:19 +00:00
// Does this value exist? Is it accessible?
if (value == undefined) {
error(object + " either has no property \""+propertyName+"\", or the property is inaccessible.");
return;
}
2011-01-23 21:39:07 +00:00
var type = typeof value;
var handler = handlerTypes[type];
2011-01-23 22:35:19 +00:00
// Do we know how to deal with this data type?
if (handler == undefined) {
2011-01-24 00:03:32 +00:00
error("Cannot create controller for data type \""+type+"\"");
2011-01-23 22:35:19 +00:00
return;
}
var controllerObject = construct( handler, arguments);
controllerObject.parent = _this;
2011-01-23 22:35:19 +00:00
// Were we able to make the controller?
if (!controllerObject) {
error("Error creating controller for \""+propertyName+"\".");
2011-01-23 22:35:19 +00:00
return;
2011-01-23 21:39:07 +00:00
}
2011-01-23 22:35:19 +00:00
// Success.
2011-01-23 22:57:12 +00:00
controllerContainer.appendChild(controllerObject.domElement);
controllers.push(controllerObject);
2011-01-24 02:59:29 +00:00
return controllerObject;
};
this.watchController = function(c) {
controllersWatched.push(c);
}
this.toggle = function() {
open ? this.hide() : this.show();
2011-01-23 23:43:02 +00:00
};
2011-01-24 02:59:29 +00:00
this.show = function() {
controllerContainer.style.height = '300px';
2011-01-24 02:59:29 +00:00
toggleButton.innerHTML = "Hide Controls";
open = true;
}
2011-01-24 02:59:29 +00:00
this.hide = function() {
controllerContainer.style.height = '0px';
2011-01-24 02:59:29 +00:00
toggleButton.innerHTML = "Show Controls";
open = false;
}
2011-01-23 21:54:09 +00:00
};