2011-01-24 19:38:52 +00:00
|
|
|
|
|
|
|
// Only works on the last one
|
|
|
|
var NumberController = function() {
|
|
|
|
|
|
|
|
this.type = "number";
|
|
|
|
|
|
|
|
Controller.apply(this, arguments);
|
|
|
|
|
|
|
|
var _this = this;
|
|
|
|
|
2011-01-24 21:22:09 +00:00
|
|
|
var isClicked = false;
|
2011-01-24 22:02:28 +00:00
|
|
|
var isDragged = false;
|
2011-01-24 21:22:09 +00:00
|
|
|
var y, py, initialValue, inc;
|
2011-01-24 19:38:52 +00:00
|
|
|
|
2011-01-24 21:22:09 +00:00
|
|
|
py = y = 0;
|
|
|
|
inc = initialValue = this.object[this.propertyName];
|
2011-01-24 19:38:52 +00:00
|
|
|
|
2011-01-24 21:22:09 +00:00
|
|
|
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
|
|
|
|
2011-01-24 21:36:08 +00:00
|
|
|
var amt;
|
|
|
|
(arguments[4] != null) ? amt = arguments[4] : amt = (max - min) * .01;
|
|
|
|
if(amt == 0) amt = 1;
|
|
|
|
|
2011-01-24 21:22:09 +00:00
|
|
|
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);
|
2011-01-24 21:22:09 +00:00
|
|
|
this.domElement.appendChild(button);
|
2011-01-24 19:38:52 +00:00
|
|
|
|
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);
|
|
|
|
|
2011-01-24 21:27:45 +00:00
|
|
|
button.addEventListener('mousedown', function(e) {
|
2011-01-24 21:22:09 +00:00
|
|
|
isClicked = true;
|
2011-01-24 21:27:45 +00:00
|
|
|
}, false);
|
|
|
|
button.addEventListener('keyup', function(e) {
|
2011-01-24 21:22:09 +00:00
|
|
|
var val = parseFloat(this.value);
|
2011-01-24 20:53:33 +00:00
|
|
|
if(isNaN(val)) {
|
2011-01-24 21:22:09 +00:00
|
|
|
inc = initialValue;
|
2011-01-24 20:53:33 +00:00
|
|
|
} else {
|
2011-01-24 21:22:09 +00:00
|
|
|
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;
|
2011-01-24 21:27:45 +00:00
|
|
|
}, false);
|
|
|
|
document.addEventListener('mouseup', function(e) {
|
2011-01-24 21:22:09 +00:00
|
|
|
isClicked = false;
|
2011-01-24 22:02:28 +00:00
|
|
|
isDragged = false;
|
2011-01-24 21:27:45 +00:00
|
|
|
}, false);
|
|
|
|
document.addEventListener('mousemove', function(e) {
|
2011-01-24 21:22:09 +00:00
|
|
|
if(isClicked) {
|
2011-01-24 20:53:33 +00:00
|
|
|
e.preventDefault();
|
2011-01-24 21:22:09 +00:00
|
|
|
py = y;
|
|
|
|
y = e.offsetY;
|
|
|
|
var dy = y - py;
|
|
|
|
if(dy < 0) {
|
|
|
|
if(max != null)
|
2011-01-24 21:36:08 +00:00
|
|
|
(inc >= max) ? inc = max : inc+=amt;
|
2011-01-24 19:38:52 +00:00
|
|
|
else
|
2011-01-24 21:22:09 +00:00
|
|
|
inc++;
|
|
|
|
} else if(dy > 0) {
|
|
|
|
|
|
|
|
if(min != null)
|
2011-01-24 21:36:08 +00:00
|
|
|
(inc <= min) ? inc = min : inc-=amt;
|
2011-01-24 19:38:52 +00:00
|
|
|
else
|
2011-01-24 21:22:09 +00:00
|
|
|
inc--;
|
2011-01-24 19:38:52 +00:00
|
|
|
}
|
2011-01-24 22:02:28 +00:00
|
|
|
} else if(isDragged) {
|
|
|
|
if(inc != slider.value) inc = slider.value;
|
2011-01-24 19:38:52 +00:00
|
|
|
}
|
2011-01-24 22:02:28 +00:00
|
|
|
updateValue(inc);
|
2011-01-24 21:27:45 +00:00
|
|
|
}, false);
|
2011-01-24 19:38:52 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-01-24 19:38:52 +00:00
|
|
|
this.__defineSetter__("position", function(val) {
|
2011-01-24 21:22:09 +00:00
|
|
|
inc = val;
|
2011-01-24 22:02:28 +00:00
|
|
|
updateValue(val);
|
2011-01-24 21:09:55 +00:00
|
|
|
// possibly push to an array here so that
|
|
|
|
// we have a record of "defined" / "presets"
|
|
|
|
// ????
|
2011-01-24 19:38:52 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
NumberController.prototype = new Controller();
|
|
|
|
NumberController.prototype.constructor = NumberController;
|