configure_input_player.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 <vector>
  11. #include <QWidget>
  12. #include "common/param_package.h"
  13. #include "common/settings.h"
  14. #include "ui_configure_input.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. class InputProfiles;
  25. namespace InputCommon {
  26. class InputSubsystem;
  27. }
  28. namespace InputCommon::Polling {
  29. class DevicePoller;
  30. enum class DeviceType;
  31. } // namespace InputCommon::Polling
  32. namespace Ui {
  33. class ConfigureInputPlayer;
  34. }
  35. class ConfigureInputPlayer : public QWidget {
  36. Q_OBJECT
  37. public:
  38. explicit ConfigureInputPlayer(QWidget* parent, std::size_t player_index, QWidget* bottom_row,
  39. InputCommon::InputSubsystem* input_subsystem_,
  40. InputProfiles* profiles_, bool debug = false);
  41. ~ConfigureInputPlayer() override;
  42. /// Save all button configurations to settings file.
  43. void ApplyConfiguration();
  44. /**
  45. * Attempts to connect the currently selected controller in the HID backend.
  46. * This function will not do anything if it is not connected in the frontend.
  47. */
  48. void TryConnectSelectedController();
  49. /**
  50. * Attempts to disconnect the currently selected controller in the HID backend.
  51. * This function will not do anything if the configuration has not changed.
  52. */
  53. void TryDisconnectSelectedController();
  54. /// Set the connection state checkbox (used to sync state).
  55. void ConnectPlayer(bool connected);
  56. /// Update the input devices combobox.
  57. void UpdateInputDeviceCombobox();
  58. /// Updates the list of controller profiles.
  59. void UpdateInputProfiles();
  60. /// Restore all buttons to their default values.
  61. void RestoreDefaults();
  62. /// Clear all input configuration.
  63. void ClearAll();
  64. signals:
  65. /// Emitted when this controller is connected by the user.
  66. void Connected(bool connected);
  67. /// Emitted when the Handheld mode is selected (undocked with dual joycons attached).
  68. void HandheldStateChanged(bool is_handheld);
  69. /// Emitted when the input devices combobox is being refreshed.
  70. void RefreshInputDevices();
  71. /**
  72. * Emitted when the input profiles combobox is being refreshed.
  73. * The player_index represents the current player's index, and the profile combobox
  74. * will not be updated for this index as they are already updated by other mechanisms.
  75. */
  76. void RefreshInputProfiles(std::size_t player_index);
  77. protected:
  78. void showEvent(QShowEvent* event) override;
  79. private:
  80. void changeEvent(QEvent* event) override;
  81. void RetranslateUI();
  82. /// Load configuration settings.
  83. void LoadConfiguration();
  84. /// Called when the button was pressed.
  85. void HandleClick(QPushButton* button, std::size_t button_id,
  86. std::function<void(const Common::ParamPackage&)> new_input_setter,
  87. InputCommon::Polling::DeviceType type);
  88. /// Finish polling and configure input using the input_setter.
  89. void SetPollingResult(const Common::ParamPackage& params, bool abort);
  90. /// Checks whether a given input can be accepted.
  91. bool IsInputAcceptable(const Common::ParamPackage& params) const;
  92. /// Handle mouse button press events.
  93. void mousePressEvent(QMouseEvent* event) override;
  94. /// Handle key press events.
  95. void keyPressEvent(QKeyEvent* event) override;
  96. /// Update UI to reflect current configuration.
  97. void UpdateUI();
  98. /// Sets the available controllers.
  99. void SetConnectableControllers();
  100. /// Gets the Controller Type for a given controller combobox index.
  101. Settings::ControllerType GetControllerTypeFromIndex(int index) const;
  102. /// Gets the controller combobox index for a given Controller Type.
  103. int GetIndexFromControllerType(Settings::ControllerType type) const;
  104. /// Update the available input devices.
  105. void UpdateInputDevices();
  106. /// Update the current controller icon.
  107. void UpdateControllerIcon();
  108. /// Hides and disables controller settings based on the current controller type.
  109. void UpdateControllerAvailableButtons();
  110. /// Disables controller settings based on the current controller type.
  111. void UpdateControllerEnabledButtons();
  112. /// Shows or hides motion groupboxes based on the current controller type.
  113. void UpdateMotionButtons();
  114. /// Alters the button names based on the current controller type.
  115. void UpdateControllerButtonNames();
  116. /// Gets the default controller mapping for this device and auto configures the input to match.
  117. void UpdateMappingWithDefaults();
  118. /// Creates a controller profile.
  119. void CreateProfile();
  120. /// Deletes the selected controller profile.
  121. void DeleteProfile();
  122. /// Loads the selected controller profile.
  123. void LoadProfile();
  124. /// Saves the current controller configuration into a selected controller profile.
  125. void SaveProfile();
  126. std::unique_ptr<Ui::ConfigureInputPlayer> ui;
  127. std::size_t player_index;
  128. bool debug;
  129. InputCommon::InputSubsystem* input_subsystem;
  130. InputProfiles* profiles;
  131. std::unique_ptr<QTimer> timeout_timer;
  132. std::unique_ptr<QTimer> poll_timer;
  133. /// Stores a pair of "Connected Controllers" combobox index and Controller Type enum.
  134. std::vector<std::pair<int, Settings::ControllerType>> index_controller_type_pairs;
  135. static constexpr int PLAYER_COUNT = 8;
  136. std::array<QCheckBox*, PLAYER_COUNT> player_connected_checkbox;
  137. /// This will be the the setting function when an input is awaiting configuration.
  138. std::optional<std::function<void(const Common::ParamPackage&)>> input_setter;
  139. std::array<Common::ParamPackage, Settings::NativeButton::NumButtons> buttons_param;
  140. std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs> analogs_param;
  141. std::array<Common::ParamPackage, Settings::NativeMotion::NumMotions> motions_param;
  142. static constexpr int ANALOG_SUB_BUTTONS_NUM = 4;
  143. /// Each button input is represented by a QPushButton.
  144. std::array<QPushButton*, Settings::NativeButton::NumButtons> button_map;
  145. /// A group of four QPushButtons represent one analog input. The buttons each represent up,
  146. /// down, left, right, respectively.
  147. std::array<std::array<QPushButton*, ANALOG_SUB_BUTTONS_NUM>, Settings::NativeAnalog::NumAnalogs>
  148. analog_map_buttons;
  149. /// Each motion input is represented by a QPushButton.
  150. std::array<QPushButton*, Settings::NativeMotion::NumMotions> motion_map;
  151. std::array<QLabel*, Settings::NativeAnalog::NumAnalogs> analog_map_deadzone_label;
  152. std::array<QSlider*, Settings::NativeAnalog::NumAnalogs> analog_map_deadzone_slider;
  153. std::array<QGroupBox*, Settings::NativeAnalog::NumAnalogs> analog_map_modifier_groupbox;
  154. std::array<QPushButton*, Settings::NativeAnalog::NumAnalogs> analog_map_modifier_button;
  155. std::array<QLabel*, Settings::NativeAnalog::NumAnalogs> analog_map_modifier_label;
  156. std::array<QSlider*, Settings::NativeAnalog::NumAnalogs> analog_map_modifier_slider;
  157. std::array<QGroupBox*, Settings::NativeAnalog::NumAnalogs> analog_map_range_groupbox;
  158. std::array<QSpinBox*, Settings::NativeAnalog::NumAnalogs> analog_map_range_spinbox;
  159. static const std::array<std::string, ANALOG_SUB_BUTTONS_NUM> analog_sub_buttons;
  160. std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>> device_pollers;
  161. /// A flag to indicate that the "Map Analog Stick" pop-up has been shown and accepted once.
  162. bool map_analog_stick_accepted{};
  163. /// A flag to indicate if keyboard keys are okay when configuring an input. If this is false,
  164. /// keyboard events are ignored.
  165. bool want_keyboard_mouse{};
  166. /// List of physical devices users can map with. If a SDL backed device is selected, then you
  167. /// can use this device to get a default mapping.
  168. std::vector<Common::ParamPackage> input_devices;
  169. /// Bottom row is where console wide settings are held, and its "owned" by the parent
  170. /// ConfigureInput widget. On show, add this widget to the main layout. This will change the
  171. /// parent of the widget to this widget (but thats fine).
  172. QWidget* bottom_row;
  173. };