Add weapon cycling bindings for mouse and joystick buttons. Add weapon cycling bindi...
[chocolate-doom.git] / setup / txt_mouseinput.c
blob05c89b39f145319622d3663fe0cd9bc400ae2a4d
1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 2006 Simon Howard
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 // 02111-1307, USA.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "doomkeys.h"
28 #include "txt_mouseinput.h"
29 #include "txt_gui.h"
30 #include "txt_io.h"
31 #include "txt_label.h"
32 #include "txt_window.h"
34 #define MOUSE_INPUT_WIDTH 8
36 static int MousePressCallback(txt_window_t *window,
37 int x, int y, int b,
38 TXT_UNCAST_ARG(mouse_input))
40 TXT_CAST_ARG(txt_mouse_input_t, mouse_input);
42 // Got the mouse press. Save to the variable and close the window.
44 *mouse_input->variable = b - TXT_MOUSE_BASE;
45 TXT_EmitSignal(mouse_input, "set");
46 TXT_CloseWindow(window);
48 return 1;
51 static void OpenPromptWindow(txt_mouse_input_t *mouse_input)
53 txt_window_t *window;
54 txt_label_t *label;
56 window = TXT_NewWindow(NULL);
57 TXT_SetWindowAction(window, TXT_HORIZ_LEFT, NULL);
58 TXT_SetWindowAction(window, TXT_HORIZ_CENTER,
59 TXT_NewWindowAbortAction(window));
60 TXT_SetWindowAction(window, TXT_HORIZ_RIGHT, NULL);
62 label = TXT_NewLabel("Press the new mouse button...");
64 TXT_AddWidget(window, label);
65 TXT_SetWidgetAlign(label, TXT_HORIZ_CENTER);
67 TXT_SetMouseListener(window, MousePressCallback, mouse_input);
70 static void TXT_MouseInputSizeCalc(TXT_UNCAST_ARG(mouse_input))
72 TXT_CAST_ARG(txt_mouse_input_t, mouse_input);
74 // All mouseinputs are the same size.
76 mouse_input->widget.w = MOUSE_INPUT_WIDTH;
77 mouse_input->widget.h = 1;
80 static void GetMouseButtonDescription(int button, char *buf)
82 switch (button)
84 case 0:
85 strcpy(buf, "LEFT");
86 break;
87 case 1:
88 strcpy(buf, "RIGHT");
89 break;
90 case 2:
91 strcpy(buf, "MID");
92 break;
93 default:
94 sprintf(buf, "BUTTON #%i", button);
95 break;
99 static void TXT_MouseInputDrawer(TXT_UNCAST_ARG(mouse_input), int selected)
101 TXT_CAST_ARG(txt_mouse_input_t, mouse_input);
102 char buf[20];
103 int i;
105 if (*mouse_input->variable < 0)
107 strcpy(buf, "(none)");
109 else
111 GetMouseButtonDescription(*mouse_input->variable, buf);
114 if (selected)
116 TXT_BGColor(TXT_COLOR_GREY, 0);
118 else
120 TXT_BGColor(TXT_COLOR_BLUE, 0);
123 TXT_FGColor(TXT_COLOR_BRIGHT_WHITE);
125 TXT_DrawString(buf);
127 for (i=strlen(buf); i<MOUSE_INPUT_WIDTH; ++i)
129 TXT_DrawString(" ");
133 static void TXT_MouseInputDestructor(TXT_UNCAST_ARG(mouse_input))
137 static int TXT_MouseInputKeyPress(TXT_UNCAST_ARG(mouse_input), int mouse)
139 TXT_CAST_ARG(txt_mouse_input_t, mouse_input);
141 if (mouse == KEY_ENTER)
143 // Open a window to prompt for the new mouse press
145 OpenPromptWindow(mouse_input);
147 return 1;
150 return 0;
153 static void TXT_MouseInputMousePress(TXT_UNCAST_ARG(widget), int x, int y, int b)
155 TXT_CAST_ARG(txt_mouse_input_t, widget);
157 // Clicking is like pressing enter
159 if (b == TXT_MOUSE_LEFT)
161 TXT_MouseInputKeyPress(widget, KEY_ENTER);
165 txt_widget_class_t txt_mouse_input_class =
167 TXT_MouseInputSizeCalc,
168 TXT_MouseInputDrawer,
169 TXT_MouseInputKeyPress,
170 TXT_MouseInputDestructor,
171 TXT_MouseInputMousePress,
172 NULL,
175 txt_mouse_input_t *TXT_NewMouseInput(int *variable)
177 txt_mouse_input_t *mouse_input;
179 mouse_input = malloc(sizeof(txt_mouse_input_t));
181 TXT_InitWidget(mouse_input, &txt_mouse_input_class);
182 mouse_input->variable = variable;
184 return mouse_input;