2011-04-15 23:54:31 +00:00
|
|
|
var GUI = function(parameters) {
|
2011-01-23 23:43:02 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var _this = this;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var MIN_WIDTH = 240;
|
|
|
|
var MAX_WIDTH = 500;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var controllers = [];
|
|
|
|
var listening = [];
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var autoListen = true;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var listenInterval;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
// Sum total of heights of controllers in this gui
|
|
|
|
var controllerHeight;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var curControllerContainerHeight = 0;
|
|
|
|
|
2011-02-11 19:12:07 +00:00
|
|
|
var _this = this;
|
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var open = false;
|
|
|
|
var width = 280;
|
|
|
|
|
|
|
|
// Prevents checkForOverflow bug in which loaded gui appearance
|
|
|
|
// settings are not respected by presence of scrollbar.
|
|
|
|
var explicitOpenHeight = false;
|
|
|
|
|
|
|
|
// How big we get when we open
|
|
|
|
var openHeight;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var name;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var resizeTo = 0;
|
|
|
|
var resizeTimeout;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-04-15 23:54:31 +00:00
|
|
|
|
2011-04-16 00:23:43 +00:00
|
|
|
this.domElement = document.createElement('div');
|
2011-02-10 20:50:57 +00:00
|
|
|
this.domElement.setAttribute('class', 'guidat');
|
|
|
|
this.domElement.style.width = width+'px';
|
|
|
|
|
|
|
|
var controllerContainer = document.createElement('div');
|
|
|
|
controllerContainer.setAttribute('class', 'guidat-controllers');
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
// Firefox hack to prevent horizontal scrolling
|
|
|
|
controllerContainer.addEventListener('DOMMouseScroll', function(e) {
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var scrollAmount = this.scrollTop;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
if (e.wheelDelta) {
|
2011-02-11 19:12:07 +00:00
|
|
|
scrollAmount+=e.wheelDelta;
|
2011-02-10 20:50:57 +00:00
|
|
|
} else if (e.detail) {
|
|
|
|
scrollAmount+=e.detail;
|
|
|
|
}
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
if (e.preventDefault) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
e.returnValue = false;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
controllerContainer.scrollTop = scrollAmount;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
}, false);
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
controllerContainer.style.height = '0px';
|
|
|
|
|
|
|
|
var toggleButton = document.createElement('a');
|
|
|
|
toggleButton.setAttribute('class', 'guidat-toggle');
|
|
|
|
toggleButton.setAttribute('href', '#');
|
|
|
|
toggleButton.innerHTML = "Show Controls";
|
2011-02-11 19:12:07 +00:00
|
|
|
|
|
|
|
var toggleDragged = false;
|
|
|
|
var dragDisplacementY = 0;
|
|
|
|
var togglePressed = false;
|
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var my, pmy, mx, pmx;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var resize = function(e) {
|
|
|
|
pmy = my;
|
|
|
|
pmx = mx;
|
|
|
|
my = e.pageY;
|
|
|
|
mx = e.pageX;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var dmy = my - pmy;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
|
|
|
if (!open) {
|
2011-02-10 20:50:57 +00:00
|
|
|
if (dmy > 0) {
|
|
|
|
open = true;
|
|
|
|
curControllerContainerHeight = openHeight = 1;
|
|
|
|
toggleButton.innerHTML = name || "Hide Controls";
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
// TODO: Flip this if you want to resize to the left.
|
|
|
|
var dmx = pmx - mx;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
|
|
|
if (dmy > 0 &&
|
2011-02-10 20:50:57 +00:00
|
|
|
curControllerContainerHeight > controllerHeight) {
|
|
|
|
var d = GUI.map(curControllerContainerHeight, controllerHeight, controllerHeight + 100, 1, 0);
|
|
|
|
dmy *= d;
|
|
|
|
}
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
toggleDragged = true;
|
|
|
|
dragDisplacementY += dmy;
|
|
|
|
dragDisplacementX += dmx;
|
|
|
|
openHeight += dmy;
|
|
|
|
width += dmx;
|
|
|
|
curControllerContainerHeight += dmy;
|
|
|
|
controllerContainer.style.height = openHeight+'px';
|
|
|
|
width = GUI.constrain(width, MIN_WIDTH, MAX_WIDTH);
|
|
|
|
_this.domElement.style.width = width+'px';
|
|
|
|
checkForOverflow();
|
|
|
|
};
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
toggleButton.addEventListener('mousedown', function(e) {
|
|
|
|
pmy = my = e.pageY;
|
|
|
|
pmx = mx = e.pageX;
|
|
|
|
togglePressed = true;
|
|
|
|
e.preventDefault();
|
|
|
|
dragDisplacementY = 0;
|
|
|
|
dragDisplacementX = 0;
|
|
|
|
document.addEventListener('mousemove', resize, false);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
toggleButton.addEventListener('click', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
document.addEventListener('mouseup', function(e) {
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
if (togglePressed && !toggleDragged) {
|
|
|
|
_this.toggle();
|
|
|
|
}
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
if (togglePressed && toggleDragged) {
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
if (dragDisplacementX == 0) {
|
2011-02-11 19:12:07 +00:00
|
|
|
adaptToScrollbar();
|
2011-02-10 20:50:57 +00:00
|
|
|
}
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
if (openHeight > controllerHeight) {
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
clearTimeout(resizeTimeout);
|
|
|
|
openHeight = resizeTo = controllerHeight;
|
|
|
|
beginResize();
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
} else if (controllerContainer.children.length >= 1) {
|
|
|
|
|
|
|
|
var singleControllerHeight = controllerContainer.children[0].offsetHeight;
|
|
|
|
clearTimeout(resizeTimeout);
|
|
|
|
var target = Math.round(curControllerContainerHeight/singleControllerHeight)*singleControllerHeight-1;
|
|
|
|
resizeTo = target;
|
|
|
|
if (resizeTo <= 0) {
|
|
|
|
_this.hide();
|
|
|
|
openHeight = singleControllerHeight*2;
|
|
|
|
} else {
|
|
|
|
openHeight = resizeTo;
|
|
|
|
beginResize();
|
|
|
|
}
|
|
|
|
}
|
2011-02-11 19:12:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
document.removeEventListener('mousemove', resize, false);
|
|
|
|
e.preventDefault();
|
|
|
|
toggleDragged = false;
|
|
|
|
togglePressed = false;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
}, false);
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.domElement.appendChild(controllerContainer);
|
|
|
|
this.domElement.appendChild(toggleButton);
|
2011-04-16 00:23:43 +00:00
|
|
|
|
|
|
|
if ( parameters.domElement ){
|
|
|
|
GUI.autoPlace = false;
|
|
|
|
parameters.domElement.appendChild(this.domElement);
|
|
|
|
}
|
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
if (GUI.autoPlace) {
|
|
|
|
if(GUI.autoPlaceContainer == null) {
|
|
|
|
GUI.autoPlaceContainer = document.createElement('div');
|
|
|
|
GUI.autoPlaceContainer.setAttribute("id", "guidat");
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
document.body.appendChild(GUI.autoPlaceContainer);
|
|
|
|
}
|
|
|
|
GUI.autoPlaceContainer.appendChild(this.domElement);
|
|
|
|
}
|
2011-04-16 00:23:43 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.autoListenIntervalTime = 1000/60;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var createListenInterval = function() {
|
|
|
|
listenInterval = setInterval(function() {
|
|
|
|
_this.listen();
|
|
|
|
}, this.autoListenIntervalTime);
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.__defineSetter__("autoListen", function(v) {
|
|
|
|
autoListen = v;
|
|
|
|
if (!autoListen) {
|
|
|
|
clearInterval(listenInterval);
|
2011-02-11 19:12:07 +00:00
|
|
|
} else {
|
2011-02-10 20:50:57 +00:00
|
|
|
if (listening.length > 0) createListenInterval();
|
|
|
|
}
|
|
|
|
});
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.__defineGetter__("autoListen", function(v) {
|
|
|
|
return autoListen;
|
|
|
|
});
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.listenTo = function(controller) {
|
|
|
|
// TODO: check for duplicates
|
|
|
|
if (listening.length == 0) {
|
|
|
|
createListenInterval();
|
|
|
|
}
|
|
|
|
listening.push(controller);
|
|
|
|
};
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.unlistenTo = function(controller) {
|
|
|
|
// TODO: test this
|
|
|
|
for(var i = 0; i < listening.length; i++) {
|
|
|
|
if(listening[i] == controller) listening.splice(i, 1);
|
|
|
|
}
|
|
|
|
if(listening.length <= 0) {
|
|
|
|
clearInterval(listenInterval);
|
|
|
|
}
|
|
|
|
};
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.listen = function(whoToListenTo) {
|
|
|
|
var arr = whoToListenTo || listening;
|
|
|
|
for (var i in arr) {
|
|
|
|
arr[i].updateDisplay();
|
|
|
|
}
|
|
|
|
};
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.listenAll = function() {
|
|
|
|
this.listen(controllers);
|
2011-02-11 19:12:07 +00:00
|
|
|
}
|
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.autoListen = true;
|
|
|
|
|
|
|
|
var alreadyControlled = function(object, propertyName) {
|
|
|
|
for (var i in controllers) {
|
|
|
|
if (controllers[i].object == object &&
|
|
|
|
controllers[i].propertyName == propertyName) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var construct = function(constructor, args) {
|
|
|
|
function F() {
|
|
|
|
return constructor.apply(this, args);
|
|
|
|
}
|
|
|
|
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)) {
|
2011-02-11 19:12:07 +00:00
|
|
|
// GUI.error("Controller for \"" + propertyName+"\" already added.");
|
|
|
|
// return;
|
2011-02-10 20:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var value = object[propertyName];
|
|
|
|
|
|
|
|
// Does this value exist? Is it accessible?
|
|
|
|
if (value == undefined) {
|
|
|
|
GUI.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) {
|
|
|
|
GUI.error("Cannot create controller for data type \""+type+"\"");
|
|
|
|
return;
|
|
|
|
}
|
2011-02-11 19:12:07 +00:00
|
|
|
|
|
|
|
var args = [this]; // Set first arg (parent) to this
|
2011-02-10 20:50:57 +00:00
|
|
|
for (var j = 0; j < arguments.length; j++) {
|
|
|
|
args.push(arguments[j]);
|
|
|
|
}
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var controllerObject = construct(handler, args);
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
// Were we able to make the controller?
|
|
|
|
if (!controllerObject) {
|
|
|
|
GUI.error("Error creating controller for \""+propertyName+"\".");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success.
|
|
|
|
controllerContainer.appendChild(controllerObject.domElement);
|
|
|
|
controllers.push(controllerObject);
|
|
|
|
GUI.allControllers.push(controllerObject);
|
|
|
|
|
|
|
|
// Do we have a saved value for this controller?
|
2011-02-11 19:12:07 +00:00
|
|
|
if (type != "function" &&
|
2011-02-10 20:50:57 +00:00
|
|
|
GUI.saveIndex < GUI.savedValues.length) {
|
|
|
|
controllerObject.setValue(GUI.savedValues[GUI.saveIndex]);
|
|
|
|
GUI.saveIndex++;
|
|
|
|
}
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
// Compute sum height of controllers.
|
|
|
|
checkForOverflow();
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
// Prevents checkForOverflow bug in which loaded gui appearance
|
|
|
|
// settings are not respected by presence of scrollbar.
|
|
|
|
if (!explicitOpenHeight) {
|
|
|
|
openHeight = controllerHeight;
|
|
|
|
}
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
return controllerObject;
|
2011-02-11 19:12:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var checkForOverflow = function() {
|
|
|
|
controllerHeight = 0;
|
|
|
|
for (var i in controllers) {
|
|
|
|
controllerHeight += controllers[i].domElement.offsetHeight;
|
|
|
|
}
|
|
|
|
if (controllerHeight - 1 > openHeight) {
|
|
|
|
controllerContainer.style.overflowY = "auto";
|
|
|
|
} else {
|
|
|
|
controllerContainer.style.overflowY = "hidden";
|
2011-02-11 19:12:07 +00:00
|
|
|
}
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var handlerTypes = {
|
|
|
|
"number": GUI.NumberController,
|
|
|
|
"string": GUI.StringController,
|
|
|
|
"boolean": GUI.BooleanController,
|
|
|
|
"function": GUI.FunctionController
|
|
|
|
};
|
2011-02-11 19:12:07 +00:00
|
|
|
|
|
|
|
var alreadyControlled = function(object, propertyName) {
|
|
|
|
for (var i in controllers) {
|
|
|
|
if (controllers[i].object == object &&
|
|
|
|
controllers[i].propertyName == propertyName) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
var construct = function(constructor, args) {
|
|
|
|
|
2011-01-24 00:47:26 +00:00
|
|
|
function F() {
|
|
|
|
return constructor.apply(this, args);
|
|
|
|
}
|
|
|
|
F.prototype = constructor.prototype;
|
|
|
|
return new F();
|
|
|
|
};
|
2011-01-23 23:43:02 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.reset = function() {
|
|
|
|
// TODO
|
2011-02-11 19:12:07 +00:00
|
|
|
}
|
2011-02-10 20:50:57 +00:00
|
|
|
|
|
|
|
// GUI ... GUI
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.toggle = function() {
|
|
|
|
open ? this.hide() : this.show();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.show = function() {
|
|
|
|
toggleButton.innerHTML = name || "Hide Controls";
|
|
|
|
resizeTo = openHeight;
|
|
|
|
clearTimeout(resizeTimeout);
|
|
|
|
beginResize();
|
|
|
|
open = true;
|
2011-02-11 19:12:07 +00:00
|
|
|
}
|
2011-02-10 20:50:57 +00:00
|
|
|
|
|
|
|
this.hide = function() {
|
|
|
|
toggleButton.innerHTML = name || "Show Controls";
|
|
|
|
resizeTo = 0;
|
|
|
|
clearTimeout(resizeTimeout);
|
|
|
|
beginResize();
|
|
|
|
open = false;
|
2011-02-11 19:12:07 +00:00
|
|
|
}
|
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.name = function(n) {
|
|
|
|
name = n;
|
|
|
|
toggleButton.innerHTML = n;
|
2011-02-11 19:12:07 +00:00
|
|
|
}
|
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
// used in saveURL
|
|
|
|
this.appearanceVars = function() {
|
2011-02-11 19:12:07 +00:00
|
|
|
return [open, width, openHeight, controllerContainer.scrollTop]
|
|
|
|
}
|
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var beginResize = function() {
|
|
|
|
//console.log("Resizing from " + curControllerContainerHeight + " to " + resizeTo);
|
|
|
|
curControllerContainerHeight += (resizeTo - curControllerContainerHeight)*0.6;
|
|
|
|
if (Math.abs(curControllerContainerHeight-resizeTo) < 1) {
|
|
|
|
curControllerContainerHeight = resizeTo;
|
2011-02-11 19:12:07 +00:00
|
|
|
adaptToScrollbar();
|
|
|
|
|
|
|
|
} else {
|
2011-02-10 20:50:57 +00:00
|
|
|
resizeTimeout = setTimeout(beginResize, 1000/30);
|
|
|
|
}
|
|
|
|
controllerContainer.style.height = Math.round(curControllerContainerHeight)+'px';
|
|
|
|
checkForOverflow();
|
2011-02-11 19:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var adaptToScrollbar = function() {
|
|
|
|
// Clears lingering slider column
|
|
|
|
_this.domElement.style.width = (width+1)+'px';
|
|
|
|
setTimeout(function() {
|
|
|
|
_this.domElement.style.width = width+'px';
|
|
|
|
}, 1);
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
// Load saved appearance:
|
|
|
|
|
|
|
|
if (GUI.guiIndex < GUI.savedAppearanceVars.length) {
|
|
|
|
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
width = parseInt(GUI.savedAppearanceVars[GUI.guiIndex][1]);
|
|
|
|
_this.domElement.style.width = width+"px";
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
openHeight = parseInt(GUI.savedAppearanceVars[GUI.guiIndex][2]);
|
|
|
|
explicitOpenHeight = true;
|
|
|
|
if (eval(GUI.savedAppearanceVars[GUI.guiIndex][0]) == true) {
|
|
|
|
curControllerContainerHeight = openHeight;
|
2011-02-11 19:12:07 +00:00
|
|
|
var t = GUI.savedAppearanceVars[GUI.guiIndex][3]
|
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
// Hack.
|
|
|
|
setTimeout(function() {
|
|
|
|
controllerContainer.scrollTop = t;
|
|
|
|
}, 0);
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
if (GUI.scrollTop > -1) {
|
|
|
|
document.body.scrollTop = GUI.scrollTop;
|
|
|
|
}
|
|
|
|
resizeTo = openHeight;
|
|
|
|
this.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI.guiIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI.allGuis.push(this);
|
2011-01-31 15:47:07 +00:00
|
|
|
|
2011-02-14 21:34:27 +00:00
|
|
|
// Add hide listener if this is the first GUI.
|
|
|
|
if (GUI.allGuis.length == 1) {
|
|
|
|
window.addEventListener('keyup', function(e) {
|
|
|
|
// Hide on "H"
|
|
|
|
if (e.keyCode == 72) {
|
|
|
|
GUI.toggleHide();
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
2011-01-23 21:54:09 +00:00
|
|
|
};
|
2011-01-29 04:04:33 +00:00
|
|
|
|
2011-02-14 21:34:27 +00:00
|
|
|
// Do not set this directly.
|
|
|
|
GUI.hidden = false;
|
|
|
|
|
|
|
|
|
2011-01-31 04:53:33 +00:00
|
|
|
// Static members
|
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
GUI.autoPlace = true;
|
|
|
|
GUI.autoPlaceContainer = null;
|
2011-01-31 04:53:33 +00:00
|
|
|
GUI.allControllers = [];
|
2011-01-31 15:47:07 +00:00
|
|
|
GUI.allGuis = [];
|
2011-01-31 04:53:33 +00:00
|
|
|
|
2011-02-14 21:34:27 +00:00
|
|
|
|
|
|
|
GUI.toggleHide = function() {
|
|
|
|
if (GUI.hidden) {
|
|
|
|
GUI.show();
|
|
|
|
} else {
|
|
|
|
GUI.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI.show = function() {
|
|
|
|
GUI.hidden = false;
|
|
|
|
for (var i in GUI.allGuis) {
|
|
|
|
GUI.allGuis[i].domElement.style.display = "block";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI.hide = function() {
|
|
|
|
GUI.hidden = true;
|
|
|
|
for (var i in GUI.allGuis) {
|
|
|
|
GUI.allGuis[i].domElement.style.display = "none";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
GUI.saveURL = function() {
|
2011-02-10 21:00:01 +00:00
|
|
|
var url = GUI.replaceGetVar("saveString", GUI.getSaveString());
|
2011-02-10 20:50:57 +00:00
|
|
|
window.location = url;
|
2011-01-31 04:53:33 +00:00
|
|
|
};
|
|
|
|
|
2011-02-01 00:49:14 +00:00
|
|
|
GUI.scrollTop = -1;
|
|
|
|
|
2011-01-31 04:53:33 +00:00
|
|
|
GUI.load = function(saveString) {
|
2011-02-01 00:49:14 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
//GUI.savedAppearanceVars = [];
|
|
|
|
var vals = saveString.split(",");
|
|
|
|
var numGuis = parseInt(vals[0]);
|
|
|
|
GUI.scrollTop = parseInt(vals[1]);
|
|
|
|
for (var i = 0; i < numGuis; i++) {
|
|
|
|
var appr = vals.splice(2, 4);
|
|
|
|
GUI.savedAppearanceVars.push(appr);
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI.savedValues = vals.splice(2, vals.length);
|
|
|
|
|
2011-01-31 04:53:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GUI.savedValues = [];
|
2011-01-31 15:47:07 +00:00
|
|
|
GUI.savedAppearanceVars = [];
|
2011-01-31 04:53:33 +00:00
|
|
|
|
|
|
|
GUI.getSaveString = function() {
|
2011-01-31 15:47:07 +00:00
|
|
|
|
2011-02-10 21:00:01 +00:00
|
|
|
var vals = [],
|
|
|
|
i;
|
2011-02-10 20:50:57 +00:00
|
|
|
|
|
|
|
vals.push(GUI.allGuis.length);
|
|
|
|
vals.push(document.body.scrollTop);
|
|
|
|
|
|
|
|
|
2011-02-10 21:00:01 +00:00
|
|
|
for (i in GUI.allGuis) {
|
2011-02-10 20:50:57 +00:00
|
|
|
var av = GUI.allGuis[i].appearanceVars();
|
|
|
|
for (var j = 0; j < av.length; j++) {
|
|
|
|
vals.push(av[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-10 21:00:01 +00:00
|
|
|
for (i in GUI.allControllers) {
|
2011-02-10 20:50:57 +00:00
|
|
|
|
|
|
|
// We don't save values for functions.
|
|
|
|
if (GUI.allControllers[i].type == "function") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var v = GUI.allControllers[i].getValue();
|
|
|
|
|
|
|
|
// Round numbers so they don't get enormous
|
|
|
|
if (GUI.allControllers[i].type == "number") {
|
|
|
|
v = GUI.roundToDecimal(v, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
vals.push(v);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return vals.join(',');
|
|
|
|
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|
2011-01-31 04:53:33 +00:00
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
GUI.getVarFromURL = function(v) {
|
2011-01-31 04:53:33 +00:00
|
|
|
|
|
|
|
var vars = [], hash;
|
|
|
|
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
|
|
|
|
|
|
|
|
for (var i = 0; i < hashes.length; i++) {
|
2011-02-10 21:00:01 +00:00
|
|
|
hash = hashes[i].split("=");
|
2011-01-31 04:53:33 +00:00
|
|
|
if (hash == undefined) continue;
|
2011-02-10 20:50:57 +00:00
|
|
|
if (hash[0] == v) {
|
|
|
|
return hash[1];
|
|
|
|
}
|
2011-01-31 04:53:33 +00:00
|
|
|
}
|
2011-02-10 20:50:57 +00:00
|
|
|
|
|
|
|
return null;
|
2011-01-31 04:53:33 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
GUI.replaceGetVar = function(varName, val) {
|
|
|
|
|
|
|
|
var vars = [], hash;
|
|
|
|
var loc = window.location.href;
|
|
|
|
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
|
|
|
|
|
|
|
|
for (var i = 0; i < hashes.length; i++) {
|
2011-02-10 21:00:01 +00:00
|
|
|
hash = hashes[i].split("=");
|
2011-01-31 04:53:33 +00:00
|
|
|
if (hash == undefined) continue;
|
2011-02-10 20:50:57 +00:00
|
|
|
if (hash[0] == varName) {
|
|
|
|
return loc.replace(hash[1], val);
|
|
|
|
}
|
2011-01-31 04:53:33 +00:00
|
|
|
}
|
2011-02-10 20:50:57 +00:00
|
|
|
|
|
|
|
if (window.location.href.indexOf('?') != -1) {
|
|
|
|
return loc + "&"+varName+"="+val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return loc+"?"+varName+"="+val;
|
|
|
|
|
2011-01-31 04:53:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GUI.saveIndex = 0;
|
2011-01-31 15:47:07 +00:00
|
|
|
GUI.guiIndex = 0;
|
2011-01-31 04:53:33 +00:00
|
|
|
|
|
|
|
GUI.showSaveString = function() {
|
2011-02-10 20:50:57 +00:00
|
|
|
alert(GUI.getSaveString());
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|
2011-01-30 23:03:09 +00:00
|
|
|
|
2011-01-29 04:04:33 +00:00
|
|
|
// Util functions
|
|
|
|
|
|
|
|
GUI.makeUnselectable = function(elem) {
|
2011-02-10 20:50:57 +00:00
|
|
|
elem.onselectstart = function() { return false; };
|
|
|
|
elem.style.MozUserSelect = "none";
|
|
|
|
elem.style.KhtmlUserSelect = "none";
|
|
|
|
elem.unselectable = "on";
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-29 04:04:33 +00:00
|
|
|
GUI.makeSelectable = function(elem) {
|
2011-02-10 20:50:57 +00:00
|
|
|
elem.onselectstart = function() { };
|
|
|
|
elem.style.MozUserSelect = "auto";
|
|
|
|
elem.style.KhtmlUserSelect = "auto";
|
|
|
|
elem.unselectable = "off";
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|
2011-01-29 04:04:33 +00:00
|
|
|
|
|
|
|
GUI.map = function(v, i1, i2, o1, o2) {
|
2011-02-10 21:00:01 +00:00
|
|
|
return o1 + (o2 - o1) * ((v - i1) / (i2 - i1));
|
|
|
|
};
|
2011-01-30 23:03:09 +00:00
|
|
|
|
|
|
|
GUI.constrain = function (v, o1, o2) {
|
2011-02-10 20:50:57 +00:00
|
|
|
if (v < o1) v = o1;
|
|
|
|
else if (v > o2) v = o2;
|
|
|
|
return v;
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|
2011-01-30 23:03:09 +00:00
|
|
|
|
|
|
|
GUI.error = function(str) {
|
2011-02-10 20:50:57 +00:00
|
|
|
if (typeof console.error == 'function') {
|
|
|
|
console.error("[GUI ERROR] " + str);
|
|
|
|
}
|
2011-01-30 23:03:09 +00:00
|
|
|
};
|
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
GUI.roundToDecimal = function(n, decimals) {
|
2011-02-10 20:50:57 +00:00
|
|
|
var t = Math.pow(10, decimals);
|
|
|
|
return Math.round(n*t)/t;
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|
2011-01-31 15:47:07 +00:00
|
|
|
|
2011-02-01 18:45:22 +00:00
|
|
|
GUI.extendController = function(clazz) {
|
2011-02-10 20:50:57 +00:00
|
|
|
clazz.prototype = new GUI.Controller();
|
|
|
|
clazz.prototype.constructor = clazz;
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|
2011-02-01 18:45:22 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
if (GUI.getVarFromURL('saveString') != null) GUI.load(GUI.getVarFromURL('saveString'));
|