2011-02-02 18:12:55 +00:00
|
|
|
GUI.Slider = function(numberController, min, max, step, initValue) {
|
2011-01-25 09:35:45 +00:00
|
|
|
|
|
|
|
var min = min;
|
|
|
|
var max = max;
|
|
|
|
var step = step;
|
|
|
|
|
|
|
|
var clicked = false;
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
var x, px;
|
2011-01-25 08:22:04 +00:00
|
|
|
|
|
|
|
this.domElement = document.createElement('div');
|
2011-01-25 22:10:40 +00:00
|
|
|
this.domElement.setAttribute('class', 'guidat-slider-bg');
|
2011-01-26 22:55:56 +00:00
|
|
|
|
|
|
|
this.fg = document.createElement('div');
|
2011-01-25 08:22:04 +00:00
|
|
|
this.fg.setAttribute('class', 'guidat-slider-fg');
|
|
|
|
|
|
|
|
this.domElement.appendChild(this.fg);
|
|
|
|
|
2011-01-25 09:35:45 +00:00
|
|
|
var findPos = function(obj) {
|
|
|
|
var curleft = curtop = 0;
|
|
|
|
if (obj.offsetParent) {
|
|
|
|
do {
|
|
|
|
curleft += obj.offsetLeft;
|
|
|
|
curtop += obj.offsetTop;
|
|
|
|
} while (obj = obj.offsetParent);
|
|
|
|
return [curleft,curtop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.__defineSetter__('value', function(e) {
|
2011-01-29 04:04:33 +00:00
|
|
|
var pct = GUI.map(e, min, max, 0, 100);
|
2011-01-25 09:35:45 +00:00
|
|
|
this.fg.style.width = pct+"%";
|
|
|
|
});
|
|
|
|
|
|
|
|
var onDrag = function(e) {
|
|
|
|
if (!clicked) return;
|
|
|
|
var pos = findPos(_this.domElement);
|
2011-01-29 04:04:33 +00:00
|
|
|
var val = GUI.map(e.pageX, pos[0], pos[0] + _this.domElement.offsetWidth, min, max);
|
2011-01-25 09:35:45 +00:00
|
|
|
val = Math.round(val/step)*step;
|
2011-01-29 04:04:33 +00:00
|
|
|
numberController.setValue(val);
|
2011-01-25 09:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.domElement.addEventListener('mousedown', function(e) {
|
|
|
|
clicked = true;
|
|
|
|
x = px = e.pageX;
|
2011-01-25 22:10:40 +00:00
|
|
|
_this.domElement.setAttribute('class', 'guidat-slider-bg active');
|
|
|
|
_this.fg.setAttribute('class', 'guidat-slider-fg active');
|
2011-01-25 09:35:45 +00:00
|
|
|
onDrag(e);
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
|
2011-01-25 22:10:40 +00:00
|
|
|
document.addEventListener('mouseup', function(e) {
|
|
|
|
_this.domElement.setAttribute('class', 'guidat-slider-bg');
|
|
|
|
_this.fg.setAttribute('class', 'guidat-slider-fg');
|
2011-01-25 09:35:45 +00:00
|
|
|
clicked = false;
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
document.addEventListener('mousemove', onDrag, false);
|
|
|
|
|
|
|
|
this.value = initValue;
|
|
|
|
|
2011-01-29 03:56:32 +00:00
|
|
|
}
|