I think most GUIs like GTK and MSWindows are permissible about what
constitutes an interruption to a double click, so I wouldn’t just use
"LastEventWasALeftClick" if you didn’t want a one pixel movement of the
mouse to kill a double click. You’ll have to decide what works best for your
application and what your users expect. You could also extend the SDL_event
union with an extended SDL_MouseButtonEvent:
/* Mouse EXPANDED button event structure /
typedef struct SDL_MouseButtonEventX {
Uint8 type; / SDL_MOUSEBUTTONDOWN or SDL_MOUSEBUTTONUP /
Uint8 which; / The mouse device index /
Uint8 button; / The mouse button index /
Uint8 state; / SDL_PRESSED or SDL_RELEASED /
Uint16 x, y; / The X/Y coordinates of the mouse at press time /
Uint8 counter; / double? triple? quadruple click? the sky’s the limit!
*/
} SDL_MouseButtonEventX;
/* Make sure our expansion doesn’t overrun the length of the SDL_Event union
*/
#if sizeof(SDL_Event) < sizeof(SDL_MouseButtonEventX)
#error SDL_MouseButtonEventX is too big!
#endif
/* Make sure our new struct expands properly on the old mouse button struct
/
#define offsetOf(struct,member) (size_t)(((struct)NULL)->member)
#if offsetOf(SDL_MouseButtonEvent, type) != offsetOf(SDL_MouseButtonX, type)
||
offsetOf(SDL_MouseButtonEvent, which) != offsetOf(SDL_MouseButtonX,
which) ||
offsetOf(SDL_MouseButtonEvent, button) != offsetOf(SDL_MouseButtonX,
button) ||
offsetOf(SDL_MouseButtonEvent, state) != offsetOf(SDL_MouseButtonX,
state) ||
offsetOf(SDL_MouseButtonEvent, x != offsetOf(SDL_MouseButtonX, x) ||
offsetOf(SDL_MouseButtonEvent, y != offsetOf(SDL_MouseButtonX, y)
#error SDL_MouseButtonEventX is discontiguous with SDL_MouseButtonEvent!
#endif
/* This might be useful if your event filter uses SDL_PeepEvents() */
#define EVMASK SDL_EVENTMASK(SDL_MOUSEMOTION) |
SDL_EVENTMASK(SDL_MOUSEBUTTONDOWN) |
SDL_EVENTMASK(SDL_MOUSEBUTTTONUP) |
SDL_EVENTMASK(SDL_KEYBUTTONDOWN) |
SDL_EVENTMASK(SDL_KEYBUTTTONUP)
static int OurEventFilter(const SDL_Event event) {
/ Do your magic in here, then register OurEventFilter with
SDL_SetEventFilter() /
event->counter = 1; // set this to something more interesting later on,
then in your event loop
// you can cast the SDL_Event to an SDL_MouseButtonEventX to access the
’counter’ member
}On Wed, Mar 17, 2010 at 2:57 AM, Lilly wrote:
Hello,
I’d like to know how you usually implement a double click detection.
I made a quick search and then made this function :
Code:
int DoubleClickDetected (void)
{
static Uint32 LastClickTicks;
Uint32 CurrentClickTicks;
/* The first time this function is called, LastClickTicks has not been
initialised yet. /
if (! LastClickTicks)
{
LastClickTicks = SDL_GetTicks ();
return FALSE;
}
else
{
CurrentClickTicks = SDL_GetTicks ();
/ If the period between the two clicks is smaller or equal to a
pre-defined number, we report a DoubleClick event. /
if (CurrentClickTicks - LastClickTicks <= DOUBLE_CLICK_MAX_DELAY)
{
/ Update LastClickTicks and signal a DoubleClick. /
LastClickTicks = CurrentClickTicks;
return TRUE;
}
else
{
/ Update LastClickTicks and signal a SingleClick. */
LastClickTicks = CurrentClickTicks;
return FALSE;
}
}
}
The problem about this function is that a click followed by an unrelated
event (such as a mouse move) and followed by another click will be
considered as a double-click (if the events are fast enough), which is not
usual.
If I were to declare a global boolean LastEventWasALeftClick I think it
would work, but I’d have to reinitialize it to 0 for all other events, which
is not handy, and besides I thought it was best not to use global variables.
So what’s the best solution to detect a double-click ?
Thanks.
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
–
http://codebad.com/