configure_input_player.h 6.6 KB

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