Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added hints SDL_HINT_MOUSE_DOUBLE_CLICK_TIME and SDL_HINT_MOUSE_DOUBL…
…E_CLICK_RADIUS to allow tuning double-click sensitivity.

Also increased the default double-click radius to 32 pixels to be more forgiving for touch interfaces
  • Loading branch information
slouken committed Sep 15, 2018
1 parent 66294d3 commit 6b3e893
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
10 changes: 10 additions & 0 deletions include/SDL_hints.h
Expand Up @@ -262,6 +262,16 @@ extern "C" {
*/
#define SDL_HINT_GRAB_KEYBOARD "SDL_GRAB_KEYBOARD"

/**
* \brief A variable setting the double click time, in milliseconds.
*/
#define SDL_HINT_MOUSE_DOUBLE_CLICK_TIME "SDL_MOUSE_DOUBLE_CLICK_TIME"

/**
* \brief A variable setting the double click radius, in pixels.
*/
#define SDL_HINT_MOUSE_DOUBLE_CLICK_RADIUS "SDL_MOUSE_DOUBLE_CLICK_RADIUS"

/**
* \brief A variable setting the speed scale for mouse motion, in floating point, when the mouse is not in relative mode
*/
Expand Down
48 changes: 37 additions & 11 deletions src/events/SDL_mouse.c
Expand Up @@ -33,12 +33,38 @@

/* The mouse state */
static SDL_Mouse SDL_mouse;
static Uint32 SDL_double_click_time = 500;
static int SDL_double_click_radius = 1;

static int
SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y);

static void SDLCALL
SDL_MouseDoubleClickTimeChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
SDL_Mouse *mouse = (SDL_Mouse *)userdata;

if (hint && *hint) {
mouse->double_click_time = SDL_atoi(hint);
} else {
#ifdef __WIN32__
mouse->double_click_time = GetDoubleClickTime();
#else
mouse->double_click_time = 500;
#endif
}
}

static void SDLCALL
SDL_MouseDoubleClickRadiusChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
SDL_Mouse *mouse = (SDL_Mouse *)userdata;

if (hint && *hint) {
mouse->double_click_radius = SDL_atoi(hint);
} else {
mouse->double_click_radius = 32; /* 32 pixels seems about right for touch interfaces */
}
}

static void SDLCALL
SDL_MouseNormalSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
Expand Down Expand Up @@ -83,6 +109,12 @@ SDL_MouseInit(void)

SDL_zerop(mouse);

SDL_AddHintCallback(SDL_HINT_MOUSE_DOUBLE_CLICK_TIME,
SDL_MouseDoubleClickTimeChanged, mouse);

SDL_AddHintCallback(SDL_HINT_MOUSE_DOUBLE_CLICK_RADIUS,
SDL_MouseDoubleClickRadiusChanged, mouse);

SDL_AddHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
SDL_MouseNormalSpeedScaleChanged, mouse);

Expand Down Expand Up @@ -114,12 +146,6 @@ SDL_GetMouse(void)
return &SDL_mouse;
}

void
SDL_SetDoubleClickTime(Uint32 interval)
{
SDL_double_click_time = interval;
}

SDL_Window *
SDL_GetMouseFocus(void)
{
Expand Down Expand Up @@ -454,9 +480,9 @@ SDL_PrivateSendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state
if (state == SDL_PRESSED) {
Uint32 now = SDL_GetTicks();

if (SDL_TICKS_PASSED(now, clickstate->last_timestamp + SDL_double_click_time) ||
SDL_abs(mouse->x - clickstate->last_x) > SDL_double_click_radius ||
SDL_abs(mouse->y - clickstate->last_y) > SDL_double_click_radius) {
if (SDL_TICKS_PASSED(now, clickstate->last_timestamp + mouse->double_click_time) ||
SDL_abs(mouse->x - clickstate->last_x) > mouse->double_click_radius ||
SDL_abs(mouse->y - clickstate->last_y) > mouse->double_click_radius) {
clickstate->click_count = 0;
}
clickstate->last_timestamp = now;
Expand Down
5 changes: 2 additions & 3 deletions src/events/SDL_mouse_c.h
Expand Up @@ -90,6 +90,8 @@ typedef struct
float relative_speed_scale;
float scale_accum_x;
float scale_accum_y;
Uint32 double_click_time;
int double_click_radius;
SDL_bool touch_mouse_events;

/* Data for double-click tracking */
Expand All @@ -112,9 +114,6 @@ extern int SDL_MouseInit(void);
/* Get the mouse state structure */
SDL_Mouse *SDL_GetMouse(void);

/* Set the default double-click interval */
extern void SDL_SetDoubleClickTime(Uint32 interval);

/* Set the default mouse cursor */
extern void SDL_SetDefaultCursor(SDL_Cursor * cursor);

Expand Down
2 changes: 0 additions & 2 deletions src/video/windows/SDL_windowsmouse.c
Expand Up @@ -304,8 +304,6 @@ WIN_InitMouse(_THIS)
mouse->GetGlobalMouseState = WIN_GetGlobalMouseState;

SDL_SetDefaultCursor(WIN_CreateDefaultCursor());

SDL_SetDoubleClickTime(GetDoubleClickTime());
}

void
Expand Down

0 comments on commit 6b3e893

Please sign in to comment.