diff --git a/controller.js b/controller.js index 15af324..24ec70c 100644 --- a/controller.js +++ b/controller.js @@ -17,6 +17,12 @@ var NumberController = function() { this.inc = 0; // TODO pass argument to inc 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.setAttribute('id', this.propertyName); this.button.setAttribute('type', 'number'); @@ -37,12 +43,27 @@ var NumberController = function() { _this.y = e.offsetY; var dy = _this.y - _this.py; - if(dy > 0) - _this.button.setAttribute('value', _this.inc++); - else if(dy < 0) - _this.button.setAttribute('value', _this.inc--); + if(dy > 0) { + if(_this.max != null) + (_this.inc >= _this.max) ? _this.inc = _this.max : _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(); diff --git a/demo.html b/demo.html index 2cb5afb..b7490fc 100644 --- a/demo.html +++ b/demo.html @@ -24,7 +24,7 @@ window.onload = function() { GUI.add(controllableObject, "numberProperty"); // Creates a slider (min, max) - GUI.add(controllableObject, "anotherNumberProperty", -100, 100); + GUI.add(controllableObject, "anotherNumberProperty", -100, 100); // Creates a text field GUI.add(controllableObject, "textProperty"); diff --git a/gui.js b/gui.js index 8bd0cb5..add48df 100644 --- a/gui.js +++ b/gui.js @@ -57,8 +57,7 @@ var GUI = new function() { var addHandlers = { "number": function() { - var n = new NumberController(arguments); - return n; + return construct(NumberController, arguments); }, "string": function() { @@ -85,6 +84,14 @@ var GUI = new function() { 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() { }; -}; +}; \ No newline at end of file