CmCursorImpl.cpp 5.8 KB

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