BsPlatform.h 11 KB

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