Removed trailing whitespace. Converted tabs to 4 spaces.

This commit is contained in:
Szymon Nowak 2011-02-10 21:50:57 +01:00
parent ac562a2deb
commit 3895cc188e
7 changed files with 797 additions and 797 deletions

View File

@ -1,20 +1,20 @@
GUI.BooleanController = function() { GUI.BooleanController = function() {
this.type = "boolean"; this.type = "boolean";
GUI.Controller.apply(this, arguments); GUI.Controller.apply(this, arguments);
var _this = this; var _this = this;
var input = document.createElement('input'); var input = document.createElement('input');
input.setAttribute('type', 'checkbox'); input.setAttribute('type', 'checkbox');
this.domElement.addEventListener('click', function(e) { this.domElement.addEventListener('click', function(e) {
input.checked = !input.checked; input.checked = !input.checked;
e.preventDefault(); e.preventDefault();
_this.setValue(input.checked); _this.setValue(input.checked);
}, false); }, false);
input.addEventListener('mouseup', function(e) { input.addEventListener('mouseup', function(e) {
input.checked = !input.checked; // counteracts default. input.checked = !input.checked; // counteracts default.
}, false); }, false);
this.domElement.style.cursor = "pointer"; this.domElement.style.cursor = "pointer";
@ -22,17 +22,17 @@ GUI.BooleanController = function() {
this.domElement.appendChild(input); this.domElement.appendChild(input);
this.updateDisplay = function() { this.updateDisplay = function() {
input.checked = _this.getValue(); input.checked = _this.getValue();
}; };
this.setValue = function(val) { this.setValue = function(val) {
if (typeof val != "boolean") { if (typeof val != "boolean") {
try { try {
val = eval(val); val = eval(val);
} catch (e) {} } catch (e) {}
} }
return GUI.Controller.prototype.setValue.call(this, val); return GUI.Controller.prototype.setValue.call(this, val);
} }
}; };

View File

@ -1,30 +1,30 @@
GUI.FunctionController = function() { GUI.FunctionController = function() {
this.type = "function"; this.type = "function";
var _this = this; var _this = this;
GUI.Controller.apply(this, arguments); GUI.Controller.apply(this, arguments);
this.domElement.addEventListener('click', function() { this.domElement.addEventListener('click', function() {
_this.fire(); _this.fire();
}, false); }, false);
this.domElement.style.cursor = "pointer"; this.domElement.style.cursor = "pointer";
this.propertyNameElement.style.cursor = "pointer"; this.propertyNameElement.style.cursor = "pointer";
var fireFunction = null; var fireFunction = null;
this.onFire = function(fnc) { this.onFire = function(fnc) {
fireFunction = fnc; fireFunction = fnc;
return this; return this;
} }
this.fire = function() { this.fire = function() {
if (fireFunction != null) { if (fireFunction != null) {
fireFunction.call(this); fireFunction.call(this);
} }
_this.object[_this.propertyName].call(_this.object); _this.object[_this.propertyName].call(_this.object);
}; };
}; };
GUI.extendController(GUI.FunctionController); GUI.extendController(GUI.FunctionController);

View File

@ -1,10 +1,10 @@
GUI.Controller = function() { GUI.Controller = function() {
this.parent = arguments[0]; this.parent = arguments[0];
this.object = arguments[1]; this.object = arguments[1];
this.propertyName = arguments[2]; this.propertyName = arguments[2];
if (arguments.length > 0) this.initialValue = this.propertyName[this.object]; if (arguments.length > 0) this.initialValue = this.propertyName[this.object];
this.domElement = document.createElement('div'); this.domElement = document.createElement('div');
this.domElement.setAttribute('class', 'guidat-controller ' + this.type); this.domElement.setAttribute('class', 'guidat-controller ' + this.type);
@ -22,75 +22,75 @@ GUI.Controller.prototype.changeFunction = null;
GUI.Controller.prototype.finishChangeFunction = null; GUI.Controller.prototype.finishChangeFunction = null;
GUI.Controller.prototype.name = function(n) { GUI.Controller.prototype.name = function(n) {
this.propertyNameElement.innerHTML = n; this.propertyNameElement.innerHTML = n;
return this; return this;
}; };
GUI.Controller.prototype.reset = function() { GUI.Controller.prototype.reset = function() {
this.setValue(this.initialValue); this.setValue(this.initialValue);
return this; return this;
}; };
GUI.Controller.prototype.listen = function() { GUI.Controller.prototype.listen = function() {
this.parent.listenTo(this); this.parent.listenTo(this);
return this; return this;
} }
GUI.Controller.prototype.unlisten = function() { GUI.Controller.prototype.unlisten = function() {
this.parent.unlistenTo(this); // <--- hasn't been tested yet this.parent.unlistenTo(this); // <--- hasn't been tested yet
return this; return this;
} }
GUI.Controller.prototype.setValue = function(n) { GUI.Controller.prototype.setValue = function(n) {
this.object[this.propertyName] = n; this.object[this.propertyName] = n;
if (this.changeFunction != null) { if (this.changeFunction != null) {
this.changeFunction.call(this, n); this.changeFunction.call(this, n);
} }
this.updateDisplay(); this.updateDisplay();
return this; return this;
} }
GUI.Controller.prototype.getValue = function() { GUI.Controller.prototype.getValue = function() {
return this.object[this.propertyName]; return this.object[this.propertyName];
} }
GUI.Controller.prototype.updateDisplay = function() {} GUI.Controller.prototype.updateDisplay = function() {}
GUI.Controller.prototype.onChange = function(fnc) { GUI.Controller.prototype.onChange = function(fnc) {
this.changeFunction = fnc; this.changeFunction = fnc;
return this; return this;
} }
GUI.Controller.prototype.onFinishChange = function(fnc) { GUI.Controller.prototype.onFinishChange = function(fnc) {
this.finishChangeFunction = fnc; this.finishChangeFunction = fnc;
return this; return this;
} }
GUI.Controller.prototype.options = function() { GUI.Controller.prototype.options = function() {
var _this = this; var _this = this;
var select = document.createElement('select'); var select = document.createElement('select');
if (arguments.length == 1) { if (arguments.length == 1) {
var arr = arguments[0]; var arr = arguments[0];
for (var i in arr) { for (var i in arr) {
var opt = document.createElement('option'); var opt = document.createElement('option');
opt.innerHTML = i; opt.innerHTML = i;
opt.setAttribute('value', arr[i]); opt.setAttribute('value', arr[i]);
select.appendChild(opt); select.appendChild(opt);
} }
} else { } else {
for (var i = 0; i < arguments.length; i++) { for (var i = 0; i < arguments.length; i++) {
var opt = document.createElement('option'); var opt = document.createElement('option');
opt.innerHTML = arguments[i]; opt.innerHTML = arguments[i];
opt.setAttribute('value', arguments[i]); opt.setAttribute('value', arguments[i]);
select.appendChild(opt); select.appendChild(opt);
} }
} }
select.addEventListener('change', function() { select.addEventListener('change', function() {
_this.setValue(this.value); _this.setValue(this.value);
if (_this.finishChangeFunction != null) { if (_this.finishChangeFunction != null) {
_this.finishChangeFunction.call(this, _this.getValue()); _this.finishChangeFunction.call(this, _this.getValue());
} }
}); });
_this.domElement.appendChild(select); _this.domElement.appendChild(select);
return this; return this;
} }

View File

@ -1,6 +1,6 @@
GUI.NumberController = function() { GUI.NumberController = function() {
this.type = "number"; this.type = "number";
GUI.Controller.apply(this, arguments); GUI.Controller.apply(this, arguments);
@ -19,11 +19,11 @@ GUI.NumberController = function() {
var step = arguments[5]; var step = arguments[5];
if (!step) { if (!step) {
if (min != undefined && max != undefined) { if (min != undefined && max != undefined) {
step = (max-min)*0.01; step = (max-min)*0.01;
} else { } else {
step = 1; step = 1;
} }
} }
var numberField = document.createElement('input'); var numberField = document.createElement('input');
@ -38,113 +38,113 @@ GUI.NumberController = function() {
var slider; var slider;
if (min != undefined && max != undefined) { if (min != undefined && max != undefined) {
slider = new GUI.Slider(this, min, max, step, this.getValue()); slider = new GUI.Slider(this, min, max, step, this.getValue());
this.domElement.appendChild(slider.domElement); this.domElement.appendChild(slider.domElement);
} }
numberField.addEventListener('blur', function(e) { numberField.addEventListener('blur', function(e) {
var val = parseFloat(this.value); var val = parseFloat(this.value);
console.log(val); console.log(val);
if (!isNaN(val)) { if (!isNaN(val)) {
_this.setValue(val); _this.setValue(val);
} }
}, false); }, false);
numberField.addEventListener('mousewheel', function(e) { numberField.addEventListener('mousewheel', function(e) {
e.preventDefault(); e.preventDefault();
_this.setValue(_this.getValue() + Math.abs(e.wheelDeltaY)/e.wheelDeltaY*step); _this.setValue(_this.getValue() + Math.abs(e.wheelDeltaY)/e.wheelDeltaY*step);
return false; return false;
}, false); }, false);
numberField.addEventListener('mousedown', function(e) { numberField.addEventListener('mousedown', function(e) {
py = y = e.pageY; py = y = e.pageY;
clickedNumberField = true; clickedNumberField = true;
document.addEventListener('mousemove', dragNumberField, false); document.addEventListener('mousemove', dragNumberField, false);
document.addEventListener('mouseup', mouseup, false); document.addEventListener('mouseup', mouseup, false);
}, false); }, false);
// Handle up arrow and down arrow // Handle up arrow and down arrow
numberField.addEventListener('keydown', function(e) { numberField.addEventListener('keydown', function(e) {
switch(e.keyCode) { switch(e.keyCode) {
case 38: // up case 38: // up
var newVal = _this.getValue() + step; var newVal = _this.getValue() + step;
_this.setValue(newVal); _this.setValue(newVal);
break; break;
case 40: // down case 40: // down
var newVal = _this.getValue() - step; var newVal = _this.getValue() - step;
_this.setValue(newVal); _this.setValue(newVal);
break; break;
} }
}, false); }, false);
var mouseup = function(e) { var mouseup = function(e) {
document.removeEventListener('mousemove', dragNumberField, false); document.removeEventListener('mousemove', dragNumberField, false);
GUI.makeSelectable(_this.parent.domElement); GUI.makeSelectable(_this.parent.domElement);
GUI.makeSelectable(numberField); GUI.makeSelectable(numberField);
if (clickedNumberField && !draggedNumberField) { if (clickedNumberField && !draggedNumberField) {
numberField.focus(); numberField.focus();
numberField.select(); numberField.select();
} }
draggedNumberField = false; draggedNumberField = false;
clickedNumberField = false; clickedNumberField = false;
if (_this.finishChangeFunction != null) { if (_this.finishChangeFunction != null) {
_this.finishChangeFunction.call(this, _this.getValue()); _this.finishChangeFunction.call(this, _this.getValue());
} }
document.removeEventListener('mouseup', mouseup, false); document.removeEventListener('mouseup', mouseup, false);
} }
var dragNumberField = function(e) { var dragNumberField = function(e) {
draggedNumberField = true; draggedNumberField = true;
e.preventDefault(); e.preventDefault();
// We don't want to be highlighting this field as we scroll. // We don't want to be highlighting this field as we scroll.
// Or any other fields in this gui for that matter ... // Or any other fields in this gui for that matter ...
// TODO: Make makeUselectable go through each element and child element. // TODO: Make makeUselectable go through each element and child element.
GUI.makeUnselectable(_this.parent.domElement); GUI.makeUnselectable(_this.parent.domElement);
GUI.makeUnselectable(numberField); GUI.makeUnselectable(numberField);
py = y; py = y;
y = e.pageY; y = e.pageY;
var dy = py - y; var dy = py - y;
var newVal = _this.getValue() + dy*step; var newVal = _this.getValue() + dy*step;
_this.setValue(newVal); _this.setValue(newVal);
return false; return false;
} }
this.options = function() { this.options = function() {
_this.noSlider(); _this.noSlider();
_this.domElement.removeChild(numberField); _this.domElement.removeChild(numberField);
return GUI.Controller.prototype.options.apply(this, arguments); return GUI.Controller.prototype.options.apply(this, arguments);
}; };
this.noSlider = function() { this.noSlider = function() {
if (slider) { if (slider) {
_this.domElement.removeChild(slider.domElement); _this.domElement.removeChild(slider.domElement);
} }
return this; return this;
}; };
this.setValue = function(val) { this.setValue = function(val) {
val = parseFloat(val); val = parseFloat(val);
if (min != undefined && val <= min) { if (min != undefined && val <= min) {
val = min; val = min;
} else if (max != undefined && val >= max) { } else if (max != undefined && val >= max) {
val = max; val = max;
} }
return GUI.Controller.prototype.setValue.call(this, val); return GUI.Controller.prototype.setValue.call(this, val);
} }
this.updateDisplay = function() { this.updateDisplay = function() {
numberField.value = GUI.roundToDecimal(_this.getValue(), 4); numberField.value = GUI.roundToDecimal(_this.getValue(), 4);
if (slider) slider.value = _this.getValue(); if (slider) slider.value = _this.getValue();
} }
}; };
GUI.extendController(GUI.NumberController); GUI.extendController(GUI.NumberController);

View File

@ -1,8 +1,8 @@
GUI.StringController = function() { GUI.StringController = function() {
this.type = "string"; this.type = "string";
var _this = this; var _this = this;
GUI.Controller.apply(this, arguments); GUI.Controller.apply(this, arguments);
var input = document.createElement('input'); var input = document.createElement('input');
@ -13,31 +13,31 @@ GUI.StringController = function() {
input.setAttribute('spellcheck', 'false'); input.setAttribute('spellcheck', 'false');
this.domElement.addEventListener('mouseup', function() { this.domElement.addEventListener('mouseup', function() {
input.focus(); input.focus();
input.select(); input.select();
}, false); }, false);
// TODO: getting messed up on ctrl a // TODO: getting messed up on ctrl a
input.addEventListener('keyup', function(e) { input.addEventListener('keyup', function(e) {
if (e.keyCode == 13 && _this.finishChangeFunction != null) { if (e.keyCode == 13 && _this.finishChangeFunction != null) {
_this.finishChangeFunction.call(this, _this.getValue()); _this.finishChangeFunction.call(this, _this.getValue());
} }
_this.setValue(input.value); _this.setValue(input.value);
}, false); }, false);
input.addEventListener('blur', function() { input.addEventListener('blur', function() {
if (_this.finishChangeFunction != null) { if (_this.finishChangeFunction != null) {
_this.finishChangeFunction.call(this, _this.getValue()); _this.finishChangeFunction.call(this, _this.getValue());
} }
}, false); }, false);
this.updateDisplay = function() { this.updateDisplay = function() {
input.value = _this.getValue(); input.value = _this.getValue();
} }
this.options = function() { this.options = function() {
_this.domElement.removeChild(input); _this.domElement.removeChild(input);
return GUI.Controller.prototype.options.apply(this, arguments); return GUI.Controller.prototype.options.apply(this, arguments);
}; };
this.domElement.appendChild(input); this.domElement.appendChild(input);

View File

@ -1,69 +1,69 @@
GUI.Slider = function(numberController, min, max, step, initValue) { GUI.Slider = function(numberController, min, max, step, initValue) {
var min = min; var min = min;
var max = max; var max = max;
var step = step; var step = step;
var clicked = false; var clicked = false;
var _this = this; var _this = this;
var x, px; var x, px;
this.domElement = document.createElement('div'); this.domElement = document.createElement('div');
this.domElement.setAttribute('class', 'guidat-slider-bg'); this.domElement.setAttribute('class', 'guidat-slider-bg');
this.fg = document.createElement('div'); this.fg = document.createElement('div');
this.fg.setAttribute('class', 'guidat-slider-fg'); this.fg.setAttribute('class', 'guidat-slider-fg');
this.domElement.appendChild(this.fg); this.domElement.appendChild(this.fg);
var findPos = function(obj) { var findPos = function(obj) {
var curleft = curtop = 0; var curleft = curtop = 0;
if (obj.offsetParent) { if (obj.offsetParent) {
do { do {
curleft += obj.offsetLeft; curleft += obj.offsetLeft;
curtop += obj.offsetTop; curtop += obj.offsetTop;
} while (obj = obj.offsetParent); } while (obj = obj.offsetParent);
return [curleft,curtop]; return [curleft,curtop];
} }
} }
this.__defineSetter__('value', function(e) { this.__defineSetter__('value', function(e) {
var pct = GUI.map(e, min, max, 0, 100); var pct = GUI.map(e, min, max, 0, 100);
this.fg.style.width = pct+"%"; this.fg.style.width = pct+"%";
}); });
var onDrag = function(e) { var onDrag = function(e) {
if (!clicked) return; if (!clicked) return;
var pos = findPos(_this.domElement); var pos = findPos(_this.domElement);
var val = GUI.map(e.pageX, pos[0], pos[0] + _this.domElement.offsetWidth, min, max); var val = GUI.map(e.pageX, pos[0], pos[0] + _this.domElement.offsetWidth, min, max);
val = Math.round(val/step)*step; val = Math.round(val/step)*step;
numberController.setValue(val); numberController.setValue(val);
} }
this.domElement.addEventListener('mousedown', function(e) { this.domElement.addEventListener('mousedown', function(e) {
clicked = true; clicked = true;
x = px = e.pageX; x = px = e.pageX;
_this.domElement.setAttribute('class', 'guidat-slider-bg active'); _this.domElement.setAttribute('class', 'guidat-slider-bg active');
_this.fg.setAttribute('class', 'guidat-slider-fg active'); _this.fg.setAttribute('class', 'guidat-slider-fg active');
onDrag(e); onDrag(e);
document.addEventListener('mouseup', mouseup, false); document.addEventListener('mouseup', mouseup, false);
}, false); }, false);
var mouseup = function(e) { var mouseup = function(e) {
_this.domElement.setAttribute('class', 'guidat-slider-bg'); _this.domElement.setAttribute('class', 'guidat-slider-bg');
_this.fg.setAttribute('class', 'guidat-slider-fg'); _this.fg.setAttribute('class', 'guidat-slider-fg');
clicked = false; clicked = false;
if (numberController.finishChangeFunction != null) { if (numberController.finishChangeFunction != null) {
numberController.finishChangeFunction.call(this, numberController.getValue()); numberController.finishChangeFunction.call(this, numberController.getValue());
} }
document.removeEventListener('mouseup', mouseup, false); document.removeEventListener('mouseup', mouseup, false);
}; };
document.addEventListener('mousemove', onDrag, false); document.addEventListener('mousemove', onDrag, false);
this.value = initValue; this.value = initValue;
} }

924
gui.js

File diff suppressed because it is too large Load Diff