BsOSInputHandler.h 4.7 KB

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