shithub: choc

Download patch

ref: aa5b8eba10fb1122c64c42122bfd48d7a7c7d745
parent: 60226b1183f5537fdde881e34e863e21c7e24d7e
author: Simon Howard <[email protected]>
date: Fri Aug 20 07:30:30 EDT 2010

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

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 1958

--- a/setup/txt_mouseinput.c
+++ b/setup/txt_mouseinput.c
@@ -91,7 +91,7 @@
             strcpy(buf, "MID");
             break;
         default:
-            sprintf(buf, "BUTTON #%i", button);
+            sprintf(buf, "BUTTON #%i", button + 1);
             break;
     }
 }
@@ -153,7 +153,7 @@
 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)
--- a/src/g_game.c
+++ b/src/g_game.c
@@ -269,7 +269,6 @@
  
 #define NUMKEYS		256 
 #define MAX_JOY_BUTTONS 20
-#define MAX_MOUSE_BUTTONS 3
 
 static boolean  gamekeydown[NUMKEYS]; 
 static int      turnheld;		// for accelerative turning 
--- a/src/i_video.c
+++ b/src/i_video.c
@@ -107,6 +107,10 @@
 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 @@
 
 }
 
-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 @@
                 /*
             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,10 +587,7 @@
             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;
 
@@ -564,10 +594,7 @@
             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 @@
     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);
         
--- 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