[Change] Cleaned up input stuff.

This commit is contained in:
Allanis 2013-12-28 22:49:29 +00:00
parent 7db6ad52f9
commit 89b297887c

View File

@ -21,7 +21,7 @@
typedef struct Keybind_ { typedef struct Keybind_ {
char* name; /**< Keybinding name, taken from keybindNames[] */ char* name; /**< Keybinding name, taken from keybindNames[] */
KeybindType type; /**< type, defined in player.h. */ KeybindType type; /**< type, defined in player.h. */
unsigned int key; /**< Key/axis/button event number. */ SDLKey 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). */ SDLMod mod; /**< Key modifiers (where applicable). */
} Keybind; } Keybind;
@ -181,9 +181,7 @@ int input_getKeybind(char* keybind, KeybindType* type, SDLMod* mod, 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) (((input_keybinds[keynum]->mod == mod) || \ #define KEY(s) (strcmp(input_keybinds[keynum]->name, s)==0) /**< Shortcut for ease. */
(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) && \
@ -191,10 +189,6 @@ int input_getKeybind(char* keybind, KeybindType* type, SDLMod* mod, 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. */
mod &= ~(KMOD_CAPS | KMOD_NUM | KMOD_MODE); /* We want to ignore "global" modifiers. */
/* Accelerating. */ /* Accelerating. */
if(KEY("accel")) { if(KEY("accel")) {
@ -378,10 +372,8 @@ static void input_key(int keynum, double value, int kabs) {
/* --Events-- */ /* --Events-- */
static void input_joyaxis(const unsigned int axis, const int value); static void input_joyaxis(const unsigned int axis, const int value);
static void input_joydown(const unsigned int button); static void input_joyevent(int event, const unsigned int button);
static void input_joyup(const unsigned int button); static void input_keyevent(int event, SDLKey key, SDLMod mod);
static void input_keydown(SDLKey key);
static void input_keyup(SDLKey key);
/* Joystick. */ /* Joystick. */
@ -394,42 +386,28 @@ static void input_joyaxis(const unsigned int axis, const int value) {
input_key(i, -(input_keybinds[i]->reverse) * (double)value / 32767., 1); input_key(i, -(input_keybinds[i]->reverse) * (double)value / 32767., 1);
} }
/* Joystick button down. */ /* Joystick event. */
static void input_joydown(const unsigned int button) { static void input_joyevent(int event, const unsigned int button) {
int i; int i;
for(i = 0; strcmp(keybindNames[i], "end");i++) for(i = 0; strcmp(keybindNames[i], "end");i++)
if(input_keybinds[i]->type == KEYBIND_JBUTTON && if(input_keybinds[i]->type == KEYBIND_JBUTTON &&
input_keybinds[i]->key == button) input_keybinds[i]->key == button)
input_key(i, KEY_RELEASE, 0); input_key(i, event, 0);
}
/* Joystick button up. */
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_key(i, KEY_RELEASE, 0);
} }
/* Keyboard. */ /* Keyboard. */
/* Key down. */ /* Key event. */
static void input_keydown(SDLKey key) { static void input_keyevent(int event, SDLKey key, SDLMod mod) {
int i; int i;
for(i = 0; strcmp(keybindNames[i], "end"); i++)
if(input_keybinds[i]->type == KEYBIND_KEYBOARD &&
input_keybinds[i]->key == key)
input_key(i, KEY_PRESS, 0);
}
/* Key up. */ mod &= (KMOD_CAPS | KMOD_NUM | KMOD_MODE); /* We want to ignore "global" modifiers. */
static void input_keyup(SDLKey key) {
int i;
for(i = 0; strcmp(keybindNames[i], "end"); i++) for(i = 0; strcmp(keybindNames[i], "end"); i++)
if(input_keybinds[i]->type == KEYBIND_KEYBOARD && if((input_keybinds[i]->type == KEYBIND_KEYBOARD) &&
input_keybinds[i]->key == key) (input_keybinds[i]->key == key) &&
input_key(i, KEY_RELEASE, 0); ((input_keybinds[i]->mod == mod) || (input_keybinds[i]->mod == KMOD_ALL)))
input_key(i, event, 0);
} }
@ -462,16 +440,16 @@ void input_handle(SDL_Event* event) {
input_joyaxis(event->jaxis.axis, event->jaxis.value); input_joyaxis(event->jaxis.axis, event->jaxis.value);
break; break;
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
input_joydown(event->jbutton.button); input_joyevent(KEY_PRESS, event->jbutton.button);
break; break;
case SDL_JOYBUTTONUP: case SDL_JOYBUTTONUP:
input_joyup(event->jbutton.button); input_joyevent(KEY_RELEASE, event->jbutton.button);
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
input_keydown(event->key.keysym.sym); input_keyevent(KEY_PRESS, event->key.keysym.sym, event->key.keysym.mod);
break; break;
case SDL_KEYUP: case SDL_KEYUP:
input_keyup(event->key.keysym.sym); input_keyevent(KEY_RELEASE, event->key.keysym.sym, event->key.keysym.mod);
break; break;
} }
} }