2011-01-25 08:22:04 +00:00
|
|
|
// TODO: Provide alternate controllers for non-html5 browsers?
|
|
|
|
var NumberController = function() {
|
|
|
|
|
|
|
|
this.type = "number";
|
|
|
|
|
|
|
|
Controller.apply(this, arguments);
|
|
|
|
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
// 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-01-25 22:10:40 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
var clickedNumberField = false;
|
|
|
|
|
|
|
|
var y = py = 0;
|
|
|
|
|
|
|
|
var min = arguments[2];
|
|
|
|
var max = arguments[3];
|
|
|
|
var step = arguments[4];
|
|
|
|
|
|
|
|
if (!step) {
|
2011-01-26 22:55:56 +00:00
|
|
|
if (min != undefined && max != undefined) {
|
2011-01-25 08:22:04 +00:00
|
|
|
step = (max-min)*0.01;
|
|
|
|
} else {
|
|
|
|
step = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-26 22:55:56 +00:00
|
|
|
console.log("step " + step);
|
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
var numberField = document.createElement('input');
|
|
|
|
numberField.setAttribute('id', this.propertyName);
|
|
|
|
|
|
|
|
// Little up and down arrows are pissing me off.
|
|
|
|
numberField.setAttribute('type', 'text');
|
|
|
|
numberField.setAttribute('value', this.getValue());
|
|
|
|
|
|
|
|
if (step) numberField.setAttribute('step', step);
|
|
|
|
|
|
|
|
this.domElement.appendChild(numberField);
|
|
|
|
|
|
|
|
var slider;
|
|
|
|
|
2011-01-25 09:35:45 +00:00
|
|
|
if (min != undefined && max != undefined) {
|
|
|
|
slider = new Slider(this, min, max, step, this.getValue());
|
2011-01-25 08:22:04 +00:00
|
|
|
this.domElement.appendChild(slider.domElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
numberField.addEventListener('blur', function(e) {
|
|
|
|
var val = parseFloat(this.value);
|
|
|
|
if (!isNaN(val)) {
|
2011-01-25 09:35:45 +00:00
|
|
|
_this.updateValue(val);
|
2011-01-25 08:22:04 +00:00
|
|
|
} else {
|
|
|
|
this.value = _this.getValue();
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
numberField.addEventListener('mousewheel', function(e) {
|
|
|
|
e.preventDefault();
|
2011-01-25 09:35:45 +00:00
|
|
|
this.updateValue(_this.getValue() + Math.abs(e.wheelDeltaY)/e.wheelDeltaY*step);
|
2011-01-25 08:22:04 +00:00
|
|
|
return false;
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
numberField.addEventListener('mousedown', function(e) {
|
|
|
|
py = y = e.pageY;
|
|
|
|
clickedNumberField = true;
|
|
|
|
document.addEventListener('mousemove', dragNumberField, false);
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
document.addEventListener('mouseup', function(e) {
|
|
|
|
document.removeEventListener('mousemove', dragNumberField, false);
|
2011-01-29 00:38:58 +00:00
|
|
|
_this.makeSelectable(_this.parent.domElement);
|
2011-01-25 08:22:04 +00:00
|
|
|
_this.makeSelectable(numberField);
|
|
|
|
if (clickedNumberField && !draggedNumberField) {
|
|
|
|
numberField.focus();
|
|
|
|
numberField.select();
|
|
|
|
}
|
|
|
|
draggedNumberField = false;
|
|
|
|
clickedNumberField = false;
|
|
|
|
}, false);
|
|
|
|
|
2011-01-25 09:35:45 +00:00
|
|
|
// Kinda nast
|
|
|
|
if (navigator.appVersion.indexOf('chrome') != -1) {
|
2011-01-25 08:22:04 +00:00
|
|
|
document.addEventListener('mouseout', function(e) {
|
|
|
|
document.removeEventListener('mousemove', dragNumberField, false);
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
var dragNumberField = function(e) {
|
|
|
|
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.
|
|
|
|
_this.makeUnselectable(GUI.domElement);
|
|
|
|
_this.makeUnselectable(numberField);
|
|
|
|
|
|
|
|
py = y;
|
|
|
|
y = e.pageY;
|
|
|
|
var dy = py - y;
|
|
|
|
var newVal = _this.getValue() + dy*step;
|
2011-01-25 09:35:45 +00:00
|
|
|
_this.updateValue(newVal);
|
2011-01-25 08:22:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-25 09:35:45 +00:00
|
|
|
this.updateValue = function(val) {
|
2011-01-25 08:22:04 +00:00
|
|
|
|
|
|
|
val = parseFloat(val);
|
|
|
|
|
2011-01-25 22:10:40 +00:00
|
|
|
if (min != undefined && val <= min) {
|
2011-01-25 08:22:04 +00:00
|
|
|
val = min;
|
2011-01-25 22:10:40 +00:00
|
|
|
} else if (max != undefined && val >= max) {
|
2011-01-25 08:22:04 +00:00
|
|
|
val = max;
|
|
|
|
}
|
2011-01-28 21:19:26 +00:00
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
_this.setValue(val);
|
|
|
|
|
2011-01-26 22:55:56 +00:00
|
|
|
numberField.value = roundToDecimal(_this.getValue(), 4);
|
2011-01-25 08:22:04 +00:00
|
|
|
if (slider) slider.value = _this.getValue();
|
|
|
|
}
|
|
|
|
|
2011-01-26 22:55:56 +00:00
|
|
|
var roundToDecimal = function(n, decimals) {
|
|
|
|
var t = Math.pow(10, decimals);
|
|
|
|
return Math.round(n*t)/t;
|
|
|
|
}
|
|
|
|
|
2011-01-25 08:22:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
NumberController.prototype = new Controller();
|
2011-01-29 00:38:58 +00:00
|
|
|
NumberController.prototype.constructor = NumberController;
|