configure_input_player.h 7.6 KB

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