From 193b6994ba79949dc2b51aa5ca2efcdd7c4e66bf Mon Sep 17 00:00:00 2001 From: 61616 <33187059+61616@users.noreply.github.com> Date: Sat, 30 Apr 2022 17:53:08 -0400 Subject: [PATCH] MethodRelay, file condensing, working towards gui Added MethodRelay code from Quark. Some fixes and changes as well. Condesed some things into fewer files for so the ide is easier to use, but they will be moved back into their own files as version 1.0 approaches. Everything is prep for adding gui elements (MethodRelay included) --- B12NumbersV3/ActionCodeAll.pde | 126 +++++++ B12NumbersV3/B12Base.pde | 328 ++++++++++++++++++ B12NumbersV3/B12Digit.pde | 107 ------ B12NumbersV3/B12Float.pde | 133 ------- B12NumbersV3/B12Int.pde | 81 ----- B12NumbersV3/B12NumbersV3.pde | 28 +- B12NumbersV3/B12TimeAll.pde | 207 +++++++++++ B12NumbersV3/Clock.pde | 60 ---- B12NumbersV3/MathPad.pde | 16 - B12NumbersV3/MouseListener.pde | 16 - B12NumbersV3/STime48.pde | 50 --- B12NumbersV3/Time48.pde | 90 ----- B12NumbersV3/{B12Math.pde => guiB12Math.pde} | 0 B12NumbersV3/guiButton.pde | 99 ++++++ .../{MathDisplay.pde => guiMathDisplay.pde} | 0 B12NumbersV3/guiMathPad.pde | 50 +++ 16 files changed, 832 insertions(+), 559 deletions(-) create mode 100644 B12NumbersV3/ActionCodeAll.pde create mode 100644 B12NumbersV3/B12Base.pde delete mode 100644 B12NumbersV3/B12Digit.pde delete mode 100644 B12NumbersV3/B12Float.pde delete mode 100644 B12NumbersV3/B12Int.pde create mode 100644 B12NumbersV3/B12TimeAll.pde delete mode 100644 B12NumbersV3/Clock.pde delete mode 100644 B12NumbersV3/MathPad.pde delete mode 100644 B12NumbersV3/MouseListener.pde delete mode 100644 B12NumbersV3/STime48.pde delete mode 100644 B12NumbersV3/Time48.pde rename B12NumbersV3/{B12Math.pde => guiB12Math.pde} (100%) create mode 100644 B12NumbersV3/guiButton.pde rename B12NumbersV3/{MathDisplay.pde => guiMathDisplay.pde} (100%) create mode 100644 B12NumbersV3/guiMathPad.pde diff --git a/B12NumbersV3/ActionCodeAll.pde b/B12NumbersV3/ActionCodeAll.pde new file mode 100644 index 0000000..2495847 --- /dev/null +++ b/B12NumbersV3/ActionCodeAll.pde @@ -0,0 +1,126 @@ +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; 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){ + super(obj, name, args); + live = true; + } + + LiveMethodRelay(Object obj, String name){ + super(obj, name); + live = true; + } + + void kill(){ + live = false; + } +} + + + +import java.lang.reflect.*; +/** + A class that encapsulates a named method to be invoked. + Quark 2015 + see https://forum.processing.org/two/discussion/13093/how-to-call-function-by-string-content.html + */ +public static class MethodRelay { + + /** The object to handle the draw event */ + private Object handlerObject = null; + /** The method in drawHandlerObject to execute */ + private Method handlerMethod = null; + /** the name of the method to handle the event */ + private String handlerMethodName; + /** An array of classes that represent the function + parameters in order */ + private Class[] parameters = null; + + /** + 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 + */ + MethodRelay(Object obj, String name, Class... args) { + try { + handlerMethodName = name; + parameters = args; + handlerMethod = obj.getClass().getMethod(handlerMethodName, parameters); + handlerObject = obj; + } + catch (Exception e) { + println("Unable to find the function -"); + print(handlerMethodName + "( "); + if (parameters != null) { + for (Class c : parameters) + print(c.getSimpleName() + " "); + println(")"); + } + } + } + + /** + Register a method that has no parameters. + parameter obj the object that contains the method to invoke + parameter name the name of the method + */ + MethodRelay(Object obj, String name) { + this(obj, name, (Class[])null); + } + + /** + Execute a paramerterless method + */ + void execute() { + execute((Object[])null); + } + + /** + Execute a method with parameters + 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/B12Base.pde b/B12NumbersV3/B12Base.pde new file mode 100644 index 0000000..ae0014e --- /dev/null +++ b/B12NumbersV3/B12Base.pde @@ -0,0 +1,328 @@ +class B12Digit{ + byte value; + PVector refPos; + + B12Digit(int _value){ + if(_value >= 12 || _value < 0){ + throw new IllegalArgumentException("B12Digit only accepts decimal integers 0 through 11"); + } + value = byte(_value); + refPos = new PVector(0,0); + } + + B12Digit(char _c){ + String valid = "+-*/.:"; // Defines valid input characters + if(!inStr(valid, _c)){ throw new IllegalArgumentException("B12Char only accepts \'+ - * / . :'"); } + value = byte(_c); + refPos = new PVector(0,0); + } + + // SETTERS + void setRefPos(PVector _refPos){ refPos = _refPos; } + void setRefPos(float _x, float _y){ refPos = new PVector(_x,_y); } + void setValue(int _value){ value = byte(_value); } + + // GETTERS + PVector getRefPos(){ return refPos; } + int getValue(){ return value; } + + // RENDER CHARACTERS + void display(){ + pushMatrix(); + translate(refPos.x,refPos.y); + strokeWeight(1); + noFill(); + ellipseMode(CORNERS); + switch(value) { + // NUMBERS // + case 0: + line0(); break; + case 1: + line1(); break; + case 2: + line1(); line2(); break; + case 3: + line1(); line2(); line3(); break; + case 4: + line4(); break; + case 5: + line4(); line1(); break; + case 6: + line4(); line1(); line2(); break; + case 7: + line4(); line1(); line2(); line3(); break; + case 8: + line8(); line4(); break; + case 9: + line8(); line4(); line1(); break; + case 10: + line8(); line4(); line1(); line2(); break; + case 11: + line8(); line4(); line1(); line2(); line3(); break; + + // CHARACTERS // + case '+': + lineMinus(); linePlus(); break; + case '-': + lineMinus(); break; + case '*': + lineTimes(); break; + case '/': + lineMinus(); dotsDiv(); break; + case '.': + strokeWeight(2); period(); break; + case ':': + strokeWeight(2); colon(); break; + } + popMatrix(); + } + + // Individual shape components to build any B12 number + void line0(){ ellipse(0,-13,8,0); } + void line1(){ line(6,0,9,-10); } + void line2(){ line(3,-10,6,0); } + void line3(){ line(0,0,3,-10); } + void line4(){ line(9,-10,2,-13); } + void line8(){ line(2,-13,9,-16); } + + // Individual shape components to build any B12 character + void lineTimes(){ line(4,-7,8,-3); line(4,-3,8,-7); } + void dotsDiv(){ point(6,-8); point(6,-2); } + void lineMinus(){ line(3,-5,9,-5); } + void linePlus(){ line(6,-8,6,-2); } + void period(){ point(5,0); } + void colon(){ point(5,-2); point(5,-8); } + + + // HELPER FUNCTIONS // + boolean inStr(String st, char _c){ + try{ + int x = st.indexOf(_c); + return true; + } + catch (Exception e){ + return false; + } + } +} + + + +class B12Int { + private ArrayList digits; + private int value; + private PVector pos; + private boolean arrayLoaded; + private boolean inPosition; + private int mode;// Can be RIGHT (39), CENTER (3), or LEFT (37) + private int minLen; + + B12Int(int _value){ + value = _value; + pos = new PVector(0,0); + arrayLoaded = false; + inPosition = false; + mode = LEFT; + minLen = 0; + } + + int getValue(){ return value; } + void setValue(int _value){ value = _value; arrayLoaded = false; } + + void setPos(PVector _pos){ pos = _pos.copy(); inPosition = false; } + void setPos(float _x, float _y){ pos = new PVector(_x, _y); inPosition = false; } + PVector getPos(){ return pos; } + + void setMinLen(int i){ minLen = i; } + + void setAlignMode(int _mode){ + if(_mode == DECIMAL || _mode == LEFT || _mode == RIGHT){ mode = _mode; } + else{ println("Alignment only accepts LEFT, RIGHT, and DECIMAL"); } + } + + void display(){ + if(!arrayLoaded){ loadArray(); } + if(!inPosition){ positionDigits(); } + pushMatrix(); + translate(pos.x,pos.y); + for(int i = 0; i < digits.size(); i++){ + digits.get(i).display(); + } + popMatrix(); + } + + private void positionDigits(){ + if(mode == LEFT || mode == DECIMAL){ + for(int i = 0; i < digits.size(); i++){ + digits.get(i).setRefPos((-12 * (i+1)), 0); + } + }else if(mode == RIGHT){ + int count = 0; + for(int i = digits.size() - 1; i >= 0; i--){ + digits.get(count).setRefPos((12 * i) + 3, 0); + count++; + } + } + inPosition = true; + } + + private void loadArray(){ + digits = new ArrayList(); + int mval = abs(value); + if(mval == 0){ digits.add(new B12Digit(0)); } + while(mval != 0){ + if(mval < 12){ + digits.add(new B12Digit(mval)); + mval = 0; + }else{ + digits.add(new B12Digit(mval % 12)); + mval /= 12; + } + } + if(digits.size() < minLen){ + digits.add(new B12Digit(0)); + } + if(value < 0){ + digits.add(new B12Digit('-')); + } + + arrayLoaded = true; + } +} + + + + +class B12Float { + private ArrayList digits; + private float value; + private PVector pos; + private boolean arrayLoaded; + private boolean inPosition; + private int mode;// Can be RIGHT (39), CENTER (3), or LEFT (37) + private int places; + private int pointPlace; + + B12Float(float _value){ + value = _value; + pos = new PVector(0,0); + arrayLoaded = false; + inPosition = false; + mode = DECIMAL; + places = 4; + } + + float getValue(){ return value; } + void setValue(float _value){ value = _value; arrayLoaded = false; } + + PVector getPos(){ return pos; } + void setPos(PVector _pos){ pos = _pos.copy(); inPosition = false; } + void setPos(float _x, float _y){ pos = new PVector(_x, _y); inPosition = false; } + + int getPlaces(){ return places; } + void setPlaces(int _places){ + if(_places > 12 || _places < 0){ + throw new IllegalArgumentException("B12Float ncan only display to 12 duodecimal points"); + }else{ + places = _places; + } + } + + void setAlignMode(int _mode){ + if(_mode == DECIMAL || _mode == LEFT || _mode == RIGHT){ mode = _mode; } + else{ println("Alignment only accepts LEFT, RIGHT, and DECIMAL"); } + } + + void display(){ + if(!arrayLoaded){ loadArray(); } + if(!inPosition){ positionDigits(); } + pushMatrix(); + translate(pos.x,pos.y); + for(int i = 0; i < digits.size(); i++){ + digits.get(i).display(); + } + popMatrix(); + } + + private void positionDigits(){ + int curPos = 0; + int count = 0; + if(mode == LEFT){ + for(int i = 0; i < pointPlace; i++){ + curPos += -12; + digits.get(i).setRefPos(curPos, 0); + count++; + } + + curPos += -8; + digits.get(count).setRefPos(curPos, 0); + count++; + curPos += -6; + digits.get(count).setRefPos(curPos, 0); + count++; + + for(int i = count; i < digits.size(); i++){ + curPos += -12; + digits.get(i).setRefPos(curPos, 0); + } + }else if(mode == DECIMAL){ + curPos = -5; + digits.get(pointPlace).setRefPos(curPos,0); + curPos += -2; + for(int i = pointPlace - 1; i >= 0; i--){ + curPos += 12; + digits.get(i).setRefPos(curPos,0); + } + curPos = -2; + + for(int i = pointPlace + 1; i < digits.size(); i++){ + curPos += -12; + digits.get(i).setRefPos(curPos,0); + } + }else if(mode == RIGHT){ + for(int i = digits.size() - 1; i >= 0; i--){ + digits.get(count).setRefPos((12 * i) + 3, 0); + count++; + } + } + inPosition = true; + } + + private void loadArray(){ + digits = new ArrayList(); + B12Digit[] temp = new B12Digit[places]; + float mval = abs(value); + int whole = int(mval); + float deci = mval - float(whole); + + for(int i = 0; i < places; i++){ + deci *= 12; + temp[i] = new B12Digit(int(deci)); + deci -= float(int(deci)); + } + + for(int i = places - 1; i >= 0; i--){ + digits.add(temp[i]); + } + + pointPlace = digits.size(); + digits.add(new B12Digit('.')); + + while(whole > 0){ + if(whole < 12){ + digits.add(new B12Digit(whole)); + whole = 0; + }else{ + digits.add(new B12Digit(whole % 12)); + whole /= 12; + } + } + + if(value < 0){ + digits.add(new B12Digit('-')); + } + + arrayLoaded = true; + inPosition = false; + } +} diff --git a/B12NumbersV3/B12Digit.pde b/B12NumbersV3/B12Digit.pde deleted file mode 100644 index d5b48ec..0000000 --- a/B12NumbersV3/B12Digit.pde +++ /dev/null @@ -1,107 +0,0 @@ -class B12Digit{ - byte value; - PVector refPos; - - B12Digit(int _value){ - if(_value >= 12 || _value < 0){ - throw new IllegalArgumentException("B12Digit only accepts decimal integers 0 through 11"); - } - value = byte(_value); - refPos = new PVector(0,0); - } - - B12Digit(char _c){ - String valid = "+-*/.:"; // Defines valid input characters - if(!inStr(valid, _c)){ throw new IllegalArgumentException("B12Char only accepts \'+ - * / . :'"); } - value = byte(_c); - refPos = new PVector(0,0); - } - - // SETTERS - void setRefPos(PVector _refPos){ refPos = _refPos; } - void setRefPos(float _x, float _y){ refPos = new PVector(_x,_y); } - void setValue(int _value){ value = byte(_value); } - - // GETTERS - PVector getRefPos(){ return refPos; } - int getValue(){ return value; } - - // RENDER CHARACTERS - void display(){ - pushMatrix(); - translate(refPos.x,refPos.y); //<>// - strokeWeight(1); - noFill(); - ellipseMode(CORNERS); - switch(value) { - // NUMBERS // - case 0: - line0(); break; - case 1: - line1(); break; - case 2: - line1(); line2(); break; - case 3: - line1(); line2(); line3(); break; - case 4: - line4(); break; - case 5: - line4(); line1(); break; - case 6: - line4(); line1(); line2(); break; - case 7: - line4(); line1(); line2(); line3(); break; - case 8: - line8(); line4(); break; - case 9: - line8(); line4(); line1(); break; - case 10: - line8(); line4(); line1(); line2(); break; - case 11: - line8(); line4(); line1(); line2(); line3(); break; - - // CHARACTERS // - case '+': - lineMinus(); linePlus(); break; - case '-': - lineMinus(); break; - case '*': - lineTimes(); break; - case '/': - lineMinus(); dotsDiv(); break; - case '.': - strokeWeight(2); period(); break; - case ':': - strokeWeight(2); colon(); break; - } - popMatrix(); - } - - // Individual shape components to build any B12 number - void line0(){ ellipse(0,-13,8,0); } - void line1(){ line(6,0,9,-10); } - void line2(){ line(3,-10,6,0); } - void line3(){ line(0,0,3,-10); } - void line4(){ line(9,-10,2,-13); } - void line8(){ line(2,-13,9,-16); } - - // Individual shape components to build any B12 character - void lineTimes(){ line(4,-7,8,-3); line(4,-3,8,-7); } - void dotsDiv(){ point(6,-8); point(6,-2); } - void lineMinus(){ line(3,-5,9,-5); } - void linePlus(){ line(6,-8,6,-2); } - void period(){ point(5,0); } - void colon(){ point(5,-2); point(5,-8); } - - - // HELPER FUNCTIONS // - boolean inStr(String st, char _c){ - try{ - int x = st.indexOf(_c); - return true; - } - catch (Exception e){ - return false; - } - } -} diff --git a/B12NumbersV3/B12Float.pde b/B12NumbersV3/B12Float.pde deleted file mode 100644 index d4a880d..0000000 --- a/B12NumbersV3/B12Float.pde +++ /dev/null @@ -1,133 +0,0 @@ -class B12Float { - private ArrayList digits; - private float value; - private PVector pos; - private boolean arrayLoaded; - private boolean inPosition; - private int mode;// Can be RIGHT (39), CENTER (3), or LEFT (37) - private int places; - private int pointPlace; - - B12Float(float _value){ - value = _value; - pos = new PVector(0,0); - arrayLoaded = false; - inPosition = false; - mode = DECIMAL; - places = 4; - } - - float getValue(){ return value; } - void setValue(float _value){ value = _value; arrayLoaded = false; } - - PVector getPos(){ return pos; } - void setPos(PVector _pos){ pos = _pos.copy(); inPosition = false; } - void setPos(float _x, float _y){ pos = new PVector(_x, _y); inPosition = false; } - - int getPlaces(){ return places; } - void setPlaces(int _places){ - if(_places > 12 || _places < 0){ - throw new IllegalArgumentException("B12Float ncan only display to 12 duodecimal points"); - }else{ - places = _places; - } - } - - void setAlignMode(int _mode){ - if(_mode == DECIMAL || _mode == LEFT || _mode == RIGHT){ mode = _mode; } - else{ println("Alignment only accepts LEFT, RIGHT, and DECIMAL"); } - } - - void display(){ - if(!arrayLoaded){ loadArray(); } - if(!inPosition){ positionDigits(); } - pushMatrix(); - translate(pos.x,pos.y); - for(int i = 0; i < digits.size(); i++){ - digits.get(i).display(); - } - popMatrix(); - } - - private void positionDigits(){ - int curPos = 0; - int count = 0; - if(mode == LEFT){ - for(int i = 0; i < pointPlace; i++){ - curPos += -12; - digits.get(i).setRefPos(curPos, 0); - count++; - } - - curPos += -8; - digits.get(count).setRefPos(curPos, 0); - count++; - curPos += -6; - digits.get(count).setRefPos(curPos, 0); - count++; - - for(int i = count; i < digits.size(); i++){ - curPos += -12; - digits.get(i).setRefPos(curPos, 0); - } - }else if(mode == DECIMAL){ - curPos = -5; - digits.get(pointPlace).setRefPos(curPos,0); - curPos += -2; - for(int i = pointPlace - 1; i >= 0; i--){ - curPos += 12; - digits.get(i).setRefPos(curPos,0); - } - curPos = -2; - - for(int i = pointPlace + 1; i < digits.size(); i++){ - curPos += -12; - digits.get(i).setRefPos(curPos,0); - } - }else if(mode == RIGHT){ - for(int i = digits.size() - 1; i >= 0; i--){ - digits.get(count).setRefPos((12 * i) + 3, 0); - count++; - } - } - inPosition = true; - } - - private void loadArray(){ - digits = new ArrayList(); - B12Digit[] temp = new B12Digit[places]; - float mval = abs(value); - int whole = int(mval); - float deci = mval - float(whole); - - for(int i = 0; i < places; i++){ - deci *= 12; - temp[i] = new B12Digit(int(deci)); - deci -= float(int(deci)); - } - - for(int i = places - 1; i >= 0; i--){ - digits.add(temp[i]); - } - - pointPlace = digits.size(); - digits.add(new B12Digit('.')); - - while(whole > 0){ - if(whole < 12){ - digits.add(new B12Digit(whole)); - whole = 0; - }else{ - digits.add(new B12Digit(whole % 12)); - whole /= 12; - } - } - - if(value < 0){ - digits.add(new B12Digit('-')); - } - - arrayLoaded = true; - inPosition = false; - } -} diff --git a/B12NumbersV3/B12Int.pde b/B12NumbersV3/B12Int.pde deleted file mode 100644 index 35ac840..0000000 --- a/B12NumbersV3/B12Int.pde +++ /dev/null @@ -1,81 +0,0 @@ -class B12Int { - private ArrayList digits; - private int value; - private PVector pos; - private boolean arrayLoaded; - private boolean inPosition; - private int mode;// Can be RIGHT (39), CENTER (3), or LEFT (37) - private int minLen; - - B12Int(int _value){ - value = _value; - pos = new PVector(0,0); - arrayLoaded = false; - inPosition = false; - mode = LEFT; - minLen = 0; - } - - int getValue(){ return value; } - void setValue(int _value){ value = _value; arrayLoaded = false; } - - void setPos(PVector _pos){ pos = _pos.copy(); inPosition = false; } - void setPos(float _x, float _y){ pos = new PVector(_x, _y); inPosition = false; } - PVector getPos(){ return pos; } - - void setMinLen(int i){ minLen = i; } - - void setAlignMode(int _mode){ - if(_mode == DECIMAL || _mode == LEFT || _mode == RIGHT){ mode = _mode; } - else{ println("Alignment only accepts LEFT, RIGHT, and DECIMAL"); } - } - - void display(){ - if(!arrayLoaded){ loadArray(); } - if(!inPosition){ positionDigits(); } - pushMatrix(); - translate(pos.x,pos.y); - for(int i = 0; i < digits.size(); i++){ - digits.get(i).display(); - } - popMatrix(); - } - - private void positionDigits(){ - if(mode == LEFT || mode == DECIMAL){ - for(int i = 0; i < digits.size(); i++){ - digits.get(i).setRefPos((-12 * (i+1)), 0); - } - }else if(mode == RIGHT){ - int count = 0; - for(int i = digits.size() - 1; i >= 0; i--){ - digits.get(count).setRefPos((12 * i) + 3, 0); - count++; - } - } - inPosition = true; - } - - private void loadArray(){ - digits = new ArrayList(); - int mval = abs(value); - if(mval == 0){ digits.add(new B12Digit(0)); } - while(mval != 0){ - if(mval < 12){ - digits.add(new B12Digit(mval)); - mval = 0; - }else{ - digits.add(new B12Digit(mval % 12)); - mval /= 12; - } - } - if(digits.size() < minLen){ - digits.add(new B12Digit(0)); - } - if(value < 0){ - digits.add(new B12Digit('-')); - } - - arrayLoaded = true; - } -} diff --git a/B12NumbersV3/B12NumbersV3.pde b/B12NumbersV3/B12NumbersV3.pde index d53c4f3..854cc81 100644 --- a/B12NumbersV3/B12NumbersV3.pde +++ b/B12NumbersV3/B12NumbersV3.pde @@ -3,11 +3,21 @@ Beta version of a clock in base 12. by Nayan Sawyer started Mar 2022 - version 0.1.3 April 29 2022 + version 0.1.4.0 April 30 2022 Characters are a variation of Kaktovik Inupiaq numerals reversed and in base 12 instead of 20. I take no credit for the design. + 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. + + changelog 0.1.4.0 + - Added MethodRelay code from Quark. Some fixes and + changes as well. Condesed some things into fewer files + for so the ide is easier to use, but they will be moved + back into their own files as version 1.0 approaches. + Everything is prep for adding gui elements (MethodRelay + included) changelog 0.1.3 - Deprecated B12Char by rolling it's code into B12Digit. @@ -18,7 +28,8 @@ to be fine with simply swithing out the reference. */ -public static int DECIMAL = 65; +public static final int DECIMAL = 65; +ClickHandler ch; // Mouse event handler Clock clock; //<>// B12Digit p; @@ -26,6 +37,8 @@ B12Digit t; void setup(){ size(400,400); + ch = new ClickHandler(); + clock = new Clock(new STime48()); println("waiting"); p = new B12Digit('+'); @@ -43,9 +56,12 @@ void draw(){ } void mouseClicked(){ - clock.setTime(new Time48(16,0,0)); - - // Decide which element is on top at mouse position (maybe by state?) - method(""); + //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(mouseX,mouseY); +} + +void call(String _call){ + method(_call); } diff --git a/B12NumbersV3/B12TimeAll.pde b/B12NumbersV3/B12TimeAll.pde new file mode 100644 index 0000000..ffefc3f --- /dev/null +++ b/B12NumbersV3/B12TimeAll.pde @@ -0,0 +1,207 @@ +class Time48 extends Thread{ + private int sec48; + private int min48; + private int hour48; + private int tsec48; // Total seconds + private boolean initialized; + + // CONSTRUCTORS // + // TODO add exceptions to all contructors + Time48(){ + sec48 = 0; + min48 = 0; + hour48 = 0; + tsec48 = 0; + initialized = false; + } + + Time48(int _tsec48){ + tsec48 = _tsec48; + flattenTSec(); + initialized = true; + } + + Time48(Time48 t48){ + tsec48 = t48.tsec(); + flattenTSec(); + initialized = true; + } + + Time48(int h, int m, int s){ + if(h >= 0 && h < 24){ hour48 = h;}else{throw new IllegalArgumentException();} + if(m >= 0 && m < 48){ min48 = m;}else{throw new IllegalArgumentException();} + if(s >= 0 && s < 48){ sec48 = s;}else{throw new IllegalArgumentException();} + flattenOther(); + initialized = true; + } + + // GETTERS // + int hours(){return hour48;} + int mins(){return min48;} + int secs(){return sec48;} + int tsec(){return tsec48;} + int[] t48(){int[] out = {hour48,min48,sec48}; return out;} + boolean initialized(){return initialized;} + int b10millis(){return int(float(tsec48) * 1562.5);} + + Time48 copy(){ return new Time48(this); } + + // SETTERS // + void setHour(int h){ + if(h < 0 || h >= 24) throw new IllegalArgumentException(); + hour48 = h; + flattenOther(); + initialized = true; + } + void setMin(int m){ + if(m < 0 || m >= 48) throw new IllegalArgumentException(); + min48 = m; + flattenOther(); + initialized = true; + } + void setSec(int s){ + if(s < 0 || s >= 48) throw new IllegalArgumentException(); + sec48 = s; + flattenOther(); + initialized = true; + } + void setTsec(int s){ // TODO add exception + tsec48 = s; + flattenTSec(); + initialized = true; + } + + // PRIVATE FUNCTIONS // + private void flattenTSec(){ + sec48 = tsec48; + min48 = sec48 / 48; + hour48 = min48 / 48; + + sec48 -= min48 * 48; + min48 -= hour48 * 48; + } + private void flattenOther(){ + tsec48 = hour48*48*48 + min48*48 + sec48; + } + + + // PLACEHOLDER FUNCTIONS // + public void run(){} +} + + + +class STime48 extends Time48{ + private int offset; // Time offset in milliseconds + private int tmillis; // Actual time at which time was synced to real world in milliseconds + private int syncedat; // Offset between the start of the program (when millis() starts counting from) and when the time was last synced + private boolean synced; + + STime48(){ + super(); + offset = 0; + tmillis = 0; + syncedat = 0; + synced = false; + this.start(); + } + + // Public sync functions + public boolean synced(){return synced;} + public void syncTime(){ synced = false; } // Allows syncing after time starts running + public void setTime(Time48 _time){ + // To get offset we subtract where the current clock is from where we want it to be + offset = _time.b10millis() - millis() - tmillis + syncedat; + } + + // Threaded code + @Override + public void run(){ + while(true){ + if(!synced){sync();} + + delay(1); // MUST USE DELAY OR OFFSET DOES NOT GET CALCULATED + if(tmillis + millis() + offset > 86400000){ tmillis -= 86400000; } // Fall over at 00:00 + setTsec(int((tmillis + millis() - syncedat + offset) / 1562.5)); // Add time at sync, millis since program start, and offset, and subtract the millis between program start and sync (because we're adding millis()) + } + } + + // Initial sync code + private void sync(){ + int sec = second(); + tmillis = 0; + while(true){ + if(sec != second()){ // Wait until seconds changes so as to be as accurate as possible + tmillis = second()*1000 + minute()*60*1000 + hour()*60*60*1000; // Current time in total millis + syncedat = millis(); + synced = true; + println("synced"); + break; + } + } + } +} + + + + +class Clock { + PVector pos; + STime48 t48; + B12Int hours; + B12Int minutes; + B12Int seconds; + B12Digit sep; // TODO Just deprecated B12Char. Refactor to single array of B12Digits? + int tmillis; + + Clock(STime48 _t48) { + pos = new PVector(0, 0); + t48 = _t48; + hours = new B12Int(t48.hours()); + minutes = new B12Int(t48.mins()); + seconds = new B12Int(t48.secs()); + sep = new B12Digit(':'); // Seperation character between time columns + + hours.setMinLen(2); + minutes.setMinLen(2); // Format all the ints to show a 0 in the 12s column if they are less than 12 + seconds.setMinLen(2); + } + + // GETTERS and SETTERS // + PVector getPos() { return pos; } + void setPos(PVector _pos) { pos = _pos.copy(); } + void setPos(float _x, float _y) { pos = new PVector(_x, _y);} + + void setTime(Time48 _time) { t48.setTime(_time); } + void resetTime() { t48.setTime(new Time48(0)); } + + void display() { + if (t48.synced()) { + // Time + hours.setValue(t48.hours()); + minutes.setValue(t48.mins()); + seconds.setValue(t48.secs()); + + // Position + hours.setPos(-64, 0); + minutes.setPos(-32, 0); + seconds.setPos(0, 0); + B12Digit c1 = new B12Digit(':'); + B12Digit c2 = new B12Digit(':'); + c1.setRefPos(-34, 0); + c2.setRefPos(-66, 0); + + // Display + pushMatrix(); + translate(pos.x, pos.y); + hours.display(); + c2.display(); + minutes.display(); + c1.display(); + seconds.display(); + popMatrix(); + } else { + text("fetching current time", pos.x, pos.y); + } + } +} diff --git a/B12NumbersV3/Clock.pde b/B12NumbersV3/Clock.pde deleted file mode 100644 index dc50ba2..0000000 --- a/B12NumbersV3/Clock.pde +++ /dev/null @@ -1,60 +0,0 @@ -class Clock { - PVector pos; - STime48 t48; - B12Int hours; - B12Int minutes; - B12Int seconds; - B12Digit sep; // TODO Just deprecated B12Char. Refactor to single array of B12Digits? - int tmillis; - - Clock(STime48 _t48) { - pos = new PVector(0, 0); - t48 = _t48; - hours = new B12Int(t48.hours()); - minutes = new B12Int(t48.mins()); - seconds = new B12Int(t48.secs()); - sep = new B12Digit(':'); // Seperation character between time columns - - hours.setMinLen(2); - minutes.setMinLen(2); // Format all the ints to show a 0 in the 12s column if they are less than 12 - seconds.setMinLen(2); - } - - // GETTERS and SETTERS // - PVector getPos() { return pos; } - void setPos(PVector _pos) { pos = _pos.copy(); } - void setPos(float _x, float _y) { pos = new PVector(_x, _y);} - - void setTime(Time48 _time) { t48.setTime(_time); } - void resetTime() { t48.setTime(new Time48(0)); } - - void display() { - if (t48.synced()) { - // Time - hours.setValue(t48.hours()); - minutes.setValue(t48.mins()); - seconds.setValue(t48.secs()); - - // Position - hours.setPos(-64, 0); - minutes.setPos(-32, 0); - seconds.setPos(0, 0); - B12Digit c1 = new B12Digit(':'); - B12Digit c2 = new B12Digit(':'); - c1.setRefPos(-34, 0); - c2.setRefPos(-66, 0); - - // Display - pushMatrix(); - translate(pos.x, pos.y); - hours.display(); - c2.display(); - minutes.display(); - c1.display(); - seconds.display(); - popMatrix(); - } else { - text("fetching current time", pos.x, pos.y); - } - } -} diff --git a/B12NumbersV3/MathPad.pde b/B12NumbersV3/MathPad.pde deleted file mode 100644 index beaf929..0000000 --- a/B12NumbersV3/MathPad.pde +++ /dev/null @@ -1,16 +0,0 @@ -class MathPad{ - B12Math math; - - MathPad(B12Math _math){ - math = _math; - } - - // TODO draw a grid for buttons - // TODO draw characters in grid - // TODO detect mousepresses on the buttons (maybe a global mouse handler?) - // TODO send characters to math - - void addchar(){ - //math.expression.add(new B12Char('/')); - } -} diff --git a/B12NumbersV3/MouseListener.pde b/B12NumbersV3/MouseListener.pde deleted file mode 100644 index 56b7469..0000000 --- a/B12NumbersV3/MouseListener.pde +++ /dev/null @@ -1,16 +0,0 @@ -class MouseHandler{ - StringList clickListeners; - - MouseHandler(){ - clickListeners = new StringList(); - } - - void listen(){ - } - - -} - -class Listen{ - - } diff --git a/B12NumbersV3/STime48.pde b/B12NumbersV3/STime48.pde deleted file mode 100644 index b3e94a1..0000000 --- a/B12NumbersV3/STime48.pde +++ /dev/null @@ -1,50 +0,0 @@ -class STime48 extends Time48{ - private int offset; // Time offset in milliseconds - private int tmillis; // Actual time at which time was synced to real world in milliseconds - private int syncedat; // Offset between the start of the program (when millis() starts counting from) and when the time was last synced - private boolean synced; - - STime48(){ - super(); - offset = 0; - tmillis = 0; - syncedat = 0; - synced = false; - this.start(); - } - - // Public sync functions - public boolean synced(){return synced;} - public void syncTime(){ synced = false; } // Allows syncing after time starts running - public void setTime(Time48 _time){ - // To get offset we subtract where the current clock is from where we want it to be - offset = _time.b10millis() - millis() - tmillis + syncedat; - } - - // Threaded code - @Override - public void run(){ - while(true){ - if(!synced){sync();} - - delay(1); // MUST USE DELAY OR OFFSET DOES NOT GET CALCULATED - if(tmillis + millis() + offset > 86400000){ tmillis -= 86400000; } // Fall over at 00:00 - setTsec(int((tmillis + millis() - syncedat + offset) / 1562.5)); // Add time at sync, millis since program start, and offset, and subtract the millis between program start and sync (because we're adding millis()) - } - } - - // Initial sync code - private void sync(){ - int sec = second(); - tmillis = 0; - while(true){ - if(sec != second()){ // Wait until seconds changes so as to be as accurate as possible - tmillis = second()*1000 + minute()*60*1000 + hour()*60*60*1000; // Current time in total millis - syncedat = millis(); - synced = true; - println("synced"); - break; - } - } - } -} diff --git a/B12NumbersV3/Time48.pde b/B12NumbersV3/Time48.pde deleted file mode 100644 index b58d253..0000000 --- a/B12NumbersV3/Time48.pde +++ /dev/null @@ -1,90 +0,0 @@ -class Time48 extends Thread{ - private int sec48; - private int min48; - private int hour48; - private int tsec48; // Total seconds - private boolean initialized; - - // CONSTRUCTORS // - // TODO add exceptions to all contructors - Time48(){ - sec48 = 0; - min48 = 0; - hour48 = 0; - tsec48 = 0; - initialized = false; - } - - Time48(int _tsec48){ - tsec48 = _tsec48; - flattenTSec(); - initialized = true; - } - - Time48(Time48 t48){ - tsec48 = t48.tsec(); - flattenTSec(); - initialized = true; - } - - Time48(int h, int m, int s){ - if(h >= 0 && h < 24){ hour48 = h;}else{throw new IllegalArgumentException();} - if(m >= 0 && m < 48){ min48 = m;}else{throw new IllegalArgumentException();} - if(s >= 0 && s < 48){ sec48 = s;}else{throw new IllegalArgumentException();} - flattenOther(); - initialized = true; - } - - // GETTERS // - int hours(){return hour48;} - int mins(){return min48;} - int secs(){return sec48;} - int tsec(){return tsec48;} - int[] t48(){int[] out = {hour48,min48,sec48}; return out;} - boolean initialized(){return initialized;} - int b10millis(){return int(float(tsec48) * 1562.5);} - - Time48 copy(){ return new Time48(this); } - - // SETTERS // - void setHour(int h){ - if(h < 0 || h >= 24) throw new IllegalArgumentException(); - hour48 = h; - flattenOther(); - initialized = true; - } - void setMin(int m){ - if(m < 0 || m >= 48) throw new IllegalArgumentException(); - min48 = m; - flattenOther(); - initialized = true; - } - void setSec(int s){ - if(s < 0 || s >= 48) throw new IllegalArgumentException(); - sec48 = s; - flattenOther(); - initialized = true; - } - void setTsec(int s){ // TODO add exception - tsec48 = s; - flattenTSec(); - initialized = true; - } - - // PRIVATE FUNCTIONS // - private void flattenTSec(){ - sec48 = tsec48; - min48 = sec48 / 48; - hour48 = min48 / 48; - - sec48 -= min48 * 48; - min48 -= hour48 * 48; - } - private void flattenOther(){ - tsec48 = hour48*48*48 + min48*48 + sec48; - } - - - // PLACEHOLDER FUNCTIONS // - public void run(){} -} diff --git a/B12NumbersV3/B12Math.pde b/B12NumbersV3/guiB12Math.pde similarity index 100% rename from B12NumbersV3/B12Math.pde rename to B12NumbersV3/guiB12Math.pde diff --git a/B12NumbersV3/guiButton.pde b/B12NumbersV3/guiButton.pde new file mode 100644 index 0000000..87db69c --- /dev/null +++ b/B12NumbersV3/guiButton.pde @@ -0,0 +1,99 @@ +class Button{ + ClickHandler ch; + PVector pos; // Position to render from + PVector dim; // Second coordinate for CORNERS or len/wid for CORNER and CENTER + int radius; // Optional corner radius + int mode; + color c; + boolean mouseOver; + + Button(ClickHandler _ch, PVector _pos, PVector _dim, int _radius){ + ch = _ch; + pos = _pos.copy(); + dim = _dim.copy(); + radius = _radius; + mode = CORNER; + c = color(200); + mouseOver = false; + ch.addl(new LiveMethodRelay(this, "clicked", float.class, float.class)); + } + Button(ClickHandler _ch, PVector _pos, PVector _dim){ + this(_ch, _pos, _dim, 0); + } + + void setMode(int m){ + if(m == CORNER || m == CORNERS || m == CENTER || m == RADIUS){ + mode = m; + return; + } + return; + } + + void display(){ + rectMode(mode); + new MethodRelay(this,"mouseOver" + str(mode), float.class, float.class).execute(mouseX,mouseY); + if (mouseOver){ + fill(100); + }else{fill(c);} + rect(pos.x,pos.y,dim.x,dim.y,radius); + } + + void clicked(float x, float y){ + if(mouseOver){ + println(x + " " + y + " mouse pos"); + } + } + + + // DETECT IF MOUSE IS OVER BUTTON // + // The numbers in the method name correspond to the mode ids because the method gets called with a relay + void mouseOver0(float x, float y){ // CORNER + //println("CORNER"); + if(x < pos.x || x > dim.x + pos.x || y < pos.x || y > dim.y + pos.y) + { + mouseOver = false; + return; + } + mouseOver = true; + } + + void mouseOver1(float x, float y){ // CORNERS + //println("CORNERS"); + if(x < (pos.x > dim.x ? dim.x : pos.x) || + x > (pos.x > dim.x ? pos.x : dim.x) || + y < (pos.y > dim.y ? dim.y : pos.y) || + y > (pos.y > dim.y ? pos.y : dim.y)) + { + mouseOver = false; + return; + } + + mouseOver = true; + } + + void mouseOver2(float x, float y){ // RADIUS + //println("RADIUS"); + if(x < pos.x - dim.x || + x > pos.x + dim.x || + y < pos.x - dim.y || + y > pos.x + dim.y) + { + mouseOver = false; + return; + } + mouseOver = true; + } + + void mouseOver3(float x, float y){ // CENTER + //println("CENTER"); + if(x < pos.x - dim.x/2 || + x > pos.x + dim.x/2 || + y < pos.x - dim.y/2 || + y > pos.y + dim.y/2) + { + mouseOver = false; + return; + } + mouseOver = true; + } +} diff --git a/B12NumbersV3/MathDisplay.pde b/B12NumbersV3/guiMathDisplay.pde similarity index 100% rename from B12NumbersV3/MathDisplay.pde rename to B12NumbersV3/guiMathDisplay.pde diff --git a/B12NumbersV3/guiMathPad.pde b/B12NumbersV3/guiMathPad.pde new file mode 100644 index 0000000..ea234a4 --- /dev/null +++ b/B12NumbersV3/guiMathPad.pde @@ -0,0 +1,50 @@ +class MathPad{ + B12Math math; + + MathPad(B12Math _math){ + math = _math; + } + + // TODO draw a grid for buttons + // TODO draw characters in grid + // TODO detect mousepresses on the buttons (maybe a global mouse handler?) + // TODO send characters to math + + void addchar(){ + //math.expression.add(new B12Char('/')); + } +} + +// Deprecated since MethodRelay added +//class Button{ +// PVector pos; // Position to render from +// PVector dim; // Second coordinate for CORNERS or len/wid for CORNER and CENTER +// int radius; // Optional corner radius +// color c; +// Listener trigger; +// BListener clickListener; + +// Button(PVector _pos, PVector _dim, Listener _trigger){ +// pos = _pos.copy(); +// dim = _dim.copy(); +// trigger = _trigger; +// } +// Button(PVector _pos, PVector _dim, int _radius, Listener _trigger){ +// pos = _pos.copy(); +// dim = _dim.copy(); +// trigger = _trigger; +// radius = _radius; +// } + +// class BListener extends Listener{ +// Button parent; +// BListener(Handler mh, Button t){ +// super(mh); +// parent = t; +// } + +// void trigger(){ +// parent.trigger(); +// } +// } +//}