configure_input_simple.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <tuple>
  6. #include "ui_configure_input_simple.h"
  7. #include "yuzu/configuration/configure_input.h"
  8. #include "yuzu/configuration/configure_input_player.h"
  9. #include "yuzu/configuration/configure_input_simple.h"
  10. #include "yuzu/uisettings.h"
  11. namespace {
  12. template <typename Dialog, typename... Args>
  13. void CallConfigureDialog(ConfigureInputSimple* caller, Args&&... args) {
  14. caller->ApplyConfiguration();
  15. Dialog dialog(caller, std::forward<Args>(args)...);
  16. const auto res = dialog.exec();
  17. if (res == QDialog::Accepted) {
  18. dialog.ApplyConfiguration();
  19. }
  20. }
  21. // OnProfileSelect functions should (when applicable):
  22. // - Set controller types
  23. // - Set controller enabled
  24. // - Set docked mode
  25. // - Set advanced controller config/enabled (i.e. debug, kbd, mouse, touch)
  26. //
  27. // OnProfileSelect function should NOT however:
  28. // - Reset any button mappings
  29. // - Open any dialogs
  30. // - Block in any way
  31. constexpr std::size_t PLAYER_0_INDEX = 0;
  32. constexpr std::size_t HANDHELD_INDEX = 8;
  33. void HandheldOnProfileSelect() {
  34. Settings::values.players[HANDHELD_INDEX].connected = true;
  35. Settings::values.players[HANDHELD_INDEX].type = Settings::ControllerType::DualJoycon;
  36. for (std::size_t player = 0; player < HANDHELD_INDEX; ++player) {
  37. Settings::values.players[player].connected = false;
  38. }
  39. Settings::values.use_docked_mode = false;
  40. Settings::values.keyboard_enabled = false;
  41. Settings::values.mouse_enabled = false;
  42. Settings::values.debug_pad_enabled = false;
  43. Settings::values.touchscreen.enabled = true;
  44. }
  45. void DualJoyconsDockedOnProfileSelect() {
  46. Settings::values.players[PLAYER_0_INDEX].connected = true;
  47. Settings::values.players[PLAYER_0_INDEX].type = Settings::ControllerType::DualJoycon;
  48. for (std::size_t player = 1; player <= HANDHELD_INDEX; ++player) {
  49. Settings::values.players[player].connected = false;
  50. }
  51. Settings::values.use_docked_mode = true;
  52. Settings::values.keyboard_enabled = false;
  53. Settings::values.mouse_enabled = false;
  54. Settings::values.debug_pad_enabled = false;
  55. Settings::values.touchscreen.enabled = true;
  56. }
  57. // Name, OnProfileSelect (called when selected in drop down), OnConfigure (called when configure
  58. // is clicked)
  59. using InputProfile = std::tuple<const char*, void (*)(), void (*)(ConfigureInputSimple*)>;
  60. constexpr std::array<InputProfile, 3> INPUT_PROFILES{{
  61. {QT_TR_NOOP("Single Player - Handheld - Undocked"), HandheldOnProfileSelect,
  62. [](ConfigureInputSimple* caller) {
  63. CallConfigureDialog<ConfigureInputPlayer>(caller, HANDHELD_INDEX, false);
  64. }},
  65. {QT_TR_NOOP("Single Player - Dual Joycons - Docked"), DualJoyconsDockedOnProfileSelect,
  66. [](ConfigureInputSimple* caller) {
  67. CallConfigureDialog<ConfigureInputPlayer>(caller, PLAYER_0_INDEX, false);
  68. }},
  69. {QT_TR_NOOP("Custom"), [] {}, CallConfigureDialog<ConfigureInput>},
  70. }};
  71. } // namespace
  72. void ApplyInputProfileConfiguration(int profile_index) {
  73. std::get<1>(
  74. INPUT_PROFILES.at(std::min(profile_index, static_cast<int>(INPUT_PROFILES.size() - 1))))();
  75. }
  76. ConfigureInputSimple::ConfigureInputSimple(QWidget* parent)
  77. : QWidget(parent), ui(std::make_unique<Ui::ConfigureInputSimple>()) {
  78. ui->setupUi(this);
  79. for (const auto& profile : INPUT_PROFILES) {
  80. const QString label = tr(std::get<0>(profile));
  81. ui->profile_combobox->addItem(label, label);
  82. }
  83. connect(ui->profile_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  84. &ConfigureInputSimple::OnSelectProfile);
  85. connect(ui->profile_configure, &QPushButton::clicked, this, &ConfigureInputSimple::OnConfigure);
  86. LoadConfiguration();
  87. }
  88. ConfigureInputSimple::~ConfigureInputSimple() = default;
  89. void ConfigureInputSimple::ApplyConfiguration() {
  90. auto index = ui->profile_combobox->currentIndex();
  91. // Make the stored index for "Custom" very large so that if new profiles are added it
  92. // doesn't change.
  93. if (index >= static_cast<int>(INPUT_PROFILES.size() - 1)) {
  94. index = std::numeric_limits<int>::max();
  95. }
  96. UISettings::values.profile_index = index;
  97. }
  98. void ConfigureInputSimple::changeEvent(QEvent* event) {
  99. if (event->type() == QEvent::LanguageChange) {
  100. RetranslateUI();
  101. }
  102. QWidget::changeEvent(event);
  103. }
  104. void ConfigureInputSimple::RetranslateUI() {
  105. ui->retranslateUi(this);
  106. }
  107. void ConfigureInputSimple::LoadConfiguration() {
  108. const auto index = UISettings::values.profile_index;
  109. if (index >= static_cast<int>(INPUT_PROFILES.size()) || index < 0) {
  110. ui->profile_combobox->setCurrentIndex(static_cast<int>(INPUT_PROFILES.size() - 1));
  111. } else {
  112. ui->profile_combobox->setCurrentIndex(index);
  113. }
  114. }
  115. void ConfigureInputSimple::OnSelectProfile(int index) {
  116. const auto old_docked = Settings::values.use_docked_mode;
  117. ApplyInputProfileConfiguration(index);
  118. OnDockedModeChanged(old_docked, Settings::values.use_docked_mode);
  119. }
  120. void ConfigureInputSimple::OnConfigure() {
  121. std::get<2>(INPUT_PROFILES.at(ui->profile_combobox->currentIndex()))(this);
  122. }