CmPlatformWndProc.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #include "CmPlatformWndProc.h"
  2. #include "CmRenderWindow.h"
  3. #include "CmApplication.h"
  4. #include "CmInput.h"
  5. #include "CmDebug.h"
  6. namespace CamelotFramework
  7. {
  8. bool PlatformWndProc::isShiftPressed = false;
  9. bool PlatformWndProc::isCtrlPressed = false;
  10. LRESULT CALLBACK PlatformWndProc::_win32WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  11. {
  12. if (uMsg == WM_CREATE)
  13. { // Store pointer to Win32Window in user data area
  14. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)(((LPCREATESTRUCT)lParam)->lpCreateParams));
  15. return 0;
  16. }
  17. // look up window instance
  18. // note: it is possible to get a WM_SIZE before WM_CREATE
  19. RenderWindow* win = (RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  20. if (!win)
  21. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  22. switch( uMsg )
  23. {
  24. case WM_ACTIVATE:
  25. {
  26. bool active = (LOWORD(wParam) != WA_INACTIVE);
  27. if( active )
  28. win->setActive(true);
  29. break;
  30. }
  31. case WM_SETFOCUS:
  32. {
  33. if(!win->hasFocus())
  34. windowFocusReceived(win);
  35. break;
  36. }
  37. case WM_KILLFOCUS:
  38. {
  39. if(win->hasFocus())
  40. windowFocusLost(win);
  41. break;
  42. }
  43. case WM_SYSCHAR:
  44. // return zero to bypass defProc and signal we processed the message, unless it's an ALT-space
  45. if (wParam != VK_SPACE)
  46. return 0;
  47. break;
  48. case WM_MOVE:
  49. windowMovedOrResized(win);
  50. break;
  51. case WM_DISPLAYCHANGE:
  52. windowMovedOrResized(win);
  53. break;
  54. case WM_SIZE:
  55. windowMovedOrResized(win);
  56. break;
  57. case WM_SETCURSOR:
  58. if(isCursorHidden())
  59. win32HideCursor();
  60. else
  61. {
  62. switch (LOWORD(lParam))
  63. {
  64. case HTTOPLEFT:
  65. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  66. return 0;
  67. case HTTOP:
  68. SetCursor(LoadCursor(0, IDC_SIZENS));
  69. return 0;
  70. case HTTOPRIGHT:
  71. SetCursor(LoadCursor(0, IDC_SIZENESW));
  72. return 0;
  73. case HTLEFT:
  74. SetCursor(LoadCursor(0, IDC_SIZEWE));
  75. return 0;
  76. case HTRIGHT:
  77. SetCursor(LoadCursor(0, IDC_SIZEWE));
  78. return 0;
  79. case HTBOTTOMLEFT:
  80. SetCursor(LoadCursor(0, IDC_SIZENESW));
  81. return 0;
  82. case HTBOTTOM:
  83. SetCursor(LoadCursor(0, IDC_SIZENS));
  84. return 0;
  85. case HTBOTTOMRIGHT:
  86. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  87. return 0;
  88. }
  89. win32ShowCursor();
  90. }
  91. return true;
  92. case WM_GETMINMAXINFO:
  93. // Prevent the window from going smaller than some minimu size
  94. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
  95. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
  96. break;
  97. case WM_CLOSE:
  98. {
  99. // TODO - Only stop main loop if primary window is closed!!
  100. gApplication().stopMainLoop();
  101. return 0;
  102. }
  103. case WM_NCHITTEST:
  104. {
  105. auto iterFind = mNonClientAreas.find(win);
  106. if(iterFind == mNonClientAreas.end())
  107. break;
  108. POINT mousePos;
  109. mousePos.x = GET_X_LPARAM(lParam);
  110. mousePos.y = GET_Y_LPARAM(lParam);
  111. ScreenToClient(hWnd, &mousePos);
  112. Vector2I mousePosInt;
  113. mousePosInt.x = mousePos.x;
  114. mousePosInt.y = mousePos.y;
  115. Vector<NonClientResizeArea>::type& resizeAreasPerWindow = iterFind->second.resizeAreas;
  116. for(auto area : resizeAreasPerWindow)
  117. {
  118. if(area.area.contains(mousePosInt))
  119. return translateNonClientAreaType(area.type);
  120. }
  121. Vector<RectI>::type& moveAreasPerWindow = iterFind->second.moveAreas;
  122. for(auto area : moveAreasPerWindow)
  123. {
  124. if(area.contains(mousePosInt))
  125. return HTCAPTION;
  126. }
  127. return HTCLIENT;
  128. }
  129. case WM_MOUSELEAVE:
  130. {
  131. // Note: Right now I track only mouse leaving client area. So it's possible for the "mouse left window" callback
  132. // to trigger, while the mouse is still in the non-client area of the window.
  133. mIsTrackingMouse = false; // TrackMouseEvent ends when this message is received and needs to be re-applied
  134. CM_LOCK_MUTEX(mSync);
  135. mMouseLeftWindows.push_back(win);
  136. }
  137. break;
  138. case WM_LBUTTONUP:
  139. {
  140. ReleaseCapture();
  141. Vector2I intMousePos;
  142. OSPositionalInputButtonStates btnStates;
  143. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  144. if(!onCursorButtonReleased.empty())
  145. onCursorButtonReleased(intMousePos, OSMouseButton::Left, btnStates);
  146. }
  147. break;
  148. case WM_MBUTTONUP:
  149. {
  150. ReleaseCapture();
  151. Vector2I intMousePos;
  152. OSPositionalInputButtonStates btnStates;
  153. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  154. if(!onCursorButtonReleased.empty())
  155. onCursorButtonReleased(intMousePos, OSMouseButton::Middle, btnStates);
  156. }
  157. break;
  158. case WM_RBUTTONUP:
  159. {
  160. ReleaseCapture();
  161. Vector2I intMousePos;
  162. OSPositionalInputButtonStates btnStates;
  163. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  164. if(!onCursorButtonReleased.empty())
  165. onCursorButtonReleased(intMousePos, OSMouseButton::Right, btnStates);
  166. }
  167. break;
  168. case WM_LBUTTONDOWN:
  169. {
  170. SetCapture(hWnd);
  171. Vector2I intMousePos;
  172. OSPositionalInputButtonStates btnStates;
  173. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  174. if(!onCursorButtonPressed.empty())
  175. onCursorButtonPressed(intMousePos, OSMouseButton::Left, btnStates);
  176. }
  177. break;
  178. case WM_MBUTTONDOWN:
  179. {
  180. SetCapture(hWnd);
  181. Vector2I intMousePos;
  182. OSPositionalInputButtonStates btnStates;
  183. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  184. if(!onCursorButtonPressed.empty())
  185. onCursorButtonPressed(intMousePos, OSMouseButton::Middle, btnStates);
  186. }
  187. break;
  188. case WM_RBUTTONDOWN:
  189. {
  190. SetCapture(hWnd);
  191. Vector2I intMousePos;
  192. OSPositionalInputButtonStates btnStates;
  193. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  194. if(!onCursorButtonPressed.empty())
  195. onCursorButtonPressed(intMousePos, OSMouseButton::Right, btnStates);
  196. }
  197. break;
  198. case WM_LBUTTONDBLCLK:
  199. {
  200. Vector2I intMousePos;
  201. OSPositionalInputButtonStates btnStates;
  202. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  203. if(!onCursorDoubleClick.empty())
  204. onCursorDoubleClick(intMousePos, btnStates);
  205. }
  206. break;
  207. case WM_NCMOUSEMOVE:
  208. case WM_MOUSEMOVE:
  209. {
  210. // Set up tracking so we get notified when mouse leaves the window
  211. if(!mIsTrackingMouse)
  212. {
  213. TRACKMOUSEEVENT tme = { sizeof(tme) };
  214. tme.dwFlags = TME_LEAVE;
  215. tme.hwndTrack = hWnd;
  216. TrackMouseEvent(&tme);
  217. mIsTrackingMouse = true;
  218. }
  219. if(uMsg == WM_NCMOUSEMOVE)
  220. return true;
  221. Vector2I intMousePos;
  222. OSPositionalInputButtonStates btnStates;
  223. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  224. if(!onCursorMoved.empty())
  225. onCursorMoved(intMousePos, btnStates);
  226. return true;
  227. }
  228. case WM_MOUSEWHEEL:
  229. {
  230. INT16 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  231. float wheelDeltaFlt = wheelDelta / (float)WHEEL_DELTA;
  232. if(!onMouseWheelScrolled.empty())
  233. onMouseWheelScrolled(wheelDeltaFlt);
  234. return true;
  235. }
  236. case WM_SYSKEYDOWN:
  237. case WM_KEYDOWN:
  238. {
  239. if(wParam == VK_SHIFT)
  240. {
  241. isShiftPressed = true;
  242. break;
  243. }
  244. if(wParam == VK_CONTROL)
  245. {
  246. isCtrlPressed = true;
  247. break;
  248. }
  249. InputCommandType command = InputCommandType::Backspace;
  250. if(getCommand((unsigned int)wParam, command))
  251. {
  252. if(!onInputCommand.empty())
  253. onInputCommand(command);
  254. return 0;
  255. }
  256. break;
  257. }
  258. case WM_SYSKEYUP:
  259. case WM_KEYUP:
  260. {
  261. if(wParam == VK_SHIFT)
  262. {
  263. isShiftPressed = false;
  264. }
  265. if(wParam == VK_CONTROL)
  266. {
  267. isCtrlPressed = false;
  268. }
  269. break;
  270. }
  271. case WM_CHAR:
  272. {
  273. // TODO - Not handling IME input
  274. switch (wParam)
  275. {
  276. case VK_BACK:
  277. case 0x0A: // linefeed
  278. case 0x0D: // carriage return
  279. case VK_ESCAPE:
  280. case VK_TAB:
  281. break;
  282. default: // displayable character
  283. {
  284. UINT8 scanCode = (lParam >> 16) & 0xFF;
  285. BYTE keyState[256];
  286. HKL layout = GetKeyboardLayout(0);
  287. if(GetKeyboardState(keyState) == 0)
  288. return 0;
  289. unsigned int vk = MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, layout);
  290. if(vk == 0)
  291. return 0;
  292. InputCommandType command = InputCommandType::Backspace;
  293. if(getCommand(vk, command)) // We ignore character combinations that are special commands
  294. return 0;
  295. UINT32 finalChar = (UINT32)wParam;
  296. if(!onCharInput.empty())
  297. onCharInput(finalChar);
  298. return 0;
  299. }
  300. }
  301. break;
  302. }
  303. case WM_CM_SETCAPTURE:
  304. SetCapture(hWnd);
  305. break;
  306. case WM_CM_RELEASECAPTURE:
  307. ReleaseCapture();
  308. break;
  309. case WM_CAPTURECHANGED:
  310. if(!onMouseCaptureChanged.empty())
  311. onMouseCaptureChanged();
  312. break;
  313. }
  314. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  315. }
  316. LRESULT PlatformWndProc::translateNonClientAreaType(NonClientAreaBorderType type)
  317. {
  318. LRESULT dir = HTCLIENT;
  319. switch(type)
  320. {
  321. case NonClientAreaBorderType::Left:
  322. dir = HTLEFT;
  323. break;
  324. case NonClientAreaBorderType::TopLeft:
  325. dir = HTTOPLEFT;
  326. break;
  327. case NonClientAreaBorderType::Top:
  328. dir = HTTOP;
  329. break;
  330. case NonClientAreaBorderType::TopRight:
  331. dir = HTTOPRIGHT;
  332. break;
  333. case NonClientAreaBorderType::Right:
  334. dir = HTRIGHT;
  335. break;
  336. case NonClientAreaBorderType::BottomRight:
  337. dir = HTBOTTOMRIGHT;
  338. break;
  339. case NonClientAreaBorderType::Bottom:
  340. dir = HTBOTTOM;
  341. break;
  342. case NonClientAreaBorderType::BottomLeft:
  343. dir = HTBOTTOMLEFT;
  344. break;
  345. }
  346. return dir;
  347. }
  348. void PlatformWndProc::getMouseData(HWND hWnd, WPARAM wParam, LPARAM lParam, Vector2I& mousePos, OSPositionalInputButtonStates& btnStates)
  349. {
  350. POINT clientPoint;
  351. clientPoint.x = GET_X_LPARAM(lParam);
  352. clientPoint.y = GET_Y_LPARAM(lParam);
  353. ClientToScreen(hWnd, &clientPoint);
  354. mousePos.x = clientPoint.x;
  355. mousePos.y = clientPoint.y;
  356. btnStates.mouseButtons[0] = (wParam & MK_LBUTTON) != 0;
  357. btnStates.mouseButtons[1] = (wParam & MK_MBUTTON) != 0;
  358. btnStates.mouseButtons[2] = (wParam & MK_RBUTTON) != 0;
  359. btnStates.shift = (wParam & MK_SHIFT) != 0;
  360. btnStates.ctrl = (wParam & MK_CONTROL) != 0;
  361. }
  362. bool PlatformWndProc::getCommand(unsigned int virtualKeyCode, InputCommandType& command)
  363. {
  364. switch (virtualKeyCode)
  365. {
  366. case VK_LEFT:
  367. command = isShiftPressed ? InputCommandType::SelectLeft : InputCommandType::CursorMoveLeft;
  368. return true;
  369. case VK_RIGHT:
  370. command = isShiftPressed ? InputCommandType::SelectRight : InputCommandType::CursorMoveRight;
  371. return true;
  372. case VK_UP:
  373. command = isShiftPressed ? InputCommandType::SelectUp : InputCommandType::CursorMoveUp;
  374. return true;
  375. case VK_DOWN:
  376. command = isShiftPressed ? InputCommandType::SelectDown : InputCommandType::CursorMoveDown;
  377. return true;
  378. case VK_ESCAPE:
  379. command = InputCommandType::Escape;
  380. return true;
  381. case VK_RETURN:
  382. command = InputCommandType::Return;
  383. return true;
  384. case VK_BACK:
  385. command = InputCommandType::Backspace;
  386. return true;
  387. case VK_DELETE:
  388. command = InputCommandType::Delete;
  389. return true;
  390. }
  391. return false;
  392. }
  393. }