BsOSInputHandler.h 4.6 KB

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