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.
Page Index Toggle Pages: 1
time button (Read 1385 times)
time button
May 16th, 2010, 8:38am
 
Hi. Im looking for some help.

I need to create a button that does not have to click ... when the mouse is over the button for more than two seconds, the button is on.
but when I try to call again, I do not have to wait 2 seconds. I think I need a ResetTime, but do not know how I can do ....

her is the code:

boolean button = false;

int x =50;
int y =50;
int c =100;
int a =100;
int savedTime;
int totalTime = 2000;

void setup(){
 size(200,200);
 savedTime = millis();
}

void draw(){
 int passedTime = millis() - savedTime;
 if (mouseX > x && mouseX < x+c && mouseY > y && mouseY < y+a && passedTime > totalTime) {
   button = true;
     println("2sec");
 // savedTime = millis();
 } else {
   button = false;
 }
 

 
 if (button){
   background(255);
   stroke(0);
 } else{
   background(0);
   stroke(255);
 }
 
 fill (175);
 rect (x,y,c,a);
}

Thanks
Re: time button
Reply #1 - May 16th, 2010, 10:29am
 
Well, if you uncomment the savedTime = millis(); line, it should do what you expect, no?
Re: time button
Reply #2 - May 18th, 2010, 8:06am
 
hum...no...

what im trying to do is:
...

Re: time button
Reply #3 - May 18th, 2010, 9:34am
 
Perhaps you mean "more than 4 seconds" for the second part, no?
Maybe with that (untested):
Code:
void draw(){
int passedTime = millis() - savedTime;
if (mouseX > x && mouseX < x+c && mouseY > y && mouseY < y+a) {
// Over the button
if (passedTime > totalTime) {
if (passedTime < 2 * totalTime) {
button = true;
println("2s");
} else {
// More tha 4 seconds
button = false;
println("4s");
// Reset for new activation
savedTime = millis();
} else {
button = false;
}
} else {
button = false;
}
Re: time button
Reply #4 - May 18th, 2010, 9:58am
 
hum... imagin like a "noclick" button... the button is on if the cursor is 2 seconds above him. if i need to turn off i put the mouse above the button (for example: 4 seconds) to turn it off.
Re: time button
Reply #5 - May 18th, 2010, 11:36am
 
I think there's a language barrier in operation here  Grin

My guess is that you want to toggle the button each time the user hovers over it for a certain amount of time.  In order to do that you need to follow PhiLho's lead and separate the passedTime > totalTime from the mouseOver test as follows (though note that I've renamed your 'totalTime' variable to 'hoverTime' as that seemed to better describe its purpose):

Code:
boolean button;

int x =50;
int y =50;
int c =100;
int a =100;

int savedTime;
int hoverTime = 2000;

void setup(){
size(200,200);
savedTime = millis();
}

void draw(){
 
 int passedTime = millis() - savedTime;
 if (mouseX > x && mouseX < x+c && mouseY > y && mouseY < y+a) {
   // Over the button
   if (passedTime > hoverTime) {
       // toggle the value of the boolean
       button = !button;
       // When the switch is toggled save the current time
       savedTime = millis();
   }
 }
 // Whilst the mouse isn't over the rectangle reset the time
 // not sure about the efficiency of this...
else {
  savedTime = millis();
}

if (button){
  background(255);
  stroke(0);
}
else{
  background(0);
  stroke(255);
}

fill (175);
rect (x,y,c,a);
}


button = !button; is worth remembering as it's rather useful when you need to toggle a boolean...

You'd obviously need to make some changes if you wanted a different time limit to toggle each direction... and you might also want people to explicitly mouseOut before being able to toggle the switch again; which would probably require another boolean.
Re: time button
Reply #6 - May 18th, 2010, 11:39am
 
BTW - I'm curious about the context you'd be using this in...  hovering over something is not immediately intuitive; though you could add some kind of visual hint I suppose...
Re: time button
Reply #7 - May 18th, 2010, 12:01pm
 
OK - couldn't resist adding a crude example of a visual hint:

Code:
boolean button;

int x =50;
int y =50;
int c =100;
int a =100;

int savedTime;
int hoverTime = 2000;

void setup(){
size(200,200);
savedTime = millis();
}

void draw(){
 if (button){
  background(255);
  stroke(0);
 }
 else{
   background(0);
   stroke(255);
 }

 fill(175);
 rect (x,y,c,a);
 
 int passedTime = millis() - savedTime;
 
 if (mouseX > x && mouseX < x+c && mouseY > y && mouseY < y+a) {
   // Over the button
   if (passedTime > hoverTime) {
       // toggle the value of the boolean
       button = !button;
       // When the switch is toggled save the current time
       savedTime = millis();
   }
   
   noStroke();
   fill(#ff9900);
   int h = int((passedTime/float(hoverTime))*a -2);
   // -h seems a bit hackish, but it does the trick
   rect(x+1,y+a,c-1,-h);
 }
 // Whilst the mouse isn't over the rectangle reset the time
 // not sure about the efficiency of this...
else {
  savedTime = millis();
}


}


This convinces me that it would be better to require a mouseOut before initiating another toggle...
Page Index Toggle Pages: 1