Improved button labels, based on controller style

main
Sam Lantinga 2023-07-12 09:49:09 -07:00
parent ee34805053
commit cd99ae47ef
5 changed files with 8869 additions and 54 deletions

2933
test/gamepad_face_abxy.h Normal file

File diff suppressed because it is too large Load Diff

2933
test/gamepad_face_bayx.h Normal file

File diff suppressed because it is too large Load Diff

2933
test/gamepad_face_sony.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -15,6 +15,9 @@
#include "gamepadutils.h" #include "gamepadutils.h"
#include "gamepad_front.h" #include "gamepad_front.h"
#include "gamepad_back.h" #include "gamepad_back.h"
#include "gamepad_face_abxy.h"
#include "gamepad_face_bayx.h"
#include "gamepad_face_sony.h"
#include "gamepad_battery_unknown.h" #include "gamepad_battery_unknown.h"
#include "gamepad_battery_empty.h" #include "gamepad_battery_empty.h"
#include "gamepad_battery_low.h" #include "gamepad_battery_low.h"
@ -88,12 +91,17 @@ struct GamepadImage
SDL_Renderer *renderer; SDL_Renderer *renderer;
SDL_Texture *front_texture; SDL_Texture *front_texture;
SDL_Texture *back_texture; SDL_Texture *back_texture;
SDL_Texture *face_abxy_texture;
SDL_Texture *face_bayx_texture;
SDL_Texture *face_sony_texture;
SDL_Texture *battery_texture[1 + SDL_JOYSTICK_POWER_MAX]; SDL_Texture *battery_texture[1 + SDL_JOYSTICK_POWER_MAX];
SDL_Texture *touchpad_texture; SDL_Texture *touchpad_texture;
SDL_Texture *button_texture; SDL_Texture *button_texture;
SDL_Texture *axis_texture; SDL_Texture *axis_texture;
int gamepad_width; int gamepad_width;
int gamepad_height; int gamepad_height;
int face_width;
int face_height;
int battery_width; int battery_width;
int battery_height; int battery_height;
int touchpad_width; int touchpad_width;
@ -106,9 +114,9 @@ struct GamepadImage
int x; int x;
int y; int y;
SDL_bool showing_front; SDL_bool showing_front;
SDL_bool reverse_diamond;
SDL_bool showing_battery; SDL_bool showing_battery;
SDL_bool showing_touchpad; SDL_bool showing_touchpad;
GamepadImageFaceStyle face_style;
SDL_bool buttons[SDL_GAMEPAD_BUTTON_MAX]; SDL_bool buttons[SDL_GAMEPAD_BUTTON_MAX];
int axes[SDL_GAMEPAD_AXIS_MAX]; int axes[SDL_GAMEPAD_AXIS_MAX];
@ -134,9 +142,9 @@ static SDL_Texture *CreateTexture(SDL_Renderer *renderer, unsigned char *data, u
return texture; return texture;
} }
static SDL_GamepadButton GetRemappedButton(SDL_bool reverse_diamond, SDL_GamepadButton button) static SDL_GamepadButton GetRemappedButton(GamepadImageFaceStyle face_style, SDL_GamepadButton button)
{ {
if (reverse_diamond) { if (face_style == GAMEPAD_IMAGE_FACE_BAYX) {
switch (button) { switch (button) {
case SDL_GAMEPAD_BUTTON_A: case SDL_GAMEPAD_BUTTON_A:
button = SDL_GAMEPAD_BUTTON_B; button = SDL_GAMEPAD_BUTTON_B;
@ -166,6 +174,11 @@ GamepadImage *CreateGamepadImage(SDL_Renderer *renderer)
ctx->back_texture = CreateTexture(renderer, gamepad_back_bmp, gamepad_back_bmp_len); ctx->back_texture = CreateTexture(renderer, gamepad_back_bmp, gamepad_back_bmp_len);
SDL_QueryTexture(ctx->front_texture, NULL, NULL, &ctx->gamepad_width, &ctx->gamepad_height); SDL_QueryTexture(ctx->front_texture, NULL, NULL, &ctx->gamepad_width, &ctx->gamepad_height);
ctx->face_abxy_texture = CreateTexture(renderer, gamepad_face_abxy_bmp, gamepad_face_abxy_bmp_len);
ctx->face_bayx_texture = CreateTexture(renderer, gamepad_face_bayx_bmp, gamepad_face_bayx_bmp_len);
ctx->face_sony_texture = CreateTexture(renderer, gamepad_face_sony_bmp, gamepad_face_sony_bmp_len);
SDL_QueryTexture(ctx->face_abxy_texture, NULL, NULL, &ctx->face_width, &ctx->face_height);
ctx->battery_texture[1 + SDL_JOYSTICK_POWER_UNKNOWN] = CreateTexture(renderer, gamepad_battery_unknown_bmp, gamepad_battery_unknown_bmp_len); ctx->battery_texture[1 + SDL_JOYSTICK_POWER_UNKNOWN] = CreateTexture(renderer, gamepad_battery_unknown_bmp, gamepad_battery_unknown_bmp_len);
ctx->battery_texture[1 + SDL_JOYSTICK_POWER_EMPTY] = CreateTexture(renderer, gamepad_battery_empty_bmp, gamepad_battery_empty_bmp_len); ctx->battery_texture[1 + SDL_JOYSTICK_POWER_EMPTY] = CreateTexture(renderer, gamepad_battery_empty_bmp, gamepad_battery_empty_bmp_len);
ctx->battery_texture[1 + SDL_JOYSTICK_POWER_LOW] = CreateTexture(renderer, gamepad_battery_low_bmp, gamepad_battery_low_bmp_len); ctx->battery_texture[1 + SDL_JOYSTICK_POWER_LOW] = CreateTexture(renderer, gamepad_battery_low_bmp, gamepad_battery_low_bmp_len);
@ -209,13 +222,13 @@ void SetGamepadImageShowingFront(GamepadImage *ctx, SDL_bool showing_front)
ctx->showing_front = showing_front; ctx->showing_front = showing_front;
} }
void SetGamepadImageReverseDiamond(GamepadImage *ctx, SDL_bool reverse_diamond) void SetGamepadImageFaceStyle(GamepadImage *ctx, GamepadImageFaceStyle face_style)
{ {
if (!ctx) { if (!ctx) {
return; return;
} }
ctx->reverse_diamond = reverse_diamond; ctx->face_style = face_style;
} }
void SetGamepadImageShowingBattery(GamepadImage *ctx, SDL_bool showing_battery) void SetGamepadImageShowingBattery(GamepadImage *ctx, SDL_bool showing_battery)
@ -331,7 +344,7 @@ SDL_GamepadButton GetGamepadImageButtonAt(GamepadImage *ctx, float x, float y)
rect.w = (float)ctx->button_width; rect.w = (float)ctx->button_width;
rect.h = (float)ctx->button_height; rect.h = (float)ctx->button_height;
if (SDL_PointInRectFloat(&point, &rect)) { if (SDL_PointInRectFloat(&point, &rect)) {
return GetRemappedButton(ctx->reverse_diamond, (SDL_GamepadButton)i); return GetRemappedButton(ctx->face_style, (SDL_GamepadButton)i);
} }
} }
} }
@ -404,14 +417,25 @@ void UpdateGamepadImageFromGamepad(GamepadImage *ctx, SDL_Gamepad *gamepad)
char *mapping = SDL_GetGamepadMapping(gamepad); char *mapping = SDL_GetGamepadMapping(gamepad);
if (mapping) { if (mapping) {
SDL_GamepadType gamepad_type = SDL_GetGamepadType(gamepad); SDL_GamepadType gamepad_type = SDL_GetGamepadType(gamepad);
if (gamepad_type == SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO || switch (gamepad_type) {
gamepad_type == SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT || case SDL_GAMEPAD_TYPE_PS3:
gamepad_type == SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT || case SDL_GAMEPAD_TYPE_PS4:
gamepad_type == SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR || case SDL_GAMEPAD_TYPE_PS5:
SDL_strstr(mapping, "SDL_GAMECONTROLLER_USE_BUTTON_LABELS")) { ctx->face_style = GAMEPAD_IMAGE_FACE_SONY;
ctx->reverse_diamond = SDL_TRUE; break;
} else { case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO:
ctx->reverse_diamond = SDL_FALSE; case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT:
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT:
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR:
ctx->face_style = GAMEPAD_IMAGE_FACE_BAYX;
break;
default:
if (SDL_strstr(mapping, "SDL_GAMECONTROLLER_USE_BUTTON_LABELS")) {
ctx->face_style = GAMEPAD_IMAGE_FACE_BAYX;
} else {
ctx->face_style = GAMEPAD_IMAGE_FACE_ABXY;
}
break;
} }
SDL_free(mapping); SDL_free(mapping);
} }
@ -471,16 +495,11 @@ void RenderGamepadImage(GamepadImage *ctx)
{ {
SDL_FRect dst; SDL_FRect dst;
int i; int i;
Uint8 r, g, b, a;
char label[32];
SDL_bool invert_color = SDL_FALSE;
if (!ctx) { if (!ctx) {
return; return;
} }
SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
dst.x = (float)ctx->x; dst.x = (float)ctx->x;
dst.y = (float)ctx->y; dst.y = (float)ctx->y;
dst.w = (float)ctx->gamepad_width; dst.w = (float)ctx->gamepad_width;
@ -488,46 +507,32 @@ void RenderGamepadImage(GamepadImage *ctx)
if (ctx->showing_front) { if (ctx->showing_front) {
SDL_RenderTexture(ctx->renderer, ctx->front_texture, NULL, &dst); SDL_RenderTexture(ctx->renderer, ctx->front_texture, NULL, &dst);
dst.x = (float)ctx->x + 363;
dst.y = (float)ctx->y + 116;
dst.w = (float)ctx->face_width;
dst.h = (float)ctx->face_height;
switch (ctx->face_style) {
case GAMEPAD_IMAGE_FACE_ABXY:
SDL_RenderTexture(ctx->renderer, ctx->face_abxy_texture, NULL, &dst);
break;
case GAMEPAD_IMAGE_FACE_BAYX:
SDL_RenderTexture(ctx->renderer, ctx->face_bayx_texture, NULL, &dst);
break;
case GAMEPAD_IMAGE_FACE_SONY:
SDL_RenderTexture(ctx->renderer, ctx->face_sony_texture, NULL, &dst);
break;
default:
break;
}
} else { } else {
SDL_RenderTexture(ctx->renderer, ctx->back_texture, NULL, &dst); SDL_RenderTexture(ctx->renderer, ctx->back_texture, NULL, &dst);
} }
for (i = 0; i < SDL_arraysize(button_positions); ++i) { for (i = 0; i < SDL_arraysize(button_positions); ++i) {
SDL_GamepadButton button_position = GetRemappedButton(ctx->reverse_diamond, (SDL_GamepadButton)i);
switch (i) {
case SDL_GAMEPAD_BUTTON_A:
SDL_strlcpy(label, "A", sizeof(label));
break;
case SDL_GAMEPAD_BUTTON_B:
SDL_strlcpy(label, "B", sizeof(label));
break;
case SDL_GAMEPAD_BUTTON_X:
SDL_strlcpy(label, "X", sizeof(label));
break;
case SDL_GAMEPAD_BUTTON_Y:
SDL_strlcpy(label, "Y", sizeof(label));
break;
default:
*label = '\0';
break;
}
if (*label != '\0') {
dst.x = (float)ctx->x + button_positions[button_position].x - (float)(FONT_CHARACTER_SIZE * SDL_strlen(label)) / 2;
dst.y = (float)ctx->y + button_positions[button_position].y - (float)FONT_CHARACTER_SIZE / 2;
dst.w = (float)FONT_CHARACTER_SIZE;
dst.h = (float)FONT_CHARACTER_SIZE;
if (button_position == SDL_GAMEPAD_BUTTON_B || button_position == SDL_GAMEPAD_BUTTON_X) {
SDL_SetRenderDrawColor(ctx->renderer, ~r, ~g, ~b, a);
SDLTest_DrawString(ctx->renderer, dst.x, dst.y, label);
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
} else {
SDLTest_DrawString(ctx->renderer, dst.x, dst.y, label);
}
}
if (ctx->buttons[i]) { if (ctx->buttons[i]) {
SDL_GamepadButton button_position = GetRemappedButton(ctx->face_style, (SDL_GamepadButton)i);
SDL_bool on_front = SDL_TRUE; SDL_bool on_front = SDL_TRUE;
if (i >= SDL_GAMEPAD_BUTTON_PADDLE1 && i <= SDL_GAMEPAD_BUTTON_PADDLE4) { if (i >= SDL_GAMEPAD_BUTTON_PADDLE1 && i <= SDL_GAMEPAD_BUTTON_PADDLE4) {
@ -605,6 +610,9 @@ void DestroyGamepadImage(GamepadImage *ctx)
SDL_DestroyTexture(ctx->front_texture); SDL_DestroyTexture(ctx->front_texture);
SDL_DestroyTexture(ctx->back_texture); SDL_DestroyTexture(ctx->back_texture);
SDL_DestroyTexture(ctx->face_abxy_texture);
SDL_DestroyTexture(ctx->face_bayx_texture);
SDL_DestroyTexture(ctx->face_sony_texture);
for (i = 0; i < SDL_arraysize(ctx->battery_texture); ++i) { for (i = 0; i < SDL_arraysize(ctx->battery_texture); ++i) {
SDL_DestroyTexture(ctx->battery_texture[i]); SDL_DestroyTexture(ctx->battery_texture[i]);
} }

View File

@ -14,10 +14,18 @@
typedef struct GamepadImage GamepadImage; typedef struct GamepadImage GamepadImage;
typedef enum
{
GAMEPAD_IMAGE_FACE_BLANK,
GAMEPAD_IMAGE_FACE_ABXY,
GAMEPAD_IMAGE_FACE_BAYX,
GAMEPAD_IMAGE_FACE_SONY,
} GamepadImageFaceStyle;
extern GamepadImage *CreateGamepadImage(SDL_Renderer *renderer); extern GamepadImage *CreateGamepadImage(SDL_Renderer *renderer);
extern void SetGamepadImagePosition(GamepadImage *ctx, int x, int y); extern void SetGamepadImagePosition(GamepadImage *ctx, int x, int y);
extern void SetGamepadImageShowingFront(GamepadImage *ctx, SDL_bool showing_front); extern void SetGamepadImageShowingFront(GamepadImage *ctx, SDL_bool showing_front);
extern void SetGamepadImageReverseDiamond(GamepadImage *ctx, SDL_bool reverse_diamond); extern void SetGamepadImageFaceStyle(GamepadImage *ctx, GamepadImageFaceStyle face_style);
extern void SetGamepadImageShowingBattery(GamepadImage *ctx, SDL_bool showing_battery); extern void SetGamepadImageShowingBattery(GamepadImage *ctx, SDL_bool showing_battery);
extern void SetGamepadImageShowingTouchpad(GamepadImage *ctx, SDL_bool showing_touchpad); extern void SetGamepadImageShowingTouchpad(GamepadImage *ctx, SDL_bool showing_touchpad);
extern void GetGamepadImageArea(GamepadImage *ctx, int *x, int *y, int *width, int *height); extern void GetGamepadImageArea(GamepadImage *ctx, int *x, int *y, int *width, int *height);