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