input_interpreter.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Core::HID {
  11. enum class NpadButton : u64;
  12. }
  13. namespace Service::HID {
  14. class Controller_NPad;
  15. }
  16. /**
  17. * The InputInterpreter class interfaces with HID to retrieve button press states.
  18. * Input is intended to be polled every 50ms so that a button is considered to be
  19. * held down after 400ms has elapsed since the initial button press and subsequent
  20. * repeated presses occur every 50ms.
  21. */
  22. class InputInterpreter {
  23. public:
  24. explicit InputInterpreter(Core::System& system);
  25. virtual ~InputInterpreter();
  26. /// Gets a button state from HID and inserts it into the array of button states.
  27. void PollInput();
  28. /// Resets all the button states to their defaults.
  29. void ResetButtonStates();
  30. /**
  31. * Checks whether the button is pressed.
  32. *
  33. * @param button The button to check.
  34. *
  35. * @returns True when the button is pressed.
  36. */
  37. [[nodiscard]] bool IsButtonPressed(Core::HID::NpadButton button) const;
  38. /**
  39. * Checks whether any of the buttons in the parameter list is pressed.
  40. *
  41. * @tparam HIDButton The buttons to check.
  42. *
  43. * @returns True when at least one of the buttons is pressed.
  44. */
  45. template <Core::HID::NpadButton... T>
  46. [[nodiscard]] bool IsAnyButtonPressed() {
  47. return (IsButtonPressed(T) || ...);
  48. }
  49. /**
  50. * The specified button is considered to be pressed once
  51. * if it is currently pressed and not pressed previously.
  52. *
  53. * @param button The button to check.
  54. *
  55. * @returns True when the button is pressed once.
  56. */
  57. [[nodiscard]] bool IsButtonPressedOnce(Core::HID::NpadButton button) const;
  58. /**
  59. * Checks whether any of the buttons in the parameter list is pressed once.
  60. *
  61. * @tparam T The buttons to check.
  62. *
  63. * @returns True when at least one of the buttons is pressed once.
  64. */
  65. template <Core::HID::NpadButton... T>
  66. [[nodiscard]] bool IsAnyButtonPressedOnce() const {
  67. return (IsButtonPressedOnce(T) || ...);
  68. }
  69. /**
  70. * The specified button is considered to be held down if it is pressed in all 9 button states.
  71. *
  72. * @param button The button to check.
  73. *
  74. * @returns True when the button is held down.
  75. */
  76. [[nodiscard]] bool IsButtonHeld(Core::HID::NpadButton button) const;
  77. /**
  78. * Checks whether any of the buttons in the parameter list is held down.
  79. *
  80. * @tparam T The buttons to check.
  81. *
  82. * @returns True when at least one of the buttons is held down.
  83. */
  84. template <Core::HID::NpadButton... T>
  85. [[nodiscard]] bool IsAnyButtonHeld() const {
  86. return (IsButtonHeld(T) || ...);
  87. }
  88. private:
  89. Service::HID::Controller_NPad& npad;
  90. /// Stores 9 consecutive button states polled from HID.
  91. std::array<Core::HID::NpadButton, 9> button_states{};
  92. std::size_t previous_index{};
  93. std::size_t current_index{};
  94. };