shithub: choc

Download patch

ref: 15b87abac461e37afc89cde202d6b988487f9ee5
parent: 346490a4dd1c7ee8c5c5ee8a5feee817f2480ba1
parent: 35f805c234a4fce24ecbe564b67fd0fd7acca699
author: Jonathan Dowland <jon+github@alcopop.org>
date: Mon Jul 4 17:38:22 EDT 2016

Merge pull request #744 from chocolate-doom/sdl2-mousewheel

fix mousewheel for sdl2 by synthesising button events

--- a/src/i_input.c
+++ b/src/i_input.c
@@ -355,6 +355,38 @@
     D_PostEvent(&event);
 }
 
+static void MapMouseWheelToButtons(SDL_MouseWheelEvent *wheel)
+{
+    // SDL2 distinguishes button events from mouse wheel events.
+    // We want to treat the mouse wheel as two buttons, as per
+    // SDL1
+    event_t up, down;
+    int button;
+
+    if (wheel->y <= 0)
+    {   // scroll down
+        button = 4;
+    }
+    else
+    {   // scroll up
+        button = 3;
+    }
+
+    // post a button down event
+    mouse_button_state |= (1 << button);
+    down.type = ev_mouse;
+    down.data1 = mouse_button_state;
+    down.data2 = down.data3 = 0;
+    D_PostEvent(&down);
+
+    // post a button up event
+    mouse_button_state &= ~(1 << button);
+    up.type = ev_mouse;
+    up.data1 = mouse_button_state;
+    up.data2 = up.data3 = 0;
+    D_PostEvent(&up);
+}
+
 void I_HandleMouseEvent(SDL_Event *sdlevent)
 {
     switch (sdlevent->type)
@@ -365,6 +397,10 @@
 
         case SDL_MOUSEBUTTONUP:
             UpdateMouseButtonState(sdlevent->button.button, false);
+            break;
+
+        case SDL_MOUSEWHEEL:
+            MapMouseWheelToButtons(&(sdlevent->wheel));
             break;
 
         default:
--- a/src/i_video.c
+++ b/src/i_video.c
@@ -418,6 +418,7 @@
 
             case SDL_MOUSEBUTTONDOWN:
             case SDL_MOUSEBUTTONUP:
+            case SDL_MOUSEWHEEL:
                 if (usemouse && !nomouse && window_focused)
                 {
                     I_HandleMouseEvent(&sdlevent);
--- a/textscreen/txt_sdl.c
+++ b/textscreen/txt_sdl.c
@@ -531,6 +531,20 @@
     }
 }
 
+// Convert an SDL wheel motion to a textscreen button index.
+
+static int SDLWheelToTXTButton(SDL_MouseWheelEvent *wheel)
+{
+    if (wheel->y <= 0)
+    {
+        return TXT_MOUSE_SCROLLDOWN;
+    }
+    else
+    {
+        return TXT_MOUSE_SCROLLUP;
+    }
+}
+
 static int MouseHasMoved(void)
 {
     static int last_x = 0, last_y = 0;
@@ -614,6 +628,9 @@
                     return SDLButtonToTXTButton(ev.button.button);
                 }
                 break;
+
+            case SDL_MOUSEWHEEL:
+                return SDLWheelToTXTButton(&ev.wheel);
 
             case SDL_KEYDOWN:
                 UpdateModifierState(&ev.key.keysym, 1);