BsPlatformImpl.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsInputFwd.h"
  4. #include "BsVector2I.h"
  5. #include "BsRectI.h"
  6. #include "BsEvent.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Encapsulate native cursor data so we can avoid including windows.h as it pollutes the global namespace
  11. */
  12. struct BS_CORE_EXPORT NativeCursorData
  13. {
  14. struct Pimpl;
  15. NativeCursorData();
  16. ~NativeCursorData();
  17. Pimpl* data;
  18. };
  19. /**
  20. * @brief Encapsulate drop target data so we can avoid including windows.h as it pollutes the global namespace
  21. */
  22. struct BS_CORE_EXPORT NativeDropTargetData
  23. {
  24. struct Pimpl;
  25. NativeDropTargetData();
  26. ~NativeDropTargetData();
  27. Pimpl* data;
  28. };
  29. /**
  30. * @brief Represents a specific non client area used for window resizing.
  31. */
  32. struct BS_CORE_EXPORT NonClientResizeArea
  33. {
  34. NonClientAreaBorderType type;
  35. RectI area;
  36. };
  37. /**
  38. * @brief Contains a list of window move and resize non client areas.
  39. */
  40. struct BS_CORE_EXPORT WindowNonClientAreaData
  41. {
  42. Vector<NonClientResizeArea> resizeAreas;
  43. Vector<RectI> moveAreas;
  44. };
  45. /**
  46. * @brief Provides access to various Windows operating system functions, including
  47. * the main message pump.
  48. */
  49. class BS_CORE_EXPORT Platform
  50. {
  51. public:
  52. Platform() { }
  53. virtual ~Platform() { }
  54. /**
  55. * @brief Retrieves the cursor position in screen coordinates.
  56. *
  57. * @note Thread safe.
  58. */
  59. static Vector2I getCursorPosition();
  60. /**
  61. * @brief Moves the cursor to the specified screen position.
  62. *
  63. * @note Thread safe.
  64. */
  65. static void setCursorPosition(const Vector2I& screenPos);
  66. /**
  67. * @brief Capture mouse to this window so that we get mouse input even if the mouse leaves the window area.
  68. *
  69. * @note Thread safe.
  70. */
  71. static void captureMouse(const RenderWindow& window);
  72. /**
  73. * @brief Releases the mouse capture set by "captureMouse"
  74. *
  75. * @note Thread safe.
  76. */
  77. static void releaseMouseCapture();
  78. /**
  79. * @brief Checks if provided over screen position is over the specified window.
  80. */
  81. static bool isPointOverWindow(const RenderWindow& window, const Vector2I& screenPos);
  82. /**
  83. * @brief Limit cursor movement to the specified window.
  84. *
  85. * @note Thread safe.
  86. */
  87. static void clipCursorToWindow(const RenderWindow& window);
  88. /**
  89. * @brief Clip cursor to specific area on the screen.
  90. *
  91. * @note Thread safe.
  92. */
  93. static void clipCursorToRect(const RectI& screenRect);
  94. /**
  95. * @brief Disables cursor clipping.
  96. *
  97. * @note Thread safe.
  98. */
  99. static void clipCursorDisable();
  100. /**
  101. * @brief Hides the cursor.
  102. *
  103. * @note Thread safe.
  104. */
  105. static void hideCursor();
  106. /**
  107. * @brief Shows the cursor.
  108. *
  109. * @note Thread safe.
  110. */
  111. static void showCursor();
  112. /**
  113. * @brief Query if the cursor is hidden.
  114. *
  115. * @note Thread safe.
  116. */
  117. static bool isCursorHidden() { return mIsCursorHidden; }
  118. /**
  119. * @brief Sets a cursor using a custom image.
  120. *
  121. * @param pixelData Cursor image data.
  122. * @param hotSpot Offset on the cursor image to where the actual input happens (e.g. tip of the Arrow cursor).
  123. *
  124. * @note Thread safe.
  125. */
  126. static void setCursor(PixelData& pixelData, const Vector2I& hotSpot);
  127. /**
  128. * @brief Sets custom caption non client areas for the specified window. Using custom client
  129. * areas will override window move/drag operation and trigger when user interacts
  130. * with the custom area.
  131. *
  132. * @note Thread safe.
  133. * All provided areas are relative to the specified window.
  134. * Mostly useful for frameless windows that don't have typical caption bar.
  135. */
  136. static void setCaptionNonClientAreas(const RenderWindow& window, const Vector<RectI>& nonClientAreas);
  137. /**
  138. * @brief Sets custom non client areas for the specified window. Using custom client
  139. * areas will override window resize operation and trigger when user interacts
  140. * with the custom area.
  141. *
  142. * @note Thread safe.
  143. * All provided areas are relative to the specified window.
  144. * Mostly useful for frameless windows that don't have typical border.
  145. */
  146. static void setResizeNonClientAreas(const RenderWindow& window, const Vector<NonClientResizeArea>& nonClientAreas);
  147. /**
  148. * @brief Resets the non client areas for the specified windows and allows
  149. * the platform to use the default values.
  150. *
  151. * @note Thread safe.
  152. */
  153. static void resetNonClientAreas(const RenderWindow& window);
  154. /**
  155. * @brief Adds a string to the clipboard.
  156. *
  157. * @note Thread safe.
  158. */
  159. static void copyToClipboard(const WString& string);
  160. /**
  161. * @brief Reads a string from the clipboard and returns it. If there is no
  162. * string in the clipboard it returns an empty string.
  163. *
  164. * @note Both wide and normal strings will be read, but normal strings will be converted to
  165. * a wide string before returning.
  166. *
  167. * Thread safe.
  168. */
  169. static WString copyFromClipboard();
  170. /**
  171. * @brief Populates the provided buffer with a MAC address of the first available
  172. * adapter, if one exists. If no adapters exist, returns false.
  173. */
  174. static bool getMACAddress(MACAddress& address);
  175. /**
  176. * @brief Queries the internal system performance counter you can use for very precise time
  177. * measurements. Value is in milliseconds.
  178. *
  179. * @note Thread safe.
  180. */
  181. static double queryPerformanceTimerMs();
  182. /**
  183. * @brief Creates a drop target that you can use for tracking OS drag and drop operations performed over
  184. * a certain area on the specified window.
  185. *
  186. * @param window The window on which to track drop operations.
  187. * @param x The x coordinate of the area to track, relative to window.
  188. * @param y The y coordinate of the area to track, relative to window.
  189. * @param width The width of the area to track.
  190. * @param height The height of the area to track.
  191. *
  192. * @return OSDropTarget that you will use to receive all drop data. When no longer needed make sure to destroy it with
  193. * destroyDropTarget().
  194. */
  195. static OSDropTarget& createDropTarget(const RenderWindow* window, int x, int y, unsigned int width, unsigned int height);
  196. /**
  197. * @brief Destroys a drop target previously created with createDropTarget.
  198. */
  199. static void destroyDropTarget(OSDropTarget& target);
  200. /**
  201. * @brief Message pump. Processes OS messages and returns when it's free.
  202. *
  203. * @note Internal method.
  204. * Core thread only.
  205. */
  206. static void _messagePump();
  207. /**
  208. * @brief Called during application start up from the sim thread.
  209. * Must be called before any other operations are done.
  210. *
  211. * @note Internal method.
  212. */
  213. static void _startUp();
  214. /**
  215. * @brief Called once per frame from the sim thread.
  216. *
  217. * @note Internal method.
  218. * Sim thread only.
  219. */
  220. static void _update();
  221. /**
  222. * @brief Called once per frame from the core thread.
  223. *
  224. * @note Internal method.
  225. * Core thread only.
  226. */
  227. static void _coreUpdate();
  228. /**
  229. * @brief Called during application shut down from the sim thread.
  230. *
  231. * @note Internal method.
  232. * Sim thread only.
  233. */
  234. static void _shutDown();
  235. /**
  236. * @brief Triggered when a pointer leaves the provided window.
  237. *
  238. * @note Sim thread only.
  239. */
  240. static Event<void(RenderWindow*)> onMouseLeftWindow;
  241. /**
  242. * @brief Triggered whenever the pointer moves.
  243. *
  244. * @note Core thread only.
  245. */
  246. static Event<void(const Vector2I&, OSPointerButtonStates)> onCursorMoved;
  247. /**
  248. * @brief Triggered whenever a pointer button is pressed.
  249. *
  250. * @note Core thread only.
  251. */
  252. static Event<void(const Vector2I&, OSMouseButton button, OSPointerButtonStates)> onCursorButtonPressed;
  253. /**
  254. * @brief Triggered whenever pointer button is released.
  255. *
  256. * @note Core thread only.
  257. */
  258. static Event<void(const Vector2I&, OSMouseButton button, OSPointerButtonStates)> onCursorButtonReleased;
  259. /**
  260. * @brief Triggered whenever a pointer button is double clicked.
  261. *
  262. * @note Core thread only.
  263. */
  264. static Event<void(const Vector2I&, OSPointerButtonStates)> onCursorDoubleClick;
  265. /**
  266. * @brief Triggered whenever an input command is entered.
  267. *
  268. * @note Core thread only.
  269. */
  270. static Event<void(InputCommandType)> onInputCommand;
  271. /**
  272. * @brief Triggered whenever the mouse wheel is scolled.
  273. *
  274. * @note Core thread only.
  275. */
  276. static Event<void(float)> onMouseWheelScrolled;
  277. /**
  278. * @brief Triggered whenever a character is entered.
  279. *
  280. * @note Core thread only.
  281. */
  282. static Event<void(UINT32)> onCharInput;
  283. /**
  284. * @brief Triggered whenever a window receives focus.
  285. *
  286. * @note Core thread only.
  287. */
  288. static Event<void(RenderWindow*)> onWindowFocusReceived;
  289. /**
  290. * @brief Triggered whenever a window loses focus.
  291. *
  292. * @note Core thread only.
  293. */
  294. static Event<void(RenderWindow*)> onWindowFocusLost;
  295. /**
  296. * @brief Triggered whenever a window gets moved or resized.
  297. *
  298. * @note Core thread only.
  299. */
  300. static Event<void(RenderWindow*)> onWindowMovedOrResized;
  301. /**
  302. * @brief Triggered whenever mouse capture state for the window is changed
  303. * (it receives or loses it).
  304. *
  305. * @note Core thread only.
  306. */
  307. static Event<void()> onMouseCaptureChanged;
  308. protected:
  309. static bool mIsCursorHidden;
  310. static NativeCursorData mCursor;
  311. static bool mUsingCustomCursor;
  312. static Map<const RenderWindow*, WindowNonClientAreaData> mNonClientAreas;
  313. static bool mIsTrackingMouse;
  314. static Vector<RenderWindow*> mMouseLeftWindows;
  315. static Stack<RenderWindow*> mModalWindowStack;
  316. static NativeDropTargetData mDropTargets;
  317. static bool mRequiresStartUp;
  318. static bool mRequiresShutDown;
  319. BS_STATIC_MUTEX(mSync);
  320. static void win32ShowCursor();
  321. static void win32HideCursor();
  322. static void windowFocusReceived(RenderWindow* window);
  323. static void windowFocusLost(RenderWindow* window);
  324. static void windowMovedOrResized(RenderWindow* window);
  325. };
  326. }