BsOSInputHandler.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsPlatform.h"
  4. #include "BsEvent.h"
  5. #include "BsVector2I.h"
  6. namespace BansheeEngine
  7. {
  8. /** @cond INTERNAL */
  9. /** @addtogroup Input
  10. * @{
  11. */
  12. /**
  13. * Represents a specific way of acquiring OS input. Input class (which provides a higher level input) must have at
  14. * least one OSInputHandler attached. Attach events handler to the provided signals to handle input.
  15. *
  16. * @note
  17. * Unlike RawInputHandler this class receives input from the operating system, and is used for receiving text input,
  18. * cursor position and similar.
  19. */
  20. class BS_CORE_EXPORT OSInputHandler
  21. {
  22. /** Contains information regarding a button state change event. */
  23. struct ButtonStateChange
  24. {
  25. Vector2I cursorPos;
  26. OSPointerButtonStates btnStates;
  27. OSMouseButton button;
  28. bool pressed;
  29. };
  30. /** Contains information regarding a double click event. */
  31. struct DoubleClick
  32. {
  33. Vector2I cursorPos;
  34. OSPointerButtonStates btnStates;
  35. };
  36. public:
  37. OSInputHandler();
  38. virtual ~OSInputHandler();
  39. /**
  40. * Called once per frame. Capture input here if needed.
  41. *
  42. * @note Internal method.
  43. */
  44. virtual void _update();
  45. /**
  46. * Called whenever the active window changes.
  47. *
  48. * @param[in] win Newly active window.
  49. *
  50. * @note Internal method.
  51. */
  52. virtual void _inputWindowChanged(const RenderWindow& win) { }
  53. /**
  54. * Triggers when user inputs a character. The character might be a result of pressing multiple keys, so character
  55. * input will not necessarily correspond with button presses. Provide character code of the input character.
  56. */
  57. Event<void(UINT32)> onCharInput;
  58. /**
  59. * Triggers whenever user scrolls the mouse wheel. Returns the screen position of the mouse cursor and delta amount
  60. * of mouse scroll (can be negative or positive).
  61. */
  62. Event<void(const Vector2I&, float)> onMouseWheelScrolled;
  63. /** Triggers whenever user moves the mouse cursor. */
  64. Event<void(const PointerEvent&)> onCursorMoved;
  65. /** Triggers whenever user presses one of the mouse buttons. */
  66. Event<void(const PointerEvent&)> onCursorPressed;
  67. /** Triggers whenever user releases one of the mouse buttons. */
  68. Event<void(const PointerEvent&)> onCursorReleased;
  69. /** Triggers when user clicks a mouse button quickly twice in a row. */
  70. Event<void(const PointerEvent&)> onDoubleClick;
  71. /** Triggers when user inputa a special input command, like commands user for manipulating text input. */
  72. Event<void(InputCommandType)> onInputCommand;
  73. private:
  74. BS_MUTEX(mOSInputMutex);
  75. Vector2I mLastCursorPos;
  76. Vector2I mCursorPosition;
  77. Vector2I mDelta;
  78. bool mLastCursorPosSet;
  79. float mMouseScroll;
  80. WString mInputString;
  81. Queue<ButtonStateChange> mButtonStates;
  82. Queue<DoubleClick> mDoubleClicks;
  83. Queue<InputCommandType> mInputCommands;
  84. OSPointerButtonStates mMouseMoveBtnState;
  85. HEvent mCharInputConn;
  86. HEvent mCursorMovedConn;
  87. HEvent mCursorPressedConn;
  88. HEvent mCursorReleasedConn;
  89. HEvent mCursorDoubleClickConn;
  90. HEvent mInputCommandConn;
  91. HEvent mMouseWheelScrolledConn;
  92. /**
  93. * Called from the message loop to notify user has entered a character.
  94. *
  95. * @see onCharInput
  96. */
  97. void charInput(UINT32 character);
  98. /**
  99. * Called from the message loop to notify user has moved the cursor.
  100. *
  101. * @see onCursorMoved
  102. */
  103. void cursorMoved(const Vector2I& cursorPos, OSPointerButtonStates& btnStates);
  104. /**
  105. * Called from the message loop to notify user has pressed a mouse button.
  106. *
  107. * @see onCursorPressed
  108. */
  109. void cursorPressed(const Vector2I& cursorPos, OSMouseButton button, OSPointerButtonStates& btnStates);
  110. /**
  111. * Called from the message loop to notify user has released a mouse button.
  112. *
  113. * @see onCursorReleased
  114. */
  115. void cursorReleased(const Vector2I& cursorPos, OSMouseButton button, OSPointerButtonStates& btnStates);
  116. /**
  117. * Called from the message loop to notify user has double-clicked a mouse button.
  118. *
  119. * @see onDoubleClick
  120. */
  121. void cursorDoubleClick(const Vector2I& cursorPos, OSPointerButtonStates& btnStates);
  122. /**
  123. * Called from the message loop to notify user has entered an input command.
  124. *
  125. * @see onInputCommand
  126. */
  127. void inputCommandEntered(InputCommandType commandType);
  128. /**
  129. * Called from the message loop to notify user has scrolled the mouse wheel.
  130. *
  131. * @see onMouseWheelScrolled
  132. */
  133. void mouseWheelScrolled(float scrollPos);
  134. };
  135. /** @} */
  136. /** @endcond */
  137. }