Better mod key logic in input and mouse motion check.

This commit is contained in:
Linus Probert 2018-05-20 16:53:16 +02:00
parent 821cac2fbd
commit 123ae90f59
4 changed files with 90 additions and 22 deletions

View File

@ -28,14 +28,22 @@ input_init(Input *input)
input->lastMouseButtonState = 0;
input->mouseX = 0;
input->mouseY = 0;
input->lastMouseX = 0;
input->lastMouseY = 0;
input->modKeyState = 0;
input->lastModKeyState = 0;
}
void input_reset(Input *input)
{
input->lastKeyState = input->keyState;
input->lastMouseButtonState = input->mouseButtonState;
input->lastModKeyState = input->modKeyState;
input->keyState = 0;
input->mouseButtonState = 0;
input->modKeyState = 0;
input->lastMouseX = input->mouseX;
input->lastMouseY = input->mouseY;
}
static Uint64
@ -48,9 +56,8 @@ get_event_key(SDL_Event *event)
return KEY_UP;
case SDLK_DOWN:
case SDLK_j:
return KEY_DOWN;
case SDLK_s:
return KEY_DOWN | KEY_S;
return KEY_DOWN;
case SDLK_LEFT:
case SDLK_h:
case SDLK_a:
@ -83,19 +90,27 @@ get_event_key(SDL_Event *event)
return KEY_ESC;
case SDLK_RETURN:
return KEY_ENTER;
case SDLK_LALT:
case SDLK_RALT:
return KEY_ALT;
case SDLK_LCTRL:
case SDLK_RCTRL:
return KEY_CTRL;
case SDLK_m:
return KEY_M;
default:
return 0;
}
}
static Uint32
get_event_modkey(SDL_Event *event)
{
if (event->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
switch (event->key.keysym.sym) {
case SDLK_s:
return KEY_CTRL_S;
case SDLK_m:
return KEY_CTRL_M;
default:
return 0;
}
}
return 0;
}
static Uint32
get_event_mousebutton(SDL_Event *event)
{
@ -114,10 +129,20 @@ get_event_mousebutton(SDL_Event *event)
void
input_handle_event(Input *input, SDL_Event *event)
{
if (event->type == SDL_KEYDOWN)
input->keyState |= get_event_key(event);
else if (event->type == SDL_KEYUP)
input->keyState &= ~get_event_key(event);
if (event->type == SDL_KEYDOWN) {
Uint32 key;
if ((key = get_event_modkey(event)))
input->modKeyState |= key;
else
input->keyState |= get_event_key(event);
}
else if (event->type == SDL_KEYUP) {
Uint32 key;
if ((key = get_event_modkey(event)))
input->modKeyState &= ~key;
else
input->keyState &= ~get_event_key(event);
}
else if (event->type == SDL_MOUSEBUTTONDOWN)
input->mouseButtonState |= get_event_mousebutton(event);
else if (event->type == SDL_MOUSEBUTTONUP)
@ -134,6 +159,13 @@ input_key_is_pressed(Input *input, Uint64 key)
return (input->keyState & key) && !(input->lastKeyState & key);
}
bool
input_modkey_is_pressed(Input *input, Uint32 key)
{
return (input->modKeyState & key)
&& !(input->lastModKeyState & key);
}
bool
input_key_is_released(Input *input, Uint64 key)
{
@ -152,3 +184,10 @@ input_mousebutton_is_pressed(Input *input, Uint8 button)
return (input->mouseButtonState & button)
&& !(input->lastMouseButtonState & button);
}
bool
input_mouse_moved(Input *input)
{
return (input->mouseX != input->lastMouseX)
|| (input->mouseY != input->lastMouseY);
}

View File

@ -38,10 +38,9 @@
#define KEY_NUM9 8192
#define KEY_ESC 16384
#define KEY_ENTER 32768
#define KEY_CTRL 65536
#define KEY_ALT 131072
#define KEY_M 262144
#define KEY_S 524288
#define KEY_CTRL_M 1
#define KEY_CTRL_S 2
#define MBUTTON_LEFT 1
#define MBUTTON_MIDDLE 2
@ -52,6 +51,10 @@ typedef struct Input {
Uint64 lastKeyState;
Uint32 mouseButtonState;
Uint32 lastMouseButtonState;
Uint32 modKeyState;
Uint32 lastModKeyState;
Uint32 lastMouseX;
Uint32 lastMouseY;
Uint32 mouseX;
Uint32 mouseY;
} Input;
@ -74,8 +77,14 @@ input_key_is_released(Input *, Uint64 key);
bool
input_key_is_down(Input *, Uint64 key);
bool
input_modkey_is_pressed(Input *, Uint32 key);
bool
input_mousebutton_is_pressed(Input *, Uint8 button);
bool
input_mouse_moved(Input*);
#endif // INPUT_H_

View File

@ -373,8 +373,7 @@ handle_main_input(void)
}
}
if (input_key_is_down(&input, KEY_CTRL)
&& input_key_is_pressed(&input, SDLK_m)) {
if (input_modkey_is_pressed(&input, KEY_CTRL_M)) {
if (mixer_toggle_music(&gGameState))
gui_log("Music enabled");
else
@ -382,8 +381,7 @@ handle_main_input(void)
return true;
}
if (input_key_is_down(&input, KEY_CTRL)
&& input_key_is_pressed(&input, SDLK_s)) {
if (input_modkey_is_pressed(&input, KEY_CTRL_S)) {
if (mixer_toggle_sound())
gui_log("Sound enabled");
else

View File

@ -109,6 +109,27 @@ test_mousebuttons(void **state)
assert_true(input_mousebutton_is_pressed(&input, MBUTTON_RIGHT));
}
static void
test_mouse_moved(void **state)
{
(void) state;
Input input;
input_init(&input);
input.mouseX = 30;
input.mouseY = 30;
input.lastMouseX = 30;
input.lastMouseY = 30;
assert_true(!input_mouse_moved(&input));
input.lastMouseX += 30;
input.lastMouseY += 30;
assert_true(input_mouse_moved(&input));
}
int main(int argc, char *argv[])
{
(void) argc;
@ -120,6 +141,7 @@ int main(int argc, char *argv[])
cmocka_unit_test(test_keydown),
cmocka_unit_test(test_event_parse),
cmocka_unit_test(test_mousebuttons),
cmocka_unit_test(test_mouse_moved),
};
return cmocka_run_group_tests(tests, NULL, NULL);