2011-01-29 04:04:33 +00:00
|
|
|
var GUI = function() {
|
2011-01-23 23:43:02 +00:00
|
|
|
|
|
|
|
var _this = this;
|
2011-01-23 22:35:19 +00:00
|
|
|
|
2011-01-24 20:11:19 +00:00
|
|
|
var controllers = [];
|
2011-01-29 04:04:33 +00:00
|
|
|
var listening = [];
|
|
|
|
|
|
|
|
var autoListen = true;
|
|
|
|
var listenInterval;
|
2011-01-29 04:28:12 +00:00
|
|
|
|
2011-01-29 00:38:58 +00:00
|
|
|
var _this = this, open = false,
|
|
|
|
controllers = [], controllersWatched = [];
|
2011-01-29 04:28:12 +00:00
|
|
|
|
2011-01-29 00:38:58 +00:00
|
|
|
this.domElement = document.createElement('div');
|
|
|
|
this.domElement.setAttribute('id', 'guidat');
|
|
|
|
|
|
|
|
controllerContainer = document.createElement('div');
|
|
|
|
controllerContainer.setAttribute('id', 'guidat-controllers');
|
2011-01-29 04:28:12 +00:00
|
|
|
|
|
|
|
// @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?
|
2011-01-29 00:38:58 +00:00
|
|
|
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);
|
|
|
|
|
2011-01-29 04:28:12 +00:00
|
|
|
|
2011-01-29 04:04:33 +00:00
|
|
|
this.autoListenIntervalTime = 1000/60;
|
|
|
|
|
|
|
|
var createListenInterval = function() {
|
|
|
|
listenInterval = setInterval(function() {
|
|
|
|
_this.listen();
|
|
|
|
}, this.autoListenIntervalTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.__defineSetter__("autoListen", function(v) {
|
|
|
|
autoListen = v;
|
|
|
|
if (!autoListen) {
|
|
|
|
clearInterval(listenInterval);
|
|
|
|
} else {
|
|
|
|
if (listening.length > 0) createListenInterval();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.__defineGetter__("autoListen", function(v) {
|
|
|
|
return autoListen;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.listenTo = function(controller) {
|
|
|
|
// TODO: check for duplicates
|
|
|
|
if (listening.length == 0) {
|
|
|
|
createListenInterval();
|
|
|
|
}
|
|
|
|
listening.push(controller);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.unlistenTo = function(controller) {
|
|
|
|
// TODO
|
|
|
|
};
|
|
|
|
|
|
|
|
this.listen = function(whoToListenTo) {
|
|
|
|
|
|
|
|
var arr = whoToListenTo || listening;
|
|
|
|
for (var i in arr) {
|
|
|
|
arr[i].updateDisplay();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.listenAll = function() {
|
|
|
|
this.listen(controllers);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.autoListen = true;
|
2011-01-23 23:43:02 +00:00
|
|
|
|
2011-01-29 00:38:58 +00:00
|
|
|
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
|
|
|
}
|
2011-01-29 00:38:58 +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-29 00:38:58 +00:00
|
|
|
|
2011-01-23 22:57:12 +00:00
|
|
|
// Have we already added this?
|
2011-01-24 20:11:19 +00:00
|
|
|
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-29 00:38:58 +00:00
|
|
|
|
2011-01-23 22:35:19 +00:00
|
|
|
var value = object[propertyName];
|
2011-01-29 00:38:58 +00:00
|
|
|
|
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-29 00:38:58 +00:00
|
|
|
|
2011-01-23 21:39:07 +00:00
|
|
|
var type = typeof value;
|
2011-01-29 00:38:58 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-29 04:28:12 +00:00
|
|
|
var args = [_this]; // Set first arg (parent) to this
|
2011-01-29 04:04:33 +00:00
|
|
|
for (var j = 0; j < arguments.length; j++) {
|
|
|
|
args.push(arguments[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var controllerObject = construct(handler, args);
|
2011-01-23 22:35:19 +00:00
|
|
|
|
|
|
|
// Were we able to make the controller?
|
2011-01-29 00:38:58 +00:00
|
|
|
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-29 00:38:58 +00:00
|
|
|
|
2011-01-23 22:35:19 +00:00
|
|
|
// Success.
|
2011-01-23 22:57:12 +00:00
|
|
|
controllerContainer.appendChild(controllerObject.domElement);
|
2011-01-24 20:11:19 +00:00
|
|
|
controllers.push(controllerObject);
|
2011-01-29 00:38:58 +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 = {
|
2011-01-26 22:55:56 +00:00
|
|
|
"number": NumberController,
|
|
|
|
"string": StringController,
|
|
|
|
"boolean": BooleanController,
|
|
|
|
"function": FunctionController
|
2011-01-23 21:47:42 +00:00
|
|
|
};
|
|
|
|
|
2011-01-24 20:11:19 +00:00
|
|
|
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:57:12 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
this.toggle = function() {
|
2011-01-29 01:23:20 +00:00
|
|
|
open ? this.hide() : this.show();
|
2011-01-23 23:43:02 +00:00
|
|
|
};
|
2011-01-29 00:38:58 +00:00
|
|
|
|
2011-01-24 02:59:29 +00:00
|
|
|
this.show = function() {
|
2011-01-29 00:38:58 +00:00
|
|
|
controllerContainer.style.height = '300px';
|
2011-01-24 02:59:29 +00:00
|
|
|
toggleButton.innerHTML = "Hide Controls";
|
|
|
|
open = true;
|
|
|
|
}
|
2011-01-29 00:38:58 +00:00
|
|
|
|
2011-01-24 02:59:29 +00:00
|
|
|
this.hide = function() {
|
2011-01-29 00:38:58 +00:00
|
|
|
controllerContainer.style.height = '0px';
|
2011-01-24 02:59:29 +00:00
|
|
|
toggleButton.innerHTML = "Show Controls";
|
|
|
|
open = false;
|
|
|
|
}
|
2011-01-29 00:38:58 +00:00
|
|
|
|
2011-01-23 21:54:09 +00:00
|
|
|
};
|
2011-01-29 04:04:33 +00:00
|
|
|
|
|
|
|
// Util functions
|
|
|
|
|
|
|
|
GUI.makeUnselectable = function(elem) {
|
|
|
|
elem.onselectstart = function() { return false; };
|
|
|
|
elem.style.MozUserSelect = "none";
|
|
|
|
elem.style.KhtmlUserSelect = "none";
|
|
|
|
elem.unselectable = "on";
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI.makeSelectable = function(elem) {
|
|
|
|
elem.onselectstart = function() { };
|
|
|
|
elem.style.MozUserSelect = "auto";
|
|
|
|
elem.style.KhtmlUserSelect = "auto";
|
|
|
|
elem.unselectable = "off";
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI.map = function(v, i1, i2, o1, o2) {
|
|
|
|
var v = o1 + (o2 - o1) * ((v - i1) / (i2 - i1));
|
|
|
|
if (v < o1) v = o1;
|
|
|
|
else if (v > o2) v = o2;
|
|
|
|
return v;
|
|
|
|
}
|