dat.gui/controllers/controller.js

66 lines
1.7 KiB
JavaScript
Raw Normal View History

2011-02-02 18:12:55 +00:00
GUI.Controller = function() {
this.parent = arguments[0];
this.object = arguments[1];
this.propertyName = arguments[2];
2011-01-26 01:55:59 +00:00
if (arguments.length > 0) this.initialValue = this.propertyName[this.object];
this.domElement = document.createElement('div');
this.domElement.setAttribute('class', 'guidat-controller ' + this.type);
this.propertyNameElement = document.createElement('span');
this.propertyNameElement.setAttribute('class', 'guidat-propertyname');
this.name(this.propertyName);
this.domElement.appendChild(this.propertyNameElement);
2011-02-01 04:02:22 +00:00
GUI.makeUnselectable(this.domElement);
};
2011-02-02 18:12:55 +00:00
GUI.Controller.prototype.changeFunction = null;
2011-02-02 18:12:55 +00:00
GUI.Controller.prototype.name = function(n) {
this.propertyNameElement.innerHTML = n;
return this;
};
2011-02-02 18:12:55 +00:00
GUI.Controller.prototype.reset = function() {
this.setValue(this.initialValue);
return this;
};
2011-02-02 18:12:55 +00:00
GUI.Controller.prototype.listen = function() {
this.parent.listenTo(this);
return this;
}
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
return this;
}
2011-02-02 18:12:55 +00:00
GUI.Controller.prototype.setValue = function(n) {
this.object[this.propertyName] = n;
if (this.changeFunction != null) {
this.changeFunction.call(this, n);
}
// Whenever you call setValue, the display will be updated automatically.
// This reduces some clutter in subclasses. We can also use this method for listen().
this.updateDisplay();
return this;
}
2011-02-02 18:12:55 +00:00
GUI.Controller.prototype.getValue = function() {
return this.object[this.propertyName];
}
2011-02-02 18:12:55 +00:00
GUI.Controller.prototype.updateDisplay = function() {}
2011-02-02 18:12:55 +00:00
GUI.Controller.prototype.onChange = function(fnc) {
this.changeFunction = fnc;
return this;
}