Skip to content

Commit

Permalink
Internal API for controller menu navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
fragglet committed Dec 1, 2024
1 parent ff15ecf commit 6efadf8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/sdl/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ int controller_bindings[NUM_KEYS] = {

static int button_to_key[SDL_CONTROLLER_BUTTON_MAX];

static enum menukey last_menukey = MENUKEY_NONE;
static SDL_GameController *controller = NULL;
static SDL_JoystickID controller_id;

Expand All @@ -52,6 +53,30 @@ static void RecalculateButtonMapping(void)
}
}

static void CheckForMenuKeypress(SDL_ControllerButtonEvent *event)
{
switch (event->button) {
case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
last_menukey = MENUKEY_DOWN;
break;
case SDL_CONTROLLER_BUTTON_DPAD_UP:
last_menukey = MENUKEY_UP;
break;
case SDL_CONTROLLER_BUTTON_BACK:
case SDL_CONTROLLER_BUTTON_X:
case SDL_CONTROLLER_BUTTON_Y:
last_menukey = MENUKEY_BACK;
break;
case SDL_CONTROLLER_BUTTON_START:
case SDL_CONTROLLER_BUTTON_A:
case SDL_CONTROLLER_BUTTON_B:
last_menukey = MENUKEY_START;
break;
default:
break;
}
}

void Vid_ControllerButtonDown(SDL_ControllerButtonEvent *event)
{
int gamekey;
Expand All @@ -62,6 +87,7 @@ void Vid_ControllerButtonDown(SDL_ControllerButtonEvent *event)
if (event->button >= SDL_CONTROLLER_BUTTON_MAX) {
return;
}
CheckForMenuKeypress(event);
gamekey = button_to_key[event->button];
if (gamekey != KEY_UNKNOWN) {
keysdown[gamekey] |= KEYDOWN_GAMEPAD | KEYDOWN_WAS_PRESSED;
Expand Down Expand Up @@ -111,3 +137,15 @@ void Vid_ControllerInit(void)
// SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
RecalculateButtonMapping();
}

enum menukey Vid_ControllerMenuKey(void)
{
enum menukey result = last_menukey;
last_menukey = MENUKEY_NONE;
return result;
}

bool Vid_HaveController(void)
{
return controller != NULL;
}
11 changes: 11 additions & 0 deletions src/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ enum gamekey {
NUM_KEYS,
};

enum menukey {
MENUKEY_NONE,
MENUKEY_START,
MENUKEY_BACK,
MENUKEY_UP,
MENUKEY_DOWN,
};

// Which keys are currently down or have been pressed since the last call
// to Vid_GetGameKeys:

Expand Down Expand Up @@ -132,4 +140,7 @@ extern void Vid_TouchButtonPress(const struct touch_button *b, bool pressed);
extern void Vid_ShowTouchGameControls(void);
extern void Vid_ShowTouchKeys(const char *keys);

enum menukey Vid_ControllerMenuKey(void);
bool Vid_HaveController(void);

#endif

0 comments on commit 6efadf8

Please sign in to comment.