BsPlatform.h 13 KB

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