1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-22 17:49:43 +01:00

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.
This commit is contained in:
Dav999-v 2023-03-20 02:12:49 +01:00 committed by Misa Elizabeth Kai
parent e55e9efd9b
commit 01200b5e64
3 changed files with 22 additions and 2 deletions

View file

@ -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) void BUTTONGLYPHS_init(void)
{ {
/* Set glyph array to strings for all the button glyph codepoints (U+EBxx) */ /* 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 /* 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) */ * 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) 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[GLYPH_UNKNOWN];
} }
return glyph_layout[LAYOUT_PLAYSTATION][button]; return glyph_layout[layout][button];
} }
static const char* glyph_for_vector( static const char* glyph_for_vector(

View file

@ -15,6 +15,8 @@ void BUTTONGLYPHS_init(void);
bool BUTTONGLYPHS_keyboard_is_available(void); bool BUTTONGLYPHS_keyboard_is_available(void);
bool BUTTONGLYPHS_keyboard_is_active(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_wasd_text(void);
const char* BUTTONGLYPHS_get_button(ActionSet actionset, Action action, int binding); const char* BUTTONGLYPHS_get_button(ActionSet actionset, Action action, int binding);

View file

@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include "Alloc.h" #include "Alloc.h"
#include "ButtonGlyphs.h"
#include "Exit.h" #include "Exit.h"
#include "Game.h" #include "Game.h"
#include "GlitchrunnerMode.h" #include "GlitchrunnerMode.h"
@ -174,6 +175,8 @@ void KeyPoll::Poll(void)
music.playef(4); music.playef(4);
} }
BUTTONGLYPHS_keyboard_set_active(true);
if (textentry()) if (textentry())
{ {
if (evt.key.keysym.sym == SDLK_BACKSPACE && !keybuffer.empty()) if (evt.key.keysym.sym == SDLK_BACKSPACE && !keybuffer.empty())
@ -268,6 +271,7 @@ void KeyPoll::Poll(void)
/* Controller Input */ /* Controller Input */
case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONDOWN:
buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = true; buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = true;
BUTTONGLYPHS_keyboard_set_active(false);
break; break;
case SDL_CONTROLLERBUTTONUP: case SDL_CONTROLLERBUTTONUP:
buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = false; buttonmap[(SDL_GameControllerButton) evt.cbutton.button] = false;
@ -300,6 +304,7 @@ void KeyPoll::Poll(void)
} }
break; break;
} }
BUTTONGLYPHS_keyboard_set_active(false);
break; break;
} }
case SDL_CONTROLLERDEVICEADDED: case SDL_CONTROLLERDEVICEADDED:
@ -311,6 +316,7 @@ void KeyPoll::Poll(void)
SDL_GameControllerName(toOpen) SDL_GameControllerName(toOpen)
); );
controllers[SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(toOpen))] = toOpen; controllers[SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(toOpen))] = toOpen;
BUTTONGLYPHS_keyboard_set_active(false);
break; break;
} }
case SDL_CONTROLLERDEVICEREMOVED: case SDL_CONTROLLERDEVICEREMOVED:
@ -319,6 +325,10 @@ void KeyPoll::Poll(void)
controllers.erase(evt.cdevice.which); controllers.erase(evt.cdevice.which);
vlog_info("Closing %s", SDL_GameControllerName(toClose)); vlog_info("Closing %s", SDL_GameControllerName(toClose));
SDL_GameControllerClose(toClose); SDL_GameControllerClose(toClose);
if (controllers.empty())
{
BUTTONGLYPHS_keyboard_set_active(true);
}
break; break;
} }