configure_input_player.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <functional>
  7. #include <memory>
  8. #include <optional>
  9. #include <string>
  10. #include <QDialog>
  11. #include "common/param_package.h"
  12. #include "core/settings.h"
  13. #include "ui_configure_input.h"
  14. #include "yuzu/uisettings.h"
  15. class QCheckBox;
  16. class QKeyEvent;
  17. class QLabel;
  18. class QPushButton;
  19. class QSlider;
  20. class QSpinBox;
  21. class QString;
  22. class QTimer;
  23. class QWidget;
  24. namespace InputCommon::Polling {
  25. class DevicePoller;
  26. enum class DeviceType;
  27. } // namespace InputCommon::Polling
  28. namespace Ui {
  29. class ConfigureInputPlayer;
  30. }
  31. class ConfigureInputPlayer : public QWidget {
  32. Q_OBJECT
  33. public:
  34. explicit ConfigureInputPlayer(QWidget* parent, std::size_t player_index, QWidget* bottom_row,
  35. bool debug = false);
  36. ~ConfigureInputPlayer() override;
  37. /// Save all button configurations to settings file.
  38. void ApplyConfiguration();
  39. /// Update the input devices combobox.
  40. void UpdateInputDevices();
  41. /// Restore all buttons to their default values.
  42. void RestoreDefaults();
  43. /// Clear all input configuration.
  44. void ClearAll();
  45. /// Set the connection state checkbox (used to sync state).
  46. void ConnectPlayer(bool connected);
  47. signals:
  48. /// Emitted when this controller is connected by the user.
  49. void Connected(bool connected);
  50. /// Emitted when the Handheld mode is selected (undocked with dual joycons attached).
  51. void HandheldStateChanged(bool is_handheld);
  52. /// Emitted when the input devices combobox is being refreshed.
  53. void RefreshInputDevices();
  54. protected:
  55. void showEvent(QShowEvent* event) override;
  56. private:
  57. void changeEvent(QEvent* event) override;
  58. void RetranslateUI();
  59. /// Load configuration settings.
  60. void LoadConfiguration();
  61. /// Called when the button was pressed.
  62. void HandleClick(QPushButton* button,
  63. std::function<void(const Common::ParamPackage&)> new_input_setter,
  64. InputCommon::Polling::DeviceType type);
  65. /// Finish polling and configure input using the input_setter.
  66. void SetPollingResult(const Common::ParamPackage& params, bool abort);
  67. /// Handle mouse button press events.
  68. void mousePressEvent(QMouseEvent* event) override;
  69. /// Handle key press events.
  70. void keyPressEvent(QKeyEvent* event) override;
  71. /// Update UI to reflect current configuration.
  72. void UpdateUI();
  73. /// Update the controller selection combobox
  74. void UpdateControllerCombobox();
  75. /// Update the current controller icon.
  76. void UpdateControllerIcon();
  77. /// Hides and disables controller settings based on the current controller type.
  78. void UpdateControllerAvailableButtons();
  79. /// Gets the default controller mapping for this device and auto configures the input to match.
  80. void UpdateMappingWithDefaults();
  81. std::unique_ptr<Ui::ConfigureInputPlayer> ui;
  82. std::size_t player_index;
  83. bool debug;
  84. std::unique_ptr<QTimer> timeout_timer;
  85. std::unique_ptr<QTimer> poll_timer;
  86. static constexpr int PLAYER_COUNT = 8;
  87. std::array<QCheckBox*, PLAYER_COUNT> player_connected_checkbox;
  88. /// This will be the the setting function when an input is awaiting configuration.
  89. std::optional<std::function<void(const Common::ParamPackage&)>> input_setter;
  90. std::array<Common::ParamPackage, Settings::NativeButton::NumButtons> buttons_param;
  91. std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs> analogs_param;
  92. static constexpr int ANALOG_SUB_BUTTONS_NUM = 4;
  93. /// Each button input is represented by a QPushButton.
  94. std::array<QPushButton*, Settings::NativeButton::NumButtons> button_map;
  95. /// Extra buttons for the modifiers.
  96. Common::ParamPackage lstick_mod;
  97. Common::ParamPackage rstick_mod;
  98. /// A group of four QPushButtons represent one analog input. The buttons each represent up,
  99. /// down, left, right, respectively.
  100. std::array<std::array<QPushButton*, ANALOG_SUB_BUTTONS_NUM>, Settings::NativeAnalog::NumAnalogs>
  101. analog_map_buttons;
  102. std::array<QLabel*, Settings::NativeAnalog::NumAnalogs> analog_map_deadzone_label;
  103. std::array<QSlider*, Settings::NativeAnalog::NumAnalogs> analog_map_deadzone_slider;
  104. std::array<QGroupBox*, Settings::NativeAnalog::NumAnalogs> analog_map_modifier_groupbox;
  105. std::array<QLabel*, Settings::NativeAnalog::NumAnalogs> analog_map_modifier_label;
  106. std::array<QSlider*, Settings::NativeAnalog::NumAnalogs> analog_map_modifier_slider;
  107. std::array<QGroupBox*, Settings::NativeAnalog::NumAnalogs> analog_map_range_groupbox;
  108. std::array<QSpinBox*, Settings::NativeAnalog::NumAnalogs> analog_map_range_spinbox;
  109. static const std::array<std::string, ANALOG_SUB_BUTTONS_NUM> analog_sub_buttons;
  110. std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>> device_pollers;
  111. /// A flag to indicate if keyboard keys are okay when configuring an input. If this is false,
  112. /// keyboard events are ignored.
  113. bool want_keyboard_mouse = false;
  114. /// List of physical devices users can map with. If a SDL backed device is selected, then you
  115. /// can usue this device to get a default mapping.
  116. std::vector<Common::ParamPackage> input_devices;
  117. /// Bottom row is where console wide settings are held, and its "owned" by the parent
  118. /// ConfigureInput widget. On show, add this widget to the main layout. This will change the
  119. /// parent of the widget to this widget (but thats fine).
  120. QWidget* bottom_row;
  121. };