configure_input_per_game.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-FileCopyrightText: 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/settings.h"
  4. #include "core/core.h"
  5. #include "core/hid/emulated_controller.h"
  6. #include "core/hid/hid_core.h"
  7. #include "frontend_common/config.h"
  8. #include "ui_configure_input_per_game.h"
  9. #include "yuzu/configuration/configure_input_per_game.h"
  10. #include "yuzu/configuration/input_profiles.h"
  11. ConfigureInputPerGame::ConfigureInputPerGame(Core::System& system_, QtConfig* config_,
  12. QWidget* parent)
  13. : QWidget(parent), ui(std::make_unique<Ui::ConfigureInputPerGame>()),
  14. profiles(std::make_unique<InputProfiles>()), system{system_}, config{config_} {
  15. ui->setupUi(this);
  16. const std::array labels = {
  17. ui->label_player_1, ui->label_player_2, ui->label_player_3, ui->label_player_4,
  18. ui->label_player_5, ui->label_player_6, ui->label_player_7, ui->label_player_8,
  19. };
  20. profile_comboboxes = {
  21. ui->profile_player_1, ui->profile_player_2, ui->profile_player_3, ui->profile_player_4,
  22. ui->profile_player_5, ui->profile_player_6, ui->profile_player_7, ui->profile_player_8,
  23. };
  24. Settings::values.players.SetGlobal(false);
  25. const auto& profile_names = profiles->GetInputProfileNames();
  26. const auto populate_profiles = [this, &profile_names](size_t player_index) {
  27. const auto previous_profile =
  28. Settings::values.players.GetValue()[player_index].profile_name;
  29. auto* const player_combobox = profile_comboboxes[player_index];
  30. player_combobox->addItem(tr("Use global input configuration"));
  31. for (size_t index = 0; index < profile_names.size(); ++index) {
  32. const auto& profile_name = profile_names[index];
  33. player_combobox->addItem(QString::fromStdString(profile_name));
  34. if (profile_name == previous_profile) {
  35. // offset by 1 since the first element is the global config
  36. player_combobox->setCurrentIndex(static_cast<int>(index + 1));
  37. }
  38. }
  39. };
  40. for (size_t index = 0; index < profile_comboboxes.size(); ++index) {
  41. labels[index]->setText(tr("Player %1 profile").arg(index + 1));
  42. populate_profiles(index);
  43. }
  44. LoadConfiguration();
  45. }
  46. void ConfigureInputPerGame::ApplyConfiguration() {
  47. LoadConfiguration();
  48. SaveConfiguration();
  49. }
  50. void ConfigureInputPerGame::LoadConfiguration() {
  51. static constexpr size_t HANDHELD_INDEX = 8;
  52. auto& hid_core = system.HIDCore();
  53. for (size_t player_index = 0; player_index < profile_comboboxes.size(); ++player_index) {
  54. Settings::values.players.SetGlobal(false);
  55. auto* emulated_controller = hid_core.GetEmulatedControllerByIndex(player_index);
  56. auto* const player_combobox = profile_comboboxes[player_index];
  57. const auto selection_index = player_combobox->currentIndex();
  58. if (selection_index == 0) {
  59. Settings::values.players.GetValue()[player_index].profile_name = "";
  60. if (player_index == 0) {
  61. Settings::values.players.GetValue()[HANDHELD_INDEX] = {};
  62. }
  63. Settings::values.players.SetGlobal(true);
  64. emulated_controller->ReloadFromSettings();
  65. continue;
  66. }
  67. const auto profile_name = player_combobox->itemText(selection_index).toStdString();
  68. if (profile_name.empty()) {
  69. continue;
  70. }
  71. auto& player = Settings::values.players.GetValue()[player_index];
  72. player.profile_name = profile_name;
  73. // Read from the profile into the custom player settings
  74. profiles->LoadProfile(profile_name, player_index);
  75. // Make sure the controller is connected
  76. player.connected = true;
  77. emulated_controller->ReloadFromSettings();
  78. if (player_index > 0) {
  79. continue;
  80. }
  81. // Handle Handheld cases
  82. auto& handheld_player = Settings::values.players.GetValue()[HANDHELD_INDEX];
  83. auto* handheld_controller = hid_core.GetEmulatedController(Core::HID::NpadIdType::Handheld);
  84. if (player.controller_type == Settings::ControllerType::Handheld) {
  85. handheld_player = player;
  86. } else {
  87. handheld_player = {};
  88. }
  89. handheld_controller->ReloadFromSettings();
  90. }
  91. }
  92. void ConfigureInputPerGame::SaveConfiguration() {
  93. Settings::values.players.SetGlobal(false);
  94. // Clear all controls from the config in case the user reverted back to globals
  95. config->ClearControlPlayerValues();
  96. for (size_t index = 0; index < Settings::values.players.GetValue().size(); ++index) {
  97. config->SaveQtControlPlayerValues(index);
  98. }
  99. }