diff --git a/B12NumbersV3/ActionCodeAll.pde b/B12NumbersV3/ActionCodeAll.pde index 824af29..a9bcfde 100644 --- a/B12NumbersV3/ActionCodeAll.pde +++ b/B12NumbersV3/ActionCodeAll.pde @@ -1,51 +1,88 @@ -class ClickHandler{ +class MouseHandler { LiveMethodRelay[] mrs; - - ClickHandler(){ + + MouseHandler() { mrs = new LiveMethodRelay[0]; } - - void addl(MethodRelay l){ + + void addRelay(LiveMethodRelay r) { clean(); - mrs = (LiveMethodRelay[])append(mrs,l); + if(r.getTag() == '\0'){ throw new IllegalArgumentException("MouseHandler only accepts tagged LiveMethodRelays"); } + mrs = (LiveMethodRelay[])append(mrs, r); } - - void clean(){ - if(mrs.length == 0) return; - for(int i = mrs.length -1; i >= 0; i--){ - if(!mrs[i].live){ + + void clean() { + if (mrs.length == 0) return; + for (int i = mrs.length -1; i >= 0; i--) { + if (!mrs[i].live()) { mrs[i] = mrs[mrs.length - 1]; mrs = (LiveMethodRelay[])shorten(mrs); } } } - - void cascade(float x, float y){ - for(int i = 0; i < mrs.length; i++){ - mrs[i].execute(x,y); + + void cascade(char tag, float... data) { + for (int i = 0; i < mrs.length; i++) { + if(mrs[i].getTag() == tag){ + mrs[i].execute(data); + } } } } +//class ClickHandler { +// LiveMethodRelay[] mrs; + +// ClickHandler() { +// mrs = new LiveMethodRelay[0]; +// } + +// void addl(MethodRelay l) { +// clean(); +// mrs = (LiveMethodRelay[])append(mrs, l); +// } + +// void clean() { +// if (mrs.length == 0) return; +// for (int i = mrs.length -1; i >= 0; i--) { +// if (!mrs[i].live) { +// mrs[i] = mrs[mrs.length - 1]; +// mrs = (LiveMethodRelay[])shorten(mrs); +// } +// } +// } + +// void cascade(float x, float y) { +// for (int i = 0; i < mrs.length; i++) { +// mrs[i].execute(x, y); +// } +// } +//} -class LiveMethodRelay extends MethodRelay{ - boolean live; - - LiveMethodRelay(Object obj, String name, Class... args){ + + +class LiveMethodRelay extends MethodRelay { + private boolean live; + private char tag; + + LiveMethodRelay(Object obj, String name, char _tag, Class... args) { super(obj, name, args); + tag = _tag; live = true; } - - LiveMethodRelay(Object obj, String name){ - super(obj, name); + LiveMethodRelay(Object obj, String name, Class... args) { + super(obj, name, args); + tag = '\0'; live = true; } + + char getTag() {return tag;} + void setTag(char t) {tag = t;} - void kill(){ - live = false; - } + boolean live() {return live;} + void kill() {live = false;} } @@ -72,7 +109,7 @@ public static class MethodRelay { Register a method that has parameters. parameter obj the object that contains the method to invoke parameter name the name of the method - parameter args a comma separated list of + parameter args a comma separated list of */ MethodRelay(Object obj, String name, Class... args) { try { @@ -80,7 +117,7 @@ public static class MethodRelay { parameters = args; handlerMethod = obj.getClass().getMethod(handlerMethodName, parameters); handlerObject = obj; - } + } catch (Exception e) { println("Unable to find the function -"); print(handlerMethodName + "( "); @@ -110,14 +147,14 @@ public static class MethodRelay { /** Execute a method with parameters - parameter data a comma separated list of values + parameter data a comma separated list of values to be passed to the method */ void execute(Object... data) { if (handlerObject != null) { try { handlerMethod.invoke(handlerObject, data); - } + } catch (Exception e) { println("Error on invoke"); } diff --git a/B12NumbersV3/B12NumbersV3.pde b/B12NumbersV3/B12NumbersV3.pde index 7c49f59..1055bfd 100644 --- a/B12NumbersV3/B12NumbersV3.pde +++ b/B12NumbersV3/B12NumbersV3.pde @@ -11,6 +11,14 @@ Includes method relay code be Quark - see https://forum.processing.org/two/discussion/13093/how-to-call-function-by-string-content.html for more details. + // TODO redo mouse handling + // TODO add cursor and dynamic position for MathDisplay (Maybe add a "highlighted" attribute to B12Digit?) might need some restructuring + // TODO add parsing expression to operable math string (tricky to get base 12 to base 10) + // TODO add operator and action buttons to MathPad + // TODO add parenthesis functionality + // MAYBE start clock widget structure + // MAYBE add additional operations like power, log, and trig functions + changelog 0.1.5.0 - Quite a few changes by this point. The readme has been fixed, the button class has gone through many revisions @@ -44,16 +52,16 @@ PVector offset; float sMouseX; float sMouseY; public static final int DECIMAL = 65; -ClickHandler ch; // Mouse event handler +MouseHandler mh; // Mouse event handler Calculator calc; //<>// void setup(){ size(400,400); offset = new PVector(width/2, height/2); - ch = new ClickHandler(); + mh = new MouseHandler(); - calc = new Calculator(ch); + calc = new Calculator(mh); } @@ -71,7 +79,7 @@ void mouseClicked(){ //clock.setTime(new Time48(16,0,0)); // Every clickable element needs check whether the mouse is over it every frame, and if both clicked and mouseover then do action. - ch.cascade(sMouseX, sMouseY); + mh.cascade('c', sMouseX, sMouseY); } void call(String _call){ diff --git a/B12NumbersV3/guiButton.pde b/B12NumbersV3/guiButton.pde index b845b32..88a88d6 100644 --- a/B12NumbersV3/guiButton.pde +++ b/B12NumbersV3/guiButton.pde @@ -1,5 +1,5 @@ class Button{ // TODO make most of the attributes private - ClickHandler ch; + MouseHandler mh; PVector pos; // Position to render from PVector dim; // Second coordinate for CORNERS or len/wid for CORNER and CENTER float radius; // Optional corner radius @@ -10,8 +10,8 @@ class Button{ // TODO make most of the attributes private boolean mouseOver; Object[] data; // Anything that gets passed to MethodRelay function when key pressed. Must be set manually - Button(ClickHandler _ch, PVector _pos, PVector _dim, float _radius){ - ch = _ch; + Button(MouseHandler _mh, PVector _pos, PVector _dim, float _radius){ + mh = _mh; pos = _pos.copy(); dim = _dim.copy(); radius = _radius; @@ -19,11 +19,11 @@ class Button{ // TODO make most of the attributes private col = color(200); highlight = color(100); mouseOver = false; - ch.addl(new LiveMethodRelay(this, "clicked", float.class, float.class)); + mh.addRelay(new LiveMethodRelay(this, "clicked", 'c', Class.class)); data = null; } - Button(ClickHandler _ch, PVector _pos, PVector _dim){ - this(_ch, _pos, _dim, 0); + Button(MouseHandler _mh, PVector _pos, PVector _dim){ + this(_mh, _pos, _dim, 0); } // GETTERS // @@ -40,7 +40,7 @@ class Button{ // TODO make most of the attributes private void setColor(color c){col = c; } void setColor(color c, color h){col = c; highlight = h;} void setHighlight(color h){ highlight = h; } - void setFunction(MethodRelay _function){function = _function;} // DONE finish implementation + void setFunction(MethodRelay _function){function = _function;} void setData(Object... _data){ data = _data; } // Data to pass for button presses. Ugh, note that the array already exists because it's passed as such, no need to create a new one. Stupid bug void setMode(int m){ @@ -62,9 +62,10 @@ class Button{ // TODO make most of the attributes private // MOUSE FUNCTIONS // - void clicked(float x, float y){ + void clicked(Class mp){ // mp[0] is smouseX and m[1] is smouseY + float[] _mp = float(mp); if(mouseOver){ - println(x + " " + y + " mouse pos"); + println(mp[0] + " " + mp[1] + " mouse pos"); function.execute(data); } } diff --git a/B12NumbersV3/guiCalculator.pde b/B12NumbersV3/guiCalculator.pde index 00c520e..023226d 100644 --- a/B12NumbersV3/guiCalculator.pde +++ b/B12NumbersV3/guiCalculator.pde @@ -3,9 +3,9 @@ class Calculator{ MathPad m; MathDisplay d; - Calculator(ClickHandler _ch){ + Calculator(MouseHandler _mh){ ex = new B12Expression(); - m = new MathPad(_ch,ex); + m = new MathPad(_mh,ex); d = new MathDisplay(ex); } diff --git a/B12NumbersV3/guiMathPad.pde b/B12NumbersV3/guiMathPad.pde index 554b24f..625e3b4 100644 --- a/B12NumbersV3/guiMathPad.pde +++ b/B12NumbersV3/guiMathPad.pde @@ -1,12 +1,12 @@ class MathPad{ B12Expression ex; - ClickHandler ch; + MouseHandler mh; B12Button[] buttons; PVector pos; - MathPad(ClickHandler _ch, B12Expression _ex){ + MathPad(MouseHandler _mh, B12Expression _ex){ ex = _ex; - ch = _ch; + mh = _mh; pos = new PVector(0,0); buttons = new B12Button[12]; initialize(); @@ -14,7 +14,7 @@ class MathPad{ void initialize(){ for(int i = 0; i < 12; i++){ - buttons[i] = new B12Button(ch, new PVector(25 * int(i%4),25 * floor(i/4)), new PVector(20,20),new B12Digit(i)); + buttons[i] = new B12Button(mh, new PVector(25 * int(i%4),25 * floor(i/4)), new PVector(20,20),new B12Digit(i)); buttons[i].setFunction(new MethodRelay(this, "addChar", B12Digit.class)); buttons[i].setColor(220,150); } @@ -42,14 +42,14 @@ class MathPad{ class B12Button extends Button{ B12Digit digit; - B12Button(ClickHandler _ch, PVector _pos, PVector _dim, float _radius, B12Digit _digit){ - super(_ch,_pos,_dim,_radius); + B12Button(MouseHandler _mh, PVector _pos, PVector _dim, float _radius, B12Digit _digit){ + super(_mh,_pos,_dim,_radius); //data = new Object[]{_digit}; Deprecated digit = _digit; setData(_digit); } - B12Button(ClickHandler _ch, PVector _pos, PVector _dim, B12Digit _digit){ - this(_ch, _pos, _dim, 2, _digit); + B12Button(MouseHandler _mh, PVector _pos, PVector _dim, B12Digit _digit){ + this(_mh, _pos, _dim, 2, _digit); } // GETTERS AND SETTERS //