2011-02-01 18:53:14 +00:00
|
|
|
GUI.Slider = function(numberController, min, max, step, initValue) {
|
2011-02-11 19:12:07 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var clicked = false;
|
|
|
|
var _this = this;
|
2011-01-25 09:35:45 +00:00
|
|
|
|
2011-02-11 19:12:07 +00:00
|
|
|
var onDrag = function(e) {
|
|
|
|
if (!clicked) return;
|
|
|
|
var pos = findPos(_this.domElement);
|
|
|
|
var val = GUI.map(e.pageX, pos[0], pos[0] + _this.domElement.offsetWidth, min, max);
|
|
|
|
val = Math.round(val/step)*step;
|
|
|
|
numberController.setValue(val);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.domElement.addEventListener('mousedown', function(e) {
|
|
|
|
clicked = true;
|
|
|
|
x = px = e.pageX;
|
|
|
|
_this.domElement.className += ' active';
|
|
|
|
_this.fg.className += ' active';
|
|
|
|
numberController.domElement.className += ' active';
|
|
|
|
onDrag(e);
|
|
|
|
document.addEventListener('mouseup', mouseup, false);
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
var mouseup = function(e) {
|
|
|
|
_this.domElement.className = _this.domElement.className.replace(' active', '');
|
|
|
|
_this.fg.className = _this.fg.className.replace(' active', '');
|
|
|
|
numberController.domElement.className = numberController.domElement.className.replace(' active', '');
|
|
|
|
clicked = false;
|
|
|
|
if (numberController.finishChangeFunction != null) {
|
|
|
|
numberController.finishChangeFunction.call(this, numberController.getValue());
|
|
|
|
}
|
|
|
|
document.removeEventListener('mouseup', mouseup, false);
|
|
|
|
};
|
2011-02-09 19:39:18 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
var x, px;
|
|
|
|
|
|
|
|
this.domElement = document.createElement('div');
|
|
|
|
this.domElement.setAttribute('class', 'guidat-slider-bg');
|
|
|
|
|
|
|
|
this.fg = document.createElement('div');
|
|
|
|
this.fg.setAttribute('class', 'guidat-slider-fg');
|
|
|
|
|
|
|
|
this.domElement.appendChild(this.fg);
|
|
|
|
|
|
|
|
var findPos = function(obj) {
|
2011-02-10 21:00:01 +00:00
|
|
|
var curleft = 0, curtop = 0;
|
2011-02-10 20:50:57 +00:00
|
|
|
if (obj.offsetParent) {
|
|
|
|
do {
|
|
|
|
curleft += obj.offsetLeft;
|
|
|
|
curtop += obj.offsetTop;
|
2011-02-10 21:00:01 +00:00
|
|
|
} while ((obj = obj.offsetParent));
|
2011-02-10 20:50:57 +00:00
|
|
|
return [curleft,curtop];
|
|
|
|
}
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|
2011-02-10 20:50:57 +00:00
|
|
|
|
|
|
|
this.__defineSetter__('value', function(e) {
|
|
|
|
var pct = GUI.map(e, min, max, 0, 100);
|
|
|
|
this.fg.style.width = pct+"%";
|
|
|
|
});
|
|
|
|
|
|
|
|
var onDrag = function(e) {
|
|
|
|
if (!clicked) return;
|
|
|
|
var pos = findPos(_this.domElement);
|
|
|
|
var val = GUI.map(e.pageX, pos[0], pos[0] + _this.domElement.offsetWidth, min, max);
|
|
|
|
val = Math.round(val/step)*step;
|
|
|
|
numberController.setValue(val);
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|
2011-02-10 20:50:57 +00:00
|
|
|
|
|
|
|
this.domElement.addEventListener('mousedown', function(e) {
|
|
|
|
clicked = true;
|
|
|
|
x = px = e.pageX;
|
|
|
|
_this.domElement.setAttribute('class', 'guidat-slider-bg active');
|
|
|
|
_this.fg.setAttribute('class', 'guidat-slider-fg active');
|
|
|
|
onDrag(e);
|
|
|
|
document.addEventListener('mouseup', mouseup, false);
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
var mouseup = function(e) {
|
|
|
|
_this.domElement.setAttribute('class', 'guidat-slider-bg');
|
|
|
|
_this.fg.setAttribute('class', 'guidat-slider-fg');
|
|
|
|
clicked = false;
|
|
|
|
if (numberController.finishChangeFunction != null) {
|
|
|
|
numberController.finishChangeFunction.call(this, numberController.getValue());
|
|
|
|
}
|
|
|
|
document.removeEventListener('mouseup', mouseup, false);
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('mousemove', onDrag, false);
|
|
|
|
|
|
|
|
this.value = initValue;
|
|
|
|
|
2011-02-10 21:00:01 +00:00
|
|
|
};
|