From 8eafa82debbcb239a1ddaadcf1f4cb0e521d74e7 Mon Sep 17 00:00:00 2001 From: jonobr1 Date: Sun, 23 Jan 2011 16:23:16 -0800 Subject: [PATCH 1/3] clean up of controller.js --- controller.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/controller.js b/controller.js index 6b140f2..15af324 100644 --- a/controller.js +++ b/controller.js @@ -14,14 +14,13 @@ var NumberController = function() { this.isClicked = false; this.py = this.y = 0; - // TODO pass argument to inc - this.inc = 0; + this.inc = 0; // TODO pass argument to inc this.button; this.button = document.createElement('input'); this.button.setAttribute('id', this.propertyName); this.button.setAttribute('type', 'number'); - this.button.setAttribute('value', this.y); + this.button.setAttribute('value', this.inc); this.domElement.appendChild(this.button); this.button.onmousedown = function(e) { @@ -40,7 +39,7 @@ var NumberController = function() { if(dy > 0) _this.button.setAttribute('value', _this.inc++); - else + else if(dy < 0) _this.button.setAttribute('value', _this.inc--); } }; From 9394f945bd1e81d2f65449f1ee4ec1dd7e176712 Mon Sep 17 00:00:00 2001 From: jonobr1 Date: Sun, 23 Jan 2011 16:47:26 -0800 Subject: [PATCH 2/3] min and max added to NumberController --- controller.js | 29 +++++++++++++++++++++++++---- demo.html | 2 +- gui.js | 13 ++++++++++--- 3 files changed, 36 insertions(+), 8 deletions(-) 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 From fc5830333b0a28b394a7d250f98b345b4bbe5639 Mon Sep 17 00:00:00 2001 From: jonobr1 Date: Sun, 23 Jan 2011 16:49:28 -0800 Subject: [PATCH 3/3] Only works on the last NumberController element at the moment --- controller.js | 1 + 1 file changed, 1 insertion(+) diff --git a/controller.js b/controller.js index 24ec70c..e753cc5 100644 --- a/controller.js +++ b/controller.js @@ -6,6 +6,7 @@ var Controller = function() { this.propertyName = arguments[1]; }; +// Only works on the last one var NumberController = function() { Controller.apply(this, arguments);