[Add] Added support for key modifiers.

This commit is contained in:
Allanis 2013-12-27 13:30:20 +00:00
parent b9aae94354
commit 83ac271657
3 changed files with 47 additions and 32 deletions

View File

@ -122,7 +122,7 @@ int conf_loadConfig(const char* file) {
conf_loadBool("aa_polygon", i); conf_loadBool("aa_polygon", i);
if(i) { gl_screen.flags |= OPENGL_AA_POLYGON; i = 0; } if(i) { gl_screen.flags |= OPENGL_AA_POLYGON; i = 0; }
conf_loadBool("vsync", i); conf_loadBool("vsync", i);
if(i) { gl_screen..flags |= OPENG_VSYNC; i = 0; } if(i) { gl_screen.flags |= OPENGL_VSYNC; i = 0; }
/* FPS. */ /* FPS. */
conf_loadBool("showfps", show_fps); conf_loadBool("showfps", show_fps);
conf_loadInt("maxfps", max_fps); conf_loadInt("maxfps", max_fps);
@ -187,7 +187,7 @@ int conf_loadConfig(const char* file) {
continue; continue;
} }
/* Set the keybind. */ /* Set the keybind. */
input_setKeybind((char*)keybindNames[i], type, key, reverse); input_setKeybind((char*)keybindNames[i], type, key, KMOD_ALL, reverse);
} else } else
WARN("Malformed keybind in %s", file); WARN("Malformed keybind in %s", file);

View File

@ -23,6 +23,7 @@ typedef struct Keybind_ {
KeybindType type; /**< type, defined in player.h. */ KeybindType type; /**< type, defined in player.h. */
unsigned int key; /**< Key/axis/button event number. */ unsigned int key; /**< Key/axis/button event number. */
double reverse; /**< 1. if normal, -1 if reversed, only useful for joystick axis. */ double reverse; /**< 1. if normal, -1 if reversed, only useful for joystick axis. */
SDLMod mod; /**< Key modifiers (where applicable). */
} Keybind; } Keybind;
static Keybind** input_keybinds; /**< Contains the players keybindings. */ static Keybind** input_keybinds; /**< Contains the players keybindings. */
@ -61,36 +62,37 @@ extern unsigned int player_target;
*/ */
void input_setDefault(void) { void input_setDefault(void) {
/* Movement. */ /* Movement. */
input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, 0); input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, KMOD_NONE, 0);
input_setKeybind("afterburn", KEYBIND_KEYBOARD, SDLK_UNKNOWN, 0); /* Not set. */ input_setKeybind("afterburn", KEYBIND_KEYBOARD, SDLK_UNKNOWN, KMOD_NONE, 0); /*Not set.*/
input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, 0); input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, KMOD_NONE, 0);
input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, 0); input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, KMOD_NONE, 0);
input_setKeybind("reverse", KEYBIND_KEYBOARD, SDLK_s, 0); input_setKeybind("reverse", KEYBIND_KEYBOARD, SDLK_s, KMOD_NONE, 0);
input_setKeybind("target", KEYBIND_KEYBOARD, SDLK_TAB, 0); /* Targetting. */
input_setKeybind("target_nearest", KEYBIND_KEYBOARD, SDLK_t, 0); input_setKeybind("target", KEYBIND_KEYBOARD, SDLK_TAB, KMOD_NONE, 0);
input_setKeybind("target_hostile", KEYBIND_KEYBOARD, SDLK_r, 0); input_setKeybind("target_nearest", KEYBIND_KEYBOARD, SDLK_t, KMOD_NONE, 0);
input_setKeybind("target_hostile", KEYBIND_KEYBOARD, SDLK_r, KMOD_NONE, 0);
/* Combat. */ /* Combat. */
input_setKeybind("primary", KEYBIND_KEYBOARD, SDLK_SPACE, 0); input_setKeybind("primary", KEYBIND_KEYBOARD, SDLK_SPACE, KMOD_NONE, 0);
input_setKeybind("face", KEYBIND_KEYBOARD, SDLK_f, 0); input_setKeybind("face", KEYBIND_KEYBOARD, SDLK_f, KMOD_NONE, 0);
input_setKeybind("board", KEYBIND_KEYBOARD, SDLK_b, 0); input_setKeybind("board", KEYBIND_KEYBOARD, SDLK_b, KMOD_NONE, 0);
/* Secondary weapon. */ /* Secondary weapon. */
input_setKeybind("secondary", KEYBIND_KEYBOARD, SDLK_LSHIFT, 0); input_setKeybind("secondary", KEYBIND_KEYBOARD, SDLK_LSHIFT, KMOD_NONE, 0);
input_setKeybind("secondary_next", KEYBIND_KEYBOARD, SDLK_e, 0); input_setKeybind("secondary_next", KEYBIND_KEYBOARD, SDLK_e, KMOD_NONE, 0);
/* Space */ /* Space */
input_setKeybind("autonav", KEYBIND_KEYBOARD, SDLK_n, 0); input_setKeybind("autonav", KEYBIND_KEYBOARD, SDLK_n, KMOD_NONE, 0);
input_setKeybind("target_planet", KEYBIND_KEYBOARD, SDLK_p, 0); input_setKeybind("target_planet", KEYBIND_KEYBOARD, SDLK_p, KMOD_NONE, 0);
input_setKeybind("land", KEYBIND_KEYBOARD, SDLK_l, 0); input_setKeybind("land", KEYBIND_KEYBOARD, SDLK_l, KMOD_NONE, 0);
input_setKeybind("thyperspace", KEYBIND_KEYBOARD, SDLK_h, 0); input_setKeybind("thyperspace", KEYBIND_KEYBOARD, SDLK_h, KMOD_NONE, 0);
input_setKeybind("starmap", KEYBIND_KEYBOARD, SDLK_m, 0); input_setKeybind("starmap", KEYBIND_KEYBOARD, SDLK_m, KMOD_NONE, 0);
input_setKeybind("jump", KEYBIND_KEYBOARD, SDLK_j, 0); input_setKeybind("jump", KEYBIND_KEYBOARD, SDLK_j, KMOD_NONE, 0);
/* Misc. */ /* Misc. */
input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_KP_PLUS, 0); input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_KP_PLUS, KMOD_NONE, 0);
input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_KP_MINUS, 0); input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_KP_MINUS, KMOD_NONE, 0);
input_setKeybind("screenshot", KEYBIND_KEYBOARD, SDLK_KP_MULTIPLY, 0); input_setKeybind("screenshot", KEYBIND_KEYBOARD, SDLK_KP_MULTIPLY, KMOD_NONE, 0);
input_setKeybind("pause", KEYBIND_KEYBOARD, SDLK_F1, 0); input_setKeybind("pause", KEYBIND_KEYBOARD, SDLK_F1, KMOD_NONE, 0);
input_setKeybind("menu", KEYBIND_KEYBOARD, SDLK_ESCAPE, 0); input_setKeybind("menu", KEYBIND_KEYBOARD, SDLK_ESCAPE, KMOD_NONE, 0);
input_setKeybind("info", KEYBIND_KEYBOARD, SDLK_i, 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 keybind The name of the keybind defined above.
* @param type The type of the keybind. * @param type The type of the keybind.
* @param key The key to bind to. * @param key The key to bind to.
* @param mod Modifiers to check for.
* @param reverse Whether to reverse it or not. * @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; int i;
for(i = 0; strcmp(keybindNames[i], "end"); i++) for(i = 0; strcmp(keybindNames[i], "end"); i++)
if(strcmp(keybind, input_keybinds[i]->name)==0) { if(strcmp(keybind, input_keybinds[i]->name)==0) {
input_keybinds[i]->type = type; input_keybinds[i]->type = type;
input_keybinds[i]->key = key; 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.; input_keybinds[i]->reverse = reverse ? -1. : 1.;
return; return;
} }
@ -152,11 +158,12 @@ void input_setKeybind(char* keybind, KeybindType type, int key, int reverse) {
* *
* @brief Get the value of a keybind. * @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; int i;
for(i = 0; strcmp(keybindNames[i], "end"); i++) for(i = 0; strcmp(keybindNames[i], "end"); i++)
if(strcmp(keybind, input_keybinds[i]->name)==0) { if(strcmp(keybind, input_keybinds[i]->name)==0) {
if(type != NULL) (*type) = input_keybinds[i]->type; if(type != NULL) (*type) = input_keybinds[i]->type;
if(mod != NULL) (*mod) = input_keybinds[i]->mod;
if(reverse != NULL) (*reverse) = input_keybinds[i]->reverse; if(reverse != NULL) (*reverse) = input_keybinds[i]->reverse;
return input_keybinds[i]->key; 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 value The value of the keypress (defined above).
* @param abs Whether or not it's an absolute value (for the joystick). * @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 INGAME() (!toolkit) /**< Make sure player is in game. */
#define NOHYP() \ #define NOHYP() \
(player && !pilot_isFlag(player, PILOT_HYP_PREP) && \ (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. */ !pilot_isFlag(player, PILOT_HYPERSPACE)) /**< Make sure player isn't jumping. */
static void input_key(int keynum, double value, int kabs) { static void input_key(int keynum, double value, int kabs) {
unsigned int t; unsigned int t;
SDLMod mod;
mod = SDL_GetModState(); /* Yes, we always get it just in case. */
/* Accelerating. */ /* Accelerating. */
if(KEY("accel")) { if(KEY("accel")) {

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#include <SDL/SDL.h> #include <SDL/SDL.h>
#define KMOD_ALL 0xffff /**< Comfort thing SDL is lacking. */
/* Input types. */ /* Input types. */
typedef enum { typedef enum {
KEYBIND_NULL, KEYBIND_NULL,
@ -11,8 +13,8 @@ typedef enum {
/* Set input. */ /* Set input. */
void input_setDefault(void); void input_setDefault(void);
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 input_getKeybind(char* keybind, KeybindType* type, int* reverse); int input_getKeybind(char* keybind, KeybindType* type, SDLMod* mod, int* reverse);
/* Handle the events. */ /* Handle the events. */
void input_handle(SDL_Event* event); void input_handle(SDL_Event* event);