2011-02-01 18:45:22 +00:00
|
|
|
GUI.NumberController = function() {
|
2011-01-25 08:22:04 +00:00
|
|
|
|
2011-02-10 20:50:57 +00:00
|
|
|
this.type = "number";
|
|
|
|
|
2011-02-01 18:45:22 +00:00
|
|
|
GUI.Controller.apply(this, arguments);
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
var _this = this;
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
// If we simply click and release a number field, we want to highlight it.
|
2011-01-25 22:10:40 +00:00
|
|
|
// This variable keeps track of whether or not we've dragged
|
2011-01-25 08:22:04 +00:00
|
|
|
var draggedNumberField = false;
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
var clickedNumberField = false;
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
var y = py = 0;
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-29 04:04:33 +00:00
|
|
|
var min = arguments[3];
|
|
|
|
var max = arguments[4];
|
|
|
|
var step = arguments[5];
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
if (!step) {
|
2011-02-10 20:50:57 +00:00
|
|
|
if (min != undefined && max != undefined) {
|
|
|
|
step = (max-min)*0.01;
|
|
|
|
} else {
|
|
|
|
step = 1;
|
|
|
|
}
|
2011-01-25 08:22:04 +00:00
|
|
|
}
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
var numberField = document.createElement('input');
|
|
|
|
numberField.setAttribute('id', this.propertyName);
|
|
|
|
numberField.setAttribute('type', 'text');
|
|
|
|
numberField.setAttribute('value', this.getValue());
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
if (step) numberField.setAttribute('step', step);
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
this.domElement.appendChild(numberField);
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
var slider;
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 09:35:45 +00:00
|
|
|
if (min != undefined && max != undefined) {
|
2011-02-10 20:50:57 +00:00
|
|
|
slider = new GUI.Slider(this, min, max, step, this.getValue());
|
|
|
|
this.domElement.appendChild(slider.domElement);
|
2011-01-25 08:22:04 +00:00
|
|
|
}
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
numberField.addEventListener('blur', function(e) {
|
|
|
|
var val = parseFloat(this.value);
|
2011-02-09 19:39:18 +00:00
|
|
|
console.log(val);
|
2011-01-25 08:22:04 +00:00
|
|
|
if (!isNaN(val)) {
|
2011-02-10 20:50:57 +00:00
|
|
|
_this.setValue(val);
|
2011-01-25 08:22:04 +00:00
|
|
|
}
|
|
|
|
}, false);
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
numberField.addEventListener('mousewheel', function(e) {
|
2011-02-10 20:50:57 +00:00
|
|
|
e.preventDefault();
|
|
|
|
_this.setValue(_this.getValue() + Math.abs(e.wheelDeltaY)/e.wheelDeltaY*step);
|
|
|
|
return false;
|
2011-01-25 08:22:04 +00:00
|
|
|
}, false);
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
numberField.addEventListener('mousedown', function(e) {
|
|
|
|
py = y = e.pageY;
|
2011-02-10 20:50:57 +00:00
|
|
|
clickedNumberField = true;
|
2011-01-25 08:22:04 +00:00
|
|
|
document.addEventListener('mousemove', dragNumberField, false);
|
2011-02-10 20:50:57 +00:00
|
|
|
document.addEventListener('mouseup', mouseup, false);
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
// Handle up arrow and down arrow
|
|
|
|
numberField.addEventListener('keydown', function(e) {
|
|
|
|
switch(e.keyCode) {
|
|
|
|
case 38: // up
|
|
|
|
var newVal = _this.getValue() + step;
|
|
|
|
_this.setValue(newVal);
|
|
|
|
break;
|
|
|
|
case 40: // down
|
|
|
|
var newVal = _this.getValue() - step;
|
|
|
|
_this.setValue(newVal);
|
|
|
|
break;
|
|
|
|
}
|
2011-01-25 08:22:04 +00:00
|
|
|
}, false);
|
2011-01-31 20:25:57 +00:00
|
|
|
|
2011-02-09 19:39:18 +00:00
|
|
|
var mouseup = function(e) {
|
2011-01-25 08:22:04 +00:00
|
|
|
document.removeEventListener('mousemove', dragNumberField, false);
|
2011-02-10 20:50:57 +00:00
|
|
|
GUI.makeSelectable(_this.parent.domElement);
|
2011-01-29 04:04:33 +00:00
|
|
|
GUI.makeSelectable(numberField);
|
2011-02-10 20:50:57 +00:00
|
|
|
if (clickedNumberField && !draggedNumberField) {
|
|
|
|
numberField.focus();
|
|
|
|
numberField.select();
|
2011-01-25 08:22:04 +00:00
|
|
|
}
|
|
|
|
draggedNumberField = false;
|
|
|
|
clickedNumberField = false;
|
2011-02-10 20:50:57 +00:00
|
|
|
if (_this.finishChangeFunction != null) {
|
|
|
|
_this.finishChangeFunction.call(this, _this.getValue());
|
|
|
|
}
|
|
|
|
document.removeEventListener('mouseup', mouseup, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
var dragNumberField = function(e) {
|
2011-02-10 20:50:57 +00:00
|
|
|
draggedNumberField = true;
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// We don't want to be highlighting this field as we scroll.
|
|
|
|
// Or any other fields in this gui for that matter ...
|
|
|
|
// TODO: Make makeUselectable go through each element and child element.
|
|
|
|
|
|
|
|
GUI.makeUnselectable(_this.parent.domElement);
|
|
|
|
GUI.makeUnselectable(numberField);
|
|
|
|
|
|
|
|
py = y;
|
|
|
|
y = e.pageY;
|
|
|
|
var dy = py - y;
|
|
|
|
var newVal = _this.getValue() + dy*step;
|
|
|
|
_this.setValue(newVal);
|
|
|
|
return false;
|
2011-01-25 08:22:04 +00:00
|
|
|
}
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-02-09 21:28:35 +00:00
|
|
|
this.options = function() {
|
2011-02-10 20:50:57 +00:00
|
|
|
_this.noSlider();
|
|
|
|
_this.domElement.removeChild(numberField);
|
|
|
|
return GUI.Controller.prototype.options.apply(this, arguments);
|
2011-02-09 21:28:35 +00:00
|
|
|
};
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-02-09 19:39:18 +00:00
|
|
|
this.noSlider = function() {
|
2011-02-10 20:50:57 +00:00
|
|
|
if (slider) {
|
|
|
|
_this.domElement.removeChild(slider.domElement);
|
|
|
|
}
|
|
|
|
return this;
|
2011-02-09 19:39:18 +00:00
|
|
|
};
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-29 04:04:33 +00:00
|
|
|
this.setValue = function(val) {
|
2011-02-10 20:50:57 +00:00
|
|
|
|
|
|
|
val = parseFloat(val);
|
|
|
|
|
|
|
|
if (min != undefined && val <= min) {
|
|
|
|
val = min;
|
|
|
|
} else if (max != undefined && val >= max) {
|
|
|
|
val = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GUI.Controller.prototype.setValue.call(this, val);
|
|
|
|
|
2011-01-26 22:55:56 +00:00
|
|
|
}
|
2011-02-10 20:50:57 +00:00
|
|
|
|
2011-01-29 04:04:33 +00:00
|
|
|
this.updateDisplay = function() {
|
2011-01-31 15:47:07 +00:00
|
|
|
numberField.value = GUI.roundToDecimal(_this.getValue(), 4);
|
2011-01-29 04:04:33 +00:00
|
|
|
if (slider) slider.value = _this.getValue();
|
2011-02-10 20:50:57 +00:00
|
|
|
}
|
2011-01-25 08:22:04 +00:00
|
|
|
};
|
|
|
|
|
2011-02-01 18:45:22 +00:00
|
|
|
GUI.extendController(GUI.NumberController);
|