CmPlatformImpl.cpp 8.7 KB

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