Skip to content

Commit 9a08fca

Browse files
committed
Add gamepad support for L2 / R2 buttons (triggers)
1 parent 5cd0246 commit 9a08fca

File tree

3 files changed

+100
-26
lines changed

3 files changed

+100
-26
lines changed

platforms/desktop-shared/application.cpp

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -638,40 +638,75 @@ static void sdl_events_emu(const SDL_Event* event)
638638
if (!config_input.gamepad)
639639
break;
640640

641-
if (config_input.gamepad_directional == 0)
642-
break;
643-
644641
if (event->caxis.which != id)
645642
break;
646643

647-
const int STICK_DEAD_ZONE = 8000;
648-
649-
if(event->caxis.axis == config_input.gamepad_x_axis)
644+
if (config_input.gamepad_directional == 1)
650645
{
651-
int x_motion = event->caxis.value * (config_input.gamepad_invert_x_axis ? -1 : 1);
646+
const int STICK_DEAD_ZONE = 8000;
652647

653-
if (x_motion < -STICK_DEAD_ZONE)
654-
emu_key_pressed(Left_Key);
655-
else if (x_motion > STICK_DEAD_ZONE)
656-
emu_key_pressed(Right_Key);
657-
else
648+
if(event->caxis.axis == config_input.gamepad_x_axis)
658649
{
659-
emu_key_released(Left_Key);
660-
emu_key_released(Right_Key);
650+
int x_motion = event->caxis.value * (config_input.gamepad_invert_x_axis ? -1 : 1);
651+
652+
if (x_motion < -STICK_DEAD_ZONE)
653+
emu_key_pressed(Left_Key);
654+
else if (x_motion > STICK_DEAD_ZONE)
655+
emu_key_pressed(Right_Key);
656+
else
657+
{
658+
emu_key_released(Left_Key);
659+
emu_key_released(Right_Key);
660+
}
661+
}
662+
else if(event->caxis.axis == config_input.gamepad_y_axis)
663+
{
664+
int y_motion = event->caxis.value * (config_input.gamepad_invert_y_axis ? -1 : 1);
665+
666+
if (y_motion < -STICK_DEAD_ZONE)
667+
emu_key_pressed(Up_Key);
668+
else if (y_motion > STICK_DEAD_ZONE)
669+
emu_key_pressed(Down_Key);
670+
else
671+
{
672+
emu_key_released(Up_Key);
673+
emu_key_released(Down_Key);
674+
}
661675
}
662676
}
663-
else if(event->caxis.axis == config_input.gamepad_y_axis)
677+
678+
if (event->caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || event->caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT)
664679
{
665-
int y_motion = event->caxis.value * (config_input.gamepad_invert_y_axis ? -1 : 1);
680+
int vbtn = GAMEPAD_VBTN_AXIS_BASE + event->caxis.axis;
681+
bool pressed = event->caxis.value > GAMEPAD_VBTN_AXIS_THRESHOLD;
666682

667-
if (y_motion < -STICK_DEAD_ZONE)
668-
emu_key_pressed(Up_Key);
669-
else if (y_motion > STICK_DEAD_ZONE)
670-
emu_key_pressed(Down_Key);
671-
else
683+
if (config_input.gamepad_a == vbtn)
684+
{
685+
if (pressed)
686+
emu_key_pressed(A_Key);
687+
else
688+
emu_key_released(A_Key);
689+
}
690+
if (config_input.gamepad_b == vbtn)
691+
{
692+
if (pressed)
693+
emu_key_pressed(B_Key);
694+
else
695+
emu_key_released(B_Key);
696+
}
697+
if (config_input.gamepad_start == vbtn)
698+
{
699+
if (pressed)
700+
emu_key_pressed(Start_Key);
701+
else
702+
emu_key_released(Start_Key);
703+
}
704+
if (config_input.gamepad_select == vbtn)
672705
{
673-
emu_key_released(Up_Key);
674-
emu_key_released(Down_Key);
706+
if (pressed)
707+
emu_key_pressed(Select_Key);
708+
else
709+
emu_key_released(Select_Key);
675710
}
676711
}
677712
}

platforms/desktop-shared/application.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
#define EXTERN extern
2929
#endif
3030

31+
#define GAMEPAD_VBTN_AXIS_BASE 1000
32+
#define GAMEPAD_VBTN_AXIS_THRESHOLD 3000
33+
#define GAMEPAD_VBTN_L2 (GAMEPAD_VBTN_AXIS_BASE + SDL_CONTROLLER_AXIS_TRIGGERLEFT)
34+
#define GAMEPAD_VBTN_R2 (GAMEPAD_VBTN_AXIS_BASE + SDL_CONTROLLER_AXIS_TRIGGERRIGHT)
35+
3136
EXTERN SDL_Window* application_sdl_window;
3237
EXTERN SDL_GameController* application_gamepad;
3338
EXTERN int application_added_gamepad_mappings;

platforms/desktop-shared/gui.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,10 +1439,29 @@ static void keyboard_configuration_item(const char* text, SDL_Scancode* key)
14391439
static void gamepad_configuration_item(const char* text, int* button)
14401440
{
14411441
ImGui::Text("%s", text);
1442-
ImGui::SameLine(70);
1442+
ImGui::SameLine(120);
14431443

1444-
static const char* gamepad_names[16] = {"A", "B", "X" ,"Y", "BACK", "GUIDE", "START", "L3", "R3", "L1", "R1", "UP", "DOWN", "LEFT", "RIGHT", "15"};
1445-
const char* button_name = (*button >= 0 && *button < 16) ? gamepad_names[*button] : "";
1444+
const char* button_name = "";
1445+
1446+
if (*button == SDL_CONTROLLER_BUTTON_INVALID)
1447+
{
1448+
button_name = "";
1449+
}
1450+
else if (*button >= 0 && *button < SDL_CONTROLLER_BUTTON_MAX)
1451+
{
1452+
static const char* gamepad_names[21] = {"A", "B", "X" ,"Y", "BACK", "GUIDE", "START", "L3", "R3", "L1", "R1", "UP", "DOWN", "LEFT", "RIGHT", "MISC", "PAD1", "PAD2", "PAD3", "PAD4", "TOUCH"};
1453+
button_name = gamepad_names[*button];
1454+
}
1455+
else if (*button >= GAMEPAD_VBTN_AXIS_BASE)
1456+
{
1457+
int axis = *button - GAMEPAD_VBTN_AXIS_BASE;
1458+
if (axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT)
1459+
button_name = "L2";
1460+
else if (axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT)
1461+
button_name = "R2";
1462+
else
1463+
button_name = "??";
1464+
}
14461465

14471466
char button_label[256];
14481467
snprintf(button_label, sizeof(button_label), "%s##%s", button_name, text);
@@ -1571,6 +1590,21 @@ static void popup_modal_gamepad(void)
15711590
}
15721591
}
15731592

1593+
for (int a = SDL_CONTROLLER_AXIS_LEFTX; a < SDL_CONTROLLER_AXIS_MAX; a++)
1594+
{
1595+
if (a != SDL_CONTROLLER_AXIS_TRIGGERLEFT && a != SDL_CONTROLLER_AXIS_TRIGGERRIGHT)
1596+
continue;
1597+
1598+
Sint16 value = SDL_GameControllerGetAxis(application_gamepad, (SDL_GameControllerAxis)a);
1599+
1600+
if (value > GAMEPAD_VBTN_AXIS_THRESHOLD)
1601+
{
1602+
*configured_button = GAMEPAD_VBTN_AXIS_BASE + a;
1603+
ImGui::CloseCurrentPopup();
1604+
break;
1605+
}
1606+
}
1607+
15741608
if (ImGui::Button("Cancel", ImVec2(120, 0)))
15751609
{
15761610
ImGui::CloseCurrentPopup();

0 commit comments

Comments
 (0)