From 0cc7aa1432000e6631ff65d8f6b723ef35a7996b Mon Sep 17 00:00:00 2001 From: 61616 Date: Wed, 11 May 2022 15:31:45 -0400 Subject: [PATCH] Setters, positioning, and expression parsing button setters, mathPad positioning, and expression parsing to String --- B12NumbersV3/B12Base.pde | 50 ++++++++++++++++-- B12NumbersV3/B12NumbersV3.pde | 13 +++-- B12NumbersV3/guiB12Expression.pde | 84 ++++++++++++++++++++++++++++++- B12NumbersV3/guiB12Math.pde | 4 +- B12NumbersV3/guiButton.pde | 19 ++++--- B12NumbersV3/guiMathPad.pde | 27 +++++----- B12NumbersV3/zchangelog.pde | 12 ++++- 7 files changed, 176 insertions(+), 33 deletions(-) diff --git a/B12NumbersV3/B12Base.pde b/B12NumbersV3/B12Base.pde index 20eb2e6..df988d4 100644 --- a/B12NumbersV3/B12Base.pde +++ b/B12NumbersV3/B12Base.pde @@ -1,10 +1,12 @@ abstract interface Number{ abstract PVector getPos(); + + abstract Number addn(Number num); abstract void display(); } -class B12Digit{ +class B12Digit implements Number{ private byte value; private PVector refPos; @@ -27,10 +29,12 @@ class B12Digit{ B12Digit setRefPos(PVector _refPos){ refPos = _refPos; return this;} B12Digit setRefPos(float _x, float _y){ refPos = new PVector(_x,_y); return this;} B12Digit setValue(int _value){ value = byte(_value); return this;} + Number addn(Number num){throw new UnsupportedOperationException("B12Digit does not support adding characters");} // GETTERS - PVector getRefPos(){ return refPos; } + PVector getPos(){ return refPos; } int getValue(){ return value; } + boolean isNum(){return value >= 0 && value < 12; } // RENDER CHARACTERS void display(){ @@ -133,10 +137,12 @@ class B12Int implements Number { minLen = 0; } + // GETTERS // int getValue(){ return value; } PVector getPos(){ return pos; } + B12Float toFloat(){return new B12Float(float(value)); } - + // SETTERS // B12Int setValue(int _value){ value = _value; arrayLoaded = false; return this;} B12Int setPos(PVector _pos){ pos = _pos.copy(); inPosition = false; return this;} B12Int setPos(float _x, float _y){ pos = new PVector(_x, _y); inPosition = false;return this; } @@ -147,6 +153,24 @@ class B12Int implements Number { return this; } + Number addn(Number num){ + /*if(num.getClass() == ByteE.class){ + ByteE b = (ByteE)num; + value += int(b.getValue()); + return new B12Int(value); + }*/ + if(num.getClass() == B12Int.class){ + B12Int i = (B12Int)num; + value += i.getValue(); + return new B12Int(value); + } + if(num.getClass() == B12Float.class){ + B12Float i = (B12Float)num; + return i.addn(this); + } + throw new IllegalArgumentException("addn only takes types B12Int and B12Float"); + } + void display(){ if(!arrayLoaded){ loadArray(); } if(!inPosition){ positionDigits(); } @@ -222,6 +246,7 @@ class B12Float implements Number{ float getValue(){ return value; } PVector getPos(){ return pos; } int getPlaces(){ return places; } + B12Int toInt(){return new B12Int(int(value));} B12Float setValue(float _value){ value = _value; arrayLoaded = false; return this;} @@ -241,6 +266,25 @@ class B12Float implements Number{ return this; } + Number addn(Number num){ + /*if(num.getClass() == ByteE.class){ + ByteE b = (ByteE)num; + value += float(b.getValue()); + return new B12Float(vlaue); + }*/ + if(num.getClass() == B12Int.class){ + B12Int i = (B12Int)num; + value += float(i.getValue()); + return new B12Float(value); + } + if(num.getClass() == B12Float.class){ + B12Float i = (B12Float)num; + value += i.getValue(); + return new B12Float(value); + } + throw new IllegalArgumentException("addn only takes types B12Int and B12Float"); + } + void display(){ if(!arrayLoaded){ loadArray(); } if(!inPosition){ positionDigits(); } diff --git a/B12NumbersV3/B12NumbersV3.pde b/B12NumbersV3/B12NumbersV3.pde index 6d94496..ada1289 100644 --- a/B12NumbersV3/B12NumbersV3.pde +++ b/B12NumbersV3/B12NumbersV3.pde @@ -5,15 +5,21 @@ public static final int DECIMAL = 65; MouseHandler mh; // Mouse event handler Calculator calc; -Button b; +Button reset; +Button eval; + void setup(){ size(400,400); offset = new PVector(width/2, height/2); mh = new MouseHandler(new MouseData(offset, scale)); - b = new Button(mh, new PVector(20,-20), new PVector(40,20),2).setColor(#06BA63).autoHighlight().setText("Reset").setFunction(new MethodRelay(this, "changeMode")); calc = new Calculator(mh); + + reset = new Button(mh).setPos(new PVector(20,-20), new PVector(40,20)).setRadius(2).setColor(#06BA63).autoHighlight().setText("Reset").setFunction(new MethodRelay(this, "changeMode")); + eval = new Button(mh).setPos(new PVector(20,-40), new PVector(40,20)).setRadius(2).setColor(#06BA63).autoHighlight().setText("Eval").setFunction(new MethodRelay(calc.ex, "evaluate")); + + } @@ -28,7 +34,8 @@ void draw(){ scale(scale); if(calc != null) calc.display(); - b.display(); + reset.display(); + eval.display(); } diff --git a/B12NumbersV3/guiB12Expression.pde b/B12NumbersV3/guiB12Expression.pde index 0bacd2d..51d750f 100644 --- a/B12NumbersV3/guiB12Expression.pde +++ b/B12NumbersV3/guiB12Expression.pde @@ -8,7 +8,7 @@ class B12Expression { B12Digit getDigit(int index){ return expression[index]; } int length(){return expression.length;} - B12Expression setChar(int ind, B12Digit _digit){ + B12Expression insertChar(int ind, B12Digit _digit){ expression = (B12Digit[])append(expression, _digit); // Add the new digit if(ind < expression.length - 1){ // Swap new digit for(int i = expression.length - 1; i > ind; i--){ // Start at second to last digit @@ -25,6 +25,86 @@ class B12Expression { } void evaluate(){ - //TODO set expression to evaluation of expression + String valid = "+*-/"; // valid characters to accept + String evalString = ""; // gets filled with the expression up for evaluation in base + Number cnum = null; // used to sum numbers as the array is parsed + int count = 0; // counts what column we're at for multiplying base 12 to base 10 + + + // Parse expression[] into a base 10 string that can be evaluated mathematically + if(!expression[expression.length - 1].isNum()){throw new IllegalArgumentException("Invalid input");} // check that final character is a number + for (int c = expression.length; c >= 0; c--){ + int i = c - 1; + + if (i == -1){ // At the end, add the final number if neccessary //<>// + // add number to string if present + if(cnum != null && cnum.getClass() == B12Int.class){ + B12Int t = (B12Int)cnum; + evalString = evalString + str(t.getValue()); + cnum = null; + } + else if(cnum != null && cnum.getClass() == B12Float.class){ + B12Float t = (B12Float)cnum; + evalString = evalString + str(t.getValue()); + cnum = null; + } + break; + } + + // If there is no number currently being built and the current character is a number start building a new number + if (expression[i].isNum() && cnum == null){ + count = 0; + cnum = new B12Int(int(expression[i].getValue())); + } + // If the current character is a number and there IS a number currently being built add the character into the number + else if (expression[i].isNum() && cnum != null){ + count += 1; + cnum = cnum.addn(new B12Int(int(expression[i].getValue() * (pow(12,count))))); + } + // If we run into a period and are currently building an int, switch to float and make current number the decimal. (MAY BE BROKEN) + else if (expression[i].value == '.' && cnum != null && cnum.getClass() == B12Int.class){ + B12Int temp = (B12Int)cnum; + //float f = float("." + str((B12Int)cnum.getValue())); + cnum = new B12Float(float("." + str(temp.getValue()))); + } + // If any other valid character just add it to the string after making sure to add the last built number if it exists + else if (inStr(valid,char(expression[i].value))){ + // reset number digit multiplier count + count = 0; + + // add number to string if present + if(cnum != null && cnum.getClass() == B12Int.class){ + B12Int t = (B12Int)cnum; + evalString = evalString + str(t.getValue()); + cnum = null; + } + if(cnum != null && cnum.getClass() == B12Float.class){ + B12Float t = (B12Float)cnum; + evalString = evalString + str(t.getValue()); + cnum = null; + } + + // add character to string + evalString = evalString + char(expression[i].getValue()); + } + // In all other cases fail + else{ + throw new IllegalArgumentException("Invalid input"); + } + } + + println(evalString); + } + + + // 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/guiB12Math.pde b/B12NumbersV3/guiB12Math.pde index 7a96bbd..e142912 100644 --- a/B12NumbersV3/guiB12Math.pde +++ b/B12NumbersV3/guiB12Math.pde @@ -1,4 +1,4 @@ -class B12Math { +/*class B12Math { ArrayList expression; B12Math(){ @@ -12,4 +12,4 @@ class B12Math { void evaluate(){ //TODO set expression to evaluation of expression } -} +}*/ diff --git a/B12NumbersV3/guiButton.pde b/B12NumbersV3/guiButton.pde index 555824f..82cdac2 100644 --- a/B12NumbersV3/guiButton.pde +++ b/B12NumbersV3/guiButton.pde @@ -14,12 +14,12 @@ class Button{ private boolean mouseOver; private Object[] data; // Anything that gets passed to MethodRelay function when key pressed. Must be set manually - Button(MouseHandler _mh, PVector _pos, PVector _dim, float _radius){ + Button(MouseHandler _mh){ mh = _mh; - pos = _pos.copy(); - dim = _dim.copy(); - radius = _radius; + pos = new PVector(0,0); + dim = new PVector(20,20); + radius = 1; mode = CORNER; col = color(200); colorMode(HSB); @@ -31,9 +31,6 @@ class Button{ mh.addRelay(listener); data = null; } - Button(MouseHandler _mh, PVector _pos, PVector _dim){ - this(_mh, _pos, _dim, 0); - } // GETTERS // float[] getRect(){ float[] out = {pos.x, pos.y, dim.x, dim.y}; return out; } @@ -45,6 +42,9 @@ class Button{ int getMode(){return mode; } // SETTERS // + Button setPos(PVector _pos){pos = _pos.copy(); return this;} + Button setPos(PVector _pos, PVector _dim){pos = _pos.copy(); dim = _dim.copy(); return this; } + Button setDim(PVector _dim){dim = _dim.copy(); return this;} Button setRect(PVector _pos, PVector _dim){pos = _pos; dim = _dim; return this;} Button setRadius(float rad){radius = rad; return this;} Button setColor(color c){col = c; return this;} @@ -53,7 +53,7 @@ class Button{ Button setHighlight(color h){ highlight = h; return this; } Button setText(String t){text = t; return this;} - Button setFunction(MethodRelay _function){function = _function;return this;} + Button setFunction(MethodRelay _function){function = _function; return this;} Button setData(Object... _data){ data = _data; return this;} // 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 Button setMode(int m){ if(m == CORNER || m == CORNERS || m == CENTER || m == RADIUS){mode = m;return this;} /*Otherwise*/ return this;} @@ -75,8 +75,7 @@ class Button{ void clicked(Object _mp){ // mp[0] is smouseX and m[1] is smouseY float[] mp = (float[])_mp; if(mouseOver && mouseButton == LEFT){ - //println(mp[0] + " " + mp[1] + " mouse pos"); - //println(col + " : " + highlight); + //println("clicked" + this); function.execute(data); } } diff --git a/B12NumbersV3/guiMathPad.pde b/B12NumbersV3/guiMathPad.pde index fc97f0a..fdeb079 100644 --- a/B12NumbersV3/guiMathPad.pde +++ b/B12NumbersV3/guiMathPad.pde @@ -14,20 +14,26 @@ class MathPad{ void initialize(){ for(int i = 0; i < 12; i++){ - buttons[i] = new B12Button(mh, new PVector(22 * int(i%4),22 * 2 - 22 * floor(i/4)), new PVector(20,20),new B12Digit(i)).setFunction(new MethodRelay(this, "addChar", B12Digit.class)).setColor(220,150); + /* Button position must contain it's absolute position relative to sketch 0,0 for mouseOver to work. + This means we cannot translate and traw buttons, we mumst factor the parents position into the + absolute position of the button */ + // x = pos.x + (width + gap) * (i%cols) + // y = pos.y + (height + gap) * b2rows - (height + gap) * row + PVector bPos = new PVector(pos.x + 22 * int(i%4),pos.y + 22 * 2 - 22 * floor(i/4)); + buttons[i] = new B12Button(mh ,new B12Digit(i)).setPos(bPos).setDim(new PVector(20,20)).setFunction(new MethodRelay(this, "addChar", B12Digit.class)).setColor(220,150); } } - // TODO send characters to display + // DONE send characters to display void addChar(B12Digit _digit){ ex.addChar(_digit); - println("clicked " + _digit.getValue()); + //println("clicked " + _digit.getValue()); } void display(){ pushMatrix(); - translate(pos.x,pos.y); + //translate(pos.x,pos.y); for(int i = 0; i < 12; i++){ buttons[i].display(); } @@ -40,23 +46,20 @@ class MathPad{ class B12Button extends Button{ B12Digit digit; - B12Button(MouseHandler _mh, PVector _pos, PVector _dim, float _radius, B12Digit _digit){ - super(_mh,_pos,_dim,_radius); - //data = new Object[]{_digit}; Deprecated + B12Button(MouseHandler _mh, B12Digit _digit){ + super(_mh); digit = _digit; setData(_digit); } - B12Button(MouseHandler _mh, PVector _pos, PVector _dim, B12Digit _digit){ - this(_mh, _pos, _dim, 2, _digit); - } - // GETTERS AND SETTERS // + // GETTERS AND SETTERS // B12Digit getDigit(){ return digit; } B12Button setDigit(B12Digit _digit){ digit = _digit; return this; } + // Add the B12Digit to the display method @Override void display(){ - super.display(); //<>// + super.display(); pushMatrix(); diff --git a/B12NumbersV3/zchangelog.pde b/B12NumbersV3/zchangelog.pde index 3967e19..d00a7ac 100644 --- a/B12NumbersV3/zchangelog.pde +++ b/B12NumbersV3/zchangelog.pde @@ -3,7 +3,7 @@ Beta version of a clock in base 12. by Nayan Sawyer started Mar 2022 - version 0.1.5.3 April 30 2022 + version 0.1.5.4 April 30 2022 Characters are a variation of Kaktovik Inupiaq numerals reversed and in base 12 instead of 20. I take no credit @@ -11,6 +11,8 @@ 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 add addition method to number interface as well as B12Int and B12Float + // TODO change zero character // TODO redo position data 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) @@ -19,6 +21,14 @@ // MAYBE start clock widget structure // MAYBE add additional operations like power, log, and trig functions + changelog 0.1.5.4 + - updated buttons to take minimal creation arguments, and + require the use of setters to change position, dimensions, + etc. Changed how mathpad sets the render position of + buttons to allow mathPad position to be other than 0,0. + Added parsing B12Digit array to string expression in base + 12 to B12Expression. + changelog 0.1.5.3 - restricted button presses to left mouse button only. Updated button highlight color functionality. Updated all