BsPlatform.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 Possible type of platform file dialogs.
  45. */
  46. enum class FileDialogType
  47. {
  48. OpenFile = 0x0,
  49. OpenFolder = 0x1,
  50. Save = 0x2,
  51. Multiselect = 0x10000,
  52. TypeMask = 0xFFFF
  53. };
  54. /**
  55. * @brief Types of mouse buttons provided by the OS.
  56. */
  57. enum class OSMouseButton
  58. {
  59. Left, Middle, Right, Count
  60. };
  61. /**
  62. * @brief Describes pointer (mouse, touch) states as reported
  63. * by the OS.
  64. */
  65. struct BS_CORE_EXPORT OSPointerButtonStates
  66. {
  67. OSPointerButtonStates()
  68. {
  69. mouseButtons[0] = false;
  70. mouseButtons[1] = false;
  71. mouseButtons[2] = false;
  72. shift = false;
  73. ctrl = false;
  74. }
  75. bool mouseButtons[OSMouseButton::Count];
  76. bool shift, ctrl;
  77. };
  78. /**
  79. * @brief Type of drop event type. This is used
  80. * when dragging items over drop targets.
  81. */
  82. enum class OSDropType
  83. {
  84. FileList,
  85. None
  86. };
  87. /**
  88. * @brief Drop targets allow you to register a certain portion of a window as a drop target that
  89. * accepts certain drop types. Accepted drop types are provided by the OS and include things
  90. * like file and item dragging.
  91. *
  92. * You will receive events with the specified drop area as long as it is active.
  93. */
  94. class BS_CORE_EXPORT OSDropTarget
  95. {
  96. public:
  97. /**
  98. * @brief Triggered when a pointer is being dragged over the drop area.
  99. * Provides window coordinates of the pointer position.
  100. */
  101. Event<void(INT32 x, INT32 y)> onDragOver;
  102. /**
  103. * @brief Triggered when the user completes a drop while pointer is over
  104. * the drop area.
  105. * Provides window coordinates of the pointer position.
  106. */
  107. Event<void(INT32 x, INT32 y)> onDrop;
  108. /**
  109. * @brief Triggered when a pointer enters the drop area.
  110. * Provides window coordinates of the pointer position.
  111. */
  112. Event<void(INT32 x, INT32 y)> onEnter;
  113. /**
  114. * @brief Triggered when a pointer leaves the drop area.
  115. */
  116. Event<void()> onLeave;
  117. /**
  118. * @brief Sets the drop target area, in local window coordinates.
  119. */
  120. void setArea(INT32 x, INT32 y, UINT32 width, UINT32 height);
  121. /**
  122. * @brief Gets the type of drop that this drop target is looking for. Only
  123. * valid after a drop has been triggered.
  124. */
  125. OSDropType getDropType() const { return mDropType; }
  126. /**
  127. * @brief Returns a list of files received by the drop target. Only valid
  128. * after a drop of FileList type has been triggered.
  129. */
  130. const Vector<WString>& getFileList() const { return *mFileList; }
  131. /**
  132. * @brief Internal method. Clears all internal values.
  133. */
  134. void _clear();
  135. /**
  136. * @brief Internal method. Sets the file list and marks the drop event as FileList.
  137. */
  138. void _setFileList(const Vector<WString>& fileList);
  139. /**
  140. * @brief Marks the drop area as inactive or active.
  141. */
  142. void _setActive(bool active) { mActive = active; }
  143. /**
  144. * @brief Checks is the specified position within the current drop area.
  145. * Position should be in window local coordinates.
  146. */
  147. bool _isInside(const Vector2I& pos) const;
  148. /**
  149. * @brief Returns true if the drop target is active.
  150. */
  151. bool _isActive() const { return mActive; }
  152. private:
  153. friend class Platform;
  154. OSDropTarget(const RenderWindow* ownerWindow, INT32 x, INT32 y, UINT32 width, UINT32 height);
  155. ~OSDropTarget();
  156. /**
  157. * @brief Returns a render window this drop target is attached to.
  158. */
  159. const RenderWindow* getOwnerWindow() const { return mOwnerWindow; }
  160. private:
  161. INT32 mX, mY;
  162. UINT32 mWidth, mHeight;
  163. bool mActive;
  164. const RenderWindow* mOwnerWindow;
  165. OSDropType mDropType;
  166. union
  167. {
  168. Vector<WString>* mFileList;
  169. };
  170. };
  171. /**
  172. * @brief Represents a specific non client area used for window resizing.
  173. */
  174. struct BS_CORE_EXPORT NonClientResizeArea
  175. {
  176. NonClientAreaBorderType type;
  177. Rect2I area;
  178. };
  179. /**
  180. * @brief Contains a list of window move and resize non client areas.
  181. */
  182. struct BS_CORE_EXPORT WindowNonClientAreaData
  183. {
  184. Vector<NonClientResizeArea> resizeAreas;
  185. Vector<Rect2I> moveAreas;
  186. };
  187. /**
  188. * @brief Provides access to various operating system functions, including
  189. * the main message pump.
  190. */
  191. class BS_CORE_EXPORT Platform
  192. {
  193. public:
  194. struct Pimpl;
  195. Platform() { }
  196. virtual ~Platform();
  197. /**
  198. * @brief Retrieves the cursor position in screen coordinates.
  199. *
  200. * @note Thread safe.
  201. */
  202. static Vector2I getCursorPosition();
  203. /**
  204. * @brief Moves the cursor to the specified screen position.
  205. *
  206. * @note Thread safe.
  207. */
  208. static void setCursorPosition(const Vector2I& screenPos);
  209. /**
  210. * @brief Capture mouse to this window so that we get mouse input even if the mouse leaves the window area.
  211. *
  212. * @note Thread safe.
  213. */
  214. static void captureMouse(const RenderWindow& window);
  215. /**
  216. * @brief Releases the mouse capture set by "captureMouse"
  217. *
  218. * @note Thread safe.
  219. */
  220. static void releaseMouseCapture();
  221. /**
  222. * @brief Checks if provided over screen position is over the specified window.
  223. */
  224. static bool isPointOverWindow(const RenderWindow& window, const Vector2I& screenPos);
  225. /**
  226. * @brief Limit cursor movement to the specified window.
  227. *
  228. * @note Thread safe.
  229. */
  230. static void clipCursorToWindow(const RenderWindow& window);
  231. /**
  232. * @brief Clip cursor to specific area on the screen.
  233. *
  234. * @note Thread safe.
  235. */
  236. static void clipCursorToRect(const Rect2I& screenRect);
  237. /**
  238. * @brief Disables cursor clipping.
  239. *
  240. * @note Thread safe.
  241. */
  242. static void clipCursorDisable();
  243. /**
  244. * @brief Hides the cursor.
  245. *
  246. * @note Thread safe.
  247. */
  248. static void hideCursor();
  249. /**
  250. * @brief Shows the cursor.
  251. *
  252. * @note Thread safe.
  253. */
  254. static void showCursor();
  255. /**
  256. * @brief Query if the cursor is hidden.
  257. *
  258. * @note Thread safe.
  259. */
  260. static bool isCursorHidden();
  261. /**
  262. * @brief Sets a cursor using a custom image.
  263. *
  264. * @param pixelData Cursor image data.
  265. * @param hotSpot Offset on the cursor image to where the actual input happens (e.g. tip of the Arrow cursor).
  266. *
  267. * @note Thread safe.
  268. */
  269. static void setCursor(PixelData& pixelData, const Vector2I& hotSpot);
  270. /**
  271. * @brief Sets custom caption non client areas for the specified window. Using custom client
  272. * areas will override window move/drag operation and trigger when user interacts
  273. * with the custom area.
  274. *
  275. * @note Thread safe.
  276. * All provided areas are relative to the specified window.
  277. * Mostly useful for frameless windows that don't have typical caption bar.
  278. */
  279. static void setCaptionNonClientAreas(const RenderWindowCore& window, const Vector<Rect2I>& nonClientAreas);
  280. /**
  281. * @brief Sets custom non client areas for the specified window. Using custom client
  282. * areas will override window resize operation and trigger when user interacts
  283. * with the custom area.
  284. *
  285. * @note Thread safe.
  286. * All provided areas are relative to the specified window.
  287. * Mostly useful for frameless windows that don't have typical border.
  288. */
  289. static void setResizeNonClientAreas(const RenderWindowCore& window, const Vector<NonClientResizeArea>& nonClientAreas);
  290. /**
  291. * @brief Resets the non client areas for the specified windows and allows
  292. * the platform to use the default values.
  293. *
  294. * @note Thread safe.
  295. */
  296. static void resetNonClientAreas(const RenderWindowCore& window);
  297. /**
  298. * @brief Adds a string to the clipboard.
  299. *
  300. * @note Thread safe.
  301. */
  302. static void copyToClipboard(const WString& string);
  303. /**
  304. * @brief Reads a string from the clipboard and returns it. If there is no
  305. * string in the clipboard it returns an empty string.
  306. *
  307. * @note Both wide and normal strings will be read, but normal strings will be converted to
  308. * a wide string before returning.
  309. *
  310. * Thread safe.
  311. */
  312. static WString copyFromClipboard();
  313. /**
  314. * @brief Converts a keyboard key-code to a Unicode character.
  315. *
  316. * @note Normally this will output a single character, but it can happen it outputs multiple
  317. * in case a accent/diacritic character could not be combined with the virtual key into
  318. * a single character.
  319. */
  320. static WString keyCodeToUnicode(UINT32 keyCode);
  321. /**
  322. * @brief Populates the provided buffer with a MAC address of the first available
  323. * adapter, if one exists. If no adapters exist, returns false.
  324. */
  325. static bool getMACAddress(MACAddress& address);
  326. /**
  327. * @brief Creates a new universally unique identifier and returns it as a string.
  328. */
  329. static String generateUUID();
  330. /**
  331. * @brief Queries the internal system performance counter you can use for very precise time
  332. * measurements. Value is in milliseconds.
  333. *
  334. * @note Thread safe.
  335. */
  336. static double queryPerformanceTimerMs();
  337. /**
  338. * @brief Creates a drop target that you can use for tracking OS drag and drop operations performed over
  339. * a certain area on the specified window.
  340. *
  341. * @param window The window on which to track drop operations.
  342. * @param x The x coordinate of the area to track, relative to window.
  343. * @param y The y coordinate of the area to track, relative to window.
  344. * @param width The width of the area to track.
  345. * @param height The height of the area to track.
  346. *
  347. * @return OSDropTarget that you will use to receive all drop data. When no longer needed make sure to destroy it with
  348. * destroyDropTarget().
  349. */
  350. static OSDropTarget& createDropTarget(const RenderWindow* window, int x, int y, unsigned int width, unsigned int height);
  351. /**
  352. * @brief Destroys a drop target previously created with createDropTarget.
  353. */
  354. static void destroyDropTarget(OSDropTarget& target);
  355. /**
  356. * @brief Displays a platform specific file/folder open/save dialog.
  357. *
  358. * @param type Type of dialog to open.
  359. * @param defaultPath Initial path the dialog will be set to once opened.
  360. * @param filterList Semi-colon separated list of file names or types to display in the dialog, e.g. "exe;txt;png".
  361. * Ignored if dialog is to display folders instead of files.
  362. * @param paths Output list of selected file or folder paths (if any).
  363. *
  364. * @return True if file was selected and false if selection was canceled.
  365. */
  366. static bool openBrowseDialog(FileDialogType type, const Path& defaultPath, const WString& filterList,
  367. Vector<Path>& paths);
  368. /**
  369. * @brief Message pump. Processes OS messages and returns when it's free.
  370. *
  371. * @note Internal method.
  372. * Core thread only.
  373. */
  374. static void _messagePump();
  375. /**
  376. * @brief Called during application start up from the sim thread.
  377. * Must be called before any other operations are done.
  378. *
  379. * @note Internal method.
  380. */
  381. static void _startUp();
  382. /**
  383. * @brief Called once per frame from the sim thread.
  384. *
  385. * @note Internal method.
  386. * Sim thread only.
  387. */
  388. static void _update();
  389. /**
  390. * @brief Called once per frame from the core thread.
  391. *
  392. * @note Internal method.
  393. * Core thread only.
  394. */
  395. static void _coreUpdate();
  396. /**
  397. * @brief Called during application shut down from the sim thread.
  398. *
  399. * @note Internal method.
  400. * Sim thread only.
  401. */
  402. static void _shutDown();
  403. /**
  404. * @brief Triggered when a pointer leaves the provided window.
  405. *
  406. * @note Sim thread only.
  407. */
  408. static Event<void(RenderWindowCore*)> onMouseLeftWindow;
  409. /**
  410. * @brief Triggered whenever the pointer moves.
  411. *
  412. * @note Core thread only.
  413. */
  414. static Event<void(const Vector2I&, OSPointerButtonStates)> onCursorMoved;
  415. /**
  416. * @brief Triggered whenever a pointer button is pressed.
  417. *
  418. * @note Core thread only.
  419. */
  420. static Event<void(const Vector2I&, OSMouseButton button, OSPointerButtonStates)> onCursorButtonPressed;
  421. /**
  422. * @brief Triggered whenever pointer button is released.
  423. *
  424. * @note Core thread only.
  425. */
  426. static Event<void(const Vector2I&, OSMouseButton button, OSPointerButtonStates)> onCursorButtonReleased;
  427. /**
  428. * @brief Triggered whenever a pointer button is double clicked.
  429. *
  430. * @note Core thread only.
  431. */
  432. static Event<void(const Vector2I&, OSPointerButtonStates)> onCursorDoubleClick;
  433. /**
  434. * @brief Triggered whenever an input command is entered.
  435. *
  436. * @note Core thread only.
  437. */
  438. static Event<void(InputCommandType)> onInputCommand;
  439. /**
  440. * @brief Triggered whenever the mouse wheel is scolled.
  441. *
  442. * @note Core thread only.
  443. */
  444. static Event<void(float)> onMouseWheelScrolled;
  445. /**
  446. * @brief Triggered whenever a character is entered.
  447. *
  448. * @note Core thread only.
  449. */
  450. static Event<void(UINT32)> onCharInput;
  451. /**
  452. * @brief Triggered whenever mouse capture state for the window is changed
  453. * (it receives or loses it).
  454. *
  455. * @note Core thread only.
  456. */
  457. static Event<void()> onMouseCaptureChanged;
  458. protected:
  459. static Pimpl* mData;
  460. };
  461. }