dat.gui/controller.js

93 lines
2.6 KiB
JavaScript
Raw Normal View History

2011-01-24 00:50:14 +00:00
var Controller = function(object, propertyName) {
2011-01-24 02:59:29 +00:00
this.setName = function(n) {
this.propertyNameElement.innerHTML = n;
}
2011-01-24 00:15:04 +00:00
this.domElement = document.createElement('div');
2011-01-24 02:59:29 +00:00
this.domElement.setAttribute('class', 'guidat-controller ' + this.type);
2011-01-24 00:50:14 +00:00
this.object = arguments[0];
this.propertyName = arguments[1];
2011-01-24 00:50:14 +00:00
this.propertyNameElement = document.createElement('span');
this.propertyNameElement.setAttribute('class', 'guidat-propertyname');
2011-01-24 02:59:29 +00:00
this.setName(this.propertyName);
2011-01-24 00:50:14 +00:00
this.domElement.appendChild(this.propertyNameElement);
2011-01-24 02:59:29 +00:00
};
2011-01-24 02:59:29 +00:00
// Only works on the last one
var NumberController = function() {
2011-01-24 02:59:29 +00:00
this.type = "number";
Controller.apply(this, arguments);
2011-01-24 00:15:04 +00:00
2011-01-24 02:59:29 +00:00
2011-01-24 00:15:04 +00:00
var _this = this;
this.isClicked = false;
this.py = this.y = 0;
2011-01-24 00:23:16 +00:00
this.inc = 0; // TODO pass argument to inc
2011-01-24 00:47:26 +00:00
// Get min and max
(arguments[2] != null) ? this.min = arguments[2] : this.min = null;
(arguments[3] != null) ? this.max = arguments[3] : this.max = null;
this.button = document.createElement('input');
this.button.setAttribute('id', this.propertyName);
this.button.setAttribute('type', 'number');
2011-01-24 00:23:16 +00:00
this.button.setAttribute('value', this.inc);
2011-01-24 00:15:04 +00:00
this.domElement.appendChild(this.button);
2011-01-24 00:15:04 +00:00
this.button.onmousedown = function(e) {
_this.isClicked = true;
};
document.onmouseup = function(e) {
_this.isClicked = false;
};
document.onmousemove = function(e) {
2011-01-24 00:15:04 +00:00
if(_this.isClicked) {
_this.py = _this.y;
_this.y = e.offsetY;
var dy = _this.y - _this.py;
2011-01-24 00:47:26 +00:00
if(dy > 0) {
if(_this.max != null)
(_this.inc >= _this.max) ? _this.inc = _this.max : _this.inc++;
else
_this.inc++;
} else if(dy < 0) {
if(_this.min != null)
(_this.inc <= _this.min) ? _this.inc = _this.min : _this.inc--;
else
_this.inc--;
}
_this.button.setAttribute('value', _this.inc);
2011-01-24 00:15:04 +00:00
}
};
2011-01-24 00:47:26 +00:00
this.__defineSetter__("position", function(val) {
_this.inc = val;
_this.button.setAttribute('value', _this.inc);
});
};
NumberController.prototype = new Controller();
2011-01-24 02:59:29 +00:00
NumberController.prototype.constructor = NumberController;
var StringController = function() {
this.type = "string";
Controller.apply(this, arguments);
};
StringController.prototype = new Controller();
StringController.prototype.constructor = StringController;