2
0

CmPlatformImpl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmInputFwd.h"
  4. #include "CmVector2I.h"
  5. #include "CmRectI.h"
  6. #include <boost/signals.hpp>
  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 CM_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 CM_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 CM_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 CM_EXPORT WindowNonClientAreaData
  41. {
  42. Vector<NonClientResizeArea>::type resizeAreas;
  43. Vector<RectI>::type moveAreas;
  44. };
  45. /**
  46. * @brief Provides access to various Windows operating system functions, including
  47. * the main message pump.
  48. */
  49. class CM_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>::type& 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>::type& 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 Queries the internal system performance counter you can use for very precise time
  172. * measurements. Value is in milliseconds.
  173. *
  174. * @note Thread safe.
  175. */
  176. static double queryPerformanceTimerMs();
  177. /**
  178. * @brief Creates a drop target that you can use for tracking OS drag and drop operations performed over
  179. * a certain area on the specified window.
  180. *
  181. * @param window The window on which to track drop operations.
  182. * @param x The x coordinate of the area to track, relative to window.
  183. * @param y The y coordinate of the area to track, relative to window.
  184. * @param width The width of the area to track.
  185. * @param height The height of the area to track.
  186. *
  187. * @return OSDropTarget that you will use to receive all drop data. When no longer needed make sure to destroy it with
  188. * destroyDropTarget().
  189. */
  190. static OSDropTarget& createDropTarget(const RenderWindow* window, int x, int y, unsigned int width, unsigned int height);
  191. /**
  192. * @brief Destroys a drop target previously created with createDropTarget.
  193. */
  194. static void destroyDropTarget(OSDropTarget& target);
  195. /**
  196. * @brief Message pump. Processes OS messages and returns when it's free.
  197. *
  198. * @note Internal method.
  199. * Core thread only.
  200. */
  201. static void _messagePump();
  202. /**
  203. * @brief Called during application start up from the sim thread.
  204. * Must be called before any other operations are done.
  205. *
  206. * @note Internal method.
  207. */
  208. static void _startUp();
  209. /**
  210. * @brief Called once per frame from the sim thread.
  211. *
  212. * @note Internal method.
  213. * Sim thread only.
  214. */
  215. static void _update();
  216. /**
  217. * @brief Called once per frame from the core thread.
  218. *
  219. * @note Internal method.
  220. * Core thread only.
  221. */
  222. static void _coreUpdate();
  223. /**
  224. * @brief Called during application shut down from the sim thread.
  225. *
  226. * @note Internal method.
  227. * Sim thread only.
  228. */
  229. static void _shutDown();
  230. /**
  231. * @brief Triggered when a pointer leaves the provided window.
  232. *
  233. * @note Sim thread only.
  234. */
  235. static boost::signal<void(RenderWindow*)> onMouseLeftWindow;
  236. /**
  237. * @brief Triggered whenever the pointer moves.
  238. *
  239. * @note Core thread only.
  240. */
  241. static boost::signal<void(const Vector2I&, OSPointerButtonStates)> onCursorMoved;
  242. /**
  243. * @brief Triggered whenever a pointer button is pressed.
  244. *
  245. * @note Core thread only.
  246. */
  247. static boost::signal<void(const Vector2I&, OSMouseButton button, OSPointerButtonStates)> onCursorButtonPressed;
  248. /**
  249. * @brief Triggered whenever pointer button is released.
  250. *
  251. * @note Core thread only.
  252. */
  253. static boost::signal<void(const Vector2I&, OSMouseButton button, OSPointerButtonStates)> onCursorButtonReleased;
  254. /**
  255. * @brief Triggered whenever a pointer button is double clicked.
  256. *
  257. * @note Core thread only.
  258. */
  259. static boost::signal<void(const Vector2I&, OSPointerButtonStates)> onCursorDoubleClick;
  260. /**
  261. * @brief Triggered whenever an input command is entered.
  262. *
  263. * @note Core thread only.
  264. */
  265. static boost::signal<void(InputCommandType)> onInputCommand;
  266. /**
  267. * @brief Triggered whenever the mouse wheel is scolled.
  268. *
  269. * @note Core thread only.
  270. */
  271. static boost::signal<void(float)> onMouseWheelScrolled;
  272. /**
  273. * @brief Triggered whenever a character is entered.
  274. *
  275. * @note Core thread only.
  276. */
  277. static boost::signal<void(UINT32)> onCharInput;
  278. /**
  279. * @brief Triggered whenever a window receives focus.
  280. *
  281. * @note Core thread only.
  282. */
  283. static boost::signal<void(RenderWindow*)> onWindowFocusReceived;
  284. /**
  285. * @brief Triggered whenever a window loses focus.
  286. *
  287. * @note Core thread only.
  288. */
  289. static boost::signal<void(RenderWindow*)> onWindowFocusLost;
  290. /**
  291. * @brief Triggered whenever a window gets moved or resized.
  292. *
  293. * @note Core thread only.
  294. */
  295. static boost::signal<void(RenderWindow*)> onWindowMovedOrResized;
  296. /**
  297. * @brief Triggered whenever mouse capture state for the window is changed
  298. * (it receives or loses it).
  299. *
  300. * @note Core thread only.
  301. */
  302. static boost::signal<void()> onMouseCaptureChanged;
  303. protected:
  304. static bool mIsCursorHidden;
  305. static NativeCursorData mCursor;
  306. static bool mUsingCustomCursor;
  307. static Map<const RenderWindow*, WindowNonClientAreaData>::type mNonClientAreas;
  308. static bool mIsTrackingMouse;
  309. static Vector<RenderWindow*>::type mMouseLeftWindows;
  310. static Stack<RenderWindow*>::type mModalWindowStack;
  311. static NativeDropTargetData mDropTargets;
  312. static bool mRequiresStartUp;
  313. static bool mRequiresShutDown;
  314. CM_STATIC_MUTEX(mSync);
  315. static void win32ShowCursor();
  316. static void win32HideCursor();
  317. static void windowFocusReceived(RenderWindow* window);
  318. static void windowFocusLost(RenderWindow* window);
  319. static void windowMovedOrResized(RenderWindow* window);
  320. };
  321. }