From 0914172c5377fc00d6e8235acabcca74b17a43ab Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 27 Dec 2013 23:09:47 +0000 Subject: [PATCH] [Change] Improved the mod state handling and allow two keybindings on the same key. --- src/input.c | 79 ++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/src/input.c b/src/input.c index 90b006c..c4cfadc 100644 --- a/src/input.c +++ b/src/input.c @@ -62,37 +62,37 @@ extern unsigned int player_target; */ void input_setDefault(void) { /* Movement. */ - input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, KMOD_NONE, 0); - input_setKeybind("afterburn", KEYBIND_KEYBOARD, SDLK_UNKNOWN, KMOD_NONE, 0); /*Not set.*/ - input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, KMOD_NONE, 0); - input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, KMOD_NONE, 0); - input_setKeybind("reverse", KEYBIND_KEYBOARD, SDLK_s, KMOD_NONE, 0); + input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, KMOD_ALL, 0); + input_setKeybind("afterburn", KEYBIND_KEYBOARD, SDLK_UNKNOWN, KMOD_ALL, 0); + input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, KMOD_ALL, 0); + input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, KMOD_ALL, 0); + input_setKeybind("reverse", KEYBIND_KEYBOARD, SDLK_s, KMOD_ALL, 0); /* Targetting. */ - input_setKeybind("target", KEYBIND_KEYBOARD, SDLK_TAB, KMOD_NONE, 0); - input_setKeybind("target_nearest", KEYBIND_KEYBOARD, SDLK_t, KMOD_NONE, 0); - input_setKeybind("target_hostile", KEYBIND_KEYBOARD, SDLK_r, KMOD_NONE, 0); + input_setKeybind("target", KEYBIND_KEYBOARD, SDLK_TAB, KMOD_NONE, 0); + input_setKeybind("target_nearest", KEYBIND_KEYBOARD, SDLK_t, KMOD_NONE, 0); + input_setKeybind("target_hostile", KEYBIND_KEYBOARD, SDLK_r, KMOD_NONE, 0); /* Combat. */ - input_setKeybind("primary", KEYBIND_KEYBOARD, SDLK_SPACE, KMOD_NONE, 0); - input_setKeybind("face", KEYBIND_KEYBOARD, SDLK_f, KMOD_NONE, 0); - input_setKeybind("board", KEYBIND_KEYBOARD, SDLK_b, KMOD_NONE, 0); + input_setKeybind("primary", KEYBIND_KEYBOARD, SDLK_SPACE, KMOD_NONE, 0); + input_setKeybind("face", KEYBIND_KEYBOARD, SDLK_f, KMOD_NONE, 0); + input_setKeybind("board", KEYBIND_KEYBOARD, SDLK_b, KMOD_NONE, 0); /* Secondary weapon. */ - input_setKeybind("secondary", KEYBIND_KEYBOARD, SDLK_LSHIFT, KMOD_NONE, 0); - input_setKeybind("secondary_next", KEYBIND_KEYBOARD, SDLK_e, KMOD_NONE, 0); + input_setKeybind("secondary", KEYBIND_KEYBOARD, SDLK_LSHIFT, KMOD_NONE, 0); + input_setKeybind("secondary_next", KEYBIND_KEYBOARD, SDLK_e, KMOD_NONE, 0); /* Space */ - input_setKeybind("autonav", KEYBIND_KEYBOARD, SDLK_n, KMOD_CTRL, 0); - input_setKeybind("target_planet", KEYBIND_KEYBOARD, SDLK_p, KMOD_NONE, 0); - input_setKeybind("land", KEYBIND_KEYBOARD, SDLK_l, KMOD_NONE, 0); - input_setKeybind("thyperspace", KEYBIND_KEYBOARD, SDLK_h, KMOD_NONE, 0); - input_setKeybind("starmap", KEYBIND_KEYBOARD, SDLK_m, KMOD_NONE, 0); - input_setKeybind("jump", KEYBIND_KEYBOARD, SDLK_j, KMOD_NONE, 0); + input_setKeybind("autonav", KEYBIND_KEYBOARD, SDLK_j, KMOD_LCTRL, 0); + input_setKeybind("target_planet", KEYBIND_KEYBOARD, SDLK_p, KMOD_NONE, 0); + input_setKeybind("land", KEYBIND_KEYBOARD, SDLK_l, KMOD_NONE, 0); + input_setKeybind("thyperspace", KEYBIND_KEYBOARD, SDLK_h, KMOD_NONE, 0); + input_setKeybind("starmap", KEYBIND_KEYBOARD, SDLK_m, KMOD_NONE, 0); + input_setKeybind("jump", KEYBIND_KEYBOARD, SDLK_j, KMOD_NONE, 0); /* Misc. */ - input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_KP_PLUS, KMOD_NONE, 0); - input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_KP_MINUS, KMOD_NONE, 0); - input_setKeybind("screenshot", KEYBIND_KEYBOARD, SDLK_KP_MULTIPLY, KMOD_NONE, 0); - input_setKeybind("pause", KEYBIND_KEYBOARD, SDLK_F1, KMOD_NONE, 0); - input_setKeybind("menu", KEYBIND_KEYBOARD, SDLK_ESCAPE, KMOD_NONE, 0); - input_setKeybind("info", KEYBIND_KEYBOARD, SDLK_i, KMOD_NONE, 0); + input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_KP_PLUS, KMOD_NONE, 0); + input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_KP_MINUS, KMOD_NONE, 0); + input_setKeybind("screenshot", KEYBIND_KEYBOARD, SDLK_KP_MULTIPLY, KMOD_NONE, 0); + input_setKeybind("pause", KEYBIND_KEYBOARD, SDLK_F1, KMOD_NONE, 0); + input_setKeybind("menu", KEYBIND_KEYBOARD, SDLK_ESCAPE, KMOD_NONE, 0); + input_setKeybind("info", KEYBIND_KEYBOARD, SDLK_i, KMOD_NONE, 0); } /** @@ -111,7 +111,8 @@ void input_init(void) { tmp = MALLOC_L(Keybind); tmp->name = (char*)keybindNames[i]; tmp->type = KEYBIND_NULL; - tmp->key = 0; + tmp->key = SDLK_UNKNOWN; + tmp->mod = KMOD_NONE; tmp->reverse = 1.; input_keybinds[i] = tmp; } @@ -180,9 +181,8 @@ int input_getKeybind(char* keybind, KeybindType* type, SDLMod* mod, int* reverse * @param value The value of the keypress (defined above). * @param abs Whether or not it's an absolute value (for the joystick). */ -#define KEY(s) (((input_keybinds[keynum]->mod & mod) || \ - (input_keybinds[keynum]->mod == mod) || \ - (input_keybinds[keynum]->mod == KMOD_ALL)) && \ +#define KEY(s) (((input_keybinds[keynum]->mod == mod) || \ + (input_keybinds[keynum]->mod==KMOD_ALL)) && \ (strcmp(input_keybinds[keynum]->name, s)==0)) /**< Shortcut for ease. */ #define INGAME() (!toolkit) /**< Make sure player is in game. */ #define NOHYP() \ @@ -194,6 +194,7 @@ static void input_key(int keynum, double value, int kabs) { SDLMod mod; mod = SDL_GetModState(); /* Yes, we always get it just in case. */ + mod &= ~(KMOD_CAPS | KMOD_NUM | KMOD_MODE); /* We want to ignore "global" modifiers. */ /* Accelerating. */ if(KEY("accel")) { @@ -389,10 +390,8 @@ static void input_joyaxis(const unsigned int axis, const int value) { int i; for(i = 0; strcmp(keybindNames[i], "end"); i++) if(input_keybinds[i]->type == KEYBIND_JAXIS && - input_keybinds[i]->key == axis) { + input_keybinds[i]->key == axis) input_key(i, -(input_keybinds[i]->reverse) * (double)value / 32767., 1); - return; - } } /* Joystick button down. */ @@ -400,10 +399,8 @@ static void input_joydown(const unsigned int button) { int i; for(i = 0; strcmp(keybindNames[i], "end");i++) if(input_keybinds[i]->type == KEYBIND_JBUTTON && - input_keybinds[i]->key == button) { + input_keybinds[i]->key == button) input_key(i, KEY_RELEASE, 0); - return; - } } /* Joystick button up. */ @@ -411,10 +408,8 @@ static void input_joyup(const unsigned int button) { int i; for(i = 0; strcmp(keybindNames[i], "end"); i++) if(input_keybinds[i]->type == KEYBIND_JBUTTON && - input_keybinds[i]->key == button) { + input_keybinds[i]->key == button) input_key(i, KEY_RELEASE, 0); - return; - } } /* Keyboard. */ @@ -424,10 +419,8 @@ static void input_keydown(SDLKey key) { int i; for(i = 0; strcmp(keybindNames[i], "end"); i++) if(input_keybinds[i]->type == KEYBIND_KEYBOARD && - input_keybinds[i]->key == key) { + input_keybinds[i]->key == key) input_key(i, KEY_PRESS, 0); - return; - } } /* Key up. */ @@ -435,10 +428,8 @@ static void input_keyup(SDLKey key) { int i; for(i = 0; strcmp(keybindNames[i], "end"); i++) if(input_keybinds[i]->type == KEYBIND_KEYBOARD && - input_keybinds[i]->key == key) { + input_keybinds[i]->key == key) input_key(i, KEY_RELEASE, 0); - return; - } }