From 01200b5e649ec15a14eb76c95ce8bd8735116e01 Mon Sep 17 00:00:00 2001 From: Dav999-v Date: Mon, 20 Mar 2023 02:12:49 +0100 Subject: [PATCH] Switch between controller glyphs and keyboard keys automatically If a controller button is pressed, a controller is connected (even at startup!) or an axis is moved, the game will switch to displaying controller glyphs. If a keyboard key is pressed or the last controller is removed, the game will switch to displaying keyboard keys. --- desktop_version/src/ButtonGlyphs.cpp | 12 ++++++++++-- desktop_version/src/ButtonGlyphs.h | 2 ++ desktop_version/src/KeyPoll.cpp | 10 ++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/desktop_version/src/ButtonGlyphs.cpp b/desktop_version/src/ButtonGlyphs.cpp index 9580c6f2..5be347af 100644 --- a/desktop_version/src/ButtonGlyphs.cpp +++ b/desktop_version/src/ButtonGlyphs.cpp @@ -136,6 +136,9 @@ static const char* glyph_layout[LAYOUT_TOTAL][SDL_CONTROLLER_BUTTON_RIGHTSHOULDE } }; +static bool keyboard_is_active = true; +static ButtonGlyphLayout layout = LAYOUT_GENERIC; + void BUTTONGLYPHS_init(void) { /* Set glyph array to strings for all the button glyph codepoints (U+EBxx) */ @@ -156,7 +159,12 @@ bool BUTTONGLYPHS_keyboard_is_active(void) { /* Returns true if, not only do we have a keyboard available, but it's also the * active input method. (So, show keyboard keys, if false, show controller glyphs) */ - return true; + return keyboard_is_active; +} + +void BUTTONGLYPHS_keyboard_set_active(bool active) +{ + keyboard_is_active = active; } const char* BUTTONGLYPHS_get_wasd_text(void) @@ -177,7 +185,7 @@ static const char* sdlbutton_to_glyph(const SDL_GameControllerButton button) return glyph[GLYPH_UNKNOWN]; } - return glyph_layout[LAYOUT_PLAYSTATION][button]; + return glyph_layout[layout][button]; } static const char* glyph_for_vector( diff --git a/desktop_version/src/ButtonGlyphs.h b/desktop_version/src/ButtonGlyphs.h index b6ff4243..47f30022 100644 --- a/desktop_version/src/ButtonGlyphs.h +++ b/desktop_version/src/ButtonGlyphs.h @@ -15,6 +15,8 @@ void BUTTONGLYPHS_init(void); bool BUTTONGLYPHS_keyboard_is_available(void); bool BUTTONGLYPHS_keyboard_is_active(void); +void BUTTONGLYPHS_keyboard_set_active(bool active); + const char* BUTTONGLYPHS_get_wasd_text(void); const char* BUTTONGLYPHS_get_button(ActionSet actionset, Action action, int binding); diff --git a/desktop_version/src/KeyPoll.cpp b/desktop_version/src/KeyPoll.cpp index 17d727b7..84d0a901 100644 --- a/desktop_version/src/KeyPoll.cpp +++ b/desktop_version/src/KeyPoll.cpp @@ -4,6 +4,7 @@ #include #include "Alloc.h" +#include "ButtonGlyphs.h" #include "Exit.h" #include "Game.h" #include "GlitchrunnerMode.h" @@ -174,6 +175,8 @@ void KeyPoll::Poll(void) music.playef(4); } + BUTTONGLYPHS_keyboard_set_active(true); + if (textentry()) { if (evt.key.keysym.sym == SDLK_BACKSPACE && !keybuffer.empty()) @@ -268,6 +271,7 @@ void KeyPoll::Poll(void) /* Controller Input */ case SDL_CONTROLLERBUTTONDOWN: buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = true; + BUTTONGLYPHS_keyboard_set_active(false); break; case SDL_CONTROLLERBUTTONUP: buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = false; @@ -300,6 +304,7 @@ void KeyPoll::Poll(void) } break; } + BUTTONGLYPHS_keyboard_set_active(false); break; } case SDL_CONTROLLERDEVICEADDED: @@ -311,6 +316,7 @@ void KeyPoll::Poll(void) SDL_GameControllerName(toOpen) ); controllers[SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(toOpen))] = toOpen; + BUTTONGLYPHS_keyboard_set_active(false); break; } case SDL_CONTROLLERDEVICEREMOVED: @@ -319,6 +325,10 @@ void KeyPoll::Poll(void) controllers.erase(evt.cdevice.which); vlog_info("Closing %s", SDL_GameControllerName(toClose)); SDL_GameControllerClose(toClose); + if (controllers.empty()) + { + BUTTONGLYPHS_keyboard_set_active(true); + } break; }