input_interpreter.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2020 yuzu 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 "common/common_types.h"
  7. namespace Core {
  8. class System;
  9. }
  10. namespace Service::HID {
  11. class Controller_NPad;
  12. }
  13. enum class HIDButton : u8 {
  14. A,
  15. B,
  16. X,
  17. Y,
  18. LStick,
  19. RStick,
  20. L,
  21. R,
  22. ZL,
  23. ZR,
  24. Plus,
  25. Minus,
  26. DLeft,
  27. DUp,
  28. DRight,
  29. DDown,
  30. LStickLeft,
  31. LStickUp,
  32. LStickRight,
  33. LStickDown,
  34. RStickLeft,
  35. RStickUp,
  36. RStickRight,
  37. RStickDown,
  38. LeftSL,
  39. LeftSR,
  40. RightSL,
  41. RightSR,
  42. };
  43. /**
  44. * The InputInterpreter class interfaces with HID to retrieve button press states.
  45. * Input is intended to be polled every 50ms so that a button is considered to be
  46. * held down after 400ms has elapsed since the initial button press and subsequent
  47. * repeated presses occur every 50ms.
  48. */
  49. class InputInterpreter {
  50. public:
  51. explicit InputInterpreter(Core::System& system);
  52. virtual ~InputInterpreter();
  53. /// Gets a button state from HID and inserts it into the array of button states.
  54. void PollInput();
  55. /**
  56. * The specified button is considered to be pressed once
  57. * if it is currently pressed and not pressed previously.
  58. *
  59. * @param button The button to check.
  60. *
  61. * @returns True when the button is pressed once.
  62. */
  63. [[nodiscard]] bool IsButtonPressedOnce(HIDButton button) const;
  64. /**
  65. * Checks whether any of the buttons in the parameter list is pressed once.
  66. *
  67. * @tparam HIDButton The buttons to check.
  68. *
  69. * @returns True when at least one of the buttons is pressed once.
  70. */
  71. template <HIDButton... T>
  72. [[nodiscard]] bool IsAnyButtonPressedOnce() {
  73. return (IsButtonPressedOnce(T) || ...);
  74. }
  75. /**
  76. * The specified button is considered to be held down if it is pressed in all 9 button states.
  77. *
  78. * @param button The button to check.
  79. *
  80. * @returns True when the button is held down.
  81. */
  82. [[nodiscard]] bool IsButtonHeld(HIDButton button) const;
  83. /**
  84. * Checks whether any of the buttons in the parameter list is held down.
  85. *
  86. * @tparam HIDButton The buttons to check.
  87. *
  88. * @returns True when at least one of the buttons is held down.
  89. */
  90. template <HIDButton... T>
  91. [[nodiscard]] bool IsAnyButtonHeld() {
  92. return (IsButtonHeld(T) || ...);
  93. }
  94. private:
  95. Service::HID::Controller_NPad& npad;
  96. /// Stores 9 consecutive button states polled from HID.
  97. std::array<u32, 9> button_states{};
  98. std::size_t previous_index{};
  99. std::size_t current_index{};
  100. };