2
0

CmCursorImpl.cpp 6.7 KB

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