Skip to content

Commit

Permalink
gui/controllers dialog: Implement swap for controller ports.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zangetsu38 committed Sep 22, 2024
1 parent 77abeaa commit e50573d
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions vita3k/gui/src/controllers_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,39 @@ static void add_bind_to_table(GuiState &gui, EmuEnvState &emuenv, const SDL_Game
ImGui::PopID();
}

void swap_controller_ports(CtrlState &state, int source_port, int dest_port) {
// Check if the ports are valid
if (source_port < 1 || source_port > SCE_CTRL_MAX_WIRELESS_NUM || dest_port < 1 || dest_port > SCE_CTRL_MAX_WIRELESS_NUM) {
LOG_ERROR("Ports are not valid.");
return;
}

// Find the controllers corresponding to the source and destination ports
auto source_controller_it = std::find_if(state.controllers.begin(), state.controllers.end(),
[source_port](const auto &pair) { return pair.second.port == source_port; });
auto dest_controller_it = std::find_if(state.controllers.begin(), state.controllers.end(),
[dest_port](const auto &pair) { return pair.second.port == dest_port; });

// Check that both controllers exist
if (source_controller_it == state.controllers.end() || dest_controller_it == state.controllers.end()) {
LOG_ERROR("Unable to find one or more controllers on the specified ports.");
return;
}

LOG_INFO("Controller on source port {} {} swapped with controller on destination port {} {}", source_port, state.controllers_name[source_port - 1], dest_port, state.controllers_name[dest_port - 1]);

// Swap controllers info and player index
SDL_GameControllerSetPlayerIndex(source_controller_it->second.controller.get(), dest_port - 1);
std::swap(source_controller_it->second.port, dest_controller_it->second.port);
std::swap(source_controller_it->second.has_accel, dest_controller_it->second.has_accel);
std::swap(source_controller_it->second.has_gyro, dest_controller_it->second.has_gyro);
std::swap(source_controller_it->second.has_led, dest_controller_it->second.has_led);

// Swap controller names
std::swap(state.controllers_name[source_port - 1], state.controllers_name[dest_port - 1]);
std::swap(state.controllers_has_motion_support[source_port - 1], state.controllers_has_motion_support[dest_port - 1]);
}

void draw_controllers_dialog(GuiState &gui, EmuEnvState &emuenv) {
const ImVec2 VIEWPORT_POS(emuenv.viewport_pos.x, emuenv.viewport_pos.y);
const ImVec2 VIEWPORT_SIZE(emuenv.viewport_size.x, emuenv.viewport_size.y);
Expand All @@ -185,8 +218,6 @@ void draw_controllers_dialog(GuiState &gui, EmuEnvState &emuenv) {

const auto has_controllers = ctrl.controllers_num > 0;

if (has_controllers)
ImGui::SetNextWindowSize(ImVec2(VIEWPORT_SIZE.x / 2.5f, 0), ImGuiCond_Always);
ImGui::SetNextWindowPos(ImVec2(VIEWPORT_POS.x + (VIEWPORT_SIZE.x / 2.f), VIEWPORT_POS.y + (VIEWPORT_SIZE.y / 2.f)), ImGuiCond_Always, ImVec2(0.5f, 0.5f));
ImGui::Begin("##controllers", &gui.controls_menu.controllers_dialog, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings);
ImGui::SetWindowFontScale(RES_SCALE.x);
Expand Down Expand Up @@ -214,10 +245,24 @@ void draw_controllers_dialog(GuiState &gui, EmuEnvState &emuenv) {
ImGui::Spacing();
ImGui::TableSetColumnIndex(2);
ImGui::TextColored(GUI_COLOR_TEXT_TITLE, "%s", "Motion Support");
std::vector<std::string> port_names;
for (int j = 0; j < ctrl.controllers_num; ++j) {
port_names.push_back(fmt::format("{}", j));
}
std::vector<const char *> port_name_ptrs;
for (const auto &name : port_names) {
port_name_ptrs.push_back(name.c_str());
}
static int selected_port = -1;
for (auto i = 0; i < ctrl.controllers_num; i++) {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text("%-8d", i);
selected_port = i;
ImGui::PushID(i);
ImGui::SetNextItemWidth(50.f * emuenv.dpi_scale);
if (ImGui::Combo("##swap_port", &selected_port, port_name_ptrs.data(), static_cast<int>(port_names.size())))
swap_controller_ports(ctrl, i + 1, selected_port + 1);
ImGui::PopID();
ImGui::TableSetColumnIndex(1);
if (ImGui::Button(ctrl.controllers_name[i]))
rebinds_is_open = true;
Expand Down

0 comments on commit e50573d

Please sign in to comment.