dat.gui/controller.number.js

109 lines
3.1 KiB
JavaScript
Raw Normal View History

// Only works on the last one
var NumberController = function() {
this.type = "number";
Controller.apply(this, arguments);
var _this = this;
var isClicked = false;
2011-01-24 22:02:28 +00:00
var isDragged = false;
var y, py, initialValue, inc;
py = y = 0;
inc = initialValue = this.object[this.propertyName];
var min, max;
(arguments[2] != null) ? min = arguments[2] : min = null;
(arguments[3] != null) ? max = arguments[3] : max = null;
2011-01-24 20:53:33 +00:00
var amt;
(arguments[4] != null) ? amt = arguments[4] : amt = (max - min) * .01;
if(amt == 0) amt = 1;
var button = document.createElement('input');
button.setAttribute('id', this.propertyName);
button.setAttribute('type', this.type);
2011-01-24 22:02:28 +00:00
button.setAttribute('value', inc);
this.domElement.appendChild(button);
2011-01-24 22:02:28 +00:00
var slider = document.createElement('input');
slider.setAttribute('id', this.propertyName + "-slider");
slider.setAttribute('type', 'range');
slider.setAttribute('value', inc);
if(min != null && max != null) {
slider.setAttribute('min', min);
slider.setAttribute('max', max);
}
slider.setAttribute('step', amt);
this.domElement.appendChild(slider);
button.addEventListener('mousedown', function(e) {
isClicked = true;
}, false);
button.addEventListener('keyup', function(e) {
var val = parseFloat(this.value);
2011-01-24 20:53:33 +00:00
if(isNaN(val)) {
inc = initialValue;
2011-01-24 20:53:33 +00:00
} else {
inc = val;
2011-01-24 20:53:33 +00:00
}
2011-01-24 22:02:28 +00:00
updateValue(inc);
}, false);
slider.addEventListener('mousedown', function(e) {
isDragged = true;
}, false);
document.addEventListener('mouseup', function(e) {
isClicked = false;
_this.makeSelectable(GUI.domElement);
_this.makeSelectable(button);
2011-01-24 22:02:28 +00:00
isDragged = false;
}, false);
document.addEventListener('mousemove', function(e) {
if(isClicked) {
2011-01-24 20:53:33 +00:00
e.preventDefault();
_this.makeUnselectable(GUI.domElement);
_this.makeUnselectable(button);
py = y;
y = e.offsetY;
var dy = y - py;
if(dy < 0) {
if(max != null)
(inc >= max) ? inc = max : inc+=amt;
else
inc++;
} else if(dy > 0) {
if(min != null)
(inc <= min) ? inc = min : inc-=amt;
else
inc--;
}
2011-01-24 22:02:28 +00:00
} else if(isDragged) {
if(inc != slider.value) inc = slider.value;
}
2011-01-24 22:02:28 +00:00
updateValue(inc);
}, false);
2011-01-24 22:02:28 +00:00
function updateValue(val) {
if(inc != val) inc = val;
button.value = val;
slider.value = val;
_this.setValue(val);
}
this.__defineSetter__("position", function(val) {
inc = val;
2011-01-24 22:02:28 +00:00
updateValue(val);
// possibly push to an array here so that
// we have a record of "defined" / "presets"
// ????
});
};
NumberController.prototype = new Controller();
NumberController.prototype.constructor = NumberController;