CmCursorImpl.cpp 6.0 KB

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