2011-02-02 18:12:55 +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-02-02 20:51:58 +00:00
|
|
|
this.changeListeners = [];
|
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-02-01 04:02:22 +00:00
|
|
|
|
|
|
|
|
2011-01-29 04:04:33 +00:00
|
|
|
GUI.makeUnselectable(this.domElement);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-02-02 20:51:58 +00:00
|
|
|
|
2011-01-29 04:04:33 +00:00
|
|
|
|
2011-02-02 18:12:55 +00:00
|
|
|
GUI.Controller.prototype.name = function(n) {
|
2011-01-29 04:04:33 +00:00
|
|
|
this.propertyNameElement.innerHTML = n;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2011-02-02 18:12:55 +00:00
|
|
|
GUI.Controller.prototype.reset = function() {
|
2011-01-31 15:47:07 +00:00
|
|
|
this.setValue(this.initialValue);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2011-02-02 18:12:55 +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-02 18:12:55 +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-02 18:12:55 +00:00
|
|
|
GUI.Controller.prototype.setValue = function(n) {
|
2011-02-08 15:40:38 +00:00
|
|
|
|
2011-01-29 04:04:33 +00:00
|
|
|
this.object[this.propertyName] = n;
|
2011-02-02 20:51:58 +00:00
|
|
|
for (var i in this.changeListeners) {
|
2011-02-08 15:40:38 +00:00
|
|
|
console.log("Telling you I changed to " + n);
|
2011-02-02 20:51:58 +00:00
|
|
|
this.changeListeners[i].call(this, n);
|
2011-01-29 04:04:33 +00:00
|
|
|
}
|
2011-01-29 04:28:12 +00:00
|
|
|
// Whenever you call setValue, the display will be updated automatically.
|
|
|
|
// This reduces some clutter in subclasses. We can also use this method for listen().
|
2011-01-29 04:04:33 +00:00
|
|
|
this.updateDisplay();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2011-02-02 18:12:55 +00:00
|
|
|
GUI.Controller.prototype.getValue = function() {
|
2011-01-29 04:04:33 +00:00
|
|
|
return this.object[this.propertyName];
|
|
|
|
}
|
|
|
|
|
2011-02-02 18:12:55 +00:00
|
|
|
GUI.Controller.prototype.updateDisplay = function() {}
|
2011-01-25 08:22:04 +00:00
|
|
|
|
2011-02-02 20:51:58 +00:00
|
|
|
GUI.Controller.prototype.addChangeListener = function(fnc) {
|
|
|
|
this.changeListeners.push(fnc);
|
2011-01-29 04:04:33 +00:00
|
|
|
return this;
|
2011-01-29 04:28:12 +00:00
|
|
|
}
|