2011-01-23 21:23:53 +00:00
|
|
|
var GUI = new function() {
|
|
|
|
|
2011-01-23 22:35:19 +00:00
|
|
|
|
|
|
|
// Contains list of properties we've add to the GUI in the following format:
|
|
|
|
// [object, propertyName, controllerDomElement]
|
|
|
|
var registeredProperties = [];
|
|
|
|
|
|
|
|
var domElement;
|
|
|
|
var started = false;
|
|
|
|
this.start = function() {
|
|
|
|
|
|
|
|
domElement = document.createElement('div');
|
|
|
|
domElement.setAttribute('id', 'guidat');
|
|
|
|
|
|
|
|
|
|
|
|
started = true;
|
|
|
|
}
|
|
|
|
|
2011-01-23 21:39:07 +00:00
|
|
|
this.add = function() {
|
|
|
|
|
2011-01-23 22:35:19 +00:00
|
|
|
if (!started) {
|
|
|
|
error("Make sure to call GUI.start() in the window.onload function");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 21:39:07 +00:00
|
|
|
|
2011-01-23 22:35:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-23 21:39:07 +00:00
|
|
|
var type = typeof value;
|
2011-01-23 21:47:42 +00:00
|
|
|
var handler = addHandlers[type];
|
2011-01-23 22:35:19 +00:00
|
|
|
|
|
|
|
// Do we know how to deal with this data type?
|
|
|
|
if (handler == undefined) {
|
|
|
|
error("Cannot create controller for data type \"" + object + "\"");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var controllerDomElement = handler.apply(this, arguments);
|
|
|
|
|
|
|
|
// Were we able to make the controller?
|
|
|
|
if (!controllerDomElement) {
|
|
|
|
error("Error creating controller for \""+propertyName+"\".");
|
|
|
|
return;
|
2011-01-23 21:39:07 +00:00
|
|
|
}
|
|
|
|
|
2011-01-23 22:35:19 +00:00
|
|
|
// Success.
|
|
|
|
registeredProperties.push([object, propertyName, controllerDomElement]);
|
|
|
|
|
2011-01-23 21:23:53 +00:00
|
|
|
}
|
|
|
|
|
2011-01-23 21:47:42 +00:00
|
|
|
var addHandlers = {
|
|
|
|
|
|
|
|
"number": function() {
|
|
|
|
//
|
|
|
|
},
|
|
|
|
|
|
|
|
"string": function() {
|
|
|
|
//
|
|
|
|
},
|
|
|
|
|
|
|
|
"boolean": function() {
|
|
|
|
//
|
|
|
|
},
|
|
|
|
|
|
|
|
"function": function() {
|
|
|
|
//
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-01-23 22:35:19 +00:00
|
|
|
var error = function(str) {
|
|
|
|
console.error("[GUI ERROR] " + str);
|
|
|
|
}
|
|
|
|
|
2011-01-23 21:39:07 +00:00
|
|
|
};
|