configure_input_player.h 7.8 KB

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