diff --git a/src/corelib/input.c b/src/corelib/input.c index ca1deaf..38ad3b1 100644 --- a/src/corelib/input.c +++ b/src/corelib/input.c @@ -14,8 +14,9 @@ input_listener_t* g_key_listeners_endptr = g_key_listeners; int _last_mouse_x=0, _last_mouse_y=0; float _last_screen_mouse_x=0.0, _last_screen_mouse_y=0.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) { memset(g_key_listeners_endptr, 0x0, sizeof(input_listener_t)); 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.negative = negative; g_key_listeners_endptr->axis.last = 0; - ++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)); g_key_listeners_endptr->type = INPUT_LISTENER_MOUSE; g_key_listeners_endptr->mouse.delegate = delegate; - g_key_listeners_endptr->mouse.min_delta = min_move; - ++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)); g_key_listeners_endptr->type = INPUT_LISTENER_BUTTON; g_key_listeners_endptr->button.delegate = delegate; g_key_listeners_endptr->button.button = button; g_key_listeners_endptr->button.last = 0; - ++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) { @@ -99,6 +100,12 @@ void process_button_listener(input_listener_t* listener) { 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() { float dx, dy; int px, py; @@ -115,6 +122,9 @@ void update_input() { case INPUT_LISTENER_MOUSE: process_mouse_listener(listener, dx, dy); break; + case INPUT_LISTENER_SCROLL: + process_scroll_listener(listener); + break; case INPUT_LISTENER_BUTTON: process_button_listener(listener); break; @@ -124,6 +134,17 @@ void update_input() { _last_mouse_x = px; _last_mouse_y = py; _last_screen_mouse_x = (float)px / 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) { diff --git a/src/corelib/input.h b/src/corelib/input.h index 42c670b..9cb423f 100644 --- a/src/corelib/input.h +++ b/src/corelib/input.h @@ -10,10 +10,12 @@ extern "C" { typedef void(*input_axis_delegate_t)(int value); typedef void(*input_mouse_delegate_t)(float dx, float dy); typedef void(*input_button_delegate_t)(int down); +typedef void(*input_scroll_delegate_t)(float delta); enum INPUT_LISTENER_TYPE_T { INPUT_LISTENER_MOUSE, INPUT_LISTENER_AXIS, + INPUT_LISTENER_SCROLL, INPUT_LISTENER_BUTTON, }; @@ -27,28 +29,32 @@ typedef struct input_listener_t { } axis; struct { input_mouse_delegate_t delegate; - float min_delta; } mouse; struct { input_button_delegate_t delegate; uint32_t button; int last; } button; + struct { + input_scroll_delegate_t delegate; + } scroll; }; } input_listener_t; 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); -extern size_t add_mouse_listener(float min_move, input_mouse_delegate_t delegate); -extern size_t add_mouse_button_listener(uint32_t button, input_button_delegate_t delegate); +extern void add_mouse_listener(input_mouse_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 mouse_screen_position(float* ox, float* oy); extern void mouse_world_position(float* ox, float* oy); extern void input_init(); extern void update_input(); +extern void input_event(SDL_Event event); extern int input_keydown(SDL_Scancode scancode); extern int input_mousedown(int mousebtn);