Better mod key logic in input and mouse motion check.
This commit is contained in:
parent
821cac2fbd
commit
123ae90f59
63
src/input.c
63
src/input.c
|
@ -28,14 +28,22 @@ input_init(Input *input)
|
||||||
input->lastMouseButtonState = 0;
|
input->lastMouseButtonState = 0;
|
||||||
input->mouseX = 0;
|
input->mouseX = 0;
|
||||||
input->mouseY = 0;
|
input->mouseY = 0;
|
||||||
|
input->lastMouseX = 0;
|
||||||
|
input->lastMouseY = 0;
|
||||||
|
input->modKeyState = 0;
|
||||||
|
input->lastModKeyState = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void input_reset(Input *input)
|
void input_reset(Input *input)
|
||||||
{
|
{
|
||||||
input->lastKeyState = input->keyState;
|
input->lastKeyState = input->keyState;
|
||||||
input->lastMouseButtonState = input->mouseButtonState;
|
input->lastMouseButtonState = input->mouseButtonState;
|
||||||
|
input->lastModKeyState = input->modKeyState;
|
||||||
input->keyState = 0;
|
input->keyState = 0;
|
||||||
input->mouseButtonState = 0;
|
input->mouseButtonState = 0;
|
||||||
|
input->modKeyState = 0;
|
||||||
|
input->lastMouseX = input->mouseX;
|
||||||
|
input->lastMouseY = input->mouseY;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Uint64
|
static Uint64
|
||||||
|
@ -48,9 +56,8 @@ get_event_key(SDL_Event *event)
|
||||||
return KEY_UP;
|
return KEY_UP;
|
||||||
case SDLK_DOWN:
|
case SDLK_DOWN:
|
||||||
case SDLK_j:
|
case SDLK_j:
|
||||||
return KEY_DOWN;
|
|
||||||
case SDLK_s:
|
case SDLK_s:
|
||||||
return KEY_DOWN | KEY_S;
|
return KEY_DOWN;
|
||||||
case SDLK_LEFT:
|
case SDLK_LEFT:
|
||||||
case SDLK_h:
|
case SDLK_h:
|
||||||
case SDLK_a:
|
case SDLK_a:
|
||||||
|
@ -83,19 +90,27 @@ get_event_key(SDL_Event *event)
|
||||||
return KEY_ESC;
|
return KEY_ESC;
|
||||||
case SDLK_RETURN:
|
case SDLK_RETURN:
|
||||||
return KEY_ENTER;
|
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:
|
default:
|
||||||
return 0;
|
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
|
static Uint32
|
||||||
get_event_mousebutton(SDL_Event *event)
|
get_event_mousebutton(SDL_Event *event)
|
||||||
{
|
{
|
||||||
|
@ -114,10 +129,20 @@ get_event_mousebutton(SDL_Event *event)
|
||||||
void
|
void
|
||||||
input_handle_event(Input *input, SDL_Event *event)
|
input_handle_event(Input *input, SDL_Event *event)
|
||||||
{
|
{
|
||||||
if (event->type == SDL_KEYDOWN)
|
if (event->type == SDL_KEYDOWN) {
|
||||||
|
Uint32 key;
|
||||||
|
if ((key = get_event_modkey(event)))
|
||||||
|
input->modKeyState |= key;
|
||||||
|
else
|
||||||
input->keyState |= get_event_key(event);
|
input->keyState |= get_event_key(event);
|
||||||
else if (event->type == SDL_KEYUP)
|
}
|
||||||
|
else if (event->type == SDL_KEYUP) {
|
||||||
|
Uint32 key;
|
||||||
|
if ((key = get_event_modkey(event)))
|
||||||
|
input->modKeyState &= ~key;
|
||||||
|
else
|
||||||
input->keyState &= ~get_event_key(event);
|
input->keyState &= ~get_event_key(event);
|
||||||
|
}
|
||||||
else if (event->type == SDL_MOUSEBUTTONDOWN)
|
else if (event->type == SDL_MOUSEBUTTONDOWN)
|
||||||
input->mouseButtonState |= get_event_mousebutton(event);
|
input->mouseButtonState |= get_event_mousebutton(event);
|
||||||
else if (event->type == SDL_MOUSEBUTTONUP)
|
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);
|
return (input->keyState & key) && !(input->lastKeyState & key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
input_modkey_is_pressed(Input *input, Uint32 key)
|
||||||
|
{
|
||||||
|
return (input->modKeyState & key)
|
||||||
|
&& !(input->lastModKeyState & key);
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
input_key_is_released(Input *input, Uint64 key)
|
input_key_is_released(Input *input, Uint64 key)
|
||||||
{
|
{
|
||||||
|
@ -152,3 +184,10 @@ input_mousebutton_is_pressed(Input *input, Uint8 button)
|
||||||
return (input->mouseButtonState & button)
|
return (input->mouseButtonState & button)
|
||||||
&& !(input->lastMouseButtonState & button);
|
&& !(input->lastMouseButtonState & button);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
input_mouse_moved(Input *input)
|
||||||
|
{
|
||||||
|
return (input->mouseX != input->lastMouseX)
|
||||||
|
|| (input->mouseY != input->lastMouseY);
|
||||||
|
}
|
||||||
|
|
17
src/input.h
17
src/input.h
|
@ -38,10 +38,9 @@
|
||||||
#define KEY_NUM9 8192
|
#define KEY_NUM9 8192
|
||||||
#define KEY_ESC 16384
|
#define KEY_ESC 16384
|
||||||
#define KEY_ENTER 32768
|
#define KEY_ENTER 32768
|
||||||
#define KEY_CTRL 65536
|
|
||||||
#define KEY_ALT 131072
|
#define KEY_CTRL_M 1
|
||||||
#define KEY_M 262144
|
#define KEY_CTRL_S 2
|
||||||
#define KEY_S 524288
|
|
||||||
|
|
||||||
#define MBUTTON_LEFT 1
|
#define MBUTTON_LEFT 1
|
||||||
#define MBUTTON_MIDDLE 2
|
#define MBUTTON_MIDDLE 2
|
||||||
|
@ -52,6 +51,10 @@ typedef struct Input {
|
||||||
Uint64 lastKeyState;
|
Uint64 lastKeyState;
|
||||||
Uint32 mouseButtonState;
|
Uint32 mouseButtonState;
|
||||||
Uint32 lastMouseButtonState;
|
Uint32 lastMouseButtonState;
|
||||||
|
Uint32 modKeyState;
|
||||||
|
Uint32 lastModKeyState;
|
||||||
|
Uint32 lastMouseX;
|
||||||
|
Uint32 lastMouseY;
|
||||||
Uint32 mouseX;
|
Uint32 mouseX;
|
||||||
Uint32 mouseY;
|
Uint32 mouseY;
|
||||||
} Input;
|
} Input;
|
||||||
|
@ -74,8 +77,14 @@ input_key_is_released(Input *, Uint64 key);
|
||||||
bool
|
bool
|
||||||
input_key_is_down(Input *, Uint64 key);
|
input_key_is_down(Input *, Uint64 key);
|
||||||
|
|
||||||
|
bool
|
||||||
|
input_modkey_is_pressed(Input *, Uint32 key);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
input_mousebutton_is_pressed(Input *, Uint8 button);
|
input_mousebutton_is_pressed(Input *, Uint8 button);
|
||||||
|
|
||||||
|
bool
|
||||||
|
input_mouse_moved(Input*);
|
||||||
|
|
||||||
#endif // INPUT_H_
|
#endif // INPUT_H_
|
||||||
|
|
||||||
|
|
|
@ -373,8 +373,7 @@ handle_main_input(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input_key_is_down(&input, KEY_CTRL)
|
if (input_modkey_is_pressed(&input, KEY_CTRL_M)) {
|
||||||
&& input_key_is_pressed(&input, SDLK_m)) {
|
|
||||||
if (mixer_toggle_music(&gGameState))
|
if (mixer_toggle_music(&gGameState))
|
||||||
gui_log("Music enabled");
|
gui_log("Music enabled");
|
||||||
else
|
else
|
||||||
|
@ -382,8 +381,7 @@ handle_main_input(void)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input_key_is_down(&input, KEY_CTRL)
|
if (input_modkey_is_pressed(&input, KEY_CTRL_S)) {
|
||||||
&& input_key_is_pressed(&input, SDLK_s)) {
|
|
||||||
if (mixer_toggle_sound())
|
if (mixer_toggle_sound())
|
||||||
gui_log("Sound enabled");
|
gui_log("Sound enabled");
|
||||||
else
|
else
|
||||||
|
|
|
@ -109,6 +109,27 @@ test_mousebuttons(void **state)
|
||||||
assert_true(input_mousebutton_is_pressed(&input, MBUTTON_RIGHT));
|
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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
(void) argc;
|
(void) argc;
|
||||||
|
@ -120,6 +141,7 @@ int main(int argc, char *argv[])
|
||||||
cmocka_unit_test(test_keydown),
|
cmocka_unit_test(test_keydown),
|
||||||
cmocka_unit_test(test_event_parse),
|
cmocka_unit_test(test_event_parse),
|
||||||
cmocka_unit_test(test_mousebuttons),
|
cmocka_unit_test(test_mousebuttons),
|
||||||
|
cmocka_unit_test(test_mouse_moved),
|
||||||
};
|
};
|
||||||
|
|
||||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||||
|
|
Loading…
Reference in New Issue