Merge pull request 'fix event based axis input giving results that are not in range -1 to 1' (#8) from event-input into move-and-slide

Reviewed-on: #8
pull/7/head
Sara 2023-06-18 20:24:31 +00:00
commit fd99159b27
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.positive = positive;
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;
}
@ -101,13 +102,14 @@ static inline
void _handle_key_event(const SDL_Event event) {
for(input_listener_t* listener = g_key_listeners; listener < g_key_listeners_endptr; ++listener) {
if(listener->type == INPUT_LISTENER_AXIS) {
if(listener->axis.positive == event.key.keysym.scancode
|| listener->axis.negative == event.key.keysym.scancode) {
const SDL_Scancode scode = event.key.keysym.scancode;
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.positive == scode) {
listener->axis.last_positive = event.key.state == SDL_PRESSED;
}
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 {
input_axis_delegate_t delegate;
SDL_Scancode positive, negative;
int last;
int last_positive, last_negative;
} axis;
struct {
input_mouse_delegate_t delegate;