configure_input_player.h 5.7 KB

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