Hi @Thundercat,
here is an approach for a simple button handling …
class Button {
int x,y,w,h;
String label;
boolean mouse_over, mouse_pressed, mouse_pressed_state;
public Button(int x,int y,int w,int h, String label) {
this.x=x;
this.y=y;
this.w=w;
this.h=h;
this.label=label;
}
public boolean hover(int mX, int mY) {
mouse_over = (mouseX > x && mouseY < x+w && mouseY > y && mouseY < y+h);
return mouse_over;
}
public void update(int mX, int mY, boolean state) {
if (hover(mX,mY))
mouse_pressed=state;
}
public boolean isPressed() {
if (mouse_pressed) {
mouse_pressed=false;
return true;
}
return false;
}
public void render() {
push();
noFill();
stroke(mouse_pressed ? color(255,0,0) : mouse_over ? color(0,255,0) : color(255));
translate(x,y);
rect(0,0,w,h);
fill(mouse_pressed ? color(255,0,0) : mouse_over ? color(0,255,0) : color(255));
translate(x,h/2);
text(label,x+10,0);
pop();
mouse_pressed=false;
}
}
Button[] buttons;
void mousePressed() {
for (Button b:buttons) {
b.update(mouseX, mouseY,true);
}
}
void mouseMoved() {
for (Button b:buttons) {
b.update(mouseX, mouseY,false);
}
}
void mouseReleased() {
for (Button b:buttons) {
b.update(mouseX, mouseY,false);
}
}
void setup() {
size(400,400);
buttons = new Button[2];
buttons[0] = new Button(10,10,100,40,"Button 1");
buttons[1] = new Button(10,60,100,40,"Button 2");
}
void draw() {
background(0);
for (Button b:buttons) {
if (b.isPressed()) {
println("Button " + b.label + " is pressed");
}
b.render();
}
}