min and max added to NumberController

This commit is contained in:
jonobr1 2011-01-23 16:47:26 -08:00
parent 8eafa82deb
commit 9394f945bd
3 changed files with 36 additions and 8 deletions

View File

@ -17,6 +17,12 @@ var NumberController = function() {
this.inc = 0; // TODO pass argument to inc this.inc = 0; // TODO pass argument to inc
this.button; this.button;
console.log(arguments);
// Get min and max
(arguments[2] != null) ? this.min = arguments[2] : this.min = null;
(arguments[3] != null) ? this.max = arguments[3] : this.max = null;
this.button = document.createElement('input'); this.button = document.createElement('input');
this.button.setAttribute('id', this.propertyName); this.button.setAttribute('id', this.propertyName);
this.button.setAttribute('type', 'number'); this.button.setAttribute('type', 'number');
@ -37,12 +43,27 @@ var NumberController = function() {
_this.y = e.offsetY; _this.y = e.offsetY;
var dy = _this.y - _this.py; var dy = _this.y - _this.py;
if(dy > 0) if(dy > 0) {
_this.button.setAttribute('value', _this.inc++); if(_this.max != null)
else if(dy < 0) (_this.inc >= _this.max) ? _this.inc = _this.max : _this.inc++;
_this.button.setAttribute('value', _this.inc--); else
_this.inc++;
} else if(dy < 0) {
if(_this.min != null)
(_this.inc <= _this.min) ? _this.inc = _this.min : _this.inc--;
else
_this.inc--;
}
_this.button.setAttribute('value', _this.inc);
} }
}; };
this.__defineSetter__("position", function(val) {
_this.inc = val;
_this.button.setAttribute('value', _this.inc);
});
}; };
NumberController.prototype = new Controller(); NumberController.prototype = new Controller();

View File

@ -24,7 +24,7 @@ window.onload = function() {
GUI.add(controllableObject, "numberProperty"); GUI.add(controllableObject, "numberProperty");
// Creates a slider (min, max) // Creates a slider (min, max)
GUI.add(controllableObject, "anotherNumberProperty", -100, 100); GUI.add(controllableObject, "anotherNumberProperty", -100, 100);
// Creates a text field // Creates a text field
GUI.add(controllableObject, "textProperty"); GUI.add(controllableObject, "textProperty");

13
gui.js
View File

@ -57,8 +57,7 @@ var GUI = new function() {
var addHandlers = { var addHandlers = {
"number": function() { "number": function() {
var n = new NumberController(arguments); return construct(NumberController, arguments);
return n;
}, },
"string": function() { "string": function() {
@ -85,6 +84,14 @@ var GUI = new function() {
console.error("[GUI ERROR] " + str); console.error("[GUI ERROR] " + str);
} }
}; };
var construct = function(constructor, args) {
function F() {
return constructor.apply(this, args);
}
F.prototype = constructor.prototype;
return new F();
};
@ -143,4 +150,4 @@ var GUI = new function() {
}; };
}; };