removed add_*_listener return types / added scroll input event type

pull/1/head
Sara 2023-04-20 22:57:10 +02:00
parent 85182ed22c
commit fb6b76f0cf
2 changed files with 41 additions and 14 deletions

View File

@ -14,8 +14,9 @@ input_listener_t* g_key_listeners_endptr = g_key_listeners;
int _last_mouse_x=0, _last_mouse_y=0; int _last_mouse_x=0, _last_mouse_y=0;
float _last_screen_mouse_x=0.0, _last_screen_mouse_y=0.0; float _last_screen_mouse_x=0.0, _last_screen_mouse_y=0.0;
uint32_t _mouse_left_seconds = 0; uint32_t _mouse_left_seconds = 0;
float _scroll_delta=0;
size_t add_listener_for(SDL_Scancode negative, SDL_Scancode positive, void add_key_listener(SDL_Scancode negative, SDL_Scancode positive,
input_axis_delegate_t delegate) { input_axis_delegate_t delegate) {
memset(g_key_listeners_endptr, 0x0, sizeof(input_listener_t)); memset(g_key_listeners_endptr, 0x0, sizeof(input_listener_t));
g_key_listeners_endptr->type = INPUT_LISTENER_AXIS; g_key_listeners_endptr->type = INPUT_LISTENER_AXIS;
@ -23,30 +24,30 @@ size_t add_listener_for(SDL_Scancode negative, SDL_Scancode positive,
g_key_listeners_endptr->axis.positive = positive; g_key_listeners_endptr->axis.positive = positive;
g_key_listeners_endptr->axis.negative = negative; g_key_listeners_endptr->axis.negative = negative;
g_key_listeners_endptr->axis.last = 0; g_key_listeners_endptr->axis.last = 0;
++g_key_listeners_endptr; ++g_key_listeners_endptr;
return (size_t)(g_key_listeners_endptr - g_key_listeners)-1;
} }
size_t add_mouse_listener(float min_move, input_mouse_delegate_t delegate) { void add_mouse_listener(input_mouse_delegate_t delegate) {
memset(g_key_listeners_endptr, 0x0, sizeof(input_listener_t)); memset(g_key_listeners_endptr, 0x0, sizeof(input_listener_t));
g_key_listeners_endptr->type = INPUT_LISTENER_MOUSE; g_key_listeners_endptr->type = INPUT_LISTENER_MOUSE;
g_key_listeners_endptr->mouse.delegate = delegate; g_key_listeners_endptr->mouse.delegate = delegate;
g_key_listeners_endptr->mouse.min_delta = min_move;
++g_key_listeners_endptr; ++g_key_listeners_endptr;
return (size_t)(g_key_listeners_endptr - g_key_listeners)-1;
} }
size_t add_mouse_button_listener(uint32_t button, input_button_delegate_t delegate) { void add_mouse_button_listener(uint32_t button, input_button_delegate_t delegate) {
memset(g_key_listeners_endptr, 0x0, sizeof(input_listener_t)); memset(g_key_listeners_endptr, 0x0, sizeof(input_listener_t));
g_key_listeners_endptr->type = INPUT_LISTENER_BUTTON; g_key_listeners_endptr->type = INPUT_LISTENER_BUTTON;
g_key_listeners_endptr->button.delegate = delegate; g_key_listeners_endptr->button.delegate = delegate;
g_key_listeners_endptr->button.button = button; g_key_listeners_endptr->button.button = button;
g_key_listeners_endptr->button.last = 0; g_key_listeners_endptr->button.last = 0;
++g_key_listeners_endptr; ++g_key_listeners_endptr;
return (size_t)(g_key_listeners_endptr - g_key_listeners)-1; }
void add_scroll_listener(input_scroll_delegate_t delegate) {
memset(g_key_listeners_endptr, 0x0, sizeof(input_listener_t));
g_key_listeners_endptr->type = INPUT_LISTENER_SCROLL;
g_key_listeners_endptr->scroll.delegate = delegate;
++g_key_listeners_endptr;
} }
void remove_listener_at(size_t index) { void remove_listener_at(size_t index) {
@ -99,6 +100,12 @@ void process_button_listener(input_listener_t* listener) {
listener->button.last = is_down; listener->button.last = is_down;
} }
void process_scroll_listener(input_listener_t* listener) {
if(_scroll_delta != 0) {
listener->scroll.delegate(_scroll_delta);
}
}
void update_input() { void update_input() {
float dx, dy; float dx, dy;
int px, py; int px, py;
@ -115,6 +122,9 @@ void update_input() {
case INPUT_LISTENER_MOUSE: case INPUT_LISTENER_MOUSE:
process_mouse_listener(listener, dx, dy); process_mouse_listener(listener, dx, dy);
break; break;
case INPUT_LISTENER_SCROLL:
process_scroll_listener(listener);
break;
case INPUT_LISTENER_BUTTON: case INPUT_LISTENER_BUTTON:
process_button_listener(listener); process_button_listener(listener);
break; break;
@ -124,6 +134,17 @@ void update_input() {
_last_mouse_x = px; _last_mouse_y = py; _last_mouse_x = px; _last_mouse_y = py;
_last_screen_mouse_x = (float)px / width; _last_screen_mouse_x = (float)px / width;
_last_screen_mouse_y = (float)py / width; _last_screen_mouse_y = (float)py / width;
_scroll_delta = 0;
}
void input_event(SDL_Event event) {
switch(event.type) {
default:
return;
case SDL_MOUSEWHEEL:
_scroll_delta = event.wheel.y;
break;
}
} }
int input_keydown(SDL_Scancode scancode) { int input_keydown(SDL_Scancode scancode) {

View File

@ -10,10 +10,12 @@ extern "C" {
typedef void(*input_axis_delegate_t)(int value); typedef void(*input_axis_delegate_t)(int value);
typedef void(*input_mouse_delegate_t)(float dx, float dy); typedef void(*input_mouse_delegate_t)(float dx, float dy);
typedef void(*input_button_delegate_t)(int down); typedef void(*input_button_delegate_t)(int down);
typedef void(*input_scroll_delegate_t)(float delta);
enum INPUT_LISTENER_TYPE_T { enum INPUT_LISTENER_TYPE_T {
INPUT_LISTENER_MOUSE, INPUT_LISTENER_MOUSE,
INPUT_LISTENER_AXIS, INPUT_LISTENER_AXIS,
INPUT_LISTENER_SCROLL,
INPUT_LISTENER_BUTTON, INPUT_LISTENER_BUTTON,
}; };
@ -27,28 +29,32 @@ typedef struct input_listener_t {
} axis; } axis;
struct { struct {
input_mouse_delegate_t delegate; input_mouse_delegate_t delegate;
float min_delta;
} mouse; } mouse;
struct { struct {
input_button_delegate_t delegate; input_button_delegate_t delegate;
uint32_t button; uint32_t button;
int last; int last;
} button; } button;
struct {
input_scroll_delegate_t delegate;
} scroll;
}; };
} input_listener_t; } input_listener_t;
extern const Uint8* g_key_states; extern const Uint8* g_key_states;
extern size_t add_listener_for(SDL_Scancode negative, SDL_Scancode positive, extern void add_key_listener(SDL_Scancode negative, SDL_Scancode positive,
input_axis_delegate_t delegate); input_axis_delegate_t delegate);
extern size_t add_mouse_listener(float min_move, input_mouse_delegate_t delegate); extern void add_mouse_listener(input_mouse_delegate_t delegate);
extern size_t add_mouse_button_listener(uint32_t button, input_button_delegate_t delegate); extern void add_mouse_button_listener(uint32_t button, input_button_delegate_t delegate);
extern void add_scroll_listener(input_scroll_delegate_t delegate);
extern void remove_listener_at(size_t index); extern void remove_listener_at(size_t index);
extern void mouse_screen_position(float* ox, float* oy); extern void mouse_screen_position(float* ox, float* oy);
extern void mouse_world_position(float* ox, float* oy); extern void mouse_world_position(float* ox, float* oy);
extern void input_init(); extern void input_init();
extern void update_input(); extern void update_input();
extern void input_event(SDL_Event event);
extern int input_keydown(SDL_Scancode scancode); extern int input_keydown(SDL_Scancode scancode);
extern int input_mousedown(int mousebtn); extern int input_mousedown(int mousebtn);