CmPlatformImpl.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmInputFwd.h"
  4. #include "CmVector2I.h"
  5. #include "CmRectI.h"
  6. #include <boost/signals.hpp>
  7. namespace CamelotFramework
  8. {
  9. // Encapsulate native cursor type so we can avoid including windows.h as it pollutes the global namespace
  10. struct CM_EXPORT NativeCursorData
  11. {
  12. struct Pimpl;
  13. NativeCursorData();
  14. ~NativeCursorData();
  15. Pimpl* data;
  16. };
  17. // Encapsulate native cursor type so we can avoid including windows.h as it pollutes the global namespace
  18. struct CM_EXPORT NativeDropTargetData
  19. {
  20. struct Pimpl;
  21. NativeDropTargetData();
  22. ~NativeDropTargetData();
  23. Pimpl* data;
  24. };
  25. struct CM_EXPORT NonClientResizeArea
  26. {
  27. NonClientAreaBorderType type;
  28. RectI area;
  29. };
  30. struct CM_EXPORT WindowNonClientAreaData
  31. {
  32. Vector<NonClientResizeArea>::type resizeAreas;
  33. Vector<RectI>::type moveAreas;
  34. };
  35. /**
  36. * @brief Provides access to various Windows operating system functions, including
  37. * the main message pump.
  38. */
  39. class CM_EXPORT Platform
  40. {
  41. public:
  42. Platform() { }
  43. virtual ~Platform() { }
  44. /**
  45. * @brief Moves the cursor to the specified screen position.
  46. *
  47. * @note Thread safe.
  48. */
  49. static void setCursorPosition(const Vector2I& screenPos);
  50. /**
  51. * @brief Capture mouse to this window so that we get mouse input even if the mouse leaves the window area.
  52. *
  53. * @note Thread safe.
  54. */
  55. static void captureMouse(const RenderWindow& window);
  56. /**
  57. * @brief Releases the mouse capture set by "captureMouse"
  58. *
  59. * @note Thread safe.
  60. */
  61. static void releaseMouseCapture();
  62. /**
  63. * @brief Checks if provided over screen position is over the specified window.
  64. */
  65. static bool isPointOverWindow(const RenderWindow& window, const Vector2I& screenPos);
  66. /**
  67. * @brief Limit cursor movement to the specified window.
  68. *
  69. * @note Thread safe.
  70. */
  71. static void clipCursorToWindow(const RenderWindow& window);
  72. /**
  73. * @brief Clip cursor to specific area on the screen.
  74. *
  75. * @note Thread safe.
  76. */
  77. static void clipCursorToRect(const RectI& screenRect);
  78. /**
  79. * @brief Disables cursor clipping.
  80. *
  81. * @note Thread safe.
  82. */
  83. static void clipCursorDisable();
  84. /**
  85. * @brief Hides the cursor.
  86. *
  87. * @note Thread safe.
  88. */
  89. static void hideCursor();
  90. /**
  91. * @brief Shows the cursor.
  92. *
  93. * @note Thread safe.
  94. */
  95. static void showCursor();
  96. /**
  97. * @brief Query if the cursor is hidden.
  98. *
  99. * @note Thread safe.
  100. */
  101. static bool isCursorHidden() { return mIsCursorHidden; }
  102. /**
  103. * @brief Sets a cursor icon. Uses built-in platform cursor types.
  104. *
  105. * @note Thread safe.
  106. */
  107. static void setCursor(CursorType type);
  108. /**
  109. * @brief Sets a cursor using a custom image.
  110. *
  111. * @param pixelData Cursor image data.
  112. * @param hotSpot Offset on the cursor image to where the actual input happens (e.g. tip of the Arrow cursor).
  113. *
  114. * @note Thread safe.
  115. */
  116. static void setCustomCursor(PixelData& pixelData, const Vector2I& hotSpot);
  117. /**
  118. * @brief Sets custom caption non client areas for the specified window. Using custom client
  119. * areas will override window move/drag operation and trigger when user interacts
  120. * with the custom area.
  121. *
  122. * @note Thread safe.
  123. * All provided areas are relative to the specified window.
  124. * Mostly useful for frameless windows that don't have typical caption bar.
  125. */
  126. static void setCaptionNonClientAreas(const RenderWindow& window, const Vector<RectI>::type& nonClientAreas);
  127. /**
  128. * @brief Sets custom non client areas for the specified window. Using custom client
  129. * areas will override window resize operation and trigger when user interacts
  130. * with the custom area.
  131. *
  132. * @note Thread safe.
  133. * All provided areas are relative to the specified window.
  134. * Mostly useful for frameless windows that don't have typical border.
  135. */
  136. static void setResizeNonClientAreas(const RenderWindow& window, const Vector<NonClientResizeArea>::type& nonClientAreas);
  137. /**
  138. * @brief Resets the non client areas for the specified windows and allows
  139. * the platform to use the default values.
  140. *
  141. * @note Thread safe.
  142. */
  143. static void resetNonClientAreas(const RenderWindow& window);
  144. /**
  145. * @brief Adds a string to the clipboard.
  146. */
  147. static void copyToClipboard(const WString& string);
  148. /**
  149. * @brief Reads a string from the clipboard and returns it. If there is no
  150. * string in the clipboard it returns an empty string.
  151. *
  152. * @note Both wide and normal strings will be read, but normal strings will be converted to
  153. * a wide string before returning.
  154. */
  155. static WString copyFromClipboard();
  156. /**
  157. * @brief Queries the internal system performance counter you can use for very precise time
  158. * measurements. Value is in milliseconds.
  159. */
  160. static double queryPerformanceTimerMs();
  161. /**
  162. * @brief Creates a drop target that you can use for tracking OS drag and drop operations performed over
  163. * a certain area on the specified window.
  164. *
  165. * @param window The window on which to track drop operations.
  166. * @param x The x coordinate of the area to track, relative to window.
  167. * @param y The y coordinate of the area to track, relative to window.
  168. * @param width The width of the area to track.
  169. * @param height The height of the area to track.
  170. *
  171. * @return OSDropTarget that you will use to receive all drop data. When no longer needed make sure to destroy it with
  172. * destroyDropTarget().
  173. */
  174. static OSDropTarget& createDropTarget(const RenderWindow* window, int x, int y, unsigned int width, unsigned int height);
  175. /**
  176. * @brief Destroys a drop target previously created with createDropTarget.
  177. */
  178. static void destroyDropTarget(OSDropTarget& target);
  179. /**
  180. * @brief Message pump. Processes OS messages and returns when it's free.
  181. *
  182. * @note This method must be called from the core thread.
  183. * Internal method.
  184. */
  185. static void messagePump();
  186. /**
  187. * @brief Called during application start up from the sim thread.
  188. * Must be called before any other operations are done.
  189. */
  190. static void startUp();
  191. /**
  192. * @brief Called once per frame from the sim thread.
  193. *
  194. * @note Internal method.
  195. */
  196. static void update();
  197. /**
  198. * @brief Called once per frame from the core thread.
  199. *
  200. * @note Internal method.
  201. */
  202. static void coreUpdate();
  203. /**
  204. * @brief Called during application shut down from the sim thread.
  205. */
  206. static void shutDown();
  207. // Callbacks triggered on the sim thread
  208. static boost::signal<void(RenderWindow*)> onMouseLeftWindow;
  209. // Callbacks triggered on the core thread. Be careful so that none
  210. // of the connected methods call methods intended for sim thread.
  211. static boost::signal<void(const Vector2I&, OSPositionalInputButtonStates)> onCursorMoved;
  212. static boost::signal<void(const Vector2I&, OSMouseButton button, OSPositionalInputButtonStates)> onCursorButtonPressed;
  213. static boost::signal<void(const Vector2I&, OSMouseButton button, OSPositionalInputButtonStates)> onCursorButtonReleased;
  214. static boost::signal<void(const Vector2I&, OSPositionalInputButtonStates)> onCursorDoubleClick;
  215. static boost::signal<void(InputCommandType)> onInputCommand;
  216. static boost::signal<void(float)> onMouseWheelScrolled;
  217. static boost::signal<void(UINT32)> onCharInput;
  218. static boost::signal<void(RenderWindow*)> onWindowFocusReceived;
  219. static boost::signal<void(RenderWindow*)> onWindowFocusLost;
  220. static boost::signal<void(RenderWindow*)> onWindowMovedOrResized;
  221. static boost::signal<void()> onMouseCaptureChanged;
  222. protected:
  223. static bool mIsCursorHidden;
  224. static NativeCursorData mCursor;
  225. static bool mUsingCustomCursor;
  226. static Map<const RenderWindow*, WindowNonClientAreaData>::type mNonClientAreas;
  227. static bool mIsTrackingMouse;
  228. static Vector<RenderWindow*>::type mMouseLeftWindows;
  229. static NativeDropTargetData mDropTargets;
  230. static bool mRequiresStartUp;
  231. static bool mRequiresShutDown;
  232. CM_STATIC_MUTEX(mSync);
  233. static void win32ShowCursor();
  234. static void win32HideCursor();
  235. static void windowFocusReceived(RenderWindow* window);
  236. static void windowFocusLost(RenderWindow* window);
  237. static void windowMovedOrResized(RenderWindow* window);
  238. };
  239. }