foxygit / doom Log in
commit aa5b8eba10fb1122c64c42122bfd48d7a7c7d745
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Fri Aug 20 11:30:30 2010 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Fri Aug 20 11:30:30 2010 +0000

    Extend mouse code to support up to 8 buttons (allows mouse wheel to be
    used).

    Subversion-branch: /trunk/chocolate-doom
    Subversion-revision: 1958
---
 setup/txt_mouseinput.c |  4 +--
 src/g_game.c           |  1 -
 src/i_video.c          | 77 ++++++++++++++++++++++++++++++++++----------------
 src/i_video.h          |  5 ++--
 4 files changed, 57 insertions(+), 30 deletions(-)

diff --git a/setup/txt_mouseinput.c b/setup/txt_mouseinput.c
index 05c89b39..8b87e651 100644
--- a/setup/txt_mouseinput.c
+++ b/setup/txt_mouseinput.c
@@ -91,7 +91,7 @@ static void GetMouseButtonDescription(int button, char *buf)
             strcpy(buf, "MID");
             break;
         default:
-            sprintf(buf, "BUTTON #%i", button);
+            sprintf(buf, "BUTTON #%i", button + 1);
             break;
     }
 }
@@ -153,7 +153,7 @@ static int TXT_MouseInputKeyPress(TXT_UNCAST_ARG(mouse_input), int mouse)
 static void TXT_MouseInputMousePress(TXT_UNCAST_ARG(widget), int x, int y, int b)
 {
     TXT_CAST_ARG(txt_mouse_input_t, widget);
-
+
     // Clicking is like pressing enter

     if (b == TXT_MOUSE_LEFT)
diff --git a/src/g_game.c b/src/g_game.c
index bf9560db..95cd77a6 100644
--- a/src/g_game.c
+++ b/src/g_game.c
@@ -269,7 +269,6 @@ static const struct

 #define NUMKEYS		256
 #define MAX_JOY_BUTTONS 20
-#define MAX_MOUSE_BUTTONS 3

 static boolean  gamekeydown[NUMKEYS];
 static int      turnheld;		// for accelerative turning
diff --git a/src/i_video.c b/src/i_video.c
index 91b670b8..d2d0d169 100644
--- a/src/i_video.c
+++ b/src/i_video.c
@@ -107,6 +107,10 @@ static boolean initialized = false;
 static boolean nomouse = false;
 extern int usemouse;

+// Bit mask of mouse button state.
+
+static unsigned int mouse_button_state = 0;
+
 // if true, screens[0] is screen->pixel

 static boolean native_surface;
@@ -430,29 +434,58 @@ void I_StartFrame (void)

 }

-static int MouseButtonState(void)
+static void UpdateMouseButtonState(unsigned int button, boolean on)
 {
-    Uint8 state;
-    int result = 0;
+    event_t event;

-#if SDL_VERSION_ATLEAST(1, 3, 0)
-    state = SDL_GetMouseState(0, NULL, NULL);
-#else
-    state = SDL_GetMouseState(NULL, NULL);
-#endif
+    if (button < SDL_BUTTON_LEFT || button > MAX_MOUSE_BUTTONS)
+    {
+        return;
+    }

     // Note: button "0" is left, button "1" is right,
     // button "2" is middle for Doom.  This is different
     // to how SDL sees things.

-    if (state & SDL_BUTTON(1))
-        result |= 1;
-    if (state & SDL_BUTTON(3))
-        result |= 2;
-    if (state & SDL_BUTTON(2))
-        result |= 4;
+    switch (button)
+    {
+        case SDL_BUTTON_LEFT:
+            button = 0;
+            break;

-    return result;
+        case SDL_BUTTON_RIGHT:
+            button = 1;
+            break;
+
+        case SDL_BUTTON_MIDDLE:
+            button = 2;
+            break;
+
+        default:
+            // SDL buttons are indexed from 1.
+            --button;
+            break;
+    }
+
+    printf("button %i -> %s\n", button, on ? "on" : "off");
+
+    // Turn bit representing this button on or off.
+
+    if (on)
+    {
+        mouse_button_state |= (1 << button);
+    }
+    else
+    {
+        mouse_button_state &= ~(1 << button);
+    }
+
+    // Post an event with the new button state.
+
+    event.type = ev_mouse;
+    event.data1 = mouse_button_state;
+    event.data2 = event.data3 = 0;
+    D_PostEvent(&event);
 }

 static int AccelerateMouse(int val)
@@ -544,7 +577,7 @@ void I_GetEvent(void)
                 /*
             case SDL_MOUSEMOTION:
                 event.type = ev_mouse;
-                event.data1 = MouseButtonState();
+                event.data1 = mouse_button_state;
                 event.data2 = AccelerateMouse(sdlevent.motion.xrel);
                 event.data3 = -AccelerateMouse(sdlevent.motion.yrel);
                 D_PostEvent(&event);
@@ -554,20 +587,14 @@ void I_GetEvent(void)
             case SDL_MOUSEBUTTONDOWN:
 		if (usemouse && !nomouse)
 		{
-                    event.type = ev_mouse;
-                    event.data1 = MouseButtonState();
-                    event.data2 = event.data3 = 0;
-                    D_PostEvent(&event);
+                    UpdateMouseButtonState(sdlevent.button.button, true);
 		}
                 break;

             case SDL_MOUSEBUTTONUP:
 		if (usemouse && !nomouse)
 		{
-                    event.type = ev_mouse;
-                    event.data1 = MouseButtonState();
-                    event.data2 = event.data3 = 0;
-                    D_PostEvent(&event);
+                    UpdateMouseButtonState(sdlevent.button.button, false);
 		}
                 break;

@@ -637,7 +664,7 @@ static void I_ReadMouse(void)
     if (x != 0 || y != 0)
     {
         ev.type = ev_mouse;
-        ev.data1 = MouseButtonState();
+        ev.data1 = mouse_button_state;
         ev.data2 = AccelerateMouse(x);
         ev.data3 = -AccelerateMouse(y);

diff --git a/src/i_video.h b/src/i_video.h
index bd5de24a..94ffcc29 100644
--- a/src/i_video.h
+++ b/src/i_video.h
@@ -28,10 +28,11 @@
 #ifndef __I_VIDEO__
 #define __I_VIDEO__

-
 #include "doomtype.h"

-typedef struct
+#define MAX_MOUSE_BUTTONS 8
+
+typedef struct
 {
         // Screen width and height