2011-02-01 18:45:22 +00:00
|
|
|
GUI.Controller = function() {
|
2011-01-25 08:22:04 +00:00
|
|
|
|
2011-01-29 04:04:33 +00:00
|
|
|
this.parent = arguments[0];
|
|
|
|
this.object = arguments[1];
|
|
|
|
this.propertyName = arguments[2];
|
2011-01-26 01:55:59 +00:00
|
|
|
|
2011-01-31 15:47:07 +00:00
|
|
|
if (arguments.length > 0) this.initialValue = this.propertyName[this.object];
|
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
this.domElement = document.createElement('div');
|
|
|
|
this.domElement.setAttribute('class', 'guidat-controller ' + this.type);
|
|
|
|
|
|
|
|
this.propertyNameElement = document.createElement('span');
|
|
|
|
this.propertyNameElement.setAttribute('class', 'guidat-propertyname');
|
2011-01-29 04:04:33 +00:00
|
|
|
this.name(this.propertyName);
|
2011-01-25 08:22:04 +00:00
|
|
|
this.domElement.appendChild(this.propertyNameElement);
|
|
|
|
|
2011-01-29 04:04:33 +00:00
|
|
|
GUI.makeUnselectable(this.domElement);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-02-01 18:45:22 +00:00
|
|
|
GUI.Controller.prototype.changeFunction = null;
|
2011-02-09 19:39:18 +00:00
|
|
|
GUI.Controller.prototype.finishChangeFunction = null;
|
2011-01-29 04:04:33 +00:00
|
|
|
|
2011-02-01 18:45:22 +00:00
|
|
|
GUI.Controller.prototype.name = function(n) {
|
2011-01-29 04:04:33 +00:00
|
|
|
this.propertyNameElement.innerHTML = n;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2011-02-01 18:45:22 +00:00
|
|
|
GUI.Controller.prototype.reset = function() {
|
2011-01-31 15:47:07 +00:00
|
|
|
this.setValue(this.initialValue);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2011-02-01 18:45:22 +00:00
|
|
|
GUI.Controller.prototype.listen = function() {
|
2011-01-29 04:04:33 +00:00
|
|
|
this.parent.listenTo(this);
|
2011-01-31 15:47:07 +00:00
|
|
|
return this;
|
2011-01-29 04:04:33 +00:00
|
|
|
}
|
2011-01-29 04:28:12 +00:00
|
|
|
|
2011-02-01 18:45:22 +00:00
|
|
|
GUI.Controller.prototype.unlisten = function() {
|
2011-02-01 03:06:34 +00:00
|
|
|
this.parent.unlistenTo(this); // <--- hasn't been tested yet
|
2011-01-31 15:47:07 +00:00
|
|
|
return this;
|
2011-01-29 04:28:12 +00:00
|
|
|
}
|
2011-01-29 04:04:33 +00:00
|
|
|
|
2011-02-01 18:45:22 +00:00
|
|
|
GUI.Controller.prototype.setValue = function(n) {
|
2011-01-29 04:04:33 +00:00
|
|
|
this.object[this.propertyName] = n;
|
|
|
|
if (this.changeFunction != null) {
|
|
|
|
this.changeFunction.call(this, n);
|
|
|
|
}
|
|
|
|
this.updateDisplay();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2011-02-01 18:45:22 +00:00
|
|
|
GUI.Controller.prototype.getValue = function() {
|
2011-01-29 04:04:33 +00:00
|
|
|
return this.object[this.propertyName];
|
|
|
|
}
|
|
|
|
|
2011-02-01 18:45:22 +00:00
|
|
|
GUI.Controller.prototype.updateDisplay = function() {}
|
2011-01-25 08:22:04 +00:00
|
|
|
|
2011-02-01 18:45:22 +00:00
|
|
|
GUI.Controller.prototype.onChange = function(fnc) {
|
2011-01-29 04:04:33 +00:00
|
|
|
this.changeFunction = fnc;
|
|
|
|
return this;
|
2011-01-29 04:28:12 +00:00
|
|
|
}
|
2011-02-09 19:39:18 +00:00
|
|
|
GUI.Controller.prototype.onFinishChange = function(fnc) {
|
|
|
|
this.finishChangeFunction = fnc;
|
|
|
|
return this;
|
|
|
|
}
|
2011-02-09 21:28:35 +00:00
|
|
|
|
|
|
|
GUI.Controller.prototype.options = function() {
|
|
|
|
var _this = this;
|
|
|
|
var select = document.createElement('select');
|
|
|
|
if (arguments.length == 1) {
|
|
|
|
var arr = arguments[0];
|
|
|
|
for (var i in arr) {
|
|
|
|
var opt = document.createElement('option');
|
|
|
|
opt.innerHTML = i;
|
|
|
|
opt.setAttribute('value', arr[i]);
|
|
|
|
select.appendChild(opt);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
|
|
var opt = document.createElement('option');
|
|
|
|
opt.innerHTML = arguments[i];
|
|
|
|
opt.setAttribute('value', arguments[i]);
|
|
|
|
select.appendChild(opt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select.addEventListener('change', function() {
|
|
|
|
_this.setValue(this.value);
|
|
|
|
if (_this.finishChangeFunction != null) {
|
|
|
|
_this.finishChangeFunction.call(this, _this.getValue());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_this.domElement.appendChild(select);
|
|
|
|
return this;
|
|
|
|
}
|