This commit is contained in:
Romain Vimont 2021-12-28 23:19:01 +01:00
parent ac32280a43
commit 05ef36545c
2 changed files with 46 additions and 9 deletions

View file

@ -1,8 +1,8 @@
#include <input_events.h>
static inline uint16_t
sc_mod_from_sdl(uint16_t mod) {
return mod;
sc_mods_state_from_sdl(uint16_t mods_state) {
return mods_state;
}
static inline enum sc_keycode
@ -33,6 +33,22 @@ sc_action_from_sdl_mousebutton_type(uint32_t type) {
return SC_ACTION_UP;
}
static inline enum sc_mouse_button
sc_mouse_button_from_sdl(uint8_t button) {
if (button >= SDL_BUTTON_LEFT && button <= SDL_BUTTON_X2) {
// SC_MOUSE_BUTTON_* constants are initialized from SDL_BUTTON(index)
return SDL_BUTTON(button);
}
return SC_MOUSE_BUTTON_UNKNOWN;
}
static inline uint8_t
sc_mouse_buttons_state_from_sdl(uint32_t buttons_state) {
assert(buttons_state < 0x100); // fits in uint8_t
return buttons_state;
}
void
sc_key_event_from_sdl(struct sc_key_event *event,
const SDL_KeyboardEvent *sdl) {
@ -40,7 +56,7 @@ sc_key_event_from_sdl(struct sc_key_event *event,
event->keycode = sc_keycode_from_sdl(sdl->keysym.sym);
event->scancode = sc_scancode_from_sdl(sdl->keysym.scancode);
event->repeat = sdl->repeat;
event->mod = sc_mod_from_sdl(sdl->keysym.mod);
event->mods_state = sc_mods_state_from_sdl(sdl->keysym.mod);
}
void
@ -48,3 +64,10 @@ sc_text_event_from_sdl(struct sc_text_event *event,
const SDL_TextInputEvent *sdl) {
event->text = sdl->text;
}
void
sc_mouse_click_event_from_sdl(struct sc_mouse_click_event *event,
const struct SDL_MouseButtonEvent *sdl) {
event->action = sc_action_from_sdl_mousebutton_type(sdl->type);
event->button = sc_mouse_button_from_sdl(sdl->button);
}

View file

@ -282,6 +282,17 @@ enum sc_scancode {
SC_SCANCODE_RGUI = SDL_SCANCODE_RGUI,
};
// On purpose, only use the "mask" values (1, 2, 4, 8, 16) for a single button,
// to avoid unnecessary conversions (and confusion).
enum sc_mouse_button {
SC_MOUSE_BUTTON_UNKNOWN = 0,
SC_MOUSE_BUTTON_LEFT = SDL_BUTTON(SDL_BUTTON_LEFT),
SC_MOUSE_BUTTON_RIGHT = SDL_BUTTON(SDL_BUTTON_RIGHT),
SC_MOUSE_BUTTON_MIDDLE = SDL_BUTTON(SDL_BUTTON_MIDDLE),
SC_MOUSE_BUTTON_X1 = SDL_BUTTON(SDL_BUTTON_X1),
SC_MOUSE_BUTTON_X2 = SDL_BUTTON(SDL_BUTTON_X2),
};
static_assert(sizeof(enum sc_mod) >= sizeof(SDL_Keymod),
"SDL_Keymod must be convertible to sc_mod");
@ -295,7 +306,7 @@ struct sc_key_event {
enum sc_action action;
enum sc_keycode keycode;
enum sc_scancode scancode;
uint16_t mod; // bitwise-OR of sc_mod values
uint16_t mods_state; // bitwise-OR of sc_mod values
uint8_t repeat;
};
@ -303,12 +314,10 @@ struct sc_text_event {
const char *text; // not owned
};
struct sc_touch_event {
};
struct sc_mouse_click_event {
enum sc_action action;
enum sc_mouse_button button;
struct sc_position position;
};
struct sc_mouse_wheel_event {
@ -316,9 +325,14 @@ struct sc_mouse_wheel_event {
};
struct sc_mouse_motion_event {
uint8_t buttons_state; // bitwise-OR of sc_mouse_button values
};
struct sc_touch_event {
};
void
sc_key_event_from_sdl(struct sc_key_event *event, const SDL_KeyboardEvent *sdl);