diff options
-rw-r--r-- | nuklear_ui/blastem_nuklear.c | 2 | ||||
-rw-r--r-- | nuklear_ui/nuklear_sdl_gles2.h | 24 | ||||
-rwxr-xr-x | render_sdl.c | 26 | ||||
-rw-r--r-- | render_sdl.h | 1 |
4 files changed, 52 insertions, 1 deletions
diff --git a/nuklear_ui/blastem_nuklear.c b/nuklear_ui/blastem_nuklear.c index 05ccb75..8773b90 100644 --- a/nuklear_ui/blastem_nuklear.c +++ b/nuklear_ui/blastem_nuklear.c @@ -2001,6 +2001,7 @@ void blastem_nuklear_render(void) void ui_idle_loop(void) { + render_enable_gamepad_events(1); const uint32_t MIN_UI_DELAY = 15; static uint32_t last; while (current_view != view_play) @@ -2017,6 +2018,7 @@ void ui_idle_loop(void) persist_config(config); config_dirty = 0; } + render_enable_gamepad_events(0); } static void handle_event(SDL_Event *event) { diff --git a/nuklear_ui/nuklear_sdl_gles2.h b/nuklear_ui/nuklear_sdl_gles2.h index 31a4b8b..9dd3852 100644 --- a/nuklear_ui/nuklear_sdl_gles2.h +++ b/nuklear_ui/nuklear_sdl_gles2.h @@ -414,6 +414,30 @@ nk_sdl_handle_event(SDL_Event *evt) else nk_input_key(ctx, NK_KEY_RIGHT, down); } else return 0; return 1; + } else if (evt->type == SDL_CONTROLLERBUTTONDOWN || evt->type == SDL_CONTROLLERBUTTONUP) { + int down = evt->type == SDL_CONTROLLERBUTTONDOWN; + if (evt->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP) { + nk_input_key(ctx, NK_KEY_UP, down); + } else if (evt->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN) { + nk_input_key(ctx, NK_KEY_DOWN, down); + } else if (evt->cbutton.button == SDL_CONTROLLER_BUTTON_A || evt->cbutton.button == SDL_CONTROLLER_BUTTON_START) { + nk_input_key(ctx, NK_KEY_ENTER, down); + } + } else if (evt->type == SDL_CONTROLLERAXISMOTION) { + if (evt->caxis.axis == SDL_CONTROLLER_AXIS_LEFTY || evt->caxis.axis == SDL_CONTROLLER_AXIS_RIGHTY) { + int down = abs(evt->caxis.value) > 2000; + if (evt->caxis.value >= 0) { + if (ctx->input.keyboard.keys[NK_KEY_UP].down) { + nk_input_key(ctx, NK_KEY_UP, 0); + } + nk_input_key(ctx, NK_KEY_DOWN, down); + } else { + if (ctx->input.keyboard.keys[NK_KEY_DOWN].down) { + nk_input_key(ctx, NK_KEY_DOWN, 0); + } + nk_input_key(ctx, NK_KEY_UP, down); + } + } } else if (evt->type == SDL_MOUSEBUTTONDOWN || evt->type == SDL_MOUSEBUTTONUP) { /* mouse button */ int down = evt->type == SDL_MOUSEBUTTONDOWN; diff --git a/render_sdl.c b/render_sdl.c index f1c8153..c23944e 100755 --- a/render_sdl.c +++ b/render_sdl.c @@ -921,12 +921,29 @@ char* render_joystick_type_id(int index) SDL_GameController *render_get_controller(int index) { - if (index >= MAX_JOYSTICKS) { + if (index >= MAX_JOYSTICKS || !joysticks[index]) { return NULL; } return SDL_GameControllerOpen(joystick_sdl_index[index]); } +static uint8_t gc_events_enabled; +static SDL_GameController *controllers[MAX_JOYSTICKS]; +void render_enable_gamepad_events(uint8_t enabled) +{ + if (enabled != gc_events_enabled) { + gc_events_enabled = enabled; + for (int i = 0; i < MAX_JOYSTICKS; i++) { + if (enabled) { + controllers[i] = render_get_controller(i); + } else if (controllers[i]) { + SDL_GameControllerClose(controllers[i]); + controllers[i] = NULL; + } + } + } +} + static uint32_t overscan_top[NUM_VID_STD] = {2, 21}; static uint32_t overscan_bot[NUM_VID_STD] = {1, 17}; static uint32_t overscan_left[NUM_VID_STD] = {13, 13}; @@ -989,6 +1006,9 @@ static int32_t handle_event(SDL_Event *event) SDL_Joystick * joy = joysticks[index] = SDL_JoystickOpen(event->jdevice.which); joystick_sdl_index[index] = event->jdevice.which; joystick_index_locked[index] = 0; + if (gc_events_enabled) { + controllers[index] = SDL_GameControllerOpen(event->jdevice.which); + } if (joy) { debug_message("Joystick %d added: %s\n", index, SDL_JoystickName(joy)); debug_message("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy)); @@ -1002,6 +1022,10 @@ static int32_t handle_event(SDL_Event *event) if (index >= 0) { SDL_JoystickClose(joysticks[index]); joysticks[index] = NULL; + if (controllers[index]) { + SDL_GameControllerClose(controllers[index]); + controllers[index] = NULL; + } debug_message("Joystick %d removed\n", index); } else { debug_message("Failed to find removed joystick with instance ID: %d\n", index); diff --git a/render_sdl.h b/render_sdl.h index a785d21..932fa50 100644 --- a/render_sdl.h +++ b/render_sdl.h @@ -11,5 +11,6 @@ SDL_Joystick *render_get_joystick(int index); SDL_GameController *render_get_controller(int index); int render_lookup_button(char *name); int render_lookup_axis(char *name); +void render_enable_gamepad_events(uint8_t enabled); #endif //RENDER_SDL_H_ |