CmPlatformImpl.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. #include "CmPlatform.h"
  2. #include "CmRenderWindow.h"
  3. #include "CmPixelUtil.h"
  4. #include "CmApplication.h"
  5. #include "CmWin32Defs.h"
  6. #include "CmDebug.h"
  7. #include "Win32/CmWin32DropTarget.h"
  8. namespace CamelotFramework
  9. {
  10. boost::signal<void(RenderWindow*)> Platform::onMouseLeftWindow;
  11. boost::signal<void(const Int2&, OSPositionalInputButtonStates)> Platform::onCursorMoved;
  12. boost::signal<void(const Int2&, OSMouseButton button, OSPositionalInputButtonStates)> Platform::onCursorButtonPressed;
  13. boost::signal<void(const Int2&, OSMouseButton button, OSPositionalInputButtonStates)> Platform::onCursorButtonReleased;
  14. boost::signal<void(const Int2&, OSPositionalInputButtonStates)> Platform::onCursorDoubleClick;
  15. boost::signal<void(InputCommandType)> Platform::onInputCommand;
  16. boost::signal<void(float)> Platform::onMouseWheelScrolled;
  17. boost::signal<void(UINT32)> Platform::onCharInput;
  18. boost::signal<void(RenderWindow*)> Platform::onWindowFocusReceived;
  19. boost::signal<void(RenderWindow*)> Platform::onWindowFocusLost;
  20. boost::signal<void(RenderWindow*)> Platform::onWindowMovedOrResized;
  21. boost::signal<void()> Platform::onMouseCaptureChanged;
  22. Map<const RenderWindow*, WindowNonClientAreaData>::type Platform::mNonClientAreas;
  23. bool Platform::mIsTrackingMouse = false;
  24. Vector<RenderWindow*>::type Platform::mMouseLeftWindows;
  25. NativeDropTargetData Platform::mDropTargets;
  26. bool Platform::mRequiresStartUp = false;
  27. bool Platform::mRequiresShutDown = false;
  28. CM_STATIC_MUTEX_CLASS_INSTANCE(mSync, Platform);
  29. struct NativeCursorData::Pimpl
  30. {
  31. HCURSOR cursor;
  32. };
  33. NativeCursorData::NativeCursorData()
  34. {
  35. data = cm_new<Pimpl>();
  36. }
  37. NativeCursorData::~NativeCursorData()
  38. {
  39. cm_delete(data);
  40. }
  41. struct NativeDropTargetData::Pimpl
  42. {
  43. Map<const RenderWindow*, Win32DropTarget*>::type dropTargetsPerWindow;
  44. Vector<Win32DropTarget*>::type dropTargetsToInitialize;
  45. Vector<Win32DropTarget*>::type dropTargetsToDestroy;
  46. };
  47. NativeDropTargetData::NativeDropTargetData()
  48. {
  49. data = cm_new<Pimpl>();
  50. }
  51. NativeDropTargetData::~NativeDropTargetData()
  52. {
  53. cm_delete(data);
  54. }
  55. bool Platform::mIsCursorHidden = false;
  56. NativeCursorData Platform::mCursor;
  57. bool Platform::mUsingCustomCursor = false;
  58. void Platform::setCursorPosition(const Int2& screenPos)
  59. {
  60. SetCursorPos(screenPos.x, screenPos.y);
  61. }
  62. void Platform::captureMouse(const RenderWindow& window)
  63. {
  64. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  65. HWND hwnd;
  66. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  67. PostMessage(hwnd, WM_CM_SETCAPTURE, WPARAM(hwnd), 0);
  68. }
  69. void Platform::releaseMouseCapture()
  70. {
  71. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  72. HWND hwnd;
  73. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  74. PostMessage(hwnd, WM_CM_RELEASECAPTURE, WPARAM(hwnd), 0);
  75. }
  76. bool Platform::isPointOverWindow(const RenderWindow& window, const Int2& screenPos)
  77. {
  78. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  79. POINT point;
  80. point.x = screenPos.x;
  81. point.y = screenPos.y;
  82. HWND hwndToCheck;
  83. window.getCustomAttribute("WINDOW", &hwndToCheck);
  84. HWND hwndUnderPos = WindowFromPoint(point);
  85. return hwndUnderPos == hwndToCheck;
  86. }
  87. void Platform::hideCursor()
  88. {
  89. mIsCursorHidden = true;
  90. // ShowCursor(FALSE) doesn't work. Presumably because we're in the wrong thread, and using
  91. // WM_SETCURSOR in message loop to hide the cursor is smarter solution anyway.
  92. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  93. HWND hwnd;
  94. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  95. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  96. }
  97. void Platform::showCursor()
  98. {
  99. mIsCursorHidden = false;
  100. // ShowCursor(FALSE) doesn't work. Presumably because we're in the wrong thread, and using
  101. // WM_SETCURSOR in message loop to hide the cursor is smarter solution anyway.
  102. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  103. HWND hwnd;
  104. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  105. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  106. }
  107. void Platform::clipCursorToWindow(const RenderWindow& window)
  108. {
  109. HWND hwnd;
  110. window.getCustomAttribute("WINDOW", &hwnd);
  111. // Clip cursor to the window
  112. RECT clipWindowRect;
  113. if(GetWindowRect(hwnd, &clipWindowRect))
  114. {
  115. ClipCursor(&clipWindowRect);
  116. }
  117. }
  118. void Platform::clipCursorToRect(const Rect& screenRect)
  119. {
  120. RECT clipWindowRect;
  121. clipWindowRect.left = screenRect.x;
  122. clipWindowRect.top = screenRect.y;
  123. clipWindowRect.right = screenRect.x + screenRect.width;
  124. clipWindowRect.bottom = screenRect.y + screenRect.height;
  125. ClipCursor(&clipWindowRect);
  126. }
  127. void Platform::clipCursorDisable()
  128. {
  129. ClipCursor(NULL);
  130. }
  131. void Platform::setCursor(CursorType type)
  132. {
  133. if(mUsingCustomCursor)
  134. {
  135. SetCursor(0);
  136. DestroyIcon(mCursor.data->cursor);
  137. mUsingCustomCursor = false;
  138. }
  139. switch(type)
  140. {
  141. case CursorType::Arrow:
  142. mCursor.data->cursor = LoadCursor(0, IDC_ARROW);
  143. break;
  144. case CursorType::Wait:
  145. mCursor.data->cursor = LoadCursor(0, IDC_WAIT);
  146. break;
  147. case CursorType::IBeam:
  148. mCursor.data->cursor = LoadCursor(0, IDC_IBEAM);
  149. break;
  150. case CursorType::Help:
  151. mCursor.data->cursor = LoadCursor(0, IDC_HELP);
  152. break;
  153. case CursorType::Hand:
  154. mCursor.data->cursor = LoadCursor(0, IDC_HAND);
  155. break;
  156. case CursorType::SizeAll:
  157. mCursor.data->cursor = LoadCursor(0, IDC_SIZEALL);
  158. break;
  159. case CursorType::SizeNESW:
  160. mCursor.data->cursor = LoadCursor(0, IDC_SIZENESW);
  161. break;
  162. case CursorType::SizeNS:
  163. mCursor.data->cursor = LoadCursor(0, IDC_SIZENS);
  164. break;
  165. case CursorType::SizeNWSE:
  166. mCursor.data->cursor = LoadCursor(0, IDC_SIZENWSE);
  167. break;
  168. case CursorType::SizeWE:
  169. mCursor.data->cursor = LoadCursor(0, IDC_SIZEWE);
  170. break;
  171. }
  172. // Make sure we notify the message loop to perform the actual cursor update
  173. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  174. HWND hwnd;
  175. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  176. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  177. }
  178. // TODO - Add support for animated custom cursor
  179. void Platform::setCustomCursor(PixelData& pixelData, const Int2& hotSpot)
  180. {
  181. if(mUsingCustomCursor)
  182. {
  183. SetCursor(0);
  184. DestroyIcon(mCursor.data->cursor);
  185. }
  186. mUsingCustomCursor = true;
  187. BITMAPV5HEADER bi;
  188. ZeroMemory(&bi,sizeof(BITMAPV5HEADER));
  189. bi.bV5Size = sizeof(BITMAPV5HEADER);
  190. bi.bV5Width = pixelData.getWidth();
  191. bi.bV5Height = pixelData.getHeight();
  192. bi.bV5Planes = 1;
  193. bi.bV5BitCount = 32;
  194. bi.bV5Compression = BI_BITFIELDS;
  195. bi.bV5RedMask = 0x00FF0000;
  196. bi.bV5GreenMask = 0x0000FF00;
  197. bi.bV5BlueMask = 0x000000FF;
  198. bi.bV5AlphaMask = 0xFF000000;
  199. HDC hDC = GetDC(NULL);
  200. void* data = nullptr;
  201. HBITMAP hBitmap = CreateDIBSection(hDC, (BITMAPINFO *)&bi, DIB_RGB_COLORS,
  202. (void**)&data, NULL, (DWORD)0);
  203. HDC hBitmapDC = CreateCompatibleDC(hDC);
  204. ReleaseDC(NULL, hDC);
  205. // Create an empty mask bitmap.
  206. HBITMAP hMonoBitmap = CreateBitmap(pixelData.getWidth(), pixelData.getHeight(), 1, 1, NULL);
  207. //Select the bitmaps to DC
  208. HBITMAP hOldBitmap = (HBITMAP)SelectObject(hBitmapDC, hBitmap);
  209. //Scan each pixel of the source bitmap and create the masks
  210. Color pixel;
  211. DWORD *dst = (DWORD*)data;
  212. for(UINT32 y = 0; y < pixelData.getHeight(); ++y)
  213. {
  214. for(UINT32 x = 0; x < pixelData.getWidth(); ++x)
  215. {
  216. pixel = pixelData.getColorAt(x, pixelData.getHeight() - y - 1);
  217. *dst = pixel.getAsBGRA();
  218. dst++;
  219. }
  220. }
  221. SelectObject(hBitmapDC, hOldBitmap);
  222. DeleteDC(hBitmapDC);
  223. ICONINFO iconinfo = {0};
  224. iconinfo.fIcon = FALSE;
  225. iconinfo.xHotspot = (DWORD)hotSpot.x;
  226. iconinfo.yHotspot = (DWORD)hotSpot.y;
  227. iconinfo.hbmMask = hMonoBitmap;
  228. iconinfo.hbmColor = hBitmap;
  229. mCursor.data->cursor = CreateIconIndirect(&iconinfo);
  230. DeleteObject(hBitmap);
  231. DeleteObject(hMonoBitmap);
  232. // Make sure we notify the message loop to perform the actual cursor update
  233. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  234. HWND hwnd;
  235. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  236. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  237. }
  238. void Platform::setCaptionNonClientAreas(const RenderWindow& window, const Vector<Rect>::type& nonClientAreas)
  239. {
  240. CM_LOCK_MUTEX(mSync);
  241. mNonClientAreas[&window].moveAreas = nonClientAreas;
  242. }
  243. void Platform::setResizeNonClientAreas(const RenderWindow& window, const Vector<NonClientResizeArea>::type& nonClientAreas)
  244. {
  245. CM_LOCK_MUTEX(mSync);
  246. mNonClientAreas[&window].resizeAreas = nonClientAreas;
  247. }
  248. void Platform::resetNonClientAreas(const RenderWindow& window)
  249. {
  250. CM_LOCK_MUTEX(mSync);
  251. auto iterFind = mNonClientAreas.find(&window);
  252. if(iterFind != end(mNonClientAreas))
  253. mNonClientAreas.erase(iterFind);
  254. }
  255. void Platform::copyToClipboard(const WString& string)
  256. {
  257. HANDLE hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (string.size() + 1) * sizeof(WString::value_type));
  258. WString::value_type* buffer = (WString::value_type*)GlobalLock(hData);
  259. string.copy(buffer, string.size());
  260. buffer[string.size()] = '\0';
  261. GlobalUnlock(hData);
  262. if(OpenClipboard(NULL))
  263. {
  264. EmptyClipboard();
  265. SetClipboardData(CF_UNICODETEXT, hData);
  266. CloseClipboard();
  267. }
  268. else
  269. {
  270. GlobalFree(hData);
  271. }
  272. }
  273. WString Platform::copyFromClipboard()
  274. {
  275. if(OpenClipboard(NULL))
  276. {
  277. HANDLE hData = GetClipboardData(CF_UNICODETEXT);
  278. if(hData != NULL)
  279. {
  280. WString::value_type* buffer = (WString::value_type*)GlobalLock(hData);
  281. WString string(buffer);
  282. GlobalUnlock(hData);
  283. CloseClipboard();
  284. return string;
  285. }
  286. else
  287. {
  288. CloseClipboard();
  289. return L"";
  290. }
  291. }
  292. return L"";
  293. }
  294. double Platform::queryPerformanceTimerMs()
  295. {
  296. LARGE_INTEGER counterValue;
  297. QueryPerformanceCounter(&counterValue);
  298. LARGE_INTEGER counterFreq;
  299. QueryPerformanceFrequency(&counterFreq);
  300. return (double)counterValue.QuadPart / (counterFreq.QuadPart * 0.001);
  301. }
  302. OSDropTarget& Platform::createDropTarget(const RenderWindow* window, int x, int y, unsigned int width, unsigned int height)
  303. {
  304. Win32DropTarget* win32DropTarget = nullptr;
  305. auto iterFind = mDropTargets.data->dropTargetsPerWindow.find(window);
  306. if(iterFind == mDropTargets.data->dropTargetsPerWindow.end())
  307. {
  308. HWND hwnd;
  309. window->getCustomAttribute("WINDOW", &hwnd);
  310. win32DropTarget = cm_new<Win32DropTarget>(hwnd);
  311. mDropTargets.data->dropTargetsPerWindow[window] = win32DropTarget;
  312. {
  313. CM_LOCK_MUTEX(mSync);
  314. mDropTargets.data->dropTargetsToInitialize.push_back(win32DropTarget);
  315. }
  316. }
  317. else
  318. win32DropTarget = iterFind->second;
  319. OSDropTarget* newDropTarget = new (cm_alloc<OSDropTarget>()) OSDropTarget(window, x, y, width, height);
  320. win32DropTarget->registerDropTarget(newDropTarget);
  321. return *newDropTarget;
  322. }
  323. void Platform::destroyDropTarget(OSDropTarget& target)
  324. {
  325. auto iterFind = mDropTargets.data->dropTargetsPerWindow.find(target.getOwnerWindow());
  326. if(iterFind == mDropTargets.data->dropTargetsPerWindow.end())
  327. {
  328. LOGWRN("Attempting to destroy a drop target but cannot find its parent window.");
  329. }
  330. else
  331. {
  332. Win32DropTarget* win32DropTarget = iterFind->second;
  333. win32DropTarget->unregisterDropTarget(&target);
  334. if(win32DropTarget->getNumDropTargets() == 0)
  335. {
  336. mDropTargets.data->dropTargetsPerWindow.erase(iterFind);
  337. {
  338. CM_LOCK_MUTEX(mSync);
  339. mDropTargets.data->dropTargetsToDestroy.push_back(win32DropTarget);
  340. }
  341. }
  342. }
  343. CM_PVT_DELETE(OSDropTarget, &target);
  344. }
  345. void Platform::messagePump()
  346. {
  347. MSG msg;
  348. while(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  349. {
  350. TranslateMessage(&msg);
  351. DispatchMessage(&msg);
  352. }
  353. }
  354. void Platform::startUp()
  355. {
  356. CM_LOCK_MUTEX(mSync);
  357. mRequiresStartUp = true;
  358. }
  359. void Platform::update()
  360. {
  361. Vector<RenderWindow*>::type windowsCopy;
  362. {
  363. CM_LOCK_MUTEX(mSync);
  364. windowsCopy = mMouseLeftWindows;
  365. mMouseLeftWindows.clear();
  366. }
  367. for(auto& window : windowsCopy)
  368. {
  369. if(!onMouseLeftWindow.empty())
  370. onMouseLeftWindow(window);
  371. }
  372. for(auto& dropTarget : mDropTargets.data->dropTargetsPerWindow)
  373. {
  374. dropTarget.second->update();
  375. }
  376. }
  377. void Platform::coreUpdate()
  378. {
  379. {
  380. CM_LOCK_MUTEX(mSync);
  381. if(mRequiresStartUp)
  382. {
  383. OleInitialize(nullptr);
  384. mRequiresStartUp = false;
  385. }
  386. }
  387. {
  388. CM_LOCK_MUTEX(mSync);
  389. for(auto& dropTargetToInit : mDropTargets.data->dropTargetsToInitialize)
  390. {
  391. dropTargetToInit->registerWithOS();
  392. }
  393. mDropTargets.data->dropTargetsToInitialize.clear();
  394. }
  395. {
  396. CM_LOCK_MUTEX(mSync);
  397. for(auto& dropTargetToDestroy : mDropTargets.data->dropTargetsToDestroy)
  398. {
  399. dropTargetToDestroy->unregisterWithOS();
  400. dropTargetToDestroy->Release();
  401. }
  402. mDropTargets.data->dropTargetsToDestroy.clear();
  403. }
  404. messagePump();
  405. {
  406. CM_LOCK_MUTEX(mSync);
  407. if(mRequiresShutDown)
  408. {
  409. OleUninitialize();
  410. mRequiresShutDown = false;
  411. }
  412. }
  413. }
  414. void Platform::shutDown()
  415. {
  416. CM_LOCK_MUTEX(mSync);
  417. mRequiresShutDown = true;
  418. }
  419. void Platform::windowFocusReceived(RenderWindow* window)
  420. {
  421. if(!onWindowFocusReceived.empty())
  422. onWindowFocusReceived(window);
  423. }
  424. void Platform::windowFocusLost(RenderWindow* window)
  425. {
  426. if(!onWindowFocusLost.empty())
  427. onWindowFocusLost(window);
  428. }
  429. void Platform::windowMovedOrResized(RenderWindow* window)
  430. {
  431. if(!onWindowMovedOrResized.empty())
  432. onWindowMovedOrResized(window);
  433. }
  434. void Platform::win32ShowCursor()
  435. {
  436. SetCursor(mCursor.data->cursor);
  437. }
  438. void Platform::win32HideCursor()
  439. {
  440. SetCursor(nullptr);
  441. }
  442. }