BsPlatform.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. /** Represents a specific non client area used for window resizing. */
  64. struct BS_CORE_EXPORT NonClientResizeArea
  65. {
  66. NonClientAreaBorderType type;
  67. Rect2I area;
  68. };
  69. /** Contains a list of window move and resize non client areas. */
  70. struct BS_CORE_EXPORT WindowNonClientAreaData
  71. {
  72. Vector<NonClientResizeArea> resizeAreas;
  73. Vector<Rect2I> moveAreas;
  74. };
  75. /** Provides access to various operating system functions, including the main message pump. */
  76. class BS_CORE_EXPORT Platform
  77. {
  78. public:
  79. struct Pimpl;
  80. Platform() { }
  81. virtual ~Platform();
  82. /**
  83. * Retrieves the cursor position in screen coordinates.
  84. *
  85. * @note Thread safe.
  86. */
  87. static Vector2I getCursorPosition();
  88. /**
  89. * Moves the cursor to the specified screen position.
  90. *
  91. * @note Thread safe.
  92. */
  93. static void setCursorPosition(const Vector2I& screenPos);
  94. /**
  95. * Capture mouse to this window so that we get mouse input even if the mouse leaves the window area.
  96. *
  97. * @note Thread safe.
  98. */
  99. static void captureMouse(const RenderWindow& window);
  100. /**
  101. * Releases the mouse capture set by captureMouse().
  102. *
  103. * @note Thread safe.
  104. */
  105. static void releaseMouseCapture();
  106. /**
  107. * Checks if provided over screen position is over the specified window.
  108. */
  109. static bool isPointOverWindow(const RenderWindow& window, const Vector2I& screenPos);
  110. /**
  111. * Limit cursor movement to the specified window.
  112. *
  113. * @note Thread safe.
  114. */
  115. static void clipCursorToWindow(const RenderWindow& window);
  116. /**
  117. * Clip cursor to specific area on the screen.
  118. *
  119. * @note Thread safe.
  120. */
  121. static void clipCursorToRect(const Rect2I& screenRect);
  122. /**
  123. * Disables cursor clipping.
  124. *
  125. * @note Thread safe.
  126. */
  127. static void clipCursorDisable();
  128. /**
  129. * Hides the cursor.
  130. *
  131. * @note Thread safe.
  132. */
  133. static void hideCursor();
  134. /**
  135. * Shows the cursor.
  136. *
  137. * @note Thread safe.
  138. */
  139. static void showCursor();
  140. /**
  141. * Query if the cursor is hidden.
  142. *
  143. * @note Thread safe.
  144. */
  145. static bool isCursorHidden();
  146. /**
  147. * Sets a cursor using a custom image.
  148. *
  149. * @param[in] pixelData Cursor image data.
  150. * @param[in] hotSpot Offset on the cursor image to where the actual input happens (for example tip of the
  151. * Arrow cursor).
  152. *
  153. * @note Thread safe.
  154. */
  155. static void setCursor(PixelData& pixelData, const Vector2I& hotSpot);
  156. /**
  157. * Sets an icon for the main application window.
  158. *
  159. * @param[in] pixelData Icon image data. This will be resized to the required icon size, depending on platform
  160. * implementation.
  161. *
  162. * @note Thread safe.
  163. */
  164. static void setIcon(const PixelData& pixelData);
  165. /**
  166. * Sets custom caption non client areas for the specified window. Using custom client areas will override window
  167. * move/drag operation and trigger when user interacts with the custom area.
  168. *
  169. * @note
  170. * Thread safe.
  171. * @note
  172. * All provided areas are relative to the specified window. Mostly useful for frameless windows that don't have
  173. * typical caption bar.
  174. */
  175. static void setCaptionNonClientAreas(const ct::RenderWindow& window, const Vector<Rect2I>& nonClientAreas);
  176. /**
  177. * Sets custom non client areas for the specified window. Using custom client areas will override window resize
  178. * operation and trigger when user interacts with the custom area.
  179. *
  180. * @note
  181. * Thread safe.
  182. * @note
  183. * All provided areas are relative to the specified window. Mostly useful for frameless windows that don't have
  184. * typical border.
  185. */
  186. static void setResizeNonClientAreas(const ct::RenderWindow& window, const Vector<NonClientResizeArea>& nonClientAreas);
  187. /**
  188. * Resets the non client areas for the specified windows and allows the platform to use the default values.
  189. *
  190. * @note Thread safe.
  191. */
  192. static void resetNonClientAreas(const ct::RenderWindow& window);
  193. /**
  194. * Causes the current thread to pause execution for the specified amount of time.
  195. *
  196. * @param[in] duration Duration in milliseconds. Providing zero will give up the current time-slice.
  197. *
  198. * @note This method relies on timer granularity being set to 1 millisecond. If it is not, you can expect
  199. * this method to potentially take significantly longer if you are providing it with low ms values (<10).
  200. */
  201. static void sleep(UINT32 duration);
  202. /**
  203. * Opens the provided folder using the default application, as specified by the operating system.
  204. *
  205. * @param[in] path Absolute path to the folder to open.
  206. */
  207. static void openFolder(const Path& path);
  208. /**
  209. * Adds a string to the clipboard.
  210. *
  211. * @note Thread safe.
  212. */
  213. static void copyToClipboard(const WString& string);
  214. /**
  215. * Reads a string from the clipboard and returns it. If there is no string in the clipboard it returns an empty
  216. * string.
  217. *
  218. * @note
  219. * Both wide and normal strings will be read, but normal strings will be converted to a wide string before returning.
  220. * @note
  221. * Thread safe.
  222. */
  223. static WString copyFromClipboard();
  224. /**
  225. * Converts a keyboard key-code to a Unicode character.
  226. *
  227. * @note
  228. * Normally this will output a single character, but it can happen it outputs multiple in case a accent/diacritic
  229. * character could not be combined with the virtual key into a single character.
  230. */
  231. static WString keyCodeToUnicode(UINT32 keyCode);
  232. /**
  233. * Message pump. Processes OS messages and returns when it's free.
  234. *
  235. * @note Core thread only.
  236. */
  237. static void _messagePump();
  238. /** Called during application start up from the sim thread. Must be called before any other operations are done. */
  239. static void _startUp();
  240. /**
  241. * Called once per frame from the sim thread.
  242. *
  243. * @note Sim thread only.
  244. */
  245. static void _update();
  246. /**
  247. * Called once per frame from the core thread.
  248. *
  249. * @note Core thread only.
  250. */
  251. static void _coreUpdate();
  252. /**
  253. * Called during application shut down from the sim thread.
  254. *
  255. * @note Sim thread only.
  256. */
  257. static void _shutDown();
  258. /**
  259. * Triggered whenever the pointer moves.
  260. *
  261. * @note Core thread only.
  262. */
  263. static Event<void(const Vector2I&, const OSPointerButtonStates&)> onCursorMoved;
  264. /**
  265. * Triggered whenever a pointer button is pressed.
  266. *
  267. * @note Core thread only.
  268. */
  269. static Event<void(const Vector2I&, OSMouseButton button, const OSPointerButtonStates&)> onCursorButtonPressed;
  270. /**
  271. * Triggered whenever pointer button is released.
  272. *
  273. * @note Core thread only.
  274. */
  275. static Event<void(const Vector2I&, OSMouseButton button, const OSPointerButtonStates&)> onCursorButtonReleased;
  276. /**
  277. * Triggered whenever a pointer button is double clicked.
  278. *
  279. * @note Core thread only.
  280. */
  281. static Event<void(const Vector2I&, const OSPointerButtonStates&)> onCursorDoubleClick;
  282. /**
  283. * Triggered whenever an input command is entered.
  284. *
  285. * @note Core thread only.
  286. */
  287. static Event<void(InputCommandType)> onInputCommand;
  288. /**
  289. * Triggered whenever the mouse wheel is scolled.
  290. *
  291. * @note Core thread only.
  292. */
  293. static Event<void(float)> onMouseWheelScrolled;
  294. /**
  295. * Triggered whenever a character is entered.
  296. *
  297. * @note Core thread only.
  298. */
  299. static Event<void(UINT32)> onCharInput;
  300. /**
  301. * Triggered whenever mouse capture state for the window is changed (it receives or loses it).
  302. *
  303. * @note Core thread only.
  304. */
  305. static Event<void()> onMouseCaptureChanged;
  306. protected:
  307. static Pimpl* mData;
  308. };
  309. /** @} */
  310. }