From fae9e043dfa2ff8dfd33e8ab9ce7cd24a5035393 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 1 Nov 2021 13:54:16 +0900 Subject: [PATCH] [input] Move button name and description This puts the name and description into the button itself, making it much easier to look them up. --- include/QF/input/binding.h | 4 +- libs/input/in_button.c | 10 +-- nq/source/cl_input.c | 178 ++++++++++++++++++++----------------- qw/source/cl_input.c | 178 ++++++++++++++++++++----------------- 4 files changed, 193 insertions(+), 177 deletions(-) diff --git a/include/QF/input/binding.h b/include/QF/input/binding.h index effa27fc4..60cf27196 100644 --- a/include/QF/input/binding.h +++ b/include/QF/input/binding.h @@ -91,6 +91,7 @@ typedef struct in_button_s { int down[2]; ///< button ids holding this button down int state; ///< in_button_state const char *name; + const char *description; } in_button_t; typedef struct in_axisbinding_s { @@ -211,8 +212,7 @@ IN_ButtonReleased (in_button_t *button) void IN_ButtonAction (in_button_t *buttin, int id, int pressed); -int IN_RegisterButton (in_button_t *button, const char *name, - const char *description); +int IN_RegisterButton (in_button_t *button); int IN_RegisterAxis (in_axis_t *axis, const char *name, const char *description); in_button_t *IN_FindButton (const char *name); diff --git a/libs/input/in_button.c b/libs/input/in_button.c index 793ce97cc..1c0fe51f7 100644 --- a/libs/input/in_button.c +++ b/libs/input/in_button.c @@ -45,8 +45,6 @@ #include "QF/sys.h" typedef struct regbutton_s { - const char *name; - const char *description; in_button_t *button; char *press_cmd; char *release_cmd; @@ -58,7 +56,7 @@ static const char * button_get_key (const void *b, void *data) { __auto_type regbutton = (const regbutton_t *) b; - return regbutton->name; + return regbutton->button->name; } static void @@ -162,16 +160,14 @@ button_release_cmd (void *_b) } VISIBLE int -IN_RegisterButton (in_button_t *button, const char *name, - const char *description) +IN_RegisterButton (in_button_t *button) { + const char *name = button->name; if (Hash_Find (button_tab, name)) { return 0; } size_t size = strlen (name) + 2; regbutton_t *regbutton = malloc (sizeof (regbutton_t) + 2 * size); - regbutton->name = name; - regbutton->description = description; regbutton->button = button; regbutton->press_cmd = (char *) (regbutton + 1); diff --git a/nq/source/cl_input.c b/nq/source/cl_input.c index c480700e1..3f22eafee 100644 --- a/nq/source/cl_input.c +++ b/nq/source/cl_input.c @@ -50,88 +50,99 @@ #include "nq/include/client.h" #include "nq/include/host.h" -/* - KEY BUTTONS +in_button_t in_left = { + .name = "left", + .description = "When active the player is turning left" +}; +in_button_t in_right = { + .name = "right", + .description = "When active the player is turning right" +}; +in_button_t in_forward = { + .name = "forward", + .description = "When active the player is moving forward" +}; +in_button_t in_back = { + .name = "back", + .description = "When active the player is moving backwards" +}; +in_button_t in_lookup = { + .name = "lookup", + .description = "When active the player's view is looking up" +}; +in_button_t in_lookdown = { + .name = "lookdown", + .description = "When active the player's view is looking down" +}; +in_button_t in_moveleft = { + .name = "moveleft", + .description = "When active the player is strafing left" +}; +in_button_t in_moveright = { + .name = "moveright", + .description = "When active the player is strafing right" +}; +in_button_t in_use = { + .name = "use", + .description = "Left over command for opening doors and triggering" + " switches" +}; +in_button_t in_jump = { + .name = "jump", + .description = "When active the player is jumping" +}; +in_button_t in_attack = { + .name = "attack", + .description = "When active player is firing/using current weapon" +}; +in_button_t in_up = { + .name = "moveup", + .description = "When active the player is swimming up in a liquid" +}; +in_button_t in_down = { + .name = "movedown", + .description = "When active the player is swimming down in a liquid" +}; +in_button_t in_strafe = { + .name = "strafe", + .description = "When active, +left and +right function like +moveleft and" + " +moveright" +}; +in_button_t in_klook = { + .name = "klook", + .description = "When active, +forward and +back perform +lookup and" + " +lookdown" +}; +in_button_t in_speed = { + .name = "speed", + .description = "When active the player is running" +}; +in_button_t in_mlook = { + .name = "mlook", + .description = "When active moving the mouse or joystick forwards " + "and backwards performs +lookup and " + "+lookdown" +}; - Continuous button event tracking is complicated by the fact that two - different input sources (say, mouse button 1 and the control key) can - both press the same button, but the button should be released only when - both of the pressing key have been released. - - When a key event issues a button command (+forward, +attack, etc), it - appends its key number as a parameter to the command so it can be - matched up with the release. - - state bit 0 is the current state of the key - state bit 1 is edge triggered on the up to down transition - state bit 2 is edge triggered on the down to up transition -*/ - -in_button_t in_left, in_right, in_forward, in_back; -in_button_t in_lookup, in_lookdown, in_moveleft, in_moveright; -in_button_t in_use, in_jump, in_attack; -in_button_t in_up, in_down; -in_button_t in_strafe, in_klook, in_speed, in_mlook; - -static struct { - const char *name; - in_button_t *button; - const char *description; -} cl_in_buttons[] = { - { "left", &in_left, - "When active the player is turning left" - }, - { "right", &in_right, - "When active the player is turning right" - }, - { "forward", &in_forward, - "When active the player is moving forward" - }, - { "back", &in_back, - "When active the player is moving backwards" - }, - { "lookup", &in_lookup, - "When active the player's view is looking up" - }, - { "lookdown", &in_lookdown, - "When active the player's view is looking down" - }, - { "moveleft", &in_moveleft, - "When active the player is strafing left" - }, - { "moveright", &in_moveright, - "When active the player is strafing right" - }, - { "use", &in_use, - "Left over command for opening doors and triggering switches" - }, - { "jump", &in_jump, - "When active the player is jumping" - }, - { "attack", &in_attack, - "When active player is firing/using current weapon" - }, - { "moveup", &in_up, - "When active the player is swimming up in a liquid" - }, - { "movedown", &in_down, - "When active the player is swimming down in a liquid" - }, - { "strafe", &in_strafe, - "When active, +left and +right function like +moveleft and +moveright" - }, - { "speed", &in_speed, - "When active the player is running" - }, - { "klook", &in_klook, - "When active, +forward and +back perform +lookup and +lookdown" - }, - { "mlook", &in_mlook, - "When active moving the mouse or joystick forwards " - "and backwards performs +lookup and " - "+lookdown" - }, - { } +static in_button_t *cl_in_buttons[] = { + &in_left, + &in_right, + &in_forward, + &in_back, + &in_lookup, + &in_lookdown, + &in_moveleft, + &in_moveright, + &in_use, + &in_jump, + &in_attack, + &in_up, + &in_down, + &in_strafe, + &in_klook, + &in_speed, + &in_mlook, + 0 }; int in_impulse; @@ -351,9 +362,8 @@ CL_SendMove (usercmd_t *cmd) void CL_Input_Init (void) { - for (int i = 0; cl_in_buttons[i].name; i++) { - IN_RegisterButton (cl_in_buttons[i].button, cl_in_buttons[i].name, - cl_in_buttons[i].description); + for (int i = 0; cl_in_buttons[i]; i++) { + IN_RegisterButton (cl_in_buttons[i]); } Cmd_AddDataCommand ("impulse", IN_Impulse, 0, "Call a game function or QuakeC function."); diff --git a/qw/source/cl_input.c b/qw/source/cl_input.c index 9bbc86e6a..cd6b497c6 100644 --- a/qw/source/cl_input.c +++ b/qw/source/cl_input.c @@ -63,88 +63,99 @@ cvar_t *cl_nodelta; cvar_t *cl_maxnetfps; cvar_t *cl_spamimpulse; -/* - KEY BUTTONS +in_button_t in_left = { + .name = "left", + .description = "When active the player is turning left" +}; +in_button_t in_right = { + .name = "right", + .description = "When active the player is turning right" +}; +in_button_t in_forward = { + .name = "forward", + .description = "When active the player is moving forward" +}; +in_button_t in_back = { + .name = "back", + .description = "When active the player is moving backwards" +}; +in_button_t in_lookup = { + .name = "lookup", + .description = "When active the player's view is looking up" +}; +in_button_t in_lookdown = { + .name = "lookdown", + .description = "When active the player's view is looking down" +}; +in_button_t in_moveleft = { + .name = "moveleft", + .description = "When active the player is strafing left" +}; +in_button_t in_moveright = { + .name = "moveright", + .description = "When active the player is strafing right" +}; +in_button_t in_use = { + .name = "use", + .description = "Left over command for opening doors and triggering" + " switches" +}; +in_button_t in_jump = { + .name = "jump", + .description = "When active the player is jumping" +}; +in_button_t in_attack = { + .name = "attack", + .description = "When active player is firing/using current weapon" +}; +in_button_t in_up = { + .name = "moveup", + .description = "When active the player is swimming up in a liquid" +}; +in_button_t in_down = { + .name = "movedown", + .description = "When active the player is swimming down in a liquid" +}; +in_button_t in_strafe = { + .name = "strafe", + .description = "When active, +left and +right function like +moveleft and" + " +moveright" +}; +in_button_t in_klook = { + .name = "klook", + .description = "When active, +forward and +back perform +lookup and" + " +lookdown" +}; +in_button_t in_speed = { + .name = "speed", + .description = "When active the player is running" +}; +in_button_t in_mlook = { + .name = "mlook", + .description = "When active moving the mouse or joystick forwards " + "and backwards performs +lookup and " + "+lookdown" +}; - Continuous button event tracking is complicated by the fact that two - different input sources (say, mouse button 1 and the control key) can - both press the same button, but the button should be released only when - both of the pressing key have been released. - - When a key event issues a button command (+forward, +attack, etc), it - appends its key number as a parameter to the command so it can be - matched up with the release. - - state bit 0 is the current state of the key - state bit 1 is edge triggered on the up to down transition - state bit 2 is edge triggered on the down to up transition -*/ - -in_button_t in_left, in_right, in_forward, in_back; -in_button_t in_lookup, in_lookdown, in_moveleft, in_moveright; -in_button_t in_use, in_jump, in_attack; -in_button_t in_up, in_down; -in_button_t in_strafe, in_klook, in_speed, in_mlook; - -static struct { - const char *name; - in_button_t *button; - const char *description; -} cl_in_buttons[] = { - { "left", &in_left, - "When active the player is turning left" - }, - { "right", &in_right, - "When active the player is turning right" - }, - { "forward", &in_forward, - "When active the player is moving forward" - }, - { "back", &in_back, - "When active the player is moving backwards" - }, - { "lookup", &in_lookup, - "When active the player's view is looking up" - }, - { "lookdown", &in_lookdown, - "When active the player's view is looking down" - }, - { "moveleft", &in_moveleft, - "When active the player is strafing left" - }, - { "moveright", &in_moveright, - "When active the player is strafing right" - }, - { "use", &in_use, - "Left over command for opening doors and triggering switches" - }, - { "jump", &in_jump, - "When active the player is jumping" - }, - { "attack", &in_attack, - "When active player is firing/using current weapon" - }, - { "moveup", &in_up, - "When active the player is swimming up in a liquid" - }, - { "movedown", &in_down, - "When active the player is swimming down in a liquid" - }, - { "strafe", &in_strafe, - "When active, +left and +right function like +moveleft and +moveright" - }, - { "speed", &in_speed, - "When active the player is running" - }, - { "klook", &in_klook, - "When active, +forward and +back perform +lookup and +lookdown" - }, - { "mlook", &in_mlook, - "When active moving the mouse or joystick forwards " - "and backwards performs +lookup and " - "+lookdown" - }, - { } +static in_button_t *cl_in_buttons[] = { + &in_left, + &in_right, + &in_forward, + &in_back, + &in_lookup, + &in_lookdown, + &in_moveleft, + &in_moveright, + &in_use, + &in_jump, + &in_attack, + &in_up, + &in_down, + &in_strafe, + &in_klook, + &in_speed, + &in_mlook, + 0 }; int in_impulse; @@ -508,9 +519,8 @@ CL_SendCmd (void) void CL_Input_Init (void) { - for (int i = 0; cl_in_buttons[i].name; i++) { - IN_RegisterButton (cl_in_buttons[i].button, cl_in_buttons[i].name, - cl_in_buttons[i].description); + for (int i = 0; cl_in_buttons[i]; i++) { + IN_RegisterButton (cl_in_buttons[i]); } Cmd_AddDataCommand ("impulse", IN_Impulse, 0, "Call a game function or QuakeC function.");