Added SDL_HINT_MOUSE_NORMAL_SPEED_SCALE and SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE to scale the speed of the mouse.
authorSam Lantinga <slouken@libsdl.org>
Fri, 02 Dec 2016 21:01:13 -0800
changeset 106734f02752f8d85
parent 10672 10fb0ebc4fbf
child 10674 120c2e96b094
Added SDL_HINT_MOUSE_NORMAL_SPEED_SCALE and SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE to scale the speed of the mouse.
This currently doesn't affect absolute motion, which would need to be implemented on each windowing system so the cursor matches the reported mouse coordinates.
include/SDL_hints.h
src/events/SDL_mouse.c
src/events/SDL_mouse_c.h
     1.1 --- a/include/SDL_hints.h	Fri Dec 02 02:25:12 2016 -0800
     1.2 +++ b/include/SDL_hints.h	Fri Dec 02 21:01:13 2016 -0800
     1.3 @@ -233,6 +233,16 @@
     1.4  #define SDL_HINT_GRAB_KEYBOARD              "SDL_GRAB_KEYBOARD"
     1.5  
     1.6  /**
     1.7 + *  \brief  A variable setting the speed scale for mouse motion, in floating point, when the mouse is not in relative mode
     1.8 + */
     1.9 +#define SDL_HINT_MOUSE_NORMAL_SPEED_SCALE    "SDL_MOUSE_NORMAL_SPEED_SCALE"
    1.10 +
    1.11 +/**
    1.12 + *  \brief  A variable setting the scale for mouse motion, in floating point, when the mouse is in relative mode
    1.13 + */
    1.14 +#define SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE    "SDL_MOUSE_RELATIVE_SPEED_SCALE"
    1.15 +
    1.16 +/**
    1.17   *  \brief  A variable controlling whether relative mouse mode is implemented using mouse warping
    1.18   *
    1.19   *  This variable can be set to the following values:
     2.1 --- a/src/events/SDL_mouse.c	Fri Dec 02 02:25:12 2016 -0800
     2.2 +++ b/src/events/SDL_mouse.c	Fri Dec 02 21:01:13 2016 -0800
     2.3 @@ -40,12 +40,34 @@
     2.4  static int
     2.5  SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y);
     2.6  
     2.7 +static void
     2.8 +SDL_MouseNormalSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
     2.9 +{
    2.10 +    SDL_Mouse *mouse = (SDL_Mouse *)userdata;
    2.11 +
    2.12 +    mouse->normal_speed_scale = SDL_atof(hint);
    2.13 +}
    2.14 +
    2.15 +static void
    2.16 +SDL_MouseRelativeSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
    2.17 +{
    2.18 +    SDL_Mouse *mouse = (SDL_Mouse *)userdata;
    2.19 +
    2.20 +    mouse->relative_speed_scale = SDL_atof(hint);
    2.21 +}
    2.22 +
    2.23  /* Public functions */
    2.24  int
    2.25  SDL_MouseInit(void)
    2.26  {
    2.27      SDL_Mouse *mouse = SDL_GetMouse();
    2.28  
    2.29 +    SDL_AddHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
    2.30 +                        SDL_MouseNormalSpeedScaleChanged, mouse);
    2.31 +
    2.32 +    SDL_AddHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
    2.33 +                        SDL_MouseRelativeSpeedScaleChanged, mouse);
    2.34 +
    2.35      mouse->cursor_shown = SDL_TRUE;
    2.36  
    2.37      return (0);
    2.38 @@ -200,6 +222,21 @@
    2.39  }
    2.40  
    2.41  static int
    2.42 +GetScaledMouseDelta(float scale, int value, float *accum)
    2.43 +{
    2.44 +    if (scale != 1.0f) {
    2.45 +        *accum += scale * value;
    2.46 +        if (*accum >= 0.0f) {
    2.47 +            value = (int)SDL_floor(*accum);
    2.48 +        } else {
    2.49 +            value = (int)SDL_ceil(*accum);
    2.50 +        }
    2.51 +        *accum -= value;
    2.52 +    }
    2.53 +    return value;
    2.54 +}
    2.55 +
    2.56 +static int
    2.57  SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y)
    2.58  {
    2.59      SDL_Mouse *mouse = SDL_GetMouse();
    2.60 @@ -221,6 +258,13 @@
    2.61      }
    2.62  
    2.63      if (relative) {
    2.64 +        if (mouse->relative_mode) {
    2.65 +            x = GetScaledMouseDelta(mouse->relative_speed_scale, x, &mouse->scale_accum_x);
    2.66 +            y = GetScaledMouseDelta(mouse->relative_speed_scale, y, &mouse->scale_accum_y);
    2.67 +        } else {
    2.68 +            x = GetScaledMouseDelta(mouse->normal_speed_scale, x, &mouse->scale_accum_x);
    2.69 +            y = GetScaledMouseDelta(mouse->normal_speed_scale, y, &mouse->scale_accum_y);
    2.70 +        }
    2.71          xrel = x;
    2.72          yrel = y;
    2.73          x = (mouse->last_x + xrel);
    2.74 @@ -475,6 +519,12 @@
    2.75      }
    2.76  
    2.77      SDL_zerop(mouse);
    2.78 +
    2.79 +    SDL_DelHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
    2.80 +                        SDL_MouseNormalSpeedScaleChanged, mouse);
    2.81 +
    2.82 +    SDL_DelHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
    2.83 +                        SDL_MouseRelativeSpeedScaleChanged, mouse);
    2.84  }
    2.85  
    2.86  Uint32
    2.87 @@ -603,6 +653,8 @@
    2.88          }
    2.89      }
    2.90      mouse->relative_mode = enabled;
    2.91 +    mouse->scale_accum_x = 0.0f;
    2.92 +    mouse->scale_accum_y = 0.0f;
    2.93  
    2.94      if (mouse->focus) {
    2.95          SDL_UpdateWindowGrab(mouse->focus);
     3.1 --- a/src/events/SDL_mouse_c.h	Fri Dec 02 02:25:12 2016 -0800
     3.2 +++ b/src/events/SDL_mouse_c.h	Fri Dec 02 21:01:13 2016 -0800
     3.3 @@ -83,6 +83,10 @@
     3.4      Uint32 buttonstate;
     3.5      SDL_bool relative_mode;
     3.6      SDL_bool relative_mode_warp;
     3.7 +    float normal_speed_scale;
     3.8 +    float relative_speed_scale;
     3.9 +    float scale_accum_x;
    3.10 +    float scale_accum_y;
    3.11  
    3.12      /* Data for double-click tracking */
    3.13      int num_clickstates;