Sign in to follow this  
JonBonazza

[SDL] Best approach to "mouse button held"

Recommended Posts

What is the best approach to test whether the left mouse button has been held down and not just clicked?

I am using SDL, specificaly the SDL_PollEvent() function to test for click, so should I just keep track of the time, then compare it to a pre-defined interval constant and then check for the same event?

Share this post


Link to post
Share on other sites
The best way to test it would depend on how you want to use the information. For example, if you want this information so you can do something if the mouse button is held while dragging, then you would just check the state member of the SDL_MOUSEMOTIONEVENT structure.

Share this post


Link to post
Share on other sites
Thanks for the reply.

I don't really care if it's dragging or not. I just want a way to distinguish between a click and a hold.

The thing is, the new version of SDL has android support, and it works, but instead of using the SDL_TouchEvent struct like other supported touch screen devices, it uses the SDL_MouseButtonEvent struct. Why they did this, I do not know. At any rate, I am ultimately trying to distinguish between when a users taps the screen, and when the user taps and holds a point on the screen.

Share this post


Link to post
Share on other sites
The easiest way would be..(Note this is just a Pseudo code )
[code]
//Initialize everything
const Uint8 Button = SDL_BUTTON_LEFT;
//Add
bool dragging;
int offsetX = 0;
int offsetY = 0;
bool intersects( int objectX, int objectY, int objectW, int objectH, int mouseX, int mouseY )
{
if( mouseX < objectX || mouseY < objectY )
{
return false;
}
if( mouseX > objectX + objectW || mouseY > objectY + objectH )
{
return false;
}
return true;
}
/// and then adding all other functions etc.
//Then in the handle input function you do this
if( event.type == SDL_MOUSEBUTTONDOWN )
{
if( ( event.button.button &Button ) == Button && intersects( x, y, w, h, event.button.x, event.button.y ) )
{
offsetX = event.button.x - x;
offsetY = event.button.y - y;
dragging = true;
}
}
else if( event.type == SDL_MOUSEBUTTONUP )
{
if( event.button.button &Button ) == Button && dragging )
{
offsetX = 0;
offsetY = 0;
dragging = false;
}
else if( event.type == SDL_MOUSEMOTION )
{
if( dragging )
{
x = event.motion.x - offsetX;
y = event.motion.y - offsetY;
}
}
[/code]
This is just a pseudo code.. Try implementing it on your code
Hope this helps :D

Share this post


Link to post
Share on other sites
Thanks, but I think you misunderstood my problem (or I am misunderstanding you). I don't care about dragging, really. What I am trying to do is differentiate between a tap ( a quick touch on the screen) and a hold (the screen is touched and held there for a second before it is released).

Share this post


Link to post
Share on other sites
You could have a class that receives click down / click up events (just pass the events to the class if you want) and it stores the time between the click down and up- then depending on the timing that you ordain, you can ask it if a click or a hold happened.

If you don't like that method- on click down you can update a uint32_t with SDL_GetTicks() on click down- then on click up, store the difference between the stored number and SDL_GetTicks() and you have the time of the click. If you'd like some pseudo/real code just ask.

Share this post


Link to post
Share on other sites
[quote name='ultifinitus' timestamp='1318095969' post='4870541']
You could have a class that receives click down / click up events (just pass the events to the class if you want) and it stores the time between the click down and up- then depending on the timing that you ordain, you can ask it if a click or a hold happened.

If you don't like that method- on click down you can update a uint32_t with SDL_GetTicks() on click down- then on click up, store the difference between the stored number and SDL_GetTicks() and you have the time of the click. If you'd like some pseudo/real code just ask.
[/quote]

Thanks! The first approach is pretty much what I was planning to implement, but I wanted to get a second opinion.

As for code, I am sure I can figure it out, but if not, then I will check back.

Also, it's funny you mention the SDL_GetTicks() method. I was actually just looking for a viable replacement for timeGetTime() for timing purposes., as the timeGetTime() method (and hte DWORD data type) is windows only, and the Android OS runs on top of a linux kernel. Do you think that this would be a fair replacement (provided I throttle it to make up for faster/slower PCs)? If not, can you reccommend a better solution?

Share this post


Link to post
Share on other sites
[quote name='jonbonazza' timestamp='1318100102' post='4870557']
[quote name='ultifinitus' timestamp='1318095969' post='4870541']
You could have a class that receives click down / click up events (just pass the events to the class if you want) and it stores the time between the click down and up- then depending on the timing that you ordain, you can ask it if a click or a hold happened.

If you don't like that method- on click down you can update a uint32_t with SDL_GetTicks() on click down- then on click up, store the difference between the stored number and SDL_GetTicks() and you have the time of the click. If you'd like some pseudo/real code just ask.
[/quote]

Thanks! The first approach is pretty much what I was planning to implement, but I wanted to get a second opinion.

As for code, I am sure I can figure it out, but if not, then I will check back.

Also, it's funny you mention the SDL_GetTicks() method. I was actually just looking for a viable replacement for timeGetTime() for timing purposes., as the timeGetTime() method (and hte DWORD data type) is windows only, and the Android OS runs on top of a linux kernel. Do you think that this would be a fair replacement (provided I throttle it to make up for faster/slower PCs)? If not, can you reccommend a better solution?
[/quote]

SDL_GetTicks() returns the number of milliseconds that has passed since you called SDL_Init() , it should (with 1.2.14 or later atleast) use the highest resolution method available for the target platform (On windows it can choose between timegettime and qpc, on Linux between gettimeofday and clock_gettime).

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