CmPlatformImpl.cpp 11 KB

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