From 0a29eddd798e0722b74d0653fcd293b977cb5704 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Thu, 31 Aug 2006 18:13:40 +0000 Subject: Add key and mouse input widgets for selecting keys and mouse buttons. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 583 --- setup/keyboard.c | 85 ++++++++++++++++------- setup/mouse.c | 60 +++++++++++++++-- setup/txt_keyinput.c | 151 +++++++++++++++++++++++++++++++++++++++++ setup/txt_keyinput.h | 47 +++++++++++++ setup/txt_mouseinput.c | 180 +++++++++++++++++++++++++++++++++++++++++++++++++ setup/txt_mouseinput.h | 47 +++++++++++++ 6 files changed, 540 insertions(+), 30 deletions(-) create mode 100644 setup/txt_keyinput.c create mode 100644 setup/txt_keyinput.h create mode 100644 setup/txt_mouseinput.c create mode 100644 setup/txt_mouseinput.h (limited to 'setup') diff --git a/setup/keyboard.c b/setup/keyboard.c index 61b12a97..c635e93c 100644 --- a/setup/keyboard.c +++ b/setup/keyboard.c @@ -1,6 +1,51 @@ #include "textscreen.h" -int dummy; +#include "txt_keyinput.h" + +int key_left = KEY_LEFTARROW; +int key_right = KEY_RIGHTARROW; +int key_up = KEY_UPARROW; +int key_down = KEY_DOWNARROW; +int key_strafeleft = ','; +int key_straferight = '.'; +int key_fire = KEY_RCTRL; +int key_use = ' '; +int key_strafe = KEY_RALT; +int key_speed = KEY_RSHIFT; + +static int *allkeys[] = {&key_left, &key_right, &key_up, &key_down, + &key_strafeleft, &key_straferight, &key_fire, + &key_use, &key_strafe, &key_speed}; + +// Callback invoked when a key control is set + +static void KeySetCallback(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(variable)) +{ + TXT_CAST_ARG(int, variable); + int i; + + for (i=0; i #include "textscreen.h" +#include "txt_mouseinput.h" + int novert; int speed; int accel; int threshold; +int mouseb_fire; +int mouseb_strafe; +int mouseb_forward; + +static int *all_mouse_buttons[] = {&mouseb_fire, &mouseb_strafe, + &mouseb_forward}; + +static void MouseSetCallback(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(variable)) +{ + TXT_CAST_ARG(int, variable); + int i; + + // Check if the same mouse button is used for a different action + // If so, set the other action(s) to -1 (unset) + + for (i=0; i +#include + +#include "doomkeys.h" + +#include "txt_keyinput.h" +#include "txt_io.h" +#include "txt_label.h" +#include "txt_window.h" + +#define KEY_INPUT_WIDTH 10 + +static int KeyPressCallback(txt_window_t *window, int key, + TXT_UNCAST_ARG(key_input)) +{ + TXT_CAST_ARG(txt_key_input_t, key_input); + + if (key != KEY_ESCAPE) + { + // Got the key press. Save to the variable and close the window. + + *key_input->variable = key; + TXT_EmitSignal(key_input, "set"); + TXT_CloseWindow(window); + return 1; + } + else + { + return 0; + } +} + +static void OpenPromptWindow(txt_key_input_t *key_input) +{ + txt_window_t *window; + txt_label_t *label; + + window = TXT_NewWindow(NULL); + TXT_SetWindowAction(window, TXT_HORIZ_LEFT, NULL); + TXT_SetWindowAction(window, TXT_HORIZ_CENTER, + TXT_NewWindowAbortAction(window)); + TXT_SetWindowAction(window, TXT_HORIZ_RIGHT, NULL); + + label = TXT_NewLabel("Press the new key..."); + + TXT_AddWidget(window, label); + TXT_SetWidgetAlign(label, TXT_HORIZ_CENTER); + + TXT_SetKeyListener(window, KeyPressCallback, key_input); +} + +static void TXT_KeyInputSizeCalc(TXT_UNCAST_ARG(key_input)) +{ + TXT_CAST_ARG(txt_key_input_t, key_input); + + // All keyinputs are the same size. + + key_input->widget.w = KEY_INPUT_WIDTH; + key_input->widget.h = 1; +} + + +static void TXT_KeyInputDrawer(TXT_UNCAST_ARG(key_input), int selected) +{ + TXT_CAST_ARG(txt_key_input_t, key_input); + char buf[20]; + int i; + + if (*key_input->variable == 0) + { + strcpy(buf, ""); + } + else + { + TXT_GetKeyDescription(*key_input->variable, buf); + } + + if (selected) + { + TXT_BGColor(TXT_COLOR_GREY, 0); + } + else + { + TXT_BGColor(TXT_COLOR_BLUE, 0); + } + + TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); + + TXT_DrawString(buf); + + for (i=strlen(buf); ivariable = variable; + + return key_input; +} + diff --git a/setup/txt_keyinput.h b/setup/txt_keyinput.h new file mode 100644 index 00000000..e52d3041 --- /dev/null +++ b/setup/txt_keyinput.h @@ -0,0 +1,47 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// $Id$ +// +// Copyright(C) 1993-1996 Id Software, Inc. +// Copyright(C) 2006 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// + +#ifndef TXT_KEY_INPUT_H +#define TXT_KEY_INPUT_H + +typedef struct txt_key_input_s txt_key_input_t; + +#include "txt_widget.h" + +// +// A key input is like an input box. When selected, a box pops up +// allowing a key to be selected. +// + +struct txt_key_input_s +{ + txt_widget_t widget; + int *variable; +}; + +txt_key_input_t *TXT_NewKeyInput(int *variable); + +#endif /* #ifndef TXT_KEY_INPUT_H */ + + diff --git a/setup/txt_mouseinput.c b/setup/txt_mouseinput.c new file mode 100644 index 00000000..1c81e8dd --- /dev/null +++ b/setup/txt_mouseinput.c @@ -0,0 +1,180 @@ + +#include +#include +#include + +#include "doomkeys.h" + +#include "txt_mouseinput.h" +#include "txt_io.h" +#include "txt_label.h" +#include "txt_window.h" + +#define MOUSE_INPUT_WIDTH 10 + +static int MouseButtonToSetting(int b) +{ + switch (b) + { + case TXT_MOUSE_LEFT: + return 0; + case TXT_MOUSE_RIGHT: + return 1; + case TXT_MOUSE_MIDDLE: + return 2; + default: + return -1; + } +} + +static int MousePressCallback(txt_window_t *window, + int x, int y, int b, + TXT_UNCAST_ARG(mouse_input)) +{ + TXT_CAST_ARG(txt_mouse_input_t, mouse_input); + + // Got the mouse press. Save to the variable and close the window. + + *mouse_input->variable = MouseButtonToSetting(b); + TXT_EmitSignal(mouse_input, "set"); + TXT_CloseWindow(window); + + return 1; +} + +static void OpenPromptWindow(txt_mouse_input_t *mouse_input) +{ + txt_window_t *window; + txt_label_t *label; + + window = TXT_NewWindow(NULL); + TXT_SetWindowAction(window, TXT_HORIZ_LEFT, NULL); + TXT_SetWindowAction(window, TXT_HORIZ_CENTER, + TXT_NewWindowAbortAction(window)); + TXT_SetWindowAction(window, TXT_HORIZ_RIGHT, NULL); + + label = TXT_NewLabel("Press the new mouse button..."); + + TXT_AddWidget(window, label); + TXT_SetWidgetAlign(label, TXT_HORIZ_CENTER); + + TXT_SetMouseListener(window, MousePressCallback, mouse_input); +} + +static void TXT_MouseInputSizeCalc(TXT_UNCAST_ARG(mouse_input)) +{ + TXT_CAST_ARG(txt_mouse_input_t, mouse_input); + + // All mouseinputs are the same size. + + mouse_input->widget.w = MOUSE_INPUT_WIDTH; + mouse_input->widget.h = 1; +} + +static void GetMouseButtonDescription(int button, char *buf) +{ + switch (button) + { + case 0: + strcpy(buf, "LEFT"); + break; + case 1: + strcpy(buf, "RIGHT"); + break; + case 2: + strcpy(buf, "MID"); + break; + default: + sprintf(buf, "BUTTON #%i", button); + break; + } +} + +static void TXT_MouseInputDrawer(TXT_UNCAST_ARG(mouse_input), int selected) +{ + TXT_CAST_ARG(txt_mouse_input_t, mouse_input); + char buf[20]; + int i; + + if (*mouse_input->variable == -1) + { + strcpy(buf, ""); + } + else + { + GetMouseButtonDescription(*mouse_input->variable, buf); + } + + if (selected) + { + TXT_BGColor(TXT_COLOR_GREY, 0); + } + else + { + TXT_BGColor(TXT_COLOR_BLUE, 0); + } + + TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); + + TXT_DrawString(buf); + + for (i=strlen(buf); ivariable = variable; + + return mouse_input; +} + diff --git a/setup/txt_mouseinput.h b/setup/txt_mouseinput.h new file mode 100644 index 00000000..bfefb846 --- /dev/null +++ b/setup/txt_mouseinput.h @@ -0,0 +1,47 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// $Id$ +// +// Copyright(C) 1993-1996 Id Software, Inc. +// Copyright(C) 2006 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// + +#ifndef TXT_MOUSE_INPUT_H +#define TXT_MOUSE_INPUT_H + +typedef struct txt_mouse_input_s txt_mouse_input_t; + +#include "txt_widget.h" + +// +// A mouse input is like an input box. When selected, a box pops up +// allowing a mouse to be selected. +// + +struct txt_mouse_input_s +{ + txt_widget_t widget; + int *variable; +}; + +txt_mouse_input_t *TXT_NewMouseInput(int *variable); + +#endif /* #ifndef TXT_MOUSE_INPUT_H */ + + -- cgit v1.2.3