Added the ability to specify a gamepad type in the mapping

Also renamed most cases of SDL_GAMEPAD_TYPE_UNKNOWN to SDL_GAMEPAD_TYPE_STANDARD, and SDL_GetGamepadType() will return SDL_GAMEPAD_TYPE_UNKNOWN only if the gamepad is invalid.
This commit is contained in:
Sam Lantinga
2023-07-17 12:14:37 -07:00
parent 57820071a4
commit b271e92c6e
14 changed files with 530 additions and 65 deletions

View File

@@ -1268,6 +1268,177 @@ void DestroyGamepadDisplay(GamepadDisplay *ctx)
SDL_free(ctx);
}
struct GamepadTypeDisplay
{
SDL_Renderer *renderer;
int type_highlighted;
SDL_bool type_pressed;
int type_selected;
SDL_GamepadType real_type;
SDL_Rect area;
};
GamepadTypeDisplay *CreateGamepadTypeDisplay(SDL_Renderer *renderer)
{
GamepadTypeDisplay *ctx = SDL_calloc(1, sizeof(*ctx));
if (ctx) {
ctx->renderer = renderer;
ctx->type_highlighted = SDL_GAMEPAD_TYPE_UNSELECTED;
ctx->type_selected = SDL_GAMEPAD_TYPE_UNSELECTED;
ctx->real_type = SDL_GAMEPAD_TYPE_UNKNOWN;
}
return ctx;
}
void SetGamepadTypeDisplayArea(GamepadTypeDisplay *ctx, const SDL_Rect *area)
{
if (!ctx) {
return;
}
SDL_copyp(&ctx->area, area);
}
void SetGamepadTypeDisplayHighlight(GamepadTypeDisplay *ctx, int type, SDL_bool pressed)
{
if (!ctx) {
return;
}
ctx->type_highlighted = type;
ctx->type_pressed = pressed;
}
void SetGamepadTypeDisplaySelected(GamepadTypeDisplay *ctx, int type)
{
if (!ctx) {
return;
}
ctx->type_selected = type;
}
void SetGamepadTypeDisplayRealType(GamepadTypeDisplay *ctx, SDL_GamepadType type)
{
if (!ctx) {
return;
}
ctx->real_type = type;
}
int GetGamepadTypeDisplayAt(GamepadTypeDisplay *ctx, float x, float y)
{
int i;
const float margin = 8.0f;
const float line_height = 16.0f;
SDL_FRect highlight;
SDL_FPoint point;
if (!ctx) {
return SDL_GAMEPAD_TYPE_UNSELECTED;
}
point.x = x;
point.y = y;
x = ctx->area.x + margin;
y = ctx->area.y + margin;
for (i = SDL_GAMEPAD_TYPE_UNKNOWN; i < SDL_GAMEPAD_TYPE_MAX; ++i) {
highlight.x = x;
highlight.y = y;
highlight.w = (float)ctx->area.w - (margin * 2);
highlight.h = (float)line_height;
if (SDL_PointInRectFloat(&point, &highlight)) {
return i;
}
y += line_height;
}
return SDL_GAMEPAD_TYPE_UNSELECTED;
}
static void RenderGamepadTypeHighlight(GamepadTypeDisplay *ctx, int type, const SDL_FRect *area)
{
if (type == ctx->type_highlighted || type == ctx->type_selected) {
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
if (type == ctx->type_highlighted) {
if (ctx->type_pressed) {
SDL_SetRenderDrawColor(ctx->renderer, PRESSED_COLOR);
} else {
SDL_SetRenderDrawColor(ctx->renderer, HIGHLIGHT_COLOR);
}
} else {
SDL_SetRenderDrawColor(ctx->renderer, SELECTED_COLOR);
}
SDL_RenderFillRect(ctx->renderer, area);
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
}
}
void RenderGamepadTypeDisplay(GamepadTypeDisplay *ctx)
{
float x, y;
int i;
char text[128];
const float margin = 8.0f;
const float line_height = 16.0f;
SDL_FPoint dst;
SDL_FRect highlight;
if (!ctx) {
return;
}
x = ctx->area.x + margin;
y = ctx->area.y + margin;
for (i = SDL_GAMEPAD_TYPE_UNKNOWN; i < SDL_GAMEPAD_TYPE_MAX; ++i) {
highlight.x = x;
highlight.y = y;
highlight.w = (float)ctx->area.w - (margin * 2);
highlight.h = (float)line_height;
RenderGamepadTypeHighlight(ctx, i, &highlight);
if (i == SDL_GAMEPAD_TYPE_UNKNOWN) {
if (ctx->real_type == SDL_GAMEPAD_TYPE_UNKNOWN ||
ctx->real_type == SDL_GAMEPAD_TYPE_STANDARD) {
SDL_strlcpy(text, "Auto (Standard)", sizeof(text));
} else {
SDL_snprintf(text, sizeof(text), "Auto (%s)", GetGamepadTypeString(ctx->real_type));
}
} else if (i == SDL_GAMEPAD_TYPE_STANDARD) {
SDL_strlcpy(text, "Standard", sizeof(text));
} else {
SDL_strlcpy(text, GetGamepadTypeString((SDL_GamepadType)i), sizeof(text));
}
dst.x = x + margin;
dst.y = y + line_height / 2 - FONT_CHARACTER_SIZE / 2;
SDLTest_DrawString(ctx->renderer, dst.x, dst.y, text);
y += line_height;
}
}
void DestroyGamepadTypeDisplay(GamepadTypeDisplay *ctx)
{
if (!ctx) {
return;
}
SDL_free(ctx);
}
struct JoystickDisplay
{
@@ -2185,13 +2356,14 @@ static char *JoinMapping(MappingParts *parts)
}
length += 1;
/* The sort order is: crc, platform, *, sdk, hint */
/* The sort order is: crc, platform, type, *, sdk, hint */
sort_order = SDL_stack_alloc(MappingSortEntry, parts->num_elements);
for (i = 0; i < parts->num_elements; ++i) {
sort_order[i].parts = parts;
sort_order[i].index = i;
}
SDL_qsort(sort_order, parts->num_elements, sizeof(*sort_order), SortMapping);
MoveSortedEntry("type", sort_order, parts->num_elements, SDL_TRUE);
MoveSortedEntry("platform", sort_order, parts->num_elements, SDL_TRUE);
MoveSortedEntry("crc", sort_order, parts->num_elements, SDL_TRUE);
MoveSortedEntry("sdk>=", sort_order, parts->num_elements, SDL_FALSE);
@@ -2334,7 +2506,7 @@ static char *SetMappingValue(char *mapping, const char *key, const char *value)
return mapping;
}
static char *RemoveMappingKey(char *mapping, const char *key)
static char *RemoveMappingValue(char *mapping, const char *key)
{
MappingParts parts;
int i;
@@ -2447,14 +2619,46 @@ char *SetMappingName(char *mapping, const char *name)
return RecreateMapping(&parts, mapping);
}
char *GetMappingType(const char *mapping)
const char *GetGamepadTypeString(SDL_GamepadType type)
{
return GetMappingValue(mapping, "type");
switch (type) {
case SDL_GAMEPAD_TYPE_XBOX360:
return "Xbox 360";
case SDL_GAMEPAD_TYPE_XBOXONE:
return "Xbox One";
case SDL_GAMEPAD_TYPE_PS3:
return "PS3";
case SDL_GAMEPAD_TYPE_PS4:
return "PS4";
case SDL_GAMEPAD_TYPE_PS5:
return "PS5";
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO:
return "Nintendo Switch";
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT:
return "Joy-Con (L)";
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT:
return "Joy-Con (R)";
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR:
return "Joy-Con Pair";
default:
return "";
}
}
char *SetMappingType(char *mapping, const char *type)
SDL_GamepadType GetMappingType(const char *mapping)
{
return SetMappingValue(mapping, "type", type);
return SDL_GetGamepadTypeFromString(GetMappingValue(mapping, "type"));
}
char *SetMappingType(char *mapping, SDL_GamepadType type)
{
const char *type_string = SDL_GetGamepadStringForType(type);
if (type_string == NULL || type == SDL_GAMEPAD_TYPE_UNKNOWN) {
return RemoveMappingValue(mapping, "type");
} else {
return SetMappingValue(mapping, "type", type_string);
}
}
static const char *GetElementKey(int element)
@@ -2527,7 +2731,7 @@ char *SetElementBinding(char *mapping, int element, const char *binding)
if (binding) {
return SetMappingValue(mapping, GetElementKey(element), binding);
} else {
return RemoveMappingKey(mapping, GetElementKey(element));
return RemoveMappingValue(mapping, GetElementKey(element));
}
}

View File

@@ -92,6 +92,24 @@ extern void SetGamepadDisplaySelected(GamepadDisplay *ctx, int element);
extern void RenderGamepadDisplay(GamepadDisplay *ctx, SDL_Gamepad *gamepad);
extern void DestroyGamepadDisplay(GamepadDisplay *ctx);
/* Gamepad type display */
enum
{
SDL_GAMEPAD_TYPE_UNSELECTED = -1
};
typedef struct GamepadTypeDisplay GamepadTypeDisplay;
extern GamepadTypeDisplay *CreateGamepadTypeDisplay(SDL_Renderer *renderer);
extern void SetGamepadTypeDisplayArea(GamepadTypeDisplay *ctx, const SDL_Rect *area);
extern int GetGamepadTypeDisplayAt(GamepadTypeDisplay *ctx, float x, float y);
extern void SetGamepadTypeDisplayHighlight(GamepadTypeDisplay *ctx, int type, SDL_bool pressed);
extern void SetGamepadTypeDisplaySelected(GamepadTypeDisplay *ctx, int type);
extern void SetGamepadTypeDisplayRealType(GamepadTypeDisplay *ctx, SDL_GamepadType type);
extern void RenderGamepadTypeDisplay(GamepadTypeDisplay *ctx);
extern void DestroyGamepadTypeDisplay(GamepadTypeDisplay *ctx);
/* Joystick element display */
typedef struct JoystickDisplay JoystickDisplay;
@@ -131,11 +149,14 @@ extern char *GetMappingName(const char *mapping);
/* Set the name in a mapping, freeing the mapping passed in and returning a new mapping */
extern char *SetMappingName(char *mapping, const char *name);
/* Get the friendly string for an SDL_GamepadType */
extern const char *GetGamepadTypeString(SDL_GamepadType type);
/* Return the type from a mapping, which should be freed using SDL_free(), or NULL if there is no type specified */
extern char *GetMappingType(const char *mapping);
extern SDL_GamepadType GetMappingType(const char *mapping);
/* Set the type in a mapping, freeing the mapping passed in and returning a new mapping */
extern char *SetMappingType(char *mapping, const char *type);
extern char *SetMappingType(char *mapping, SDL_GamepadType type);
/* Return true if a mapping has this element bound */
extern SDL_bool MappingHasElement(const char *mapping, int element);

View File

@@ -68,6 +68,7 @@ static SDL_Renderer *screen = NULL;
static ControllerDisplayMode display_mode = CONTROLLER_MODE_TESTING;
static GamepadImage *image = NULL;
static GamepadDisplay *gamepad_elements = NULL;
static GamepadTypeDisplay *gamepad_type = NULL;
static JoystickDisplay *joystick_elements = NULL;
static GamepadButton *setup_mapping_button = NULL;
static GamepadButton *done_mapping_button = NULL;
@@ -89,6 +90,9 @@ static Uint64 binding_advance_time = 0;
static SDL_FRect title_area;
static SDL_bool title_highlighted;
static SDL_bool title_pressed;
static SDL_FRect type_area;
static SDL_bool type_highlighted;
static SDL_bool type_pressed;
static char *controller_name;
static SDL_Joystick *virtual_joystick = NULL;
static SDL_GamepadAxis virtual_axis_active = SDL_GAMEPAD_AXIS_INVALID;
@@ -210,8 +214,12 @@ static void ClearButtonHighlights(void)
title_highlighted = SDL_FALSE;
title_pressed = SDL_FALSE;
type_highlighted = SDL_FALSE;
type_pressed = SDL_FALSE;
ClearGamepadImage(image);
SetGamepadDisplayHighlight(gamepad_elements, SDL_GAMEPAD_ELEMENT_INVALID, SDL_FALSE);
SetGamepadTypeDisplayHighlight(gamepad_type, SDL_GAMEPAD_TYPE_UNSELECTED, SDL_FALSE);
SetGamepadButtonHighlight(setup_mapping_button, SDL_FALSE, SDL_FALSE);
SetGamepadButtonHighlight(done_mapping_button, SDL_FALSE, SDL_FALSE);
SetGamepadButtonHighlight(cancel_button, SDL_FALSE, SDL_FALSE);
@@ -241,6 +249,14 @@ static void UpdateButtonHighlights(float x, float y, SDL_bool button_down)
title_pressed = SDL_FALSE;
}
if (SDL_PointInRectFloat(&point, &type_area)) {
type_highlighted = SDL_TRUE;
type_pressed = button_down;
} else {
type_highlighted = SDL_FALSE;
type_pressed = SDL_FALSE;
}
if (controller->joystick != virtual_joystick) {
gamepad_highlight_element = GetGamepadImageElementAt(image, x, y);
}
@@ -249,6 +265,11 @@ static void UpdateButtonHighlights(float x, float y, SDL_bool button_down)
}
SetGamepadDisplayHighlight(gamepad_elements, gamepad_highlight_element, button_down);
if (binding_element == SDL_GAMEPAD_ELEMENT_TYPE) {
int gamepad_highlight_type = GetGamepadTypeDisplayAt(gamepad_type, x, y);
SetGamepadTypeDisplayHighlight(gamepad_type, gamepad_highlight_type, button_down);
}
joystick_highlight_element = GetJoystickDisplayElementAt(joystick_elements, controller->joystick, x, y);
SetJoystickDisplayHighlight(joystick_elements, joystick_highlight_element, button_down);
SDL_free(joystick_highlight_element);
@@ -606,6 +627,19 @@ static void PasteControllerName(void)
CommitControllerName();
}
static void CommitGamepadType(SDL_GamepadType type)
{
char *mapping = NULL;
if (controller->mapping) {
mapping = SDL_strdup(controller->mapping);
} else {
mapping = NULL;
}
mapping = SetMappingType(mapping, type);
SetAndFreeGamepadMapping(mapping);
}
static const char *GetBindingInstruction(void)
{
switch (binding_element) {
@@ -1097,6 +1131,7 @@ static void DrawGamepadWaiting(SDL_Renderer *renderer)
static void DrawGamepadInfo(SDL_Renderer *renderer)
{
const char *type;
const char *serial;
char text[128];
float x, y;
@@ -1116,9 +1151,24 @@ static void DrawGamepadInfo(SDL_Renderer *renderer)
SDL_SetRenderDrawColor(renderer, r, g, b, a);
}
if (type_highlighted) {
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
if (type_pressed) {
SDL_SetRenderDrawColor(renderer, PRESSED_COLOR);
} else {
SDL_SetRenderDrawColor(renderer, HIGHLIGHT_COLOR);
}
SDL_RenderFillRect(renderer, &type_area);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
}
if (controller_name && *controller_name) {
x = (float)SCREEN_WIDTH / 2 - (FONT_CHARACTER_SIZE * SDL_strlen(controller_name)) / 2;
y = (float)TITLE_HEIGHT / 2 - FONT_CHARACTER_SIZE / 2;
x = title_area.x + title_area.w / 2 - (FONT_CHARACTER_SIZE * SDL_strlen(controller_name)) / 2;
y = title_area.y + title_area.h / 2 - FONT_CHARACTER_SIZE / 2;
SDLTest_DrawString(renderer, x, y, controller_name);
}
@@ -1129,6 +1179,11 @@ static void DrawGamepadInfo(SDL_Renderer *renderer)
SDLTest_DrawString(renderer, x, y, text);
}
type = GetGamepadTypeString(SDL_GetGamepadType(controller->gamepad));
x = type_area.x + type_area.w / 2 - (FONT_CHARACTER_SIZE * SDL_strlen(type)) / 2;
y = type_area.y + type_area.h / 2 - FONT_CHARACTER_SIZE / 2;
SDLTest_DrawString(renderer, x, y, type);
if (display_mode == CONTROLLER_MODE_TESTING) {
SDL_snprintf(text, SDL_arraysize(text), "VID: 0x%.4x PID: 0x%.4x",
SDL_GetJoystickVendor(controller->joystick),
@@ -1185,6 +1240,8 @@ static void DrawBindingTips(SDL_Renderer *renderer)
if (binding_element == SDL_GAMEPAD_ELEMENT_NAME) {
text = "(press RETURN to complete)";
} else if (binding_element == SDL_GAMEPAD_ELEMENT_TYPE) {
text = "(press ESC to cancel)";
} else {
bound_A = MappingHasElement(controller->mapping, SDL_GAMEPAD_BUTTON_A);
bound_B = MappingHasElement(controller->mapping, SDL_GAMEPAD_BUTTON_B);
@@ -1467,6 +1524,14 @@ static void loop(void *arg)
PasteMapping();
} else if (title_pressed) {
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_NAME, SDL_FALSE);
} else if (type_pressed) {
SetCurrentBindingElement(SDL_GAMEPAD_ELEMENT_TYPE, SDL_FALSE);
} else if (binding_element == SDL_GAMEPAD_ELEMENT_TYPE) {
int type = GetGamepadTypeDisplayAt(gamepad_type, event.button.x, event.button.y);
if (type != SDL_GAMEPAD_TYPE_UNSELECTED) {
CommitGamepadType((SDL_GamepadType)type);
StopBinding();
}
} else {
int gamepad_element = SDL_GAMEPAD_ELEMENT_INVALID;
char *joystick_element;
@@ -1599,7 +1664,12 @@ static void loop(void *arg)
}
RenderGamepadImage(image);
RenderGamepadDisplay(gamepad_elements, controller->gamepad);
if (binding_element == SDL_GAMEPAD_ELEMENT_TYPE) {
SetGamepadTypeDisplayRealType(gamepad_type, SDL_GetRealGamepadType(controller->gamepad));
RenderGamepadTypeDisplay(gamepad_type);
} else {
RenderGamepadDisplay(gamepad_elements, controller->gamepad);
}
RenderJoystickDisplay(joystick_elements, controller->joystick);
if (display_mode == CONTROLLER_MODE_TESTING) {
@@ -1741,6 +1811,11 @@ int main(int argc, char *argv[])
title_area.x = (float)PANEL_WIDTH + PANEL_SPACING;
title_area.y = (float)TITLE_HEIGHT / 2 - title_area.h / 2;
type_area.w = (float)PANEL_WIDTH - 2 * BUTTON_MARGIN;
type_area.h = (float)FONT_CHARACTER_SIZE + 2 * BUTTON_MARGIN;
type_area.x = (float)BUTTON_MARGIN;
type_area.y = (float)TITLE_HEIGHT / 2 - type_area.h / 2;
image = CreateGamepadImage(screen);
if (image == NULL) {
SDL_DestroyRenderer(screen);
@@ -1756,6 +1831,13 @@ int main(int argc, char *argv[])
area.h = GAMEPAD_HEIGHT;
SetGamepadDisplayArea(gamepad_elements, &area);
gamepad_type = CreateGamepadTypeDisplay(screen);
area.x = 0;
area.y = TITLE_HEIGHT;
area.w = PANEL_WIDTH;
area.h = GAMEPAD_HEIGHT;
SetGamepadTypeDisplayArea(gamepad_type, &area);
joystick_elements = CreateJoystickDisplay(screen);
area.x = PANEL_WIDTH + PANEL_SPACING + GAMEPAD_WIDTH + PANEL_SPACING;
area.y = TITLE_HEIGHT;
@@ -1829,6 +1911,7 @@ int main(int argc, char *argv[])
}
DestroyGamepadImage(image);
DestroyGamepadDisplay(gamepad_elements);
DestroyGamepadTypeDisplay(gamepad_type);
DestroyJoystickDisplay(joystick_elements);
DestroyGamepadButton(copy_button);
SDL_DestroyRenderer(screen);