This commit is contained in:
61616
2022-05-06 11:00:42 -04:00
parent 22035e0ab7
commit 3bc734e9b5
5 changed files with 98 additions and 52 deletions

View File

@@ -1,51 +1,88 @@
class ClickHandler{ class MouseHandler {
LiveMethodRelay[] mrs; LiveMethodRelay[] mrs;
ClickHandler(){ MouseHandler() {
mrs = new LiveMethodRelay[0]; mrs = new LiveMethodRelay[0];
} }
void addl(MethodRelay l){ void addRelay(LiveMethodRelay r) {
clean(); 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(){ void clean() {
if(mrs.length == 0) return; if (mrs.length == 0) return;
for(int i = mrs.length -1; i >= 0; i--){ for (int i = mrs.length -1; i >= 0; i--) {
if(!mrs[i].live){ if (!mrs[i].live()) {
mrs[i] = mrs[mrs.length - 1]; mrs[i] = mrs[mrs.length - 1];
mrs = (LiveMethodRelay[])shorten(mrs); mrs = (LiveMethodRelay[])shorten(mrs);
} }
} }
} }
void cascade(float x, float y){ void cascade(char tag, float... data) {
for(int i = 0; i < mrs.length; i++){ for (int i = 0; i < mrs.length; i++) {
mrs[i].execute(x,y); 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;
class LiveMethodRelay extends MethodRelay {
LiveMethodRelay(Object obj, String name, Class... args){ private boolean live;
private char tag;
LiveMethodRelay(Object obj, String name, char _tag, Class... args) {
super(obj, name, args); super(obj, name, args);
tag = _tag;
live = true; live = true;
} }
LiveMethodRelay(Object obj, String name, Class... args) {
LiveMethodRelay(Object obj, String name){ super(obj, name, args);
super(obj, name); tag = '\0';
live = true; live = true;
} }
char getTag() {return tag;}
void setTag(char t) {tag = t;}
void kill(){ boolean live() {return live;}
live = false; void kill() {live = false;}
}
} }
@@ -72,7 +109,7 @@ public static class MethodRelay {
Register a method that has parameters. Register a method that has parameters.
parameter obj the object that contains the method to invoke parameter obj the object that contains the method to invoke
parameter name the name of the method 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) { MethodRelay(Object obj, String name, Class... args) {
try { try {
@@ -80,7 +117,7 @@ public static class MethodRelay {
parameters = args; parameters = args;
handlerMethod = obj.getClass().getMethod(handlerMethodName, parameters); handlerMethod = obj.getClass().getMethod(handlerMethodName, parameters);
handlerObject = obj; handlerObject = obj;
} }
catch (Exception e) { catch (Exception e) {
println("Unable to find the function -"); println("Unable to find the function -");
print(handlerMethodName + "( "); print(handlerMethodName + "( ");
@@ -110,14 +147,14 @@ public static class MethodRelay {
/** /**
Execute a method with parameters 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 to be passed to the method
*/ */
void execute(Object... data) { void execute(Object... data) {
if (handlerObject != null) { if (handlerObject != null) {
try { try {
handlerMethod.invoke(handlerObject, data); handlerMethod.invoke(handlerObject, data);
} }
catch (Exception e) { catch (Exception e) {
println("Error on invoke"); println("Error on invoke");
} }

View File

@@ -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 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. 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 changelog 0.1.5.0
- Quite a few changes by this point. The readme has been - Quite a few changes by this point. The readme has been
fixed, the button class has gone through many revisions fixed, the button class has gone through many revisions
@@ -44,16 +52,16 @@ PVector offset;
float sMouseX; float sMouseX;
float sMouseY; float sMouseY;
public static final int DECIMAL = 65; public static final int DECIMAL = 65;
ClickHandler ch; // Mouse event handler MouseHandler mh; // Mouse event handler
Calculator calc; //<>// Calculator calc; //<>//
void setup(){ void setup(){
size(400,400); size(400,400);
offset = new PVector(width/2, height/2); 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)); //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. // 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){ void call(String _call){

View File

@@ -1,5 +1,5 @@
class Button{ // TODO make most of the attributes private class Button{ // TODO make most of the attributes private
ClickHandler ch; MouseHandler mh;
PVector pos; // Position to render from PVector pos; // Position to render from
PVector dim; // Second coordinate for CORNERS or len/wid for CORNER and CENTER PVector dim; // Second coordinate for CORNERS or len/wid for CORNER and CENTER
float radius; // Optional corner radius float radius; // Optional corner radius
@@ -10,8 +10,8 @@ class Button{ // TODO make most of the attributes private
boolean mouseOver; boolean mouseOver;
Object[] data; // Anything that gets passed to MethodRelay function when key pressed. Must be set manually 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){ Button(MouseHandler _mh, PVector _pos, PVector _dim, float _radius){
ch = _ch; mh = _mh;
pos = _pos.copy(); pos = _pos.copy();
dim = _dim.copy(); dim = _dim.copy();
radius = _radius; radius = _radius;
@@ -19,11 +19,11 @@ class Button{ // TODO make most of the attributes private
col = color(200); col = color(200);
highlight = color(100); highlight = color(100);
mouseOver = false; mouseOver = false;
ch.addl(new LiveMethodRelay(this, "clicked", float.class, float.class)); mh.addRelay(new LiveMethodRelay(this, "clicked", 'c', Class.class));
data = null; data = null;
} }
Button(ClickHandler _ch, PVector _pos, PVector _dim){ Button(MouseHandler _mh, PVector _pos, PVector _dim){
this(_ch, _pos, _dim, 0); this(_mh, _pos, _dim, 0);
} }
// GETTERS // // GETTERS //
@@ -40,7 +40,7 @@ class Button{ // TODO make most of the attributes private
void setColor(color c){col = c; } void setColor(color c){col = c; }
void setColor(color c, color h){col = c; highlight = h;} void setColor(color c, color h){col = c; highlight = h;}
void setHighlight(color h){ 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 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){ void setMode(int m){
@@ -62,9 +62,10 @@ class Button{ // TODO make most of the attributes private
// MOUSE FUNCTIONS // // 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){ if(mouseOver){
println(x + " " + y + " mouse pos"); println(mp[0] + " " + mp[1] + " mouse pos");
function.execute(data); function.execute(data);
} }
} }

View File

@@ -3,9 +3,9 @@ class Calculator{
MathPad m; MathPad m;
MathDisplay d; MathDisplay d;
Calculator(ClickHandler _ch){ Calculator(MouseHandler _mh){
ex = new B12Expression(); ex = new B12Expression();
m = new MathPad(_ch,ex); m = new MathPad(_mh,ex);
d = new MathDisplay(ex); d = new MathDisplay(ex);
} }

View File

@@ -1,12 +1,12 @@
class MathPad{ class MathPad{
B12Expression ex; B12Expression ex;
ClickHandler ch; MouseHandler mh;
B12Button[] buttons; B12Button[] buttons;
PVector pos; PVector pos;
MathPad(ClickHandler _ch, B12Expression _ex){ MathPad(MouseHandler _mh, B12Expression _ex){
ex = _ex; ex = _ex;
ch = _ch; mh = _mh;
pos = new PVector(0,0); pos = new PVector(0,0);
buttons = new B12Button[12]; buttons = new B12Button[12];
initialize(); initialize();
@@ -14,7 +14,7 @@ class MathPad{
void initialize(){ void initialize(){
for(int i = 0; i < 12; i++){ 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].setFunction(new MethodRelay(this, "addChar", B12Digit.class));
buttons[i].setColor(220,150); buttons[i].setColor(220,150);
} }
@@ -42,14 +42,14 @@ class MathPad{
class B12Button extends Button{ class B12Button extends Button{
B12Digit digit; B12Digit digit;
B12Button(ClickHandler _ch, PVector _pos, PVector _dim, float _radius, B12Digit _digit){ B12Button(MouseHandler _mh, PVector _pos, PVector _dim, float _radius, B12Digit _digit){
super(_ch,_pos,_dim,_radius); super(_mh,_pos,_dim,_radius);
//data = new Object[]{_digit}; Deprecated //data = new Object[]{_digit}; Deprecated
digit = _digit; digit = _digit;
setData(_digit); setData(_digit);
} }
B12Button(ClickHandler _ch, PVector _pos, PVector _dim, B12Digit _digit){ B12Button(MouseHandler _mh, PVector _pos, PVector _dim, B12Digit _digit){
this(_ch, _pos, _dim, 2, _digit); this(_mh, _pos, _dim, 2, _digit);
} }
// GETTERS AND SETTERS // // GETTERS AND SETTERS //