2011-01-29 04:04:33 +00:00
|
|
|
var GUI = function() {
|
2011-01-23 23:43:02 +00:00
|
|
|
|
|
|
|
var _this = this;
|
2011-02-08 20:56:00 +00:00
|
|
|
// For use with GUIScrubber
|
|
|
|
this.timer = null;
|
2011-01-23 22:35:19 +00:00
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
var MIN_WIDTH = 240;
|
2011-01-30 23:03:09 +00:00
|
|
|
var MAX_WIDTH = 500;
|
|
|
|
|
2011-01-24 20:11:19 +00:00
|
|
|
var controllers = [];
|
2011-01-29 04:04:33 +00:00
|
|
|
var listening = [];
|
|
|
|
|
|
|
|
var autoListen = true;
|
2011-01-30 23:03:09 +00:00
|
|
|
|
2011-01-29 04:04:33 +00:00
|
|
|
var listenInterval;
|
2011-01-29 04:28:12 +00:00
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
// Sum total of heights of controllers in this gui
|
|
|
|
var controllerHeight;
|
|
|
|
|
|
|
|
var curControllerContainerHeight = 0;
|
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
var _this = this;
|
2011-01-30 23:03:09 +00:00
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
var open = false;
|
2011-01-31 23:15:29 +00:00
|
|
|
|
|
|
|
// Prevents checkForOverflow bug in which loaded gui appearance
|
|
|
|
// settings are not respected by presence of scrollbar.
|
2011-01-31 15:47:07 +00:00
|
|
|
var explicitOpenHeight = false;
|
2011-02-02 18:12:55 +00:00
|
|
|
|
|
|
|
// How big we get when we open
|
2011-01-31 15:47:07 +00:00
|
|
|
var openHeight;
|
|
|
|
|
|
|
|
var name;
|
2011-01-30 23:03:09 +00:00
|
|
|
|
|
|
|
var resizeTo = 0;
|
|
|
|
var resizeTimeout;
|
2011-01-29 04:28:12 +00:00
|
|
|
|
2011-02-08 20:56:00 +00:00
|
|
|
var width = 280;
|
|
|
|
|
2011-01-29 00:38:58 +00:00
|
|
|
this.domElement = document.createElement('div');
|
2011-01-30 23:03:09 +00:00
|
|
|
this.domElement.setAttribute('class', 'guidat');
|
2011-02-08 20:56:00 +00:00
|
|
|
this.domElement.style.width = width+'px';
|
2011-01-29 00:38:58 +00:00
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
var controllerContainer = document.createElement('div');
|
|
|
|
controllerContainer.setAttribute('class', 'guidat-controllers');
|
2011-01-29 04:28:12 +00:00
|
|
|
|
2011-02-02 18:12:55 +00:00
|
|
|
|
|
|
|
// Firefox hack to prevent horizontal scrolling
|
2011-02-01 03:06:34 +00:00
|
|
|
controllerContainer.addEventListener('DOMMouseScroll', function(e) {
|
|
|
|
|
|
|
|
var scrollAmount = this.scrollTop;
|
|
|
|
|
|
|
|
if (e.wheelDelta) {
|
|
|
|
scrollAmount+=e.wheelDelta;
|
|
|
|
} else if (e.detail) {
|
|
|
|
scrollAmount+=e.detail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.preventDefault) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
e.returnValue = false;
|
|
|
|
|
|
|
|
controllerContainer.scrollTop = scrollAmount;
|
|
|
|
|
|
|
|
}, false);
|
|
|
|
|
2011-01-29 00:38:58 +00:00
|
|
|
controllerContainer.style.height = '0px';
|
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
var toggleButton = document.createElement('a');
|
|
|
|
toggleButton.setAttribute('class', 'guidat-toggle');
|
2011-01-29 00:38:58 +00:00
|
|
|
toggleButton.setAttribute('href', '#');
|
|
|
|
toggleButton.innerHTML = "Show Controls";
|
2011-01-30 23:03:09 +00:00
|
|
|
|
|
|
|
var toggleDragged = false;
|
|
|
|
var dragDisplacementY = 0;
|
|
|
|
var togglePressed = false;
|
|
|
|
|
|
|
|
var my, pmy, mx, pmx;
|
|
|
|
|
|
|
|
var resize = function(e) {
|
|
|
|
pmy = my;
|
|
|
|
pmx = mx;
|
|
|
|
my = e.pageY;
|
|
|
|
mx = e.pageX;
|
|
|
|
|
2011-02-08 20:56:00 +00:00
|
|
|
var dmy = _this.timer ? pmy - my : my - pmy;
|
2011-02-02 18:12:55 +00:00
|
|
|
|
|
|
|
if (!open) {
|
|
|
|
if (dmy < 0) {
|
|
|
|
open = true;
|
|
|
|
curControllerContainerHeight = openHeight = 1;
|
|
|
|
toggleButton.innerHTML = name || "Hide Controls";
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2011-01-31 15:47:07 +00:00
|
|
|
}
|
|
|
|
|
2011-02-08 20:56:00 +00:00
|
|
|
// TODO: Flip this if you want to resize to the right.
|
2011-01-30 23:03:09 +00:00
|
|
|
var dmx = pmx - mx;
|
|
|
|
|
|
|
|
if (dmy > 0 &&
|
|
|
|
curControllerContainerHeight > controllerHeight) {
|
|
|
|
var d = GUI.map(curControllerContainerHeight, controllerHeight, controllerHeight + 100, 1, 0);
|
2011-01-31 04:53:33 +00:00
|
|
|
dmy *= d;
|
2011-01-30 23:03:09 +00:00
|
|
|
}
|
2011-01-31 04:53:33 +00:00
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
toggleDragged = true;
|
|
|
|
dragDisplacementY += dmy;
|
|
|
|
dragDisplacementX += dmx;
|
|
|
|
openHeight += dmy;
|
2011-02-08 20:56:00 +00:00
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
curControllerContainerHeight += dmy;
|
|
|
|
controllerContainer.style.height = openHeight+'px';
|
2011-02-08 20:56:00 +00:00
|
|
|
|
|
|
|
if (!_this.timer) {
|
|
|
|
width += dmx;
|
|
|
|
width = GUI.constrain(width, MIN_WIDTH, MAX_WIDTH);
|
|
|
|
_this.domElement.style.width = width+'px';
|
|
|
|
}
|
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
checkForOverflow();
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
2011-01-29 00:38:58 +00:00
|
|
|
toggleButton.addEventListener('click', function(e) {
|
|
|
|
e.preventDefault();
|
2011-01-30 23:03:09 +00:00
|
|
|
return false;
|
2011-01-29 00:38:58 +00:00
|
|
|
}, false);
|
2011-01-30 23:03:09 +00:00
|
|
|
|
|
|
|
|
2011-02-08 20:56:00 +00:00
|
|
|
var correctWidth = function() {
|
|
|
|
// Clears lingering slider column
|
|
|
|
_this.domElement.style.width = (width+1)+'px';
|
|
|
|
setTimeout(function() {
|
|
|
|
_this.domElement.style.width = width+'px';
|
|
|
|
}, 1);
|
|
|
|
};
|
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
document.addEventListener('mouseup', function(e) {
|
|
|
|
|
|
|
|
if (togglePressed && !toggleDragged) {
|
|
|
|
|
|
|
|
_this.toggle();
|
|
|
|
|
2011-02-08 20:56:00 +00:00
|
|
|
if (!_this.timer) {
|
|
|
|
correctWidth();
|
|
|
|
}
|
2011-01-30 23:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (togglePressed && toggleDragged) {
|
|
|
|
|
2011-02-08 20:56:00 +00:00
|
|
|
if (dragDisplacementX == 0 && !_this.timer) {
|
2011-01-30 23:03:09 +00:00
|
|
|
|
2011-02-08 20:56:00 +00:00
|
|
|
correctWidth();
|
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (openHeight > controllerHeight) {
|
|
|
|
|
|
|
|
clearTimeout(resizeTimeout);
|
|
|
|
openHeight = resizeTo = controllerHeight;
|
|
|
|
beginResize();
|
|
|
|
|
|
|
|
} 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-08 20:56:00 +00:00
|
|
|
}
|
2011-01-30 23:03:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
document.removeEventListener('mousemove', resize, false);
|
|
|
|
e.preventDefault();
|
|
|
|
toggleDragged = false;
|
|
|
|
togglePressed = false;
|
|
|
|
|
|
|
|
return false;
|
2011-01-29 00:38:58 +00:00
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
}, false);
|
|
|
|
|
2011-02-02 18:12:55 +00:00
|
|
|
this.domElement.appendChild(controllerContainer);
|
2011-02-08 20:56:00 +00:00
|
|
|
this.domElement.appendChild(toggleButton);
|
2011-01-29 00:38:58 +00:00
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
if (GUI.autoPlace) {
|
|
|
|
if(GUI.autoPlaceContainer == null) {
|
|
|
|
GUI.autoPlaceContainer = document.createElement('div');
|
|
|
|
GUI.autoPlaceContainer.setAttribute("id", "guidat");
|
2011-02-01 03:06:34 +00:00
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
document.body.appendChild(GUI.autoPlaceContainer);
|
|
|
|
}
|
|
|
|
GUI.autoPlaceContainer.appendChild(this.domElement);
|
|
|
|
}
|
|
|
|
|
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) {
|
2011-01-31 23:50:06 +00:00
|
|
|
// 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-01-29 04:04:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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 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 construct = function(constructor, args) {
|
|
|
|
function F() {
|
|
|
|
return constructor.apply(this, args);
|
|
|
|
}
|
|
|
|
F.prototype = constructor.prototype;
|
|
|
|
return new F();
|
|
|
|
};
|
2011-02-02 18:12:55 +00:00
|
|
|
|
|
|
|
this.divider = function() {
|
|
|
|
controllerContainer.appendChild(document.createElement('hr'));
|
|
|
|
}
|
2011-01-29 00:38:58 +00:00
|
|
|
|
|
|
|
this.add = function() {
|
2011-02-02 18:12:55 +00:00
|
|
|
|
2011-01-23 21:39:07 +00:00
|
|
|
var object = arguments[0];
|
2011-02-02 18:12:55 +00:00
|
|
|
|
|
|
|
if (arguments.length == 1) {
|
|
|
|
for (var i in object) {
|
|
|
|
this.add(object, i);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-02-08 20:56:00 +00:00
|
|
|
GUI.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) {
|
2011-01-30 23:03:09 +00:00
|
|
|
GUI.error(object + " either has no property \""+propertyName+"\", or the property is inaccessible.");
|
2011-01-23 22:35:19 +00:00
|
|
|
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-30 23:03:09 +00:00
|
|
|
GUI.error("Cannot create controller for data type \""+type+"\"");
|
2011-01-23 22:35:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-30 23:03:09 +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) {
|
2011-01-30 23:03:09 +00:00
|
|
|
GUI.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-31 04:53:33 +00:00
|
|
|
GUI.allControllers.push(controllerObject);
|
2011-01-29 00:38:58 +00:00
|
|
|
|
2011-01-31 04:53:33 +00:00
|
|
|
// Do we have a saved value for this controller?
|
|
|
|
if (type != "function" &&
|
|
|
|
GUI.saveIndex < GUI.savedValues.length) {
|
|
|
|
controllerObject.setValue(GUI.savedValues[GUI.saveIndex]);
|
|
|
|
GUI.saveIndex++;
|
|
|
|
}
|
2011-01-31 15:47:07 +00:00
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
// Compute sum height of controllers.
|
2011-01-31 15:47:07 +00:00
|
|
|
checkForOverflow();
|
|
|
|
|
2011-01-31 23:15:29 +00:00
|
|
|
// Prevents checkForOverflow bug in which loaded gui appearance
|
|
|
|
// settings are not respected by presence of scrollbar.
|
|
|
|
if (!explicitOpenHeight) {
|
|
|
|
openHeight = controllerHeight;
|
|
|
|
}
|
2011-01-30 23:03:09 +00:00
|
|
|
|
2011-02-02 18:12:55 +00:00
|
|
|
if (this.timer != null) {
|
|
|
|
new GUI.Scrubber(controllerObject, this.timer);
|
|
|
|
}
|
|
|
|
|
2011-01-24 02:59:29 +00:00
|
|
|
return controllerObject;
|
|
|
|
|
2011-01-23 21:23:53 +00:00
|
|
|
}
|
|
|
|
|
2011-01-30 23:03:09 +00:00
|
|
|
var checkForOverflow = function() {
|
2011-01-31 15:47:07 +00:00
|
|
|
controllerHeight = 0;
|
|
|
|
for (var i in controllers) {
|
|
|
|
controllerHeight += controllers[i].domElement.offsetHeight;
|
|
|
|
}
|
2011-02-08 15:40:38 +00:00
|
|
|
return;
|
2011-01-30 23:03:09 +00:00
|
|
|
if (controllerHeight - 1 > openHeight) {
|
|
|
|
controllerContainer.style.overflowY = "auto";
|
|
|
|
} else {
|
|
|
|
controllerContainer.style.overflowY = "hidden";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-02 18:12:55 +00:00
|
|
|
var handlerTypes = {
|
|
|
|
"number": GUI.NumberController,
|
|
|
|
"string": GUI.StringController,
|
|
|
|
"boolean": GUI.BooleanController,
|
|
|
|
"function": GUI.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-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
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
this.reset = function() {
|
2011-02-02 18:12:55 +00:00
|
|
|
// TODO
|
2011-01-31 15:47:07 +00:00
|
|
|
}
|
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-30 23:03:09 +00:00
|
|
|
toggleButton.innerHTML = name || "Hide Controls";
|
|
|
|
resizeTo = openHeight;
|
|
|
|
clearTimeout(resizeTimeout);
|
|
|
|
beginResize();
|
2011-01-24 02:59:29 +00:00
|
|
|
open = true;
|
|
|
|
}
|
2011-01-29 00:38:58 +00:00
|
|
|
|
2011-01-24 02:59:29 +00:00
|
|
|
this.hide = function() {
|
2011-01-30 23:03:09 +00:00
|
|
|
toggleButton.innerHTML = name || "Show Controls";
|
|
|
|
resizeTo = 0;
|
|
|
|
clearTimeout(resizeTimeout);
|
|
|
|
beginResize();
|
2011-01-24 02:59:29 +00:00
|
|
|
open = false;
|
|
|
|
}
|
2011-01-30 23:03:09 +00:00
|
|
|
|
|
|
|
this.name = function(n) {
|
|
|
|
name = n;
|
|
|
|
toggleButton.innerHTML = n;
|
|
|
|
}
|
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
// used in saveURL
|
|
|
|
this.appearanceVars = function() {
|
2011-02-01 00:49:14 +00:00
|
|
|
return [open, width, openHeight, controllerContainer.scrollTop]
|
2011-01-31 15:47:07 +00:00
|
|
|
}
|
|
|
|
|
2011-01-30 23:03:09 +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;
|
|
|
|
} else {
|
|
|
|
resizeTimeout = setTimeout(beginResize, 1000/30);
|
|
|
|
}
|
|
|
|
controllerContainer.style.height = Math.round(curControllerContainerHeight)+'px';
|
2011-01-31 15:47:07 +00:00
|
|
|
checkForOverflow();
|
|
|
|
}
|
2011-01-31 23:15:29 +00:00
|
|
|
|
|
|
|
// Load saved appearance:
|
2011-01-31 15:47:07 +00:00
|
|
|
|
|
|
|
if (GUI.guiIndex < GUI.savedAppearanceVars.length) {
|
2011-02-01 00:49:14 +00:00
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
width = parseInt(GUI.savedAppearanceVars[GUI.guiIndex][1]);
|
2011-02-01 04:02:22 +00:00
|
|
|
//_this.domElement.style.width = width+"px";
|
2011-01-30 23:03:09 +00:00
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
openHeight = parseInt(GUI.savedAppearanceVars[GUI.guiIndex][2]);
|
2011-01-31 23:15:29 +00:00
|
|
|
explicitOpenHeight = true;
|
2011-01-31 15:47:07 +00:00
|
|
|
if (eval(GUI.savedAppearanceVars[GUI.guiIndex][0]) == true) {
|
2011-02-01 00:49:14 +00:00
|
|
|
curControllerContainerHeight = openHeight;
|
|
|
|
var t = GUI.savedAppearanceVars[GUI.guiIndex][3]
|
|
|
|
|
|
|
|
// Hack.
|
|
|
|
setTimeout(function() {
|
|
|
|
controllerContainer.scrollTop = t;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
if (GUI.scrollTop > -1) {
|
|
|
|
document.body.scrollTop = GUI.scrollTop;
|
|
|
|
}
|
|
|
|
resizeTo = openHeight;
|
2011-01-31 15:47:07 +00:00
|
|
|
this.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI.guiIndex++;
|
2011-01-30 23:03:09 +00:00
|
|
|
}
|
2011-01-29 00:38:58 +00:00
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
GUI.allGuis.push(this);
|
|
|
|
|
2011-01-23 21:54:09 +00:00
|
|
|
};
|
2011-01-29 04:04:33 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
GUI.saveURL = function() {
|
|
|
|
title = window.location;
|
|
|
|
url = GUI.replaceGetVar("saveString", GUI.getSaveString());
|
|
|
|
window.location = url;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
//GUI.savedAppearanceVars = [];
|
|
|
|
var vals = saveString.split(",");
|
2011-01-31 15:47:07 +00:00
|
|
|
var numGuis = parseInt(vals[0]);
|
2011-02-01 00:49:14 +00:00
|
|
|
GUI.scrollTop = parseInt(vals[1]);
|
2011-01-31 15:47:07 +00:00
|
|
|
for (var i = 0; i < numGuis; i++) {
|
2011-02-01 00:49:14 +00:00
|
|
|
var appr = vals.splice(2, 4);
|
2011-01-31 15:47:07 +00:00
|
|
|
GUI.savedAppearanceVars.push(appr);
|
|
|
|
}
|
2011-02-01 00:49:14 +00:00
|
|
|
|
|
|
|
GUI.savedValues = vals.splice(2, vals.length);
|
2011-02-02 18:12:55 +00:00
|
|
|
|
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-01-31 04:53:33 +00:00
|
|
|
var vals = [];
|
2011-01-31 15:47:07 +00:00
|
|
|
|
|
|
|
vals.push(GUI.allGuis.length);
|
2011-02-01 00:49:14 +00:00
|
|
|
vals.push(document.body.scrollTop);
|
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
|
|
|
|
for (var i in GUI.allGuis) {
|
|
|
|
var av = GUI.allGuis[i].appearanceVars();
|
2011-02-01 00:49:14 +00:00
|
|
|
for (var j = 0; j < av.length; j++) {
|
2011-01-31 15:47:07 +00:00
|
|
|
vals.push(av[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-31 04:53:33 +00:00
|
|
|
for (var i in GUI.allControllers) {
|
2011-01-31 15:47:07 +00:00
|
|
|
|
|
|
|
// We don't save values for functions.
|
2011-01-31 04:53:33 +00:00
|
|
|
if (GUI.allControllers[i].type == "function") {
|
|
|
|
continue;
|
|
|
|
}
|
2011-01-31 15:47:07 +00:00
|
|
|
|
2011-01-31 04:53:33 +00:00
|
|
|
var v = GUI.allControllers[i].getValue();
|
2011-01-31 15:47:07 +00:00
|
|
|
|
|
|
|
// Round numbers so they don't get enormous
|
|
|
|
if (GUI.allControllers[i].type == "number") {
|
|
|
|
v = GUI.roundToDecimal(v, 4);
|
|
|
|
}
|
|
|
|
|
2011-01-31 04:53:33 +00:00
|
|
|
vals.push(v);
|
2011-01-31 15:47:07 +00:00
|
|
|
|
2011-01-31 04:53:33 +00:00
|
|
|
}
|
2011-01-31 15:47:07 +00:00
|
|
|
|
|
|
|
return vals.join(',');
|
|
|
|
|
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++) {
|
|
|
|
hash = hashes[i].split("=")
|
|
|
|
if (hash == undefined) continue;
|
2011-01-31 15:47:07 +00:00
|
|
|
if (hash[0] == v) {
|
2011-01-31 04:53:33 +00:00
|
|
|
return hash[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
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++) {
|
|
|
|
hash = hashes[i].split("=")
|
|
|
|
if (hash == undefined) continue;
|
|
|
|
if (hash[0] == varName) {
|
|
|
|
return loc.replace(hash[1], val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window.location.href.indexOf('?') != -1) {
|
|
|
|
return loc + "&"+varName+"="+val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return loc+"?"+varName+"="+val;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
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() {
|
|
|
|
alert(GUI.getSaveString());
|
|
|
|
}
|
2011-01-30 23:03: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));
|
2011-01-30 23:03:09 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI.constrain = function (v, o1, o2) {
|
2011-01-29 04:04:33 +00:00
|
|
|
if (v < o1) v = o1;
|
|
|
|
else if (v > o2) v = o2;
|
|
|
|
return v;
|
|
|
|
}
|
2011-01-30 23:03:09 +00:00
|
|
|
|
|
|
|
GUI.error = function(str) {
|
2011-01-31 15:47:07 +00:00
|
|
|
if (typeof console.error == 'function') {
|
2011-02-02 18:12:55 +00:00
|
|
|
console.error("[GUI ERROR] " + str);
|
2011-01-30 23:03:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-02-02 20:51:58 +00:00
|
|
|
GUI.getOffset = function(obj) {
|
|
|
|
var curleft = curtop = 0;
|
|
|
|
if (obj.offsetParent) {
|
|
|
|
do {
|
|
|
|
curleft += obj.offsetLeft;
|
|
|
|
curtop += obj.offsetTop;
|
|
|
|
} while (obj = obj.offsetParent);
|
|
|
|
return {left: curleft,top: curtop};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
GUI.roundToDecimal = function(n, decimals) {
|
|
|
|
var t = Math.pow(10, decimals);
|
|
|
|
return Math.round(n*t)/t;
|
|
|
|
}
|
|
|
|
|
2011-02-02 18:12:55 +00:00
|
|
|
GUI.extendController = function(clazz) {
|
|
|
|
clazz.prototype = new GUI.Controller();
|
|
|
|
clazz.prototype.constructor = clazz;
|
|
|
|
}
|
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
if (GUI.getVarFromURL('saveString') != null) GUI.load(GUI.getVarFromURL('saveString'));
|