CmCursorImpl.cpp 8.2 KB

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