CmPlatformWndProc.cpp 12 KB

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