CmPlatformImpl.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. bool Platform::isPointOverWindow(const RenderWindow& window, const Int2& screenPos)
  54. {
  55. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  56. POINT point;
  57. point.x = screenPos.x;
  58. point.y = screenPos.y;
  59. HWND hwndToCheck;
  60. window.getCustomAttribute("WINDOW", &hwndToCheck);
  61. HWND hwndUnderPos = WindowFromPoint(point);
  62. return hwndUnderPos == hwndToCheck;
  63. }
  64. void Platform::hideCursor()
  65. {
  66. mIsCursorHidden = true;
  67. // ShowCursor(FALSE) doesn't work. Presumably because we're in the wrong thread, and using
  68. // WM_SETCURSOR in message loop to hide the cursor is smarter solution anyway.
  69. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  70. HWND hwnd;
  71. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  72. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  73. }
  74. void Platform::showCursor()
  75. {
  76. mIsCursorHidden = false;
  77. // ShowCursor(FALSE) doesn't work. Presumably because we're in the wrong thread, and using
  78. // WM_SETCURSOR in message loop to hide the cursor is smarter solution anyway.
  79. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  80. HWND hwnd;
  81. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  82. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  83. }
  84. void Platform::clipCursorToWindow(const RenderWindow& window)
  85. {
  86. HWND hwnd;
  87. window.getCustomAttribute("WINDOW", &hwnd);
  88. // Clip cursor to the window
  89. RECT clipWindowRect;
  90. if(GetWindowRect(hwnd, &clipWindowRect))
  91. {
  92. ClipCursor(&clipWindowRect);
  93. }
  94. }
  95. void Platform::clipCursorToRect(const Rect& screenRect)
  96. {
  97. RECT clipWindowRect;
  98. clipWindowRect.left = screenRect.x;
  99. clipWindowRect.top = screenRect.y;
  100. clipWindowRect.right = screenRect.x + screenRect.width;
  101. clipWindowRect.bottom = screenRect.y + screenRect.height;
  102. ClipCursor(&clipWindowRect);
  103. }
  104. void Platform::clipCursorDisable()
  105. {
  106. ClipCursor(NULL);
  107. }
  108. void Platform::setCursor(CursorType type)
  109. {
  110. if(mUsingCustomCursor)
  111. {
  112. SetCursor(0);
  113. DestroyIcon(mCursor.data->cursor);
  114. mUsingCustomCursor = false;
  115. }
  116. switch(type)
  117. {
  118. case CursorType::Arrow:
  119. mCursor.data->cursor = LoadCursor(0, IDC_ARROW);
  120. break;
  121. case CursorType::Wait:
  122. mCursor.data->cursor = LoadCursor(0, IDC_WAIT);
  123. break;
  124. case CursorType::IBeam:
  125. mCursor.data->cursor = LoadCursor(0, IDC_IBEAM);
  126. break;
  127. case CursorType::Help:
  128. mCursor.data->cursor = LoadCursor(0, IDC_HELP);
  129. break;
  130. case CursorType::Hand:
  131. mCursor.data->cursor = LoadCursor(0, IDC_HAND);
  132. break;
  133. case CursorType::SizeAll:
  134. mCursor.data->cursor = LoadCursor(0, IDC_SIZEALL);
  135. break;
  136. case CursorType::SizeNESW:
  137. mCursor.data->cursor = LoadCursor(0, IDC_SIZENESW);
  138. break;
  139. case CursorType::SizeNS:
  140. mCursor.data->cursor = LoadCursor(0, IDC_SIZENS);
  141. break;
  142. case CursorType::SizeNWSE:
  143. mCursor.data->cursor = LoadCursor(0, IDC_SIZENWSE);
  144. break;
  145. case CursorType::SizeWE:
  146. mCursor.data->cursor = LoadCursor(0, IDC_SIZEWE);
  147. break;
  148. }
  149. // Make sure we notify the message loop to perform the actual cursor update
  150. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  151. HWND hwnd;
  152. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  153. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  154. }
  155. // TODO - Add support for animated custom cursor
  156. void Platform::setCustomCursor(PixelData& pixelData, const Int2& hotSpot)
  157. {
  158. if(mUsingCustomCursor)
  159. {
  160. SetCursor(0);
  161. DestroyIcon(mCursor.data->cursor);
  162. }
  163. mUsingCustomCursor = true;
  164. BITMAPV5HEADER bi;
  165. ZeroMemory(&bi,sizeof(BITMAPV5HEADER));
  166. bi.bV5Size = sizeof(BITMAPV5HEADER);
  167. bi.bV5Width = pixelData.getWidth();
  168. bi.bV5Height = pixelData.getHeight();
  169. bi.bV5Planes = 1;
  170. bi.bV5BitCount = 32;
  171. bi.bV5Compression = BI_BITFIELDS;
  172. bi.bV5RedMask = 0x00FF0000;
  173. bi.bV5GreenMask = 0x0000FF00;
  174. bi.bV5BlueMask = 0x000000FF;
  175. bi.bV5AlphaMask = 0xFF000000;
  176. HDC hDC = GetDC(NULL);
  177. void* data = nullptr;
  178. HBITMAP hBitmap = CreateDIBSection(hDC, (BITMAPINFO *)&bi, DIB_RGB_COLORS,
  179. (void**)&data, NULL, (DWORD)0);
  180. HDC hBitmapDC = CreateCompatibleDC(hDC);
  181. ReleaseDC(NULL, hDC);
  182. // Create an empty mask bitmap.
  183. HBITMAP hMonoBitmap = CreateBitmap(pixelData.getWidth(), pixelData.getHeight(), 1, 1, NULL);
  184. //Select the bitmaps to DC
  185. HBITMAP hOldBitmap = (HBITMAP)SelectObject(hBitmapDC, hBitmap);
  186. //Scan each pixel of the source bitmap and create the masks
  187. Color pixel;
  188. DWORD *dst = (DWORD*)data;
  189. for(UINT32 y = 0; y < pixelData.getHeight(); ++y)
  190. {
  191. for(UINT32 x = 0; x < pixelData.getWidth(); ++x)
  192. {
  193. pixel = pixelData.getColorAt(x, pixelData.getHeight() - y - 1);
  194. *dst = pixel.getAsBGRA();
  195. dst++;
  196. }
  197. }
  198. SelectObject(hBitmapDC, hOldBitmap);
  199. DeleteDC(hBitmapDC);
  200. ICONINFO iconinfo = {0};
  201. iconinfo.fIcon = FALSE;
  202. iconinfo.xHotspot = (DWORD)hotSpot.x;
  203. iconinfo.yHotspot = (DWORD)hotSpot.y;
  204. iconinfo.hbmMask = hMonoBitmap;
  205. iconinfo.hbmColor = hBitmap;
  206. mCursor.data->cursor = CreateIconIndirect(&iconinfo);
  207. DeleteObject(hBitmap);
  208. DeleteObject(hMonoBitmap);
  209. // Make sure we notify the message loop to perform the actual cursor update
  210. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  211. HWND hwnd;
  212. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  213. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  214. }
  215. void Platform::setCaptionNonClientAreas(const RenderWindow& window, const Vector<Rect>::type& nonClientAreas)
  216. {
  217. CM_LOCK_MUTEX(mSync);
  218. mNonClientAreas[&window].moveAreas = nonClientAreas;
  219. }
  220. void Platform::setResizeNonClientAreas(const RenderWindow& window, const Vector<NonClientResizeArea>::type& nonClientAreas)
  221. {
  222. CM_LOCK_MUTEX(mSync);
  223. mNonClientAreas[&window].resizeAreas = nonClientAreas;
  224. }
  225. void Platform::resetNonClientAreas(const RenderWindow& window)
  226. {
  227. CM_LOCK_MUTEX(mSync);
  228. auto iterFind = mNonClientAreas.find(&window);
  229. if(iterFind != end(mNonClientAreas))
  230. mNonClientAreas.erase(iterFind);
  231. }
  232. void Platform::copyToClipboard(const WString& string)
  233. {
  234. HANDLE hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (string.size() + 1) * sizeof(WString::value_type));
  235. WString::value_type* buffer = (WString::value_type*)GlobalLock(hData);
  236. string.copy(buffer, string.size());
  237. buffer[string.size()] = '\0';
  238. GlobalUnlock(hData);
  239. if(OpenClipboard(NULL))
  240. {
  241. EmptyClipboard();
  242. SetClipboardData(CF_UNICODETEXT, hData);
  243. CloseClipboard();
  244. }
  245. else
  246. {
  247. GlobalFree(hData);
  248. }
  249. }
  250. WString Platform::copyFromClipboard()
  251. {
  252. if(OpenClipboard(NULL))
  253. {
  254. HANDLE hData = GetClipboardData(CF_UNICODETEXT);
  255. if(hData != NULL)
  256. {
  257. WString::value_type* buffer = (WString::value_type*)GlobalLock(hData);
  258. WString string(buffer);
  259. GlobalUnlock(hData);
  260. CloseClipboard();
  261. return string;
  262. }
  263. else
  264. {
  265. CloseClipboard();
  266. return L"";
  267. }
  268. }
  269. return L"";
  270. }
  271. void Platform::messagePump()
  272. {
  273. MSG msg;
  274. while(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  275. {
  276. TranslateMessage(&msg);
  277. DispatchMessage(&msg);
  278. }
  279. }
  280. void Platform::update()
  281. {
  282. Vector<RenderWindow*>::type windowsCopy;
  283. {
  284. CM_LOCK_MUTEX(mSync);
  285. windowsCopy = mMouseLeftWindows;
  286. mMouseLeftWindows.clear();
  287. }
  288. for(auto& window : windowsCopy)
  289. {
  290. if(!onMouseLeftWindow.empty())
  291. onMouseLeftWindow(window);
  292. }
  293. }
  294. void Platform::windowFocusReceived(RenderWindow* window)
  295. {
  296. if(!onWindowFocusReceived.empty())
  297. onWindowFocusReceived(window);
  298. }
  299. void Platform::windowFocusLost(RenderWindow* window)
  300. {
  301. if(!onWindowFocusLost.empty())
  302. onWindowFocusLost(window);
  303. }
  304. void Platform::windowMovedOrResized(RenderWindow* window)
  305. {
  306. if(!onWindowMovedOrResized.empty())
  307. onWindowMovedOrResized(window);
  308. }
  309. void Platform::win32ShowCursor()
  310. {
  311. SetCursor(mCursor.data->cursor);
  312. }
  313. void Platform::win32HideCursor()
  314. {
  315. SetCursor(nullptr);
  316. }
  317. }