src/events/SDL_mouse.c
changeset 10673 4f02752f8d85
parent 10609 d702ecbd8ba7
child 10674 120c2e96b094
     1.1 --- a/src/events/SDL_mouse.c	Fri Dec 02 02:25:12 2016 -0800
     1.2 +++ b/src/events/SDL_mouse.c	Fri Dec 02 21:01:13 2016 -0800
     1.3 @@ -40,12 +40,34 @@
     1.4  static int
     1.5  SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y);
     1.6  
     1.7 +static void
     1.8 +SDL_MouseNormalSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
     1.9 +{
    1.10 +    SDL_Mouse *mouse = (SDL_Mouse *)userdata;
    1.11 +
    1.12 +    mouse->normal_speed_scale = SDL_atof(hint);
    1.13 +}
    1.14 +
    1.15 +static void
    1.16 +SDL_MouseRelativeSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
    1.17 +{
    1.18 +    SDL_Mouse *mouse = (SDL_Mouse *)userdata;
    1.19 +
    1.20 +    mouse->relative_speed_scale = SDL_atof(hint);
    1.21 +}
    1.22 +
    1.23  /* Public functions */
    1.24  int
    1.25  SDL_MouseInit(void)
    1.26  {
    1.27      SDL_Mouse *mouse = SDL_GetMouse();
    1.28  
    1.29 +    SDL_AddHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
    1.30 +                        SDL_MouseNormalSpeedScaleChanged, mouse);
    1.31 +
    1.32 +    SDL_AddHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
    1.33 +                        SDL_MouseRelativeSpeedScaleChanged, mouse);
    1.34 +
    1.35      mouse->cursor_shown = SDL_TRUE;
    1.36  
    1.37      return (0);
    1.38 @@ -200,6 +222,21 @@
    1.39  }
    1.40  
    1.41  static int
    1.42 +GetScaledMouseDelta(float scale, int value, float *accum)
    1.43 +{
    1.44 +    if (scale != 1.0f) {
    1.45 +        *accum += scale * value;
    1.46 +        if (*accum >= 0.0f) {
    1.47 +            value = (int)SDL_floor(*accum);
    1.48 +        } else {
    1.49 +            value = (int)SDL_ceil(*accum);
    1.50 +        }
    1.51 +        *accum -= value;
    1.52 +    }
    1.53 +    return value;
    1.54 +}
    1.55 +
    1.56 +static int
    1.57  SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y)
    1.58  {
    1.59      SDL_Mouse *mouse = SDL_GetMouse();
    1.60 @@ -221,6 +258,13 @@
    1.61      }
    1.62  
    1.63      if (relative) {
    1.64 +        if (mouse->relative_mode) {
    1.65 +            x = GetScaledMouseDelta(mouse->relative_speed_scale, x, &mouse->scale_accum_x);
    1.66 +            y = GetScaledMouseDelta(mouse->relative_speed_scale, y, &mouse->scale_accum_y);
    1.67 +        } else {
    1.68 +            x = GetScaledMouseDelta(mouse->normal_speed_scale, x, &mouse->scale_accum_x);
    1.69 +            y = GetScaledMouseDelta(mouse->normal_speed_scale, y, &mouse->scale_accum_y);
    1.70 +        }
    1.71          xrel = x;
    1.72          yrel = y;
    1.73          x = (mouse->last_x + xrel);
    1.74 @@ -475,6 +519,12 @@
    1.75      }
    1.76  
    1.77      SDL_zerop(mouse);
    1.78 +
    1.79 +    SDL_DelHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
    1.80 +                        SDL_MouseNormalSpeedScaleChanged, mouse);
    1.81 +
    1.82 +    SDL_DelHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
    1.83 +                        SDL_MouseRelativeSpeedScaleChanged, mouse);
    1.84  }
    1.85  
    1.86  Uint32
    1.87 @@ -603,6 +653,8 @@
    1.88          }
    1.89      }
    1.90      mouse->relative_mode = enabled;
    1.91 +    mouse->scale_accum_x = 0.0f;
    1.92 +    mouse->scale_accum_y = 0.0f;
    1.93  
    1.94      if (mouse->focus) {
    1.95          SDL_UpdateWindowGrab(mouse->focus);