mirror of
https://github.com/opus-tango/B12NumbersV3.git
synced 2026-03-20 03:55:20 +00:00
Setters, positioning, and expression parsing
button setters, mathPad positioning, and expression parsing to String
This commit is contained in:
@@ -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(); }
|
||||
|
||||
@@ -5,16 +5,22 @@ 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"));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void draw(){
|
||||
@@ -28,7 +34,8 @@ void draw(){
|
||||
scale(scale);
|
||||
|
||||
if(calc != null) calc.display();
|
||||
b.display();
|
||||
reset.display();
|
||||
eval.display();
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class B12Math {
|
||||
/*class B12Math {
|
||||
ArrayList <B12Digit> expression;
|
||||
|
||||
B12Math(){
|
||||
@@ -12,4 +12,4 @@ class B12Math {
|
||||
void evaluate(){
|
||||
//TODO set expression to evaluation of expression
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
@@ -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;}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 //
|
||||
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();
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user