BsPlatform.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 "Input/BsInputFwd.h"
  6. #include "Math/BsVector2I.h"
  7. #include "Math/BsRect2I.h"
  8. #include "Utility/BsEvent.h"
  9. namespace bs
  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. OSDropTarget(const RenderWindow* ownerWindow, INT32 x, INT32 y, UINT32 width, UINT32 height);
  80. ~OSDropTarget();
  81. /**
  82. * Triggered when a pointer is being dragged over the drop area. Provides window coordinates of the pointer position.
  83. */
  84. Event<void(INT32 x, INT32 y)> onDragOver;
  85. /**
  86. * Triggered when the user completes a drop while pointer is over the drop area. Provides window coordinates of the
  87. * pointer position.
  88. */
  89. Event<void(INT32 x, INT32 y)> onDrop;
  90. /**
  91. * Triggered when a pointer enters the drop area. Provides window coordinates of the pointer position.
  92. */
  93. Event<void(INT32 x, INT32 y)> onEnter;
  94. /** Triggered when a pointer leaves the drop area. */
  95. Event<void()> onLeave;
  96. /** Sets the drop target area, in local window coordinates. */
  97. void setArea(INT32 x, INT32 y, UINT32 width, UINT32 height);
  98. /** Gets the type of drop that this drop target is looking for. Only valid after a drop has been triggered. */
  99. OSDropType getDropType() const { return mDropType; }
  100. /**
  101. * Returns a list of files received by the drop target. Only valid after a drop of FileList type has been triggered.
  102. */
  103. const Vector<WString>& getFileList() const { return *mFileList; }
  104. /** Clears all internal values. */
  105. void _clear();
  106. /** Sets the file list and marks the drop event as FileList. */
  107. void _setFileList(const Vector<WString>& fileList);
  108. /** Marks the drop area as inactive or active. */
  109. void _setActive(bool active) { mActive = active; }
  110. /** Checks is the specified position within the current drop area. Position should be in window local coordinates. */
  111. bool _isInside(const Vector2I& pos) const;
  112. /** Returns true if the drop target is active. */
  113. bool _isActive() const { return mActive; }
  114. /** Returns a render window this drop target is attached to. */
  115. const RenderWindow* _getOwnerWindow() const { return mOwnerWindow; }
  116. private:
  117. friend class Platform;
  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 (for example tip of the
  216. * Arrow 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 ct::RenderWindow& 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 ct::RenderWindow& 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 ct::RenderWindow& window);
  258. /**
  259. * Causes the current thread to pause execution for the specified amount of time.
  260. *
  261. * @param[in] duration Duration in milliseconds. Providing zero will give up the current time-slice.
  262. *
  263. * @note This method relies on timer granularity being set to 1 millisecond. If it is not, you can expect
  264. * this method to potentially take significantly longer if you are providing it with low ms values (<10).
  265. */
  266. static void sleep(UINT32 duration);
  267. /**
  268. * Creates a drop target that you can use for tracking OS drag and drop operations performed over a certain area
  269. * on the specified window.
  270. *
  271. * @param[in] window The window on which to track drop operations.
  272. * @param[in] x The x coordinate of the area to track, relative to window.
  273. * @param[in] y The y coordinate of the area to track, relative to window.
  274. * @param[in] width The width of the area to track.
  275. * @param[in] height The height of the area to track.
  276. * @return OSDropTarget that you will use to receive all drop data. When no longer needed make sure
  277. * to destroy it with destroyDropTarget().
  278. */
  279. static OSDropTarget& createDropTarget(const RenderWindow* window, INT32 x, INT32 y, UINT32 width, UINT32 height);
  280. /** Destroys a drop target previously created with createDropTarget. */
  281. static void destroyDropTarget(OSDropTarget& target);
  282. /**
  283. * Displays a platform specific file/folder open/save dialog.
  284. *
  285. * @param[in] type Type of dialog to open.
  286. * @param[in] defaultPath Initial path the dialog will be set to once opened.
  287. * @param[in] filterList Semi-colon separated list of file names or types to display in the dialog,
  288. * for example "exe;txt;png". Ignored if dialog is to display folders instead of files.
  289. * @param[out] paths Output list of selected file or folder paths (if any).
  290. * @return True if file was selected and false if selection was canceled.
  291. */
  292. static bool openBrowseDialog(FileDialogType type, const Path& defaultPath, const WString& filterList,
  293. Vector<Path>& paths);
  294. /**
  295. * Adds a string to the clipboard.
  296. *
  297. * @note Thread safe.
  298. */
  299. static void copyToClipboard(const WString& string);
  300. /**
  301. * Reads a string from the clipboard and returns it. If there is no string in the clipboard it returns an empty
  302. * string.
  303. *
  304. * @note
  305. * Both wide and normal strings will be read, but normal strings will be converted to a wide string before returning.
  306. * @note
  307. * Thread safe.
  308. */
  309. static WString copyFromClipboard();
  310. /**
  311. * Converts a keyboard key-code to a Unicode character.
  312. *
  313. * @note
  314. * Normally this will output a single character, but it can happen it outputs multiple in case a accent/diacritic
  315. * character could not be combined with the virtual key into a single character.
  316. */
  317. static WString keyCodeToUnicode(UINT32 keyCode);
  318. /**
  319. * Message pump. Processes OS messages and returns when it's free.
  320. *
  321. * @note Core thread only.
  322. */
  323. static void _messagePump();
  324. /** Called during application start up from the sim thread. Must be called before any other operations are done. */
  325. static void _startUp();
  326. /**
  327. * Called once per frame from the sim thread.
  328. *
  329. * @note Sim thread only.
  330. */
  331. static void _update();
  332. /**
  333. * Called once per frame from the core thread.
  334. *
  335. * @note Core thread only.
  336. */
  337. static void _coreUpdate();
  338. /**
  339. * Called during application shut down from the sim thread.
  340. *
  341. * @note Sim thread only.
  342. */
  343. static void _shutDown();
  344. /**
  345. * Triggered when a pointer leaves the provided window.
  346. *
  347. * @note Sim thread only.
  348. */
  349. static Event<void(ct::RenderWindow*)> onMouseLeftWindow;
  350. /**
  351. * Triggered whenever the pointer moves.
  352. *
  353. * @note Core thread only.
  354. */
  355. static Event<void(const Vector2I&, const OSPointerButtonStates&)> onCursorMoved;
  356. /**
  357. * Triggered whenever a pointer button is pressed.
  358. *
  359. * @note Core thread only.
  360. */
  361. static Event<void(const Vector2I&, OSMouseButton button, const OSPointerButtonStates&)> onCursorButtonPressed;
  362. /**
  363. * Triggered whenever pointer button is released.
  364. *
  365. * @note Core thread only.
  366. */
  367. static Event<void(const Vector2I&, OSMouseButton button, const OSPointerButtonStates&)> onCursorButtonReleased;
  368. /**
  369. * Triggered whenever a pointer button is double clicked.
  370. *
  371. * @note Core thread only.
  372. */
  373. static Event<void(const Vector2I&, const OSPointerButtonStates&)> onCursorDoubleClick;
  374. /**
  375. * Triggered whenever an input command is entered.
  376. *
  377. * @note Core thread only.
  378. */
  379. static Event<void(InputCommandType)> onInputCommand;
  380. /**
  381. * Triggered whenever the mouse wheel is scolled.
  382. *
  383. * @note Core thread only.
  384. */
  385. static Event<void(float)> onMouseWheelScrolled;
  386. /**
  387. * Triggered whenever a character is entered.
  388. *
  389. * @note Core thread only.
  390. */
  391. static Event<void(UINT32)> onCharInput;
  392. /**
  393. * Triggered whenever mouse capture state for the window is changed (it receives or loses it).
  394. *
  395. * @note Core thread only.
  396. */
  397. static Event<void()> onMouseCaptureChanged;
  398. protected:
  399. static Pimpl* mData;
  400. };
  401. /** @} */
  402. }