BsPlatform.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsInputFwd.h"
  4. #include "BsVector2I.h"
  5. #include "BsRect2I.h"
  6. #include "BsEvent.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Contains values representing default
  11. * mouse cursor types.
  12. */
  13. enum class PlatformCursorType
  14. {
  15. Arrow,
  16. Wait,
  17. IBeam,
  18. Help,
  19. Hand,
  20. SizeAll,
  21. SizeNESW,
  22. SizeNS,
  23. SizeNWSE,
  24. SizeWE
  25. };
  26. /**
  27. * @brief Contains values reprenting window non client areas.
  28. *
  29. * @note These are used for things like resize/move and tell the OS
  30. * where each of those areas are on our window.
  31. */
  32. enum class NonClientAreaBorderType
  33. {
  34. TopLeft,
  35. Top,
  36. TopRight,
  37. Left,
  38. Right,
  39. BottomLeft,
  40. Bottom,
  41. BottomRight
  42. };
  43. /**
  44. * @brief Types of mouse buttons provided by the OS.
  45. */
  46. enum class OSMouseButton
  47. {
  48. Left, Middle, Right, Count
  49. };
  50. /**
  51. * @brief Describes pointer (mouse, touch) states as reported
  52. * by the OS.
  53. */
  54. struct BS_CORE_EXPORT OSPointerButtonStates
  55. {
  56. OSPointerButtonStates()
  57. {
  58. mouseButtons[0] = false;
  59. mouseButtons[1] = false;
  60. mouseButtons[2] = false;
  61. shift = false;
  62. ctrl = false;
  63. }
  64. bool mouseButtons[OSMouseButton::Count];
  65. bool shift, ctrl;
  66. };
  67. /**
  68. * @brief Type of drop event type. This is used
  69. * when dragging items over drop targets.
  70. */
  71. enum class OSDropType
  72. {
  73. FileList,
  74. None
  75. };
  76. /**
  77. * @brief Drop targets allow you to register a certain portion of a window as a drop target that
  78. * accepts certain drop types from the OS (platform) specific drag and drop system. Accepted
  79. * drop types are provided by the OS and include things like file and item dragging.
  80. *
  81. * You will receive events with the specified drop area as long as it is active.
  82. */
  83. class BS_CORE_EXPORT OSDropTarget
  84. {
  85. public:
  86. /**
  87. * @brief Triggered when a pointer is being dragged over the drop area.
  88. * Provides window coordinates of the pointer position.
  89. */
  90. Event<void(INT32 x, INT32 y)> onDragOver;
  91. /**
  92. * @brief Triggered when the user completes a drop while pointer is over
  93. * the drop area.
  94. * Provides window coordinates of the pointer position.
  95. */
  96. Event<void(INT32 x, INT32 y)> onDrop;
  97. /**
  98. * @brief Triggered when a pointer enters the drop area.
  99. * Provides window coordinates of the pointer position.
  100. */
  101. Event<void(INT32 x, INT32 y)> onEnter;
  102. /**
  103. * @brief Triggered when a pointer leaves the drop area.
  104. */
  105. Event<void()> onLeave;
  106. /**
  107. * @brief Sets the drop target area, in local window coordinates.
  108. */
  109. void setArea(INT32 x, INT32 y, UINT32 width, UINT32 height);
  110. /**
  111. * @brief Gets the type of drop that this drop target is looking for. Only
  112. * valid after a drop has been triggered.
  113. */
  114. OSDropType getDropType() const { return mDropType; }
  115. /**
  116. * @brief Returns a list of files received by the drop target. Only valid
  117. * after a drop of FileList type has been triggered.
  118. */
  119. const Vector<WString>& getFileList() const { return *mFileList; }
  120. /**
  121. * @brief Internal method. Clears all internal values.
  122. */
  123. void _clear();
  124. /**
  125. * @brief Internal method. Sets the file list and marks the drop event as FileList.
  126. */
  127. void _setFileList(const Vector<WString>& fileList);
  128. /**
  129. * @brief Marks the drop area as inactive or active.
  130. */
  131. void _setActive(bool active) { mActive = active; }
  132. /**
  133. * @brief Checks is the specified position within the current drop area.
  134. * Position should be in window local coordinates.
  135. */
  136. bool _isInside(const Vector2I& pos) const;
  137. /**
  138. * @brief Returns true if the drop target is active.
  139. */
  140. bool _isActive() const { return mActive; }
  141. private:
  142. friend class Platform;
  143. OSDropTarget(const RenderWindow* ownerWindow, INT32 x, INT32 y, UINT32 width, UINT32 height);
  144. ~OSDropTarget();
  145. /**
  146. * @brief Returns a render window this drop target is attached to.
  147. */
  148. const RenderWindow* getOwnerWindow() const { return mOwnerWindow; }
  149. private:
  150. INT32 mX, mY;
  151. UINT32 mWidth, mHeight;
  152. bool mActive;
  153. const RenderWindow* mOwnerWindow;
  154. OSDropType mDropType;
  155. union
  156. {
  157. Vector<WString>* mFileList;
  158. };
  159. };
  160. /**
  161. * @brief Represents a specific non client area used for window resizing.
  162. */
  163. struct BS_CORE_EXPORT NonClientResizeArea
  164. {
  165. NonClientAreaBorderType type;
  166. Rect2I area;
  167. };
  168. /**
  169. * @brief Contains a list of window move and resize non client areas.
  170. */
  171. struct BS_CORE_EXPORT WindowNonClientAreaData
  172. {
  173. Vector<NonClientResizeArea> resizeAreas;
  174. Vector<Rect2I> moveAreas;
  175. };
  176. /**
  177. * @brief Provides access to various operating system functions, including
  178. * the main message pump.
  179. */
  180. class BS_CORE_EXPORT Platform
  181. {
  182. public:
  183. struct Pimpl;
  184. Platform() { }
  185. virtual ~Platform();
  186. /**
  187. * @brief Retrieves the cursor position in screen coordinates.
  188. *
  189. * @note Thread safe.
  190. */
  191. static Vector2I getCursorPosition();
  192. /**
  193. * @brief Moves the cursor to the specified screen position.
  194. *
  195. * @note Thread safe.
  196. */
  197. static void setCursorPosition(const Vector2I& screenPos);
  198. /**
  199. * @brief Capture mouse to this window so that we get mouse input even if the mouse leaves the window area.
  200. *
  201. * @note Thread safe.
  202. */
  203. static void captureMouse(const RenderWindow& window);
  204. /**
  205. * @brief Releases the mouse capture set by "captureMouse"
  206. *
  207. * @note Thread safe.
  208. */
  209. static void releaseMouseCapture();
  210. /**
  211. * @brief Checks if provided over screen position is over the specified window.
  212. */
  213. static bool isPointOverWindow(const RenderWindow& window, const Vector2I& screenPos);
  214. /**
  215. * @brief Limit cursor movement to the specified window.
  216. *
  217. * @note Thread safe.
  218. */
  219. static void clipCursorToWindow(const RenderWindow& window);
  220. /**
  221. * @brief Clip cursor to specific area on the screen.
  222. *
  223. * @note Thread safe.
  224. */
  225. static void clipCursorToRect(const Rect2I& screenRect);
  226. /**
  227. * @brief Disables cursor clipping.
  228. *
  229. * @note Thread safe.
  230. */
  231. static void clipCursorDisable();
  232. /**
  233. * @brief Hides the cursor.
  234. *
  235. * @note Thread safe.
  236. */
  237. static void hideCursor();
  238. /**
  239. * @brief Shows the cursor.
  240. *
  241. * @note Thread safe.
  242. */
  243. static void showCursor();
  244. /**
  245. * @brief Query if the cursor is hidden.
  246. *
  247. * @note Thread safe.
  248. */
  249. static bool isCursorHidden();
  250. /**
  251. * @brief Sets a cursor using a custom image.
  252. *
  253. * @param pixelData Cursor image data.
  254. * @param hotSpot Offset on the cursor image to where the actual input happens (e.g. tip of the Arrow cursor).
  255. *
  256. * @note Thread safe.
  257. */
  258. static void setCursor(PixelData& pixelData, const Vector2I& hotSpot);
  259. /**
  260. * @brief Sets custom caption non client areas for the specified window. Using custom client
  261. * areas will override window move/drag operation and trigger when user interacts
  262. * with the custom area.
  263. *
  264. * @note Thread safe.
  265. * All provided areas are relative to the specified window.
  266. * Mostly useful for frameless windows that don't have typical caption bar.
  267. */
  268. static void setCaptionNonClientAreas(const RenderWindowCore& window, const Vector<Rect2I>& nonClientAreas);
  269. /**
  270. * @brief Sets custom non client areas for the specified window. Using custom client
  271. * areas will override window resize operation and trigger when user interacts
  272. * with the custom area.
  273. *
  274. * @note Thread safe.
  275. * All provided areas are relative to the specified window.
  276. * Mostly useful for frameless windows that don't have typical border.
  277. */
  278. static void setResizeNonClientAreas(const RenderWindowCore& window, const Vector<NonClientResizeArea>& nonClientAreas);
  279. /**
  280. * @brief Resets the non client areas for the specified windows and allows
  281. * the platform to use the default values.
  282. *
  283. * @note Thread safe.
  284. */
  285. static void resetNonClientAreas(const RenderWindowCore& window);
  286. /**
  287. * @brief Creates a drop target that you can use for tracking OS drag and drop operations performed over
  288. * a certain area on the specified window.
  289. *
  290. * @param window The window on which to track drop operations.
  291. * @param x The x coordinate of the area to track, relative to window.
  292. * @param y The y coordinate of the area to track, relative to window.
  293. * @param width The width of the area to track.
  294. * @param height The height of the area to track.
  295. *
  296. * @return OSDropTarget that you will use to receive all drop data. When no longer needed make sure to destroy it with
  297. * destroyDropTarget().
  298. */
  299. static OSDropTarget& createDropTarget(const RenderWindow* window, int x, int y, unsigned int width, unsigned int height);
  300. /**
  301. * @brief Destroys a drop target previously created with createDropTarget.
  302. */
  303. static void destroyDropTarget(OSDropTarget& target);
  304. /**
  305. * @brief Message pump. Processes OS messages and returns when it's free.
  306. *
  307. * @note Internal method.
  308. * Core thread only.
  309. */
  310. static void _messagePump();
  311. /**
  312. * @brief Called during application start up from the sim thread.
  313. * Must be called before any other operations are done.
  314. *
  315. * @note Internal method.
  316. */
  317. static void _startUp();
  318. /**
  319. * @brief Called once per frame from the sim thread.
  320. *
  321. * @note Internal method.
  322. * Sim thread only.
  323. */
  324. static void _update();
  325. /**
  326. * @brief Called once per frame from the core thread.
  327. *
  328. * @note Internal method.
  329. * Core thread only.
  330. */
  331. static void _coreUpdate();
  332. /**
  333. * @brief Called during application shut down from the sim thread.
  334. *
  335. * @note Internal method.
  336. * Sim thread only.
  337. */
  338. static void _shutDown();
  339. /**
  340. * @brief Triggered when a pointer leaves the provided window.
  341. *
  342. * @note Sim thread only.
  343. */
  344. static Event<void(RenderWindowCore*)> onMouseLeftWindow;
  345. /**
  346. * @brief Triggered whenever the pointer moves.
  347. *
  348. * @note Core thread only.
  349. */
  350. static Event<void(const Vector2I&, OSPointerButtonStates)> onCursorMoved;
  351. /**
  352. * @brief Triggered whenever a pointer button is pressed.
  353. *
  354. * @note Core thread only.
  355. */
  356. static Event<void(const Vector2I&, OSMouseButton button, OSPointerButtonStates)> onCursorButtonPressed;
  357. /**
  358. * @brief Triggered whenever pointer button is released.
  359. *
  360. * @note Core thread only.
  361. */
  362. static Event<void(const Vector2I&, OSMouseButton button, OSPointerButtonStates)> onCursorButtonReleased;
  363. /**
  364. * @brief Triggered whenever a pointer button is double clicked.
  365. *
  366. * @note Core thread only.
  367. */
  368. static Event<void(const Vector2I&, OSPointerButtonStates)> onCursorDoubleClick;
  369. /**
  370. * @brief Triggered whenever an input command is entered.
  371. *
  372. * @note Core thread only.
  373. */
  374. static Event<void(InputCommandType)> onInputCommand;
  375. /**
  376. * @brief Triggered whenever the mouse wheel is scolled.
  377. *
  378. * @note Core thread only.
  379. */
  380. static Event<void(float)> onMouseWheelScrolled;
  381. /**
  382. * @brief Triggered whenever a character is entered.
  383. *
  384. * @note Core thread only.
  385. */
  386. static Event<void(UINT32)> onCharInput;
  387. /**
  388. * @brief Triggered whenever mouse capture state for the window is changed
  389. * (it receives or loses it).
  390. *
  391. * @note Core thread only.
  392. */
  393. static Event<void()> onMouseCaptureChanged;
  394. protected:
  395. static Pimpl* mData;
  396. };
  397. }