Skip to content

Commit

Permalink
Improve gamepad cpnfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
drhelius committed Apr 19, 2020
1 parent 09775eb commit 57ab08d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 48 deletions.
90 changes: 66 additions & 24 deletions platforms/desktop-shared/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,22 @@ static int sdl_init(void)

SDL_SetWindowMinimumSize(sdl_window, 644, 602);

application_gamepad = SDL_JoystickOpen(0);

if(!application_gamepad)
for (int i = 0; i < SDL_NumJoysticks(); ++i)
{
Log("Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError());
if (SDL_IsGameController(i))
{
application_gamepad = SDL_GameControllerOpen(i);
if(!application_gamepad)
{
Log("Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError());
}
else
{
Log("Game controller %d correctly detected", i);
}

break;
}
}

int w, h;
Expand All @@ -142,7 +153,7 @@ static int sdl_init(void)

static void sdl_destroy(void)
{
SDL_JoystickClose(application_gamepad);
SDL_GameControllerClose(application_gamepad);
ImGui_ImplSDL2_Shutdown();
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(sdl_window);
Expand Down Expand Up @@ -194,62 +205,93 @@ static void sdl_events_emu(const SDL_Event* event)
}
break;

case SDL_JOYBUTTONDOWN:
case SDL_CONTROLLERBUTTONDOWN:
{
if (!config_input.gamepad)
break;

if (event->jbutton.button == config_input.gamepad_b)
if (event->cbutton.button == config_input.gamepad_b)
emu_key_pressed(B_Key);
else if (event->jbutton.button == config_input.gamepad_a)
else if (event->cbutton.button == config_input.gamepad_a)
emu_key_pressed(A_Key);
else if (event->jbutton.button == config_input.gamepad_select)
else if (event->cbutton.button == config_input.gamepad_select)
emu_key_pressed(Select_Key);
else if (event->jbutton.button == config_input.gamepad_start)
else if (event->cbutton.button == config_input.gamepad_start)
emu_key_pressed(Start_Key);

if (config_input.gamepad_directional == 1)
break;

if (event->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP)
emu_key_pressed(Up_Key);
else if (event->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN)
emu_key_pressed(Down_Key);
else if (event->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT)
emu_key_pressed(Left_Key);
else if (event->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT)
emu_key_pressed(Right_Key);
}
break;

case SDL_JOYBUTTONUP:
case SDL_CONTROLLERBUTTONUP:
{
if (!config_input.gamepad)
break;

if (event->jbutton.button == config_input.gamepad_b)
if (event->cbutton.button == config_input.gamepad_b)
emu_key_released(B_Key);
else if (event->jbutton.button == config_input.gamepad_a)
else if (event->cbutton.button == config_input.gamepad_a)
emu_key_released(A_Key);
else if (event->jbutton.button == config_input.gamepad_select)
else if (event->cbutton.button == config_input.gamepad_select)
emu_key_released(Select_Key);
else if (event->jbutton.button == config_input.gamepad_start)
else if (event->cbutton.button == config_input.gamepad_start)
emu_key_released(Start_Key);

if (config_input.gamepad_directional == 1)
break;

if (event->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP)
emu_key_released(Up_Key);
else if (event->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN)
emu_key_released(Down_Key);
else if (event->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT)
emu_key_released(Left_Key);
else if (event->cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT)
emu_key_released(Right_Key);
}
break;

case SDL_JOYAXISMOTION:
case SDL_CONTROLLERAXISMOTION:
{
if (!config_input.gamepad)
break;

if (config_input.gamepad_directional == 0)
break;

const int STICK_DEAD_ZONE = 8000;

if(event->jaxis.axis == config_input.gamepad_x_axis)
if(event->caxis.axis == config_input.gamepad_x_axis)
{
int x_motion = event->jaxis.value * (config_input.gamepad_invert_x_axis ? -1 : 1);
if (x_motion < 0)
int x_motion = event->caxis.value * (config_input.gamepad_invert_x_axis ? -1 : 1);

if (x_motion < -STICK_DEAD_ZONE)
emu_key_pressed(Left_Key);
else if (x_motion > 0)
else if (x_motion > STICK_DEAD_ZONE)
emu_key_pressed(Right_Key);
else
{
emu_key_released(Left_Key);
emu_key_released(Right_Key);
}
}
else if(event->jaxis.axis == config_input.gamepad_y_axis)
else if(event->caxis.axis == config_input.gamepad_y_axis)
{
int y_motion = event->jaxis.value * (config_input.gamepad_invert_y_axis ? -1 : 1);
if (y_motion < 0)
int y_motion = event->caxis.value * (config_input.gamepad_invert_y_axis ? -1 : 1);

if (y_motion < -STICK_DEAD_ZONE)
emu_key_pressed(Up_Key);
else if (y_motion > 0)
else if (y_motion > STICK_DEAD_ZONE)
emu_key_pressed(Down_Key);
else
{
Expand Down
2 changes: 1 addition & 1 deletion platforms/desktop-shared/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define EXTERN extern
#endif

EXTERN SDL_Joystick* application_gamepad;
EXTERN SDL_GameController* application_gamepad;
EXTERN float application_display_scale;
EXTERN SDL_version application_sdl_build_version;
EXTERN SDL_version application_sdl_link_version;
Expand Down
14 changes: 8 additions & 6 deletions platforms/desktop-shared/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,15 @@ void config_read(void)
config_input.key_select = (SDL_Scancode)read_int("Input", "KeySelect", SDL_SCANCODE_SPACE);

config_input.gamepad = read_bool("Input", "Gamepad", true);
config_input.gamepad_directional = read_int("Input", "GamepadDirectional", 0);
config_input.gamepad_invert_x_axis = read_bool("Input", "GamepadInvertX", false);
config_input.gamepad_invert_y_axis = read_bool("Input", "GamepadInvertY", false);
config_input.gamepad_a = read_int("Input", "GamepadA", 1);
config_input.gamepad_b = read_int("Input", "GamepadB", 2);
config_input.gamepad_start = read_int("Input", "GamepadStart", 9);
config_input.gamepad_select = read_int("Input", "GamepadSelect", 8);
config_input.gamepad_x_axis = read_int("Input", "GamepadX", 0);
config_input.gamepad_y_axis = read_int("Input", "GamepadY", 1);
config_input.gamepad_a = read_int("Input", "GamepadA", SDL_CONTROLLER_BUTTON_B);
config_input.gamepad_b = read_int("Input", "GamepadB", SDL_CONTROLLER_BUTTON_A);
config_input.gamepad_start = read_int("Input", "GamepadStart", SDL_CONTROLLER_BUTTON_START);
config_input.gamepad_select = read_int("Input", "GamepadSelect", SDL_CONTROLLER_BUTTON_BACK);
config_input.gamepad_x_axis = read_int("Input", "GamepadX", SDL_CONTROLLER_AXIS_LEFTX);
config_input.gamepad_y_axis = read_int("Input", "GamepadY", SDL_CONTROLLER_AXIS_LEFTY);

Log("Settings loaded");
}
Expand Down Expand Up @@ -177,6 +178,7 @@ void config_write(void)
write_int("Input", "KeySelect", config_input.key_select);

write_bool("Input", "Gamepad", config_input.gamepad);
write_int("Input", "GamepadDirectional", config_input.gamepad_directional);
write_bool("Input", "GamepadInvertX", config_input.gamepad_invert_x_axis);
write_bool("Input", "GamepadInvertY", config_input.gamepad_invert_y_axis);
write_int("Input", "GamepadA", config_input.gamepad_a);
Expand Down
13 changes: 7 additions & 6 deletions platforms/desktop-shared/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ struct config_Input
SDL_Scancode key_select = SDL_SCANCODE_SPACE;

bool gamepad = true;
int gamepad_directional = 0;
bool gamepad_invert_x_axis = false;
bool gamepad_invert_y_axis = false;
int gamepad_a = 1;
int gamepad_b = 2;
int gamepad_start = 9;
int gamepad_select = 8;
int gamepad_x_axis = 0;
int gamepad_y_axis = 1;
int gamepad_a = SDL_CONTROLLER_BUTTON_B;
int gamepad_b = SDL_CONTROLLER_BUTTON_A;
int gamepad_start = SDL_CONTROLLER_BUTTON_START;
int gamepad_select = SDL_CONTROLLER_BUTTON_BACK;
int gamepad_x_axis = SDL_CONTROLLER_AXIS_LEFTX;
int gamepad_y_axis = SDL_CONTROLLER_AXIS_LEFTY;
};

EXTERN mINI::INIFile* config_ini_file;
Expand Down
32 changes: 21 additions & 11 deletions platforms/desktop-shared/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,17 +426,27 @@ static void main_menu(void)

ImGui::Separator();

ImGui::MenuItem("Enable Gamepad", "", &config_input.gamepad);

if (ImGui::BeginMenu("Gamepad Configuration"))
if (ImGui::BeginMenu("Gamepad"))
{
gamepad_configuration_item("A:", &config_input.gamepad_a);
gamepad_configuration_item("B:", &config_input.gamepad_b);
gamepad_configuration_item("START:", &config_input.gamepad_start);
gamepad_configuration_item("SELECT:", &config_input.gamepad_select);
ImGui::MenuItem("Enable Gamepad", "", &config_input.gamepad);

popup_modal_gamepad();
if (ImGui::BeginMenu("Directional Controls"))
{
ImGui::Combo("##directional", &config_input.gamepad_directional, "D-pad\0Left Analog Stick\0\0");
ImGui::EndMenu();
}

if (ImGui::BeginMenu("Button Configuration"))
{
gamepad_configuration_item("A:", &config_input.gamepad_a);
gamepad_configuration_item("B:", &config_input.gamepad_b);
gamepad_configuration_item("START:", &config_input.gamepad_start);
gamepad_configuration_item("SELECT:", &config_input.gamepad_select);

popup_modal_gamepad();

ImGui::EndMenu();
}
ImGui::EndMenu();
}

Expand Down Expand Up @@ -671,7 +681,7 @@ static void gamepad_configuration_item(const char* text, int* button)
ImGui::Text("%s", text);
ImGui::SameLine(70);

static const char* gamepad_names[16] = {"0", "A", "B" ,"3", "L", "R", "6", "7", "SELECT", "START", "10", "11", "12", "13", "14", "15"};
static const char* gamepad_names[16] = {"A", "B", "X" ,"Y", "BACK", "GUID", "START", "L3", "R3", "L1", "R1", "UP", "DOWN", "LEFT", "RIGHT", "15"};

char button_label[256];
sprintf(button_label, "%s##%s", gamepad_names[*button], text);
Expand Down Expand Up @@ -720,9 +730,9 @@ static void popup_modal_gamepad(void)
ImGui::Text("Press any button in your gamepad...\n\n");
ImGui::Separator();

for (int i = 0; i < 16; i++)
for (int i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++)
{
if (SDL_JoystickGetButton(application_gamepad, i))
if (SDL_GameControllerGetButton(application_gamepad, (SDL_GameControllerButton)i))
{
*configured_button = i;
ImGui::CloseCurrentPopup();
Expand Down

0 comments on commit 57ab08d

Please sign in to comment.