to avoid the 'is it a new click or not?' problem, why don't you use the mousePressed() method? it will run only once, just after you clicked, not at every frame. it is much easier :
Code:
boolean b;
void setup(){
size(400,400);
background(255);
b = false;
}
void draw() {
fill(125);
if((mouseX <= 250) && (mouseX >= 150) && (mouseY <= 200) && (mouseY >= 150))
fill(255);
if (b == true)
fill(255,0,0);
rect(150,150,100,50);
}
void mousePressed() {
if((mouseX <= 250) && (mouseX >= 150) && (mouseY <= 200) && (mouseY >= 150))
b = !b;
}
*******
and by the way, you could create a button class
Code:class ToggleButton {
boolean pushed = false;
int x, y, w, h;
ToggleButton(int x, int y, int w, int h) {
this.x = x; this.y = y; this.w = w; this.h = h;
}
void draw() {
fill(125);
if (isUnderMouse()) fill(255);
if (pushed) fill(200, 100, 100);
rect(x, y, w, h);
}
void testIfPressed() {
if (isUnderMouse()) pushed = !pushed;
}
boolean isUnderMouse() {
if (mouseX >= x && mouseX <= x + w && mouseY >= y && mouseY <= y + h) return true;
return false;
}
}
that you can easily reuse in your app :
Code:ToggleButton b;
void setup(){
size(400,400);
background(255);
b = new ToggleButton(250, 150, 100, 50);
}
void draw() {
background(255);
b.draw();
}
void mousePressed() {
b.testIfPressed();
}