configure_input_simple.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 HANDHELD_INDEX = 8;
  32. void HandheldOnProfileSelect() {
  33. Settings::values.players[HANDHELD_INDEX].connected = true;
  34. Settings::values.players[HANDHELD_INDEX].type = Settings::ControllerType::DualJoycon;
  35. for (std::size_t player = 0; player < HANDHELD_INDEX; ++player) {
  36. Settings::values.players[player].connected = false;
  37. }
  38. Settings::values.use_docked_mode = false;
  39. Settings::values.keyboard_enabled = false;
  40. Settings::values.mouse_enabled = false;
  41. Settings::values.debug_pad_enabled = false;
  42. Settings::values.touchscreen.enabled = true;
  43. }
  44. void DualJoyconsDockedOnProfileSelect() {
  45. Settings::values.players[0].connected = true;
  46. Settings::values.players[0].type = Settings::ControllerType::DualJoycon;
  47. for (std::size_t player = 1; player <= HANDHELD_INDEX; ++player) {
  48. Settings::values.players[player].connected = false;
  49. }
  50. Settings::values.use_docked_mode = true;
  51. Settings::values.keyboard_enabled = false;
  52. Settings::values.mouse_enabled = false;
  53. Settings::values.debug_pad_enabled = false;
  54. Settings::values.touchscreen.enabled = false;
  55. }
  56. // Name, OnProfileSelect (called when selected in drop down), OnConfigure (called when configure
  57. // is clicked)
  58. using InputProfile = std::tuple<const char*, void (*)(), void (*)(ConfigureInputSimple*)>;
  59. constexpr std::array<InputProfile, 3> INPUT_PROFILES{{
  60. {QT_TR_NOOP("Single Player - Handheld - Undocked"), HandheldOnProfileSelect,
  61. [](ConfigureInputSimple* caller) {
  62. CallConfigureDialog<ConfigureInputPlayer>(caller, HANDHELD_INDEX, false);
  63. }},
  64. {QT_TR_NOOP("Single Player - Dual Joycons - Docked"), DualJoyconsDockedOnProfileSelect,
  65. [](ConfigureInputSimple* caller) {
  66. CallConfigureDialog<ConfigureInputPlayer>(caller, 1, false);
  67. }},
  68. {QT_TR_NOOP("Custom"), [] {}, CallConfigureDialog<ConfigureInput>},
  69. }};
  70. } // namespace
  71. void ApplyInputProfileConfiguration(int profile_index) {
  72. std::get<1>(
  73. INPUT_PROFILES.at(std::min(profile_index, static_cast<int>(INPUT_PROFILES.size() - 1))))();
  74. }
  75. ConfigureInputSimple::ConfigureInputSimple(QWidget* parent)
  76. : QWidget(parent), ui(std::make_unique<Ui::ConfigureInputSimple>()) {
  77. ui->setupUi(this);
  78. for (const auto& profile : INPUT_PROFILES) {
  79. const QString label = tr(std::get<0>(profile));
  80. ui->profile_combobox->addItem(label, label);
  81. }
  82. connect(ui->profile_combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
  83. &ConfigureInputSimple::OnSelectProfile);
  84. connect(ui->profile_configure, &QPushButton::clicked, this, &ConfigureInputSimple::OnConfigure);
  85. LoadConfiguration();
  86. }
  87. ConfigureInputSimple::~ConfigureInputSimple() = default;
  88. void ConfigureInputSimple::ApplyConfiguration() {
  89. auto index = ui->profile_combobox->currentIndex();
  90. // Make the stored index for "Custom" very large so that if new profiles are added it
  91. // doesn't change.
  92. if (index >= static_cast<int>(INPUT_PROFILES.size() - 1)) {
  93. index = std::numeric_limits<int>::max();
  94. }
  95. UISettings::values.profile_index = index;
  96. }
  97. void ConfigureInputSimple::changeEvent(QEvent* event) {
  98. if (event->type() == QEvent::LanguageChange) {
  99. RetranslateUI();
  100. }
  101. QWidget::changeEvent(event);
  102. }
  103. void ConfigureInputSimple::RetranslateUI() {
  104. ui->retranslateUi(this);
  105. }
  106. void ConfigureInputSimple::LoadConfiguration() {
  107. const auto index = UISettings::values.profile_index;
  108. if (index >= static_cast<int>(INPUT_PROFILES.size()) || index < 0) {
  109. ui->profile_combobox->setCurrentIndex(static_cast<int>(INPUT_PROFILES.size() - 1));
  110. } else {
  111. ui->profile_combobox->setCurrentIndex(index);
  112. }
  113. }
  114. void ConfigureInputSimple::OnSelectProfile(int index) {
  115. const auto old_docked = Settings::values.use_docked_mode;
  116. ApplyInputProfileConfiguration(index);
  117. OnDockedModeChanged(old_docked, Settings::values.use_docked_mode);
  118. }
  119. void ConfigureInputSimple::OnConfigure() {
  120. std::get<2>(INPUT_PROFILES.at(ui->profile_combobox->currentIndex()))(this);
  121. }