CmPlatformImpl.cpp 7.2 KB

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