CmPlatformImpl.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmInt2.h"
  4. #include "CmRect.h"
  5. #include <boost/signals.hpp>
  6. namespace CamelotFramework
  7. {
  8. // Encapsulate native cursor type so we can avoid including windows.h as it pollutes the global namespace
  9. struct CM_EXPORT NativeCursorData
  10. {
  11. struct Pimpl;
  12. NativeCursorData();
  13. ~NativeCursorData();
  14. Pimpl* data;
  15. };
  16. enum class NonClientAreaBorderType
  17. {
  18. TopLeft,
  19. Top,
  20. TopRight,
  21. Left,
  22. Right,
  23. BottomLeft,
  24. Bottom,
  25. BottomRight
  26. };
  27. struct CM_EXPORT NonClientResizeArea
  28. {
  29. NonClientAreaBorderType type;
  30. Rect area;
  31. };
  32. struct CM_EXPORT WindowNonClientAreaData
  33. {
  34. Vector<NonClientResizeArea>::type resizeAreas;
  35. Vector<Rect>::type moveAreas;
  36. };
  37. /**
  38. * @brief Provides access for version Windows operating system functions, including
  39. * the main message pump.
  40. */
  41. class CM_EXPORT Platform
  42. {
  43. public:
  44. Platform() { }
  45. virtual ~Platform() { }
  46. /**
  47. * @brief Moves the cursor to the specified screen position.
  48. *
  49. * @note Thread safe.
  50. */
  51. static void setCursorPosition(const Int2& screenPos);
  52. /**
  53. * @brief Capture mouse to this window so that we get mouse input even if the mouse leaves the window area.
  54. *
  55. * @note Thread safe.
  56. */
  57. static void captureMouse(const RenderWindow& window);
  58. /**
  59. * @brief Releases the mouse capture set by "captureMouse"
  60. *
  61. * @note Thread safe.
  62. */
  63. static void releaseMouseCapture();
  64. /**
  65. * @brief Limit cursor movement to the specified window.
  66. *
  67. * @note Thread safe.
  68. */
  69. static void clipCursorToWindow(const RenderWindow& window);
  70. /**
  71. * @brief Clip cursor to specific area on the screen.
  72. *
  73. * @note Thread safe.
  74. */
  75. static void clipCursorToRect(const Rect& screenRect);
  76. /**
  77. * @brief Disables cursor clipping.
  78. *
  79. * @note Thread safe.
  80. */
  81. static void clipCursorDisable();
  82. /**
  83. * @brief Hides the cursor.
  84. *
  85. * @note Thread safe.
  86. */
  87. static void hideCursor();
  88. /**
  89. * @brief Shows the cursor.
  90. *
  91. * @note Thread safe.
  92. */
  93. static void showCursor();
  94. /**
  95. * @brief Query if the cursor is hidden.
  96. *
  97. * @note Thread safe.
  98. */
  99. static bool isCursorHidden() { return mIsCursorHidden; }
  100. /**
  101. * @brief Sets a cursor icon. Uses built-in platform cursor types.
  102. *
  103. * @note Thread safe.
  104. */
  105. static void setCursor(CursorType type);
  106. /**
  107. * @brief Sets a cursor using a custom image.
  108. *
  109. * @param pixelData Cursor image data.
  110. * @param hotSpot Offset on the cursor image to where the actual input happens (e.g. tip of the Arrow cursor).
  111. *
  112. * @note Thread safe.
  113. */
  114. static void setCustomCursor(PixelData& pixelData, const Int2& hotSpot);
  115. /**
  116. * @brief Sets custom caption non client areas for the specified window. Using custom client
  117. * areas will override window move/drag operation and trigger when user interacts
  118. * with the custom area.
  119. *
  120. * @note Thread safe.
  121. * All provided areas are relative to the specified window.
  122. * Mostly useful for frameless windows that don't have typical caption bar.
  123. */
  124. static void setCaptionNonClientAreas(const RenderWindow& window, const Vector<Rect>::type& nonClientAreas);
  125. /**
  126. * @brief Sets custom non client areas for the specified window. Using custom client
  127. * areas will override window resize operation and trigger when user interacts
  128. * with the custom area.
  129. *
  130. * @note Thread safe.
  131. * All provided areas are relative to the specified window.
  132. * Mostly useful for frameless windows that don't have typical border.
  133. */
  134. static void setResizeNonClientAreas(const RenderWindow& window, const Vector<NonClientResizeArea>::type& nonClientAreas);
  135. /**
  136. * @brief Resets the non client areas for the specified windows and allows
  137. * the platform to use the default values.
  138. *
  139. * @note Thread safe.
  140. */
  141. static void resetNonClientAreas(const RenderWindow& window);
  142. /**
  143. * @brief Message pump. Processes OS messages and returns when it's free.
  144. *
  145. * @note This method must be called from the core thread.
  146. * Internal method.
  147. */
  148. static void messagePump();
  149. /**
  150. * @brief Called once per frame from the sim thread.
  151. *
  152. * @note Internal method.
  153. */
  154. static void update();
  155. // Callbacks triggered on the sim thread
  156. static boost::signal<void(RenderWindow*)> onMouseLeftWindow;
  157. // Callbacks triggered on the core thread. Be careful so that none
  158. // of the connected methods call methods intended for sim thread.
  159. static boost::signal<void(const Int2&)> onMouseMoved;
  160. static boost::signal<void(float)> onMouseWheelScrolled;
  161. static boost::signal<void(UINT32)> onCharInput;
  162. static boost::signal<void(RenderWindow*)> onWindowFocusReceived;
  163. static boost::signal<void(RenderWindow*)> onWindowFocusLost;
  164. static boost::signal<void(RenderWindow*)> onWindowMovedOrResized;
  165. static boost::signal<void()> onMouseCaptureChanged;
  166. protected:
  167. static bool mIsCursorHidden;
  168. static NativeCursorData mCursor;
  169. static bool mUsingCustomCursor;
  170. static Map<const RenderWindow*, WindowNonClientAreaData>::type mNonClientAreas;
  171. static bool mIsTrackingMouse;
  172. static Vector<RenderWindow*>::type mMouseLeftWindows;
  173. CM_STATIC_MUTEX(mSync);
  174. static void win32ShowCursor();
  175. static void win32HideCursor();
  176. static void windowFocusReceived(RenderWindow* window);
  177. static void windowFocusLost(RenderWindow* window);
  178. static void windowMovedOrResized(RenderWindow* window);
  179. };
  180. }