From 83ac2716577c4a96905daf337aaa376208f62c54 Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 27 Dec 2013 13:30:20 +0000 Subject: [PATCH] [Add] Added support for key modifiers. --- src/conf.c | 4 ++-- src/input.c | 69 +++++++++++++++++++++++++++++++---------------------- src/input.h | 6 +++-- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/conf.c b/src/conf.c index 4501ccb..7f396c0 100644 --- a/src/conf.c +++ b/src/conf.c @@ -122,7 +122,7 @@ int conf_loadConfig(const char* file) { conf_loadBool("aa_polygon", i); if(i) { gl_screen.flags |= OPENGL_AA_POLYGON; i = 0; } conf_loadBool("vsync", i); - if(i) { gl_screen..flags |= OPENG_VSYNC; i = 0; } + if(i) { gl_screen.flags |= OPENGL_VSYNC; i = 0; } /* FPS. */ conf_loadBool("showfps", show_fps); conf_loadInt("maxfps", max_fps); @@ -187,7 +187,7 @@ int conf_loadConfig(const char* file) { continue; } /* Set the keybind. */ - input_setKeybind((char*)keybindNames[i], type, key, reverse); + input_setKeybind((char*)keybindNames[i], type, key, KMOD_ALL, reverse); } else WARN("Malformed keybind in %s", file); diff --git a/src/input.c b/src/input.c index 32f7b0c..e4bd07a 100644 --- a/src/input.c +++ b/src/input.c @@ -23,6 +23,7 @@ typedef struct Keybind_ { KeybindType type; /**< type, defined in player.h. */ unsigned int key; /**< Key/axis/button event number. */ double reverse; /**< 1. if normal, -1 if reversed, only useful for joystick axis. */ + SDLMod mod; /**< Key modifiers (where applicable). */ } Keybind; static Keybind** input_keybinds; /**< Contains the players keybindings. */ @@ -61,36 +62,37 @@ extern unsigned int player_target; */ void input_setDefault(void) { /* Movement. */ - input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, 0); - input_setKeybind("afterburn", KEYBIND_KEYBOARD, SDLK_UNKNOWN, 0); /* Not set. */ - input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, 0); - input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, 0); - input_setKeybind("reverse", KEYBIND_KEYBOARD, SDLK_s, 0); - input_setKeybind("target", KEYBIND_KEYBOARD, SDLK_TAB, 0); - input_setKeybind("target_nearest", KEYBIND_KEYBOARD, SDLK_t, 0); - input_setKeybind("target_hostile", KEYBIND_KEYBOARD, SDLK_r, 0); + 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); + /* 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); /* Combat. */ - input_setKeybind("primary", KEYBIND_KEYBOARD, SDLK_SPACE, 0); - input_setKeybind("face", KEYBIND_KEYBOARD, SDLK_f, 0); - input_setKeybind("board", KEYBIND_KEYBOARD, SDLK_b, 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, 0); - input_setKeybind("secondary_next", KEYBIND_KEYBOARD, SDLK_e, 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, 0); - input_setKeybind("target_planet", KEYBIND_KEYBOARD, SDLK_p, 0); - input_setKeybind("land", KEYBIND_KEYBOARD, SDLK_l, 0); - input_setKeybind("thyperspace", KEYBIND_KEYBOARD, SDLK_h, 0); - input_setKeybind("starmap", KEYBIND_KEYBOARD, SDLK_m, 0); - input_setKeybind("jump", KEYBIND_KEYBOARD, SDLK_j, 0); + input_setKeybind("autonav", KEYBIND_KEYBOARD, SDLK_n, KMOD_NONE, 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, 0); - input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_KP_MINUS, 0); - input_setKeybind("screenshot", KEYBIND_KEYBOARD, SDLK_KP_MULTIPLY, 0); - input_setKeybind("pause", KEYBIND_KEYBOARD, SDLK_F1, 0); - input_setKeybind("menu", KEYBIND_KEYBOARD, SDLK_ESCAPE, 0); - input_setKeybind("info", KEYBIND_KEYBOARD, SDLK_i, 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); } /** @@ -134,14 +136,18 @@ void input_exit(void) { * @param keybind The name of the keybind defined above. * @param type The type of the keybind. * @param key The key to bind to. + * @param mod Modifiers to check for. * @param reverse Whether to reverse it or not. */ -void input_setKeybind(char* keybind, KeybindType type, int key, int reverse) { +void input_setKeybind(char* keybind, KeybindType type, int key, + SDLMod mod, int reverse) { int i; for(i = 0; strcmp(keybindNames[i], "end"); i++) if(strcmp(keybind, input_keybinds[i]->name)==0) { input_keybinds[i]->type = type; input_keybinds[i]->key = key; + /* Non-keyboards get mod KMOD_ALL to always match. */ + input_keybinds[i]->mod = (type == KEYBIND_KEYBOARD) ? mod : KMOD_ALL; input_keybinds[i]->reverse = reverse ? -1. : 1.; return; } @@ -152,11 +158,12 @@ void input_setKeybind(char* keybind, KeybindType type, int key, int reverse) { * * @brief Get the value of a keybind. */ -int input_getKeybind(char* keybind, KeybindType* type, int* reverse) { +int input_getKeybind(char* keybind, KeybindType* type, SDLMod* mod, int* reverse) { int i; for(i = 0; strcmp(keybindNames[i], "end"); i++) if(strcmp(keybind, input_keybinds[i]->name)==0) { if(type != NULL) (*type) = input_keybinds[i]->type; + if(mod != NULL) (*mod) = input_keybinds[i]->mod; if(reverse != NULL) (*reverse) = input_keybinds[i]->reverse; return input_keybinds[i]->key; } @@ -173,7 +180,10 @@ int input_getKeybind(char* keybind, KeybindType* type, 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) (strcmp(input_keybinds[keynum]->name, s)==0) /**< Shortcut for ease. */ +#define KEY(s) (((input_keybinds[keynum]->mod & mod) || \ + (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() \ (player && !pilot_isFlag(player, PILOT_HYP_PREP) && \ @@ -181,6 +191,9 @@ int input_getKeybind(char* keybind, KeybindType* type, int* reverse) { !pilot_isFlag(player, PILOT_HYPERSPACE)) /**< Make sure player isn't jumping. */ static void input_key(int keynum, double value, int kabs) { unsigned int t; + SDLMod mod; + + mod = SDL_GetModState(); /* Yes, we always get it just in case. */ /* Accelerating. */ if(KEY("accel")) { diff --git a/src/input.h b/src/input.h index 4652083..96a3d0c 100644 --- a/src/input.h +++ b/src/input.h @@ -1,6 +1,8 @@ #pragma once #include +#define KMOD_ALL 0xffff /**< Comfort thing SDL is lacking. */ + /* Input types. */ typedef enum { KEYBIND_NULL, @@ -11,8 +13,8 @@ typedef enum { /* Set input. */ void input_setDefault(void); -void input_setKeybind(char* keybind, KeybindType type, int key, int reverse); -int input_getKeybind(char* keybind, KeybindType* type, int* reverse); +void input_setKeybind(char* keybind, KeybindType type, int key, SDLMod mod, int reverse); +int input_getKeybind(char* keybind, KeybindType* type, SDLMod* mod, int* reverse); /* Handle the events. */ void input_handle(SDL_Event* event);