2011-01-23 21:23:53 +00:00
|
|
|
var GUI = new function() {
|
2011-01-23 23:43:02 +00:00
|
|
|
|
|
|
|
var _this = this;
|
2011-01-23 22:35:19 +00:00
|
|
|
|
|
|
|
// Contains list of properties we've add to the GUI in the following format:
|
2011-01-23 22:57:12 +00:00
|
|
|
// [object, propertyName, controllerObject]
|
2011-01-23 22:35:19 +00:00
|
|
|
var registeredProperties = [];
|
2011-01-23 23:43:02 +00:00
|
|
|
|
2011-01-23 21:39:07 +00:00
|
|
|
this.add = function() {
|
|
|
|
|
2011-01-23 22:57:12 +00:00
|
|
|
// We need to call GUI.start() before .add()
|
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:57:12 +00:00
|
|
|
// Have we already added this?
|
|
|
|
if (registeredPropertiesContains(object, propertyName)) {
|
|
|
|
error("Controller for \"" + propertyName+"\" already added.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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) {
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-23 22:57:12 +00:00
|
|
|
var controllerObject = handler.apply(this, arguments);
|
2011-01-23 22:35:19 +00:00
|
|
|
|
|
|
|
// Were we able to make the controller?
|
2011-01-23 22:57:12 +00:00
|
|
|
if (!controllerObject) {
|
2011-01-23 22:35:19 +00:00
|
|
|
error("Error creating controller for \""+propertyName+"\".");
|
|
|
|
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);
|
|
|
|
registeredProperties.push([object, propertyName, controllerObject]);
|
2011-01-23 22:35:19 +00:00
|
|
|
|
2011-01-24 02:59:29 +00:00
|
|
|
return controllerObject;
|
|
|
|
|
2011-01-23 21:23:53 +00:00
|
|
|
}
|
|
|
|
|
2011-01-23 21:47:42 +00:00
|
|
|
var addHandlers = {
|
|
|
|
|
|
|
|
"number": function() {
|
2011-01-24 00:47:26 +00:00
|
|
|
return construct(NumberController, arguments);
|
2011-01-23 21:47:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"string": function() {
|
2011-01-24 02:59:29 +00:00
|
|
|
return construct(StringController, arguments);
|
2011-01-23 21:47:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"boolean": function() {
|
2011-01-24 10:36:57 +00:00
|
|
|
return construct(BooleanController, arguments);
|
2011-01-23 21:47:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"function": function() {
|
2011-01-24 10:36:57 +00:00
|
|
|
return construct(FunctionController, arguments);
|
2011-01-23 21:47:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-01-23 22:57:12 +00:00
|
|
|
var registeredPropertiesContains = function(object, property) {
|
|
|
|
// TODO:
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2011-01-23 22:35:19 +00:00
|
|
|
var error = function(str) {
|
2011-01-23 22:37:28 +00:00
|
|
|
if (typeof console.log == 'function') {
|
|
|
|
console.error("[GUI ERROR] " + str);
|
|
|
|
}
|
2011-01-23 22:57:12 +00:00
|
|
|
};
|
2011-01-24 00:47:26 +00:00
|
|
|
|
|
|
|
var construct = function(constructor, args) {
|
|
|
|
function F() {
|
|
|
|
return constructor.apply(this, args);
|
|
|
|
}
|
|
|
|
F.prototype = constructor.prototype;
|
|
|
|
return new F();
|
|
|
|
};
|
2011-01-23 23:43:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GUI ... GUI
|
|
|
|
|
|
|
|
var domElement;
|
|
|
|
var controllerContainer;
|
|
|
|
var started = false;
|
|
|
|
var open = false;
|
|
|
|
|
2011-01-23 23:56:22 +00:00
|
|
|
// TODO: obtain this dynamically?
|
2011-01-24 00:21:36 +00:00
|
|
|
var domElementMarginTop = 300;
|
2011-01-23 23:56:22 +00:00
|
|
|
|
2011-01-23 23:43:02 +00:00
|
|
|
this.start = function() {
|
|
|
|
|
|
|
|
domElement = document.createElement('div');
|
|
|
|
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);
|
|
|
|
|
|
|
|
domElement.appendChild(controllerContainer);
|
|
|
|
domElement.appendChild(toggleButton);
|
|
|
|
|
2011-01-23 23:56:22 +00:00
|
|
|
domElement.style.marginTop = -domElementMarginTop+"px";
|
|
|
|
|
2011-01-23 23:43:02 +00:00
|
|
|
document.body.appendChild(domElement);
|
|
|
|
|
|
|
|
started = true;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
this.toggle = function() {
|
|
|
|
|
|
|
|
if (open) {
|
2011-01-24 00:03:32 +00:00
|
|
|
|
2011-01-24 02:59:29 +00:00
|
|
|
this.hide();
|
|
|
|
|
2011-01-23 23:43:02 +00:00
|
|
|
} else {
|
|
|
|
|
2011-01-24 02:59:29 +00:00
|
|
|
this.show();
|
2011-01-23 23:43:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2011-01-23 22:35:19 +00:00
|
|
|
|
2011-01-24 02:59:29 +00:00
|
|
|
this.show = function() {
|
|
|
|
domElement.style.marginTop = 0+"px";
|
|
|
|
toggleButton.innerHTML = "Hide Controls";
|
|
|
|
open = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hide = function() {
|
|
|
|
domElement.style.marginTop = -domElementMarginTop+"px";
|
|
|
|
toggleButton.innerHTML = "Show Controls";
|
|
|
|
open = false;
|
|
|
|
}
|
2011-01-23 21:54:09 +00:00
|
|
|
};
|