foxygit / doom Log in
commit c6a63b6286567001f80a5366648ea88265abb2ce
Author:     Jonathan Dowland <jon+github@alcopop.org>
AuthorDate: Tue Mar 21 07:03:39 2017 +0000
Commit:     Jonathan Dowland <jon+github@alcopop.org>
CommitDate: Tue Mar 21 09:48:22 2017 +0000

    doom menu joystick handling cleanup

    Introduce two macros (JOY_BUTTON_MAPPED and JOY_BUTTON_PRESSED) to
    clean up the menu code that checks joystick button state and simulates
    keyboard key presses depending on context.

    Re-order the new context checks for Y/N questions so they are
    nested under the existing checks for use/fire button presses.

    This cleans up some double-presses and some undefined behaviour
    with the unbound button X.
---
 src/doom/m_menu.c | 49 +++++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 22 deletions(-)

diff --git a/src/doom/m_menu.c b/src/doom/m_menu.c
index 8d567fce..2331d5e5 100644
--- a/src/doom/m_menu.c
+++ b/src/doom/m_menu.c
@@ -1419,34 +1419,39 @@ boolean M_Responder (event_t* ev)
 	    key = key_menu_right;
 	    joywait = I_GetTime() + 2;
 	}
-
-	if (ev->data1&1)
-	{
-	    key = key_menu_forward;
-	    joywait = I_GetTime() + 5;
-	}
-	if (ev->data1&2)
-	{
-	    key = key_menu_back;
-	    joywait = I_GetTime() + 5;
-	}
-        if (joybmenu >= 0 && (ev->data1 & (1 << joybmenu)) != 0)
+
+#define JOY_BUTTON_MAPPED(x) ((x) >= 0)
+#define JOY_BUTTON_PRESSED(x) (JOY_BUTTON_MAPPED(x) && (ev->data1 & (1 << (x))) != 0)
+
+        if (JOY_BUTTON_PRESSED(joybfire))
         {
-            key = key_menu_activate;
-	    joywait = I_GetTime() + 5;
+            // Simulate a 'Y' keypress when Doom show a Y/N dialog with Fire button.
+            if (messageToPrint && messageNeedsInput)
+            {
+                key = key_menu_confirm;
+            }
+            else
+            {
+                key = key_menu_forward;
+            }
+            joywait = I_GetTime() + 5;
         }
-
-        // Simulate a 'Y' keypress when Doom show a Y/N dialog with Fire button.
-        if (messageToPrint && messageNeedsInput && joybmenu >= 0 && ev->data1&1)
+        if (JOY_BUTTON_PRESSED(joybuse))
         {
-            key = key_menu_confirm;
+            // Simulate a 'N' keypress when Doom show a Y/N dialog with Use button.
+            if (messageToPrint && messageNeedsInput)
+            {
+                key = key_menu_abort;
+            }
+            else
+            {
+                key = key_menu_back;
+            }
             joywait = I_GetTime() + 5;
         }
-
-        // Simulate a 'N' keypress when Doom show a Y/N dialog with Use button.
-        if (messageToPrint && messageNeedsInput && joybmenu >= 0 && ev->data1&2)
+        if (JOY_BUTTON_PRESSED(joybmenu))
         {
-            key = key_menu_abort;
+            key = key_menu_activate;
             joywait = I_GetTime() + 5;
         }
     }