CmPlatformWndProc.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. Int2 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<Rect>::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. Int2 intMousePos;
  138. OSPositionalInputButtonStates btnStates;
  139. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  140. if(!onCursorButtonReleased.empty())
  141. onCursorButtonReleased(intMousePos, OSMouseButton::Left, btnStates);
  142. }
  143. break;
  144. case WM_MBUTTONUP:
  145. {
  146. Int2 intMousePos;
  147. OSPositionalInputButtonStates btnStates;
  148. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  149. if(!onCursorButtonReleased.empty())
  150. onCursorButtonReleased(intMousePos, OSMouseButton::Middle, btnStates);
  151. }
  152. break;
  153. case WM_RBUTTONUP:
  154. {
  155. Int2 intMousePos;
  156. OSPositionalInputButtonStates btnStates;
  157. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  158. if(!onCursorButtonReleased.empty())
  159. onCursorButtonReleased(intMousePos, OSMouseButton::Right, btnStates);
  160. }
  161. break;
  162. case WM_LBUTTONDOWN:
  163. {
  164. Int2 intMousePos;
  165. OSPositionalInputButtonStates btnStates;
  166. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  167. if(!onCursorButtonPressed.empty())
  168. onCursorButtonPressed(intMousePos, OSMouseButton::Left, btnStates);
  169. }
  170. break;
  171. case WM_MBUTTONDOWN:
  172. {
  173. Int2 intMousePos;
  174. OSPositionalInputButtonStates btnStates;
  175. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  176. if(!onCursorButtonPressed.empty())
  177. onCursorButtonPressed(intMousePos, OSMouseButton::Middle, btnStates);
  178. }
  179. break;
  180. case WM_RBUTTONDOWN:
  181. {
  182. Int2 intMousePos;
  183. OSPositionalInputButtonStates btnStates;
  184. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  185. if(!onCursorButtonPressed.empty())
  186. onCursorButtonPressed(intMousePos, OSMouseButton::Right, btnStates);
  187. }
  188. break;
  189. case WM_NCMOUSEMOVE:
  190. case WM_MOUSEMOVE:
  191. {
  192. // Set up tracking so we get notified when mouse leaves the window
  193. if(!mIsTrackingMouse)
  194. {
  195. TRACKMOUSEEVENT tme = { sizeof(tme) };
  196. tme.dwFlags = TME_LEAVE;
  197. tme.hwndTrack = hWnd;
  198. TrackMouseEvent(&tme);
  199. mIsTrackingMouse = true;
  200. }
  201. if(uMsg == WM_NCMOUSEMOVE)
  202. return true;
  203. Int2 intMousePos;
  204. OSPositionalInputButtonStates btnStates;
  205. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  206. if(!onCursorMoved.empty())
  207. onCursorMoved(intMousePos, btnStates);
  208. return true;
  209. }
  210. case WM_MOUSEWHEEL:
  211. {
  212. INT16 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  213. float wheelDeltaFlt = wheelDelta / (float)WHEEL_DELTA;
  214. if(!onMouseWheelScrolled.empty())
  215. onMouseWheelScrolled(wheelDeltaFlt);
  216. return true;
  217. }
  218. case WM_SYSKEYDOWN:
  219. case WM_KEYDOWN:
  220. {
  221. if(wParam == VK_SHIFT)
  222. {
  223. isShiftPressed = true;
  224. break;
  225. }
  226. if(wParam == VK_CONTROL)
  227. {
  228. isCtrlPressed = true;
  229. break;
  230. }
  231. InputCommandType command = InputCommandType::Backspace;
  232. if(getCommand((unsigned int)wParam, command))
  233. {
  234. if(!onInputCommand.empty())
  235. onInputCommand(command);
  236. return 0;
  237. }
  238. break;
  239. }
  240. case WM_SYSKEYUP:
  241. case WM_KEYUP:
  242. {
  243. if(wParam == VK_SHIFT)
  244. {
  245. isShiftPressed = false;
  246. }
  247. if(wParam == VK_CONTROL)
  248. {
  249. isCtrlPressed = false;
  250. }
  251. break;
  252. }
  253. case WM_CHAR:
  254. {
  255. // TODO - Not handling IME input
  256. switch (wParam)
  257. {
  258. case VK_BACK:
  259. case 0x0A: // linefeed
  260. case 0x0D: // carriage return
  261. case VK_ESCAPE:
  262. case VK_TAB:
  263. break;
  264. default: // displayable character
  265. {
  266. UINT8 scanCode = (lParam >> 16) & 0xFF;
  267. BYTE keyState[256];
  268. HKL layout = GetKeyboardLayout(0);
  269. if(GetKeyboardState(keyState) == 0)
  270. return 0;
  271. unsigned int vk = MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, layout);
  272. if(vk == 0)
  273. return 0;
  274. InputCommandType command = InputCommandType::Backspace;
  275. if(getCommand(vk, command)) // We ignore character combinations that are special commands
  276. return 0;
  277. UINT32 finalChar = (UINT32)wParam;
  278. if(!onCharInput.empty())
  279. onCharInput(finalChar);
  280. return 0;
  281. }
  282. }
  283. break;
  284. }
  285. case WM_CM_SETCAPTURE:
  286. SetCapture(hWnd);
  287. break;
  288. case WM_CM_RELEASECAPTURE:
  289. ReleaseCapture();
  290. break;
  291. case WM_CAPTURECHANGED:
  292. if(!onMouseCaptureChanged.empty())
  293. onMouseCaptureChanged();
  294. break;
  295. }
  296. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  297. }
  298. LRESULT PlatformWndProc::translateNonClientAreaType(NonClientAreaBorderType type)
  299. {
  300. LRESULT dir = HTCLIENT;
  301. switch(type)
  302. {
  303. case NonClientAreaBorderType::Left:
  304. dir = HTLEFT;
  305. break;
  306. case NonClientAreaBorderType::TopLeft:
  307. dir = HTTOPLEFT;
  308. break;
  309. case NonClientAreaBorderType::Top:
  310. dir = HTTOP;
  311. break;
  312. case NonClientAreaBorderType::TopRight:
  313. dir = HTTOPRIGHT;
  314. break;
  315. case NonClientAreaBorderType::Right:
  316. dir = HTRIGHT;
  317. break;
  318. case NonClientAreaBorderType::BottomRight:
  319. dir = HTBOTTOMRIGHT;
  320. break;
  321. case NonClientAreaBorderType::Bottom:
  322. dir = HTBOTTOM;
  323. break;
  324. case NonClientAreaBorderType::BottomLeft:
  325. dir = HTBOTTOMLEFT;
  326. break;
  327. }
  328. return dir;
  329. }
  330. void PlatformWndProc::getMouseData(HWND hWnd, WPARAM wParam, LPARAM lParam, Int2& mousePos, OSPositionalInputButtonStates& btnStates)
  331. {
  332. POINT clientPoint;
  333. clientPoint.x = GET_X_LPARAM(lParam);
  334. clientPoint.y = GET_Y_LPARAM(lParam);
  335. ClientToScreen(hWnd, &clientPoint);
  336. mousePos.x = clientPoint.x;
  337. mousePos.y = clientPoint.y;
  338. btnStates.mouseButtons[0] = (wParam & MK_LBUTTON) != 0;
  339. btnStates.mouseButtons[1] = (wParam & MK_MBUTTON) != 0;
  340. btnStates.mouseButtons[2] = (wParam & MK_RBUTTON) != 0;
  341. btnStates.shift = (wParam & MK_SHIFT) != 0;
  342. btnStates.ctrl = (wParam & MK_CONTROL) != 0;
  343. }
  344. bool PlatformWndProc::getCommand(unsigned int virtualKeyCode, InputCommandType& command)
  345. {
  346. switch (virtualKeyCode)
  347. {
  348. case VK_LEFT:
  349. command = isShiftPressed ? InputCommandType::SelectLeft : InputCommandType::CursorMoveLeft;
  350. return true;
  351. case VK_RIGHT:
  352. command = isShiftPressed ? InputCommandType::SelectRight : InputCommandType::CursorMoveRight;
  353. return true;
  354. case VK_UP:
  355. command = isShiftPressed ? InputCommandType::SelectUp : InputCommandType::CursorMoveUp;
  356. return true;
  357. case VK_DOWN:
  358. command = isShiftPressed ? InputCommandType::SelectDown : InputCommandType::CursorMoveDown;
  359. return true;
  360. case VK_ESCAPE:
  361. command = InputCommandType::Escape;
  362. return true;
  363. case VK_RETURN:
  364. command = InputCommandType::Return;
  365. return true;
  366. case VK_BACK:
  367. command = InputCommandType::Backspace;
  368. return true;
  369. case VK_DELETE:
  370. command = InputCommandType::Delete;
  371. return true;
  372. case VK_TAB:
  373. command = InputCommandType::Tab;
  374. return true;
  375. case 0x41: // A
  376. if(isCtrlPressed)
  377. {
  378. command = InputCommandType::SelectAll;
  379. return true;
  380. }
  381. case 0x43: // C
  382. if(isCtrlPressed)
  383. {
  384. command = InputCommandType::Copy;
  385. return true;
  386. }
  387. case 0x56: // V
  388. if(isCtrlPressed)
  389. {
  390. command = InputCommandType::Paste;
  391. return true;
  392. }
  393. case 0x58: // X
  394. if(isCtrlPressed)
  395. {
  396. command = InputCommandType::Cut;
  397. return true;
  398. }
  399. case 0x5A: // Z
  400. if(isCtrlPressed)
  401. {
  402. command = InputCommandType::Undo;
  403. return true;
  404. }
  405. case 0x59: // Y
  406. if(isCtrlPressed)
  407. {
  408. command = InputCommandType::Redo;
  409. return true;
  410. }
  411. }
  412. return false;
  413. }
  414. }