Sign in to follow this  
mc61

help with a button looking realistic when pressed

Recommended Posts

Hello, I am trying to code a realistic looking button. While it is working as expected, the way I did it feels somewhat convoluted to me, so I thought of asking the experts. I never had any formal training in programming, and I learned from copying examples. Thus, my solutions are often unorthodox and inefficient. This is what I do: 1) I have of course two textures for button-up and button-down. 2) I register mouse clicks this way
void MousePress_controlwin(int leftclick, int state, int x, int y)
{
   (stuff omitted)  
   if ( (checkclick == 1) && (state == GLUT_DOWN) ) 
   {
       button[i].downflag = 1;
       button[i].function();
   }
}
3) I have the following in my display function
if ( button[i].downflag == 1 )
{            
   draw_buttonDown(&button[i]);
   button[i].downflag = 0;
}
else if ( button[i].downflag == 0 ) 
{
   draw_button(&button[i]);
}
As I said, it does what I expect. However, depending on the scene being rendered the time it spends in the down position can become unnoticeably short. I have been unable to regulate this with usleep() or something like that. Is there a better way to do this? Any hints or pointers to where to read them would be much appreciated. Thanks, mc61

Share this post


Link to post
Share on other sites
Generally with GUIs, when the mouse button is pressed 'down' you show the 'down' image and you don't apply the function until the button transitions to 'up' while the mouse is over it. (If the mouse leaves the button then you should go back to 'up' but not fire the function which should be called).

Share this post


Link to post
Share on other sites
Thanks phantom,

I am afraid I do not know how to do what you said (I tried putting the call to the function outside of the if (DOWN) statement, while still inside the if (click) statement, but it did not work. I will need to think about it :-)

Thanks again,

mc61

Share this post


Link to post
Share on other sites
The psudeo code would look something like;


if(mouse button == down)
show down image = true;
down button = button;
else if (mouse button == up)
show down image = false;
if (mouse position == over down button)
run function;
end if
end if



You basically need to catch the 'mouse button up' event in GLUT and work with it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this