BsPlatform.h 13 KB

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