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));
}
}