[Commits] SDL: Added SDL_HINT_MOUSE_NORMAL_SPEED_SCALE and SDL_HINT_MOUSE_...

libsdl.org revision control commits-owner at libsdl.org
Fri Dec 2 21:01:18 PST 2016


details:   https://hg.libsdl.org/SDL/rev/4f02752f8d85
changeset: 10673:4f02752f8d85
user:      Sam Lantinga <slouken at libsdl.org>
date:      Fri Dec 02 21:01:13 2016 -0800
description:
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.

diffstat:

 include/SDL_hints.h      |  10 +++++++++
 src/events/SDL_mouse.c   |  52 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/events/SDL_mouse_c.h |   4 +++
 3 files changed, 66 insertions(+), 0 deletions(-)

diffs (130 lines):

diff -r 10fb0ebc4fbf -r 4f02752f8d85 include/SDL_hints.h
--- a/include/SDL_hints.h	Fri Dec 02 02:25:12 2016 -0800
+++ b/include/SDL_hints.h	Fri Dec 02 21:01:13 2016 -0800
@@ -233,6 +233,16 @@
 #define SDL_HINT_GRAB_KEYBOARD              "SDL_GRAB_KEYBOARD"
 
 /**
+ *  \brief  A variable setting the speed scale for mouse motion, in floating point, when the mouse is not in relative mode
+ */
+#define SDL_HINT_MOUSE_NORMAL_SPEED_SCALE    "SDL_MOUSE_NORMAL_SPEED_SCALE"
+
+/**
+ *  \brief  A variable setting the scale for mouse motion, in floating point, when the mouse is in relative mode
+ */
+#define SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE    "SDL_MOUSE_RELATIVE_SPEED_SCALE"
+
+/**
  *  \brief  A variable controlling whether relative mouse mode is implemented using mouse warping
  *
  *  This variable can be set to the following values:
diff -r 10fb0ebc4fbf -r 4f02752f8d85 src/events/SDL_mouse.c
--- a/src/events/SDL_mouse.c	Fri Dec 02 02:25:12 2016 -0800
+++ b/src/events/SDL_mouse.c	Fri Dec 02 21:01:13 2016 -0800
@@ -40,12 +40,34 @@
 static int
 SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y);
 
+static void
+SDL_MouseNormalSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
+{
+    SDL_Mouse *mouse = (SDL_Mouse *)userdata;
+
+    mouse->normal_speed_scale = SDL_atof(hint);
+}
+
+static void
+SDL_MouseRelativeSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
+{
+    SDL_Mouse *mouse = (SDL_Mouse *)userdata;
+
+    mouse->relative_speed_scale = SDL_atof(hint);
+}
+
 /* Public functions */
 int
 SDL_MouseInit(void)
 {
     SDL_Mouse *mouse = SDL_GetMouse();
 
+    SDL_AddHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
+                        SDL_MouseNormalSpeedScaleChanged, mouse);
+
+    SDL_AddHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
+                        SDL_MouseRelativeSpeedScaleChanged, mouse);
+
     mouse->cursor_shown = SDL_TRUE;
 
     return (0);
@@ -200,6 +222,21 @@
 }
 
 static int
+GetScaledMouseDelta(float scale, int value, float *accum)
+{
+    if (scale != 1.0f) {
+        *accum += scale * value;
+        if (*accum >= 0.0f) {
+            value = (int)SDL_floor(*accum);
+        } else {
+            value = (int)SDL_ceil(*accum);
+        }
+        *accum -= value;
+    }
+    return value;
+}
+
+static int
 SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y)
 {
     SDL_Mouse *mouse = SDL_GetMouse();
@@ -221,6 +258,13 @@
     }
 
     if (relative) {
+        if (mouse->relative_mode) {
+            x = GetScaledMouseDelta(mouse->relative_speed_scale, x, &mouse->scale_accum_x);
+            y = GetScaledMouseDelta(mouse->relative_speed_scale, y, &mouse->scale_accum_y);
+        } else {
+            x = GetScaledMouseDelta(mouse->normal_speed_scale, x, &mouse->scale_accum_x);
+            y = GetScaledMouseDelta(mouse->normal_speed_scale, y, &mouse->scale_accum_y);
+        }
         xrel = x;
         yrel = y;
         x = (mouse->last_x + xrel);
@@ -475,6 +519,12 @@
     }
 
     SDL_zerop(mouse);
+
+    SDL_DelHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
+                        SDL_MouseNormalSpeedScaleChanged, mouse);
+
+    SDL_DelHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
+                        SDL_MouseRelativeSpeedScaleChanged, mouse);
 }
 
 Uint32
@@ -603,6 +653,8 @@
         }
     }
     mouse->relative_mode = enabled;
+    mouse->scale_accum_x = 0.0f;
+    mouse->scale_accum_y = 0.0f;
 
     if (mouse->focus) {
         SDL_UpdateWindowGrab(mouse->focus);
diff -r 10fb0ebc4fbf -r 4f02752f8d85 src/events/SDL_mouse_c.h
--- a/src/events/SDL_mouse_c.h	Fri Dec 02 02:25:12 2016 -0800
+++ b/src/events/SDL_mouse_c.h	Fri Dec 02 21:01:13 2016 -0800
@@ -83,6 +83,10 @@
     Uint32 buttonstate;
     SDL_bool relative_mode;
     SDL_bool relative_mode_warp;
+    float normal_speed_scale;
+    float relative_speed_scale;
+    float scale_accum_x;
+    float scale_accum_y;
 
     /* Data for double-click tracking */
     int num_clickstates;


More information about the commits mailing list