Input.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef ANKI_INPUT_INPUT_H
  2. #define ANKI_INPUT_INPUT_H
  3. #include "anki/Math.h"
  4. #include "anki/util/Singleton.h"
  5. #include "anki/util/Array.h"
  6. #include "anki/util/StdTypes.h"
  7. #include "anki/input/KeyCode.h"
  8. #include <memory>
  9. namespace anki {
  10. struct InputImpl;
  11. class NativeWindow;
  12. /// Handle the input and other events
  13. ///
  14. /// @note All positions are in NDC space
  15. class Input
  16. {
  17. public:
  18. Input()
  19. {
  20. reset();
  21. }
  22. ~Input();
  23. /// @name Acessors
  24. /// @{
  25. U32 getKey(U32 i) const
  26. {
  27. return keys[i];
  28. }
  29. U32 getMouseButton(U32 i) const
  30. {
  31. return mouseBtns[i];
  32. }
  33. const Vec2& getMousePosition() const
  34. {
  35. return mousePosNdc;
  36. }
  37. /// @}
  38. /// Initialize the platform's input system
  39. void init(NativeWindow* nativeWindow);
  40. /// Reset the keys and mouse buttons
  41. void reset();
  42. /// Populate the key and button with the new state
  43. void handleEvents();
  44. /// Move the mouse cursor to a position inside the window. Useful for
  45. /// locking the cursor into a fixed location (eg in the center of the
  46. /// screen)
  47. void moveMouse(const Vec2& posNdc);
  48. /// Hide the mouse cursor
  49. void hideCursor(Bool hide);
  50. private:
  51. NativeWindow* nativeWindow = nullptr;
  52. /// @name Keys and btns
  53. /// @{
  54. /// Shows the current key state
  55. /// - 0 times: unpressed
  56. /// - 1 times: pressed once
  57. /// - >1 times: Kept pressed 'n' times continuously
  58. Array<U32, KC_COUNT> keys;
  59. /// Mouse btns. Supporting 3 btns & wheel. @see keys
  60. Array<U32, 8> mouseBtns;
  61. /// @}
  62. Vec2 mousePosNdc; ///< The coords are in the NDC space
  63. std::shared_ptr<InputImpl> impl;
  64. };
  65. typedef Singleton<Input> InputSingleton;
  66. } // end namespace anki
  67. #endif