CmPlatformImpl.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "CmPlatform.h"
  2. #include "CmRenderWindow.h"
  3. #include "CmPixelUtil.h"
  4. #include "CmApplication.h"
  5. #include "CmWin32Defs.h"
  6. namespace CamelotFramework
  7. {
  8. boost::signal<void(const Int2&)> Platform::onMouseMoved;
  9. boost::signal<void(float)> Platform::onMouseWheelScrolled;
  10. boost::signal<void(UINT32)> Platform::onCharInput;
  11. boost::signal<void(RenderWindow*)> Platform::onWindowFocusReceived;
  12. boost::signal<void(RenderWindow*)> Platform::onWindowFocusLost;
  13. boost::signal<void(RenderWindow*)> Platform::onWindowMovedOrResized;
  14. boost::signal<void()> Platform::onMouseCaptureChanged;
  15. Map<const RenderWindow*, WindowNonClientAreaData>::type Platform::mNonClientAreas;
  16. CM_STATIC_MUTEX_CLASS_INSTANCE(mSync, Platform);
  17. struct NativeCursorData::Pimpl
  18. {
  19. HCURSOR cursor;
  20. };
  21. NativeCursorData::NativeCursorData()
  22. {
  23. data = cm_new<Pimpl>();
  24. }
  25. NativeCursorData::~NativeCursorData()
  26. {
  27. cm_delete(data);
  28. }
  29. bool Platform::mIsCursorHidden = false;
  30. NativeCursorData Platform::mCursor;
  31. bool Platform::mUsingCustomCursor = false;
  32. void Platform::setCursorPosition(const Int2& screenPos)
  33. {
  34. SetCursorPos(screenPos.x, screenPos.y);
  35. }
  36. void Platform::captureMouse(const RenderWindow& window)
  37. {
  38. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  39. HWND hwnd;
  40. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  41. PostMessage(hwnd, WM_CM_SETCAPTURE, WPARAM(hwnd), 0);
  42. }
  43. void Platform::releaseMouseCapture()
  44. {
  45. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  46. HWND hwnd;
  47. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  48. PostMessage(hwnd, WM_CM_RELEASECAPTURE, WPARAM(hwnd), 0);
  49. }
  50. void Platform::hideCursor()
  51. {
  52. mIsCursorHidden = true;
  53. // ShowCursor(FALSE) doesn't work. Presumably because we're in the wrong thread, and using
  54. // WM_SETCURSOR in message loop to hide the cursor is smarter solution anyway.
  55. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  56. HWND hwnd;
  57. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  58. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  59. }
  60. void Platform::showCursor()
  61. {
  62. mIsCursorHidden = false;
  63. // ShowCursor(FALSE) doesn't work. Presumably because we're in the wrong thread, and using
  64. // WM_SETCURSOR in message loop to hide the cursor is smarter solution anyway.
  65. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  66. HWND hwnd;
  67. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  68. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  69. }
  70. void Platform::clipCursorToWindow(const RenderWindow& window)
  71. {
  72. HWND hwnd;
  73. window.getCustomAttribute("WINDOW", &hwnd);
  74. // Clip cursor to the window
  75. RECT clipWindowRect;
  76. if(GetWindowRect(hwnd, &clipWindowRect))
  77. {
  78. ClipCursor(&clipWindowRect);
  79. }
  80. }
  81. void Platform::clipCursorToRect(const Rect& screenRect)
  82. {
  83. RECT clipWindowRect;
  84. clipWindowRect.left = screenRect.x;
  85. clipWindowRect.top = screenRect.y;
  86. clipWindowRect.right = screenRect.x + screenRect.width;
  87. clipWindowRect.bottom = screenRect.y + screenRect.height;
  88. ClipCursor(&clipWindowRect);
  89. }
  90. void Platform::clipCursorDisable()
  91. {
  92. ClipCursor(NULL);
  93. }
  94. void Platform::setCursor(CursorType type)
  95. {
  96. if(mUsingCustomCursor)
  97. {
  98. SetCursor(0);
  99. DestroyIcon(mCursor.data->cursor);
  100. mUsingCustomCursor = false;
  101. }
  102. switch(type)
  103. {
  104. case CursorType::Arrow:
  105. mCursor.data->cursor = LoadCursor(0, IDC_ARROW);
  106. break;
  107. case CursorType::Wait:
  108. mCursor.data->cursor = LoadCursor(0, IDC_WAIT);
  109. break;
  110. case CursorType::IBeam:
  111. mCursor.data->cursor = LoadCursor(0, IDC_IBEAM);
  112. break;
  113. case CursorType::Help:
  114. mCursor.data->cursor = LoadCursor(0, IDC_HELP);
  115. break;
  116. case CursorType::Hand:
  117. mCursor.data->cursor = LoadCursor(0, IDC_HAND);
  118. break;
  119. case CursorType::SizeAll:
  120. mCursor.data->cursor = LoadCursor(0, IDC_SIZEALL);
  121. break;
  122. case CursorType::SizeNESW:
  123. mCursor.data->cursor = LoadCursor(0, IDC_SIZENESW);
  124. break;
  125. case CursorType::SizeNS:
  126. mCursor.data->cursor = LoadCursor(0, IDC_SIZENS);
  127. break;
  128. case CursorType::SizeNWSE:
  129. mCursor.data->cursor = LoadCursor(0, IDC_SIZENWSE);
  130. break;
  131. case CursorType::SizeWE:
  132. mCursor.data->cursor = LoadCursor(0, IDC_SIZEWE);
  133. break;
  134. }
  135. // Make sure we notify the message loop to perform the actual cursor update
  136. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  137. HWND hwnd;
  138. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  139. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  140. }
  141. // TODO - Add support for animated custom cursor
  142. void Platform::setCustomCursor(PixelData& pixelData, const Int2& hotSpot)
  143. {
  144. if(mUsingCustomCursor)
  145. {
  146. SetCursor(0);
  147. DestroyIcon(mCursor.data->cursor);
  148. }
  149. mUsingCustomCursor = true;
  150. BITMAPV5HEADER bi;
  151. ZeroMemory(&bi,sizeof(BITMAPV5HEADER));
  152. bi.bV5Size = sizeof(BITMAPV5HEADER);
  153. bi.bV5Width = pixelData.getWidth();
  154. bi.bV5Height = pixelData.getHeight();
  155. bi.bV5Planes = 1;
  156. bi.bV5BitCount = 32;
  157. bi.bV5Compression = BI_BITFIELDS;
  158. bi.bV5RedMask = 0x00FF0000;
  159. bi.bV5GreenMask = 0x0000FF00;
  160. bi.bV5BlueMask = 0x000000FF;
  161. bi.bV5AlphaMask = 0xFF000000;
  162. HDC hDC = GetDC(NULL);
  163. void* data = nullptr;
  164. HBITMAP hBitmap = CreateDIBSection(hDC, (BITMAPINFO *)&bi, DIB_RGB_COLORS,
  165. (void**)&data, NULL, (DWORD)0);
  166. HDC hBitmapDC = CreateCompatibleDC(hDC);
  167. ReleaseDC(NULL, hDC);
  168. // Create an empty mask bitmap.
  169. HBITMAP hMonoBitmap = CreateBitmap(pixelData.getWidth(), pixelData.getHeight(), 1, 1, NULL);
  170. //Select the bitmaps to DC
  171. HBITMAP hOldBitmap = (HBITMAP)SelectObject(hBitmapDC, hBitmap);
  172. //Scan each pixel of the source bitmap and create the masks
  173. Color pixel;
  174. DWORD *dst = (DWORD*)data;
  175. for(UINT32 y = 0; y < pixelData.getHeight(); ++y)
  176. {
  177. for(UINT32 x = 0; x < pixelData.getWidth(); ++x)
  178. {
  179. pixel = pixelData.getColorAt(x, pixelData.getHeight() - y - 1);
  180. *dst = pixel.getAsBGRA();
  181. dst++;
  182. }
  183. }
  184. SelectObject(hBitmapDC, hOldBitmap);
  185. DeleteDC(hBitmapDC);
  186. ICONINFO iconinfo = {0};
  187. iconinfo.fIcon = FALSE;
  188. iconinfo.xHotspot = (DWORD)hotSpot.x;
  189. iconinfo.yHotspot = (DWORD)hotSpot.y;
  190. iconinfo.hbmMask = hMonoBitmap;
  191. iconinfo.hbmColor = hBitmap;
  192. mCursor.data->cursor = CreateIconIndirect(&iconinfo);
  193. DeleteObject(hBitmap);
  194. DeleteObject(hMonoBitmap);
  195. // Make sure we notify the message loop to perform the actual cursor update
  196. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  197. HWND hwnd;
  198. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  199. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  200. }
  201. void Platform::setCaptionNonClientAreas(const RenderWindow& window, const Vector<Rect>::type& nonClientAreas)
  202. {
  203. CM_LOCK_MUTEX(mSync);
  204. mNonClientAreas[&window].moveAreas = nonClientAreas;
  205. }
  206. void Platform::setResizeNonClientAreas(const RenderWindow& window, const Vector<NonClientResizeArea>::type& nonClientAreas)
  207. {
  208. CM_LOCK_MUTEX(mSync);
  209. mNonClientAreas[&window].resizeAreas = nonClientAreas;
  210. }
  211. void Platform::resetNonClientAreas(const RenderWindow& window)
  212. {
  213. CM_LOCK_MUTEX(mSync);
  214. auto iterFind = mNonClientAreas.find(&window);
  215. if(iterFind != end(mNonClientAreas))
  216. mNonClientAreas.erase(iterFind);
  217. }
  218. void Platform::messagePump()
  219. {
  220. MSG msg;
  221. while(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  222. {
  223. TranslateMessage(&msg);
  224. DispatchMessage(&msg);
  225. }
  226. }
  227. void Platform::windowFocusReceived(RenderWindow* window)
  228. {
  229. if(!onWindowFocusReceived.empty())
  230. onWindowFocusReceived(window);
  231. }
  232. void Platform::windowFocusLost(RenderWindow* window)
  233. {
  234. if(!onWindowFocusLost.empty())
  235. onWindowFocusLost(window);
  236. }
  237. void Platform::windowMovedOrResized(RenderWindow* window)
  238. {
  239. if(!onWindowMovedOrResized.empty())
  240. onWindowMovedOrResized(window);
  241. }
  242. void Platform::win32ShowCursor()
  243. {
  244. SetCursor(mCursor.data->cursor);
  245. }
  246. void Platform::win32HideCursor()
  247. {
  248. SetCursor(nullptr);
  249. }
  250. }