fix event based axis input giving results that are not in range -1 to 1 #8

Merged
Sara merged 1 commits from event-input into move-and-slide 2023-06-18 20:24:31 +00:00
2 changed files with 10 additions and 8 deletions

View File

@ -23,7 +23,8 @@ void add_key_listener(SDL_Scancode negative, SDL_Scancode positive,
g_key_listeners_endptr->axis.delegate = delegate; g_key_listeners_endptr->axis.delegate = delegate;
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_positive =
g_key_listeners_endptr->axis.last_negative = 0;
++g_key_listeners_endptr; ++g_key_listeners_endptr;
} }
@ -101,13 +102,14 @@ static inline
void _handle_key_event(const SDL_Event event) { void _handle_key_event(const SDL_Event event) {
for(input_listener_t* listener = g_key_listeners; listener < g_key_listeners_endptr; ++listener) { for(input_listener_t* listener = g_key_listeners; listener < g_key_listeners_endptr; ++listener) {
if(listener->type == INPUT_LISTENER_AXIS) { if(listener->type == INPUT_LISTENER_AXIS) {
if(listener->axis.positive == event.key.keysym.scancode const SDL_Scancode scode = event.key.keysym.scancode;
|| listener->axis.negative == event.key.keysym.scancode) { if(listener->axis.positive == scode) {
const SDL_Scancode scode = event.key.keysym.scancode; listener->axis.last_positive = event.key.state == SDL_PRESSED;
const int value = (event.type == SDL_KEYDOWN ? 1 : -1) * (scode == listener->axis.negative ? -1 : 1);
listener->axis.last += value;
listener->axis.delegate(listener->axis.last);
} }
if(listener->axis.negative == scode) {
listener->axis.last_negative = event.key.state == SDL_PRESSED;
}
listener->axis.delegate(listener->axis.last_positive - listener->axis.last_negative);
} }
} }
} }

View File

@ -25,7 +25,7 @@ typedef struct input_listener_t {
struct { struct {
input_axis_delegate_t delegate; input_axis_delegate_t delegate;
SDL_Scancode positive, negative; SDL_Scancode positive, negative;
int last; int last_positive, last_negative;
} axis; } axis;
struct { struct {
input_mouse_delegate_t delegate; input_mouse_delegate_t delegate;