We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › button should stay red after being pressed
Page Index Toggle Pages: 1
button should stay red after being pressed (Read 767 times)
button should stay red after being pressed
Oct 8th, 2007, 7:36pm
 
hi there

quite a basic question i guess, i just started learning:
i got a button, that when it's pressed it turns red, and should stay red even when the mousebutton is released. how can i do that?
'til now i only got it red when pressed but when i release it it goes back to grey.

i'm thankful for any help.

cheers
Re: button should stay red after being pressed
Reply #1 - Oct 8th, 2007, 7:49pm
 
You should create a boolean variable to store the state of the button. Then you decide what colour to draw the button based on that variable, not the mouse information.

You then need to set the variable to true if the mouse is pressed in the right position.
Re: button should stay red after being pressed
Reply #2 - Oct 8th, 2007, 8:52pm
 
thanks, that helped Smiley
and how could i return to the beginning status by pressing again?
Re: button should stay red after being pressed
Reply #3 - Oct 8th, 2007, 9:35pm
 
Do something in your "mouse is pressed in the right place" bit like:

Code:
buttonIsSelected=!buttonIsSelected;


That will toggle it from true to false and back again.
Re: button should stay red after being pressed
Reply #4 - Oct 9th, 2007, 8:12pm
 
JohnG is right, however, be careful - you need to make sure that what you have is a _new_ click, not just a held one (otherwise the thing will toggle on and off every frame while you hold down the button, making its state seem almost random).  Maybe you're already checking for this, but if not, keep it in mind.

To check for this, create a global variable, pmousePressed, and at the end of the draw() function set pmousePressed = mousePressed.  That way, you can tell that you have a new click if (mousePressed && !pmousePressed).
Re: button should stay red after being pressed
Reply #5 - Oct 9th, 2007, 10:21pm
 
i think i understand what you mean more or less, but still i can't think of the exact way to do it. how can i make pmousepressed a variable? it's neither an "int" nor a "float" or sth. could you maybe give me a full example?
here's what i have so far:



boolean b;

void setup(){
 size(400,400);
 background(255);
}

void draw() {
 fill(125);
 
 if((mouseX <= 250) && (mouseX >= 150) && (mouseY <= 200) && (mouseY >= 150)) {
   fill(255);
 }
 
 if((mouseX <= 250) && (mouseX >= 150) && (mouseY <= 200) && (mouseY >= 150) && mousePressed) {
   b = true;  
 }
 
 if (b == true) {
  fill(255,0,0);
 }

 rect(150,150,100,50);

}
Re: button should stay red after being pressed
Reply #6 - Oct 10th, 2007, 1:12am
 
Altering your code example:

Code:

boolean b;
boolean pmousePressed;

void setup(){
size(400,400);
background(255);
pmousePressed = false;
b = false;
}

void draw() {
fill(125);

if((mouseX <= 250) && (mouseX >= 150) && (mouseY <= 200) && (mouseY >= 150)) {
fill(255);
}

if((mouseX <= 250) && (mouseX >= 150) && (mouseY <= 200) && (mouseY >= 150) && mousePressed && (!pmousePressed)) {
b = !b;
}

if (b == true) {
fill(255,0,0);
}

rect(150,150,100,50);
pmousePressed = mousePressed;
}



Something like that should work.
Re: button should stay red after being pressed
Reply #7 - Oct 10th, 2007, 11:32am
 
brilliant! thank you very much Smiley
Re: button should stay red after being pressed
Reply #8 - Oct 10th, 2007, 2:06pm
 
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();
}
Re: button should stay red after being pressed
Reply #9 - Oct 10th, 2007, 2:13pm
 
the button example covers this topic:
http://processing.org/learning/topics/button.html
Re: button should stay red after being pressed
Reply #10 - Oct 11th, 2007, 12:05am
 
Ah, so you are right!  I've become so used to explicitly handling (usually that means discarding) key repeats in Java that I never realized that mouse clicks only show up once.  But that makes total sense now that I think about it.  Excellent!
Page Index Toggle Pages: 1