foxygit / doom Log in
commit 7579f29afb9f48e01a7f21a0726b109f66b5f2b5
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Fri Jun 22 19:14:49 2007 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Fri Jun 22 19:14:49 2007 +0000

    Don't allow two actions bound to the same button in setup.

    Subversion-branch: /trunk/chocolate-doom
    Subversion-revision: 926
---
 setup/joystick.c      | 33 ++++++++++++++++++++++++++++++++-
 setup/txt_joybinput.c |  1 +
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/setup/joystick.c b/setup/joystick.c
index eb7f9bfa..ceea9603 100644
--- a/setup/joystick.c
+++ b/setup/joystick.c
@@ -21,6 +21,7 @@

 #include <stdlib.h>

+#include "doomtype.h"
 #include "textscreen.h"
 #include "txt_joybinput.h"

@@ -64,6 +65,11 @@ int joystick_y_invert = 0;

 static txt_button_t *joystick_button;

+static int *all_joystick_buttons[] = {
+        &joybstraferight, &joybstrafeleft, &joybfire, &joybspeed,
+        &joybuse, &joybstrafe,
+};
+
 //
 // Calibration
 //
@@ -308,6 +314,25 @@ static void CalibrateJoystick(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(unused))
     SetCalibrationLabel();
 }

+void JoyButtonSetCallback(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(variable))
+{
+    TXT_CAST_ARG(int, variable);
+    int i;
+
+    // Only allow a button to be bound to one action at a time.  If
+    // we assign a key that another action is using, set that other action
+    // to -1.
+
+    for (i=0; i<arrlen(all_joystick_buttons); ++i)
+    {
+        if (variable != all_joystick_buttons[i]
+         && *variable == *all_joystick_buttons[i])
+        {
+            *all_joystick_buttons[i] = -1;
+        }
+    }
+}
+

 //
 // GUI
@@ -329,8 +354,14 @@ static void SetJoystickButtonLabel(void)

 static void AddJoystickControl(txt_table_t *table, char *label, int *var)
 {
+    txt_joystick_input_t *joy_input;
+
+    joy_input = TXT_NewJoystickInput(var);
+
     TXT_AddWidget(table, TXT_NewLabel(label));
-    TXT_AddWidget(table, TXT_NewJoystickInput(var));
+    TXT_AddWidget(table, joy_input);
+
+    TXT_SignalConnect(joy_input, "set", JoyButtonSetCallback, var);
 }

 void ConfigJoystick(void)
diff --git a/setup/txt_joybinput.c b/setup/txt_joybinput.c
index 7a94a996..1e132962 100644
--- a/setup/txt_joybinput.c
+++ b/setup/txt_joybinput.c
@@ -48,6 +48,7 @@ static int EventCallback(SDL_Event *event, TXT_UNCAST_ARG(joystick_input))
     if (event->type == SDL_JOYBUTTONDOWN)
     {
         *joystick_input->variable = event->jbutton.button;
+        TXT_EmitSignal(joystick_input, "set");
         TXT_CloseWindow(joystick_input->prompt_window);
         return 1;
     }