configure_input_player_widget.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <QFrame>
  6. #include <QPointer>
  7. #include "common/input.h"
  8. #include "common/settings_input.h"
  9. #include "common/vector_math.h"
  10. #include "core/hid/emulated_controller.h"
  11. #include "core/hid/hid_types.h"
  12. class QLabel;
  13. using AnalogParam = std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs>;
  14. using ButtonParam = std::array<Common::ParamPackage, Settings::NativeButton::NumButtons>;
  15. // Widget for representing controller animations
  16. class PlayerControlPreview : public QFrame {
  17. Q_OBJECT
  18. public:
  19. explicit PlayerControlPreview(QWidget* parent);
  20. ~PlayerControlPreview() override;
  21. // Sets the emulated controller to be displayed
  22. void SetController(Core::HID::EmulatedController* controller);
  23. // Disables events from the emulated controller
  24. void UnloadController();
  25. // Starts blinking animation at the button specified
  26. void BeginMappingButton(std::size_t button_id);
  27. // Starts moving animation at the stick specified
  28. void BeginMappingAnalog(std::size_t stick_id);
  29. // Stops any ongoing animation
  30. void EndMapping();
  31. // Handles emulated controller events
  32. void ControllerUpdate(Core::HID::ControllerTriggerType type);
  33. // Updates input on scheduled interval
  34. void UpdateInput();
  35. protected:
  36. void paintEvent(QPaintEvent* event) override;
  37. private:
  38. enum class Direction : std::size_t {
  39. None,
  40. Up,
  41. Right,
  42. Down,
  43. Left,
  44. };
  45. enum class Symbol {
  46. House,
  47. A,
  48. B,
  49. X,
  50. Y,
  51. L,
  52. R,
  53. C,
  54. SL,
  55. ZL,
  56. ZR,
  57. SR,
  58. Charging,
  59. };
  60. struct ColorMapping {
  61. QColor outline{};
  62. QColor primary{};
  63. QColor left{};
  64. QColor right{};
  65. QColor button{};
  66. QColor button2{};
  67. QColor button_turbo{};
  68. QColor font{};
  69. QColor font2{};
  70. QColor highlight{};
  71. QColor highlight2{};
  72. QColor transparent{};
  73. QColor indicator{};
  74. QColor indicator2{};
  75. QColor led_on{};
  76. QColor led_off{};
  77. QColor slider{};
  78. QColor slider_button{};
  79. QColor slider_arrow{};
  80. QColor deadzone{};
  81. QColor charging{};
  82. };
  83. void UpdateColors();
  84. void ResetInputs();
  85. // Draw controller functions
  86. void DrawHandheldController(QPainter& p, QPointF center);
  87. void DrawDualController(QPainter& p, QPointF center);
  88. void DrawLeftController(QPainter& p, QPointF center);
  89. void DrawRightController(QPainter& p, QPointF center);
  90. void DrawProController(QPainter& p, QPointF center);
  91. void DrawGCController(QPainter& p, QPointF center);
  92. // Draw body functions
  93. void DrawHandheldBody(QPainter& p, QPointF center);
  94. void DrawDualBody(QPainter& p, QPointF center);
  95. void DrawLeftBody(QPainter& p, QPointF center);
  96. void DrawRightBody(QPainter& p, QPointF center);
  97. void DrawProBody(QPainter& p, QPointF center);
  98. void DrawGCBody(QPainter& p, QPointF center);
  99. // Draw triggers functions
  100. void DrawProTriggers(QPainter& p, QPointF center,
  101. const Common::Input::ButtonStatus& left_pressed,
  102. const Common::Input::ButtonStatus& right_pressed);
  103. void DrawGCTriggers(QPainter& p, QPointF center, Common::Input::TriggerStatus left_trigger,
  104. Common::Input::TriggerStatus right_trigger);
  105. void DrawHandheldTriggers(QPainter& p, QPointF center,
  106. const Common::Input::ButtonStatus& left_pressed,
  107. const Common::Input::ButtonStatus& right_pressed);
  108. void DrawDualTriggers(QPainter& p, QPointF center,
  109. const Common::Input::ButtonStatus& left_pressed,
  110. const Common::Input::ButtonStatus& right_pressed);
  111. void DrawDualTriggersTopView(QPainter& p, QPointF center,
  112. const Common::Input::ButtonStatus& left_pressed,
  113. const Common::Input::ButtonStatus& right_pressed);
  114. void DrawDualZTriggersTopView(QPainter& p, QPointF center,
  115. const Common::Input::ButtonStatus& left_pressed,
  116. const Common::Input::ButtonStatus& right_pressed);
  117. void DrawLeftTriggers(QPainter& p, QPointF center,
  118. const Common::Input::ButtonStatus& left_pressed);
  119. void DrawLeftZTriggers(QPainter& p, QPointF center,
  120. const Common::Input::ButtonStatus& left_pressed);
  121. void DrawLeftTriggersTopView(QPainter& p, QPointF center,
  122. const Common::Input::ButtonStatus& left_pressed);
  123. void DrawLeftZTriggersTopView(QPainter& p, QPointF center,
  124. const Common::Input::ButtonStatus& left_pressed);
  125. void DrawRightTriggers(QPainter& p, QPointF center,
  126. const Common::Input::ButtonStatus& right_pressed);
  127. void DrawRightZTriggers(QPainter& p, QPointF center,
  128. const Common::Input::ButtonStatus& right_pressed);
  129. void DrawRightTriggersTopView(QPainter& p, QPointF center,
  130. const Common::Input::ButtonStatus& right_pressed);
  131. void DrawRightZTriggersTopView(QPainter& p, QPointF center,
  132. const Common::Input::ButtonStatus& right_pressed);
  133. // Draw joystick functions
  134. void DrawJoystick(QPainter& p, QPointF center, float size,
  135. const Common::Input::ButtonStatus& pressed);
  136. void DrawJoystickSideview(QPainter& p, QPointF center, float angle, float size,
  137. const Common::Input::ButtonStatus& pressed);
  138. void DrawRawJoystick(QPainter& p, QPointF center_left, QPointF center_right);
  139. void DrawJoystickProperties(QPainter& p, QPointF center,
  140. const Common::Input::AnalogProperties& properties);
  141. void DrawJoystickDot(QPainter& p, QPointF center, const Common::Input::StickStatus& stick,
  142. bool raw);
  143. void DrawProJoystick(QPainter& p, QPointF center, QPointF offset, float scalar,
  144. const Common::Input::ButtonStatus& pressed);
  145. void DrawGCJoystick(QPainter& p, QPointF center, const Common::Input::ButtonStatus& pressed);
  146. // Draw button functions
  147. void DrawCircleButton(QPainter& p, QPointF center, const Common::Input::ButtonStatus& pressed,
  148. float button_size);
  149. void DrawRoundButton(QPainter& p, QPointF center, const Common::Input::ButtonStatus& pressed,
  150. float width, float height, Direction direction = Direction::None,
  151. float radius = 2);
  152. void DrawMinusButton(QPainter& p, QPointF center, const Common::Input::ButtonStatus& pressed,
  153. int button_size);
  154. void DrawPlusButton(QPainter& p, QPointF center, const Common::Input::ButtonStatus& pressed,
  155. int button_size);
  156. void DrawGCButtonX(QPainter& p, QPointF center, const Common::Input::ButtonStatus& pressed);
  157. void DrawGCButtonY(QPainter& p, QPointF center, const Common::Input::ButtonStatus& pressed);
  158. void DrawGCButtonZ(QPainter& p, QPointF center, const Common::Input::ButtonStatus& pressed);
  159. void DrawArrowButtonOutline(QPainter& p, const QPointF center, float size = 1.0f);
  160. void DrawArrowButton(QPainter& p, QPointF center, Direction direction,
  161. const Common::Input::ButtonStatus& pressed, float size = 1.0f);
  162. void DrawTriggerButton(QPainter& p, QPointF center, Direction direction,
  163. const Common::Input::ButtonStatus& pressed);
  164. QColor GetButtonColor(QColor default_color, bool is_pressed, bool turbo);
  165. // Draw battery functions
  166. void DrawBattery(QPainter& p, QPointF center, Common::Input::BatteryLevel battery);
  167. // Draw icon functions
  168. void DrawSymbol(QPainter& p, QPointF center, Symbol symbol, float icon_size);
  169. void DrawArrow(QPainter& p, QPointF center, Direction direction, float size);
  170. // Draw motion functions
  171. void Draw3dCube(QPainter& p, QPointF center, const Common::Vec3f& euler, float size);
  172. // Draw primitive types
  173. template <size_t N>
  174. void DrawPolygon(QPainter& p, const std::array<QPointF, N>& polygon);
  175. void DrawCircle(QPainter& p, QPointF center, float size);
  176. void DrawRectangle(QPainter& p, QPointF center, float width, float height);
  177. void DrawRoundRectangle(QPainter& p, QPointF center, float width, float height, float round);
  178. void DrawText(QPainter& p, QPointF center, float text_size, const QString& text);
  179. void SetTextFont(QPainter& p, float text_size,
  180. const QString& font_family = QStringLiteral("sans-serif"));
  181. bool is_controller_set{};
  182. bool is_connected{};
  183. bool needs_redraw{};
  184. Core::HID::NpadStyleIndex controller_type;
  185. bool mapping_active{};
  186. int blink_counter{};
  187. int callback_key;
  188. QColor button_color{};
  189. ColorMapping colors{};
  190. Core::HID::LedPattern led_pattern{0, 0, 0, 0};
  191. std::size_t player_index{};
  192. Core::HID::EmulatedController* controller;
  193. std::size_t button_mapping_index{Settings::NativeButton::NumButtons};
  194. std::size_t analog_mapping_index{Settings::NativeAnalog::NumAnalogs};
  195. Core::HID::ButtonValues button_values{};
  196. Core::HID::SticksValues stick_values{};
  197. Core::HID::TriggerValues trigger_values{};
  198. Core::HID::BatteryValues battery_values{};
  199. Core::HID::MotionState motion_values{};
  200. };