CmPlatformWndProc.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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_DEADCHAR:
  254. case WM_CHAR:
  255. {
  256. // TODO - Not handling IME input
  257. switch (wParam)
  258. {
  259. case VK_BACK:
  260. case 0x0A: // linefeed
  261. case 0x0D: // carriage return
  262. case VK_ESCAPE:
  263. case VK_TAB:
  264. break;
  265. default: // displayable character
  266. {
  267. UINT8 scanCode = (lParam >> 16) & 0xFF;
  268. BYTE keyState[256];
  269. HKL layout = GetKeyboardLayout(0);
  270. if(GetKeyboardState(keyState) == 0)
  271. return 0;
  272. unsigned int vk = MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, layout);
  273. if(vk == 0)
  274. return 0;
  275. InputCommandType command = InputCommandType::Backspace;
  276. if(getCommand(vk, command)) // We ignore character combinations that are special commands
  277. return 0;
  278. bool isDeadKey = (MapVirtualKeyEx(vk, MAPVK_VK_TO_CHAR, layout) & (1 << 31)) != 0;
  279. if(isDeadKey)
  280. return 0;
  281. wchar_t buff[3] = {0};
  282. int numChars = ToUnicodeEx(vk, scanCode, keyState, buff, 3, 0, layout);
  283. // TODO - I am ignoring dead keys here - primarily because I haven't found a good way of retrieving non-combined dead key
  284. // value. ToUnicodeEx and MapVirtualKeyEx only return precombined (i.e. spacing) versions, which can't be combined using other characters.
  285. // I need non-combined version so I can use it with FoldString to apply to a certain character.
  286. UINT32 finalChar = 0;
  287. if(numChars == 1)
  288. finalChar = buff[0];
  289. else
  290. return 0;
  291. if(!onCharInput.empty())
  292. onCharInput(finalChar);
  293. return 0;
  294. }
  295. }
  296. break;
  297. }
  298. case WM_CM_SETCAPTURE:
  299. SetCapture(hWnd);
  300. break;
  301. case WM_CM_RELEASECAPTURE:
  302. ReleaseCapture();
  303. break;
  304. case WM_CAPTURECHANGED:
  305. if(!onMouseCaptureChanged.empty())
  306. onMouseCaptureChanged();
  307. break;
  308. }
  309. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  310. }
  311. LRESULT PlatformWndProc::translateNonClientAreaType(NonClientAreaBorderType type)
  312. {
  313. LRESULT dir = HTCLIENT;
  314. switch(type)
  315. {
  316. case NonClientAreaBorderType::Left:
  317. dir = HTLEFT;
  318. break;
  319. case NonClientAreaBorderType::TopLeft:
  320. dir = HTTOPLEFT;
  321. break;
  322. case NonClientAreaBorderType::Top:
  323. dir = HTTOP;
  324. break;
  325. case NonClientAreaBorderType::TopRight:
  326. dir = HTTOPRIGHT;
  327. break;
  328. case NonClientAreaBorderType::Right:
  329. dir = HTRIGHT;
  330. break;
  331. case NonClientAreaBorderType::BottomRight:
  332. dir = HTBOTTOMRIGHT;
  333. break;
  334. case NonClientAreaBorderType::Bottom:
  335. dir = HTBOTTOM;
  336. break;
  337. case NonClientAreaBorderType::BottomLeft:
  338. dir = HTBOTTOMLEFT;
  339. break;
  340. }
  341. return dir;
  342. }
  343. void PlatformWndProc::getMouseData(HWND hWnd, WPARAM wParam, LPARAM lParam, Int2& mousePos, OSPositionalInputButtonStates& btnStates)
  344. {
  345. POINT clientPoint;
  346. clientPoint.x = GET_X_LPARAM(lParam);
  347. clientPoint.y = GET_Y_LPARAM(lParam);
  348. ClientToScreen(hWnd, &clientPoint);
  349. mousePos.x = clientPoint.x;
  350. mousePos.y = clientPoint.y;
  351. btnStates.mouseButtons[0] = (wParam & MK_LBUTTON) != 0;
  352. btnStates.mouseButtons[1] = (wParam & MK_MBUTTON) != 0;
  353. btnStates.mouseButtons[2] = (wParam & MK_RBUTTON) != 0;
  354. btnStates.shift = (wParam & MK_SHIFT) != 0;
  355. btnStates.ctrl = (wParam & MK_CONTROL) != 0;
  356. }
  357. bool PlatformWndProc::getCommand(unsigned int virtualKeyCode, InputCommandType& command)
  358. {
  359. switch (virtualKeyCode)
  360. {
  361. case VK_LEFT:
  362. command = isShiftPressed ? InputCommandType::SelectLeft : InputCommandType::CursorMoveLeft;
  363. return true;
  364. case VK_RIGHT:
  365. command = isShiftPressed ? InputCommandType::SelectRight : InputCommandType::CursorMoveRight;
  366. return true;
  367. case VK_UP:
  368. command = isShiftPressed ? InputCommandType::SelectUp : InputCommandType::CursorMoveUp;
  369. return true;
  370. case VK_DOWN:
  371. command = isShiftPressed ? InputCommandType::SelectDown : InputCommandType::CursorMoveDown;
  372. return true;
  373. case VK_ESCAPE:
  374. command = InputCommandType::Escape;
  375. return true;
  376. case VK_RETURN:
  377. command = InputCommandType::Return;
  378. return true;
  379. case VK_BACK:
  380. command = InputCommandType::Backspace;
  381. return true;
  382. case VK_DELETE:
  383. command = InputCommandType::Delete;
  384. return true;
  385. case VK_TAB:
  386. command = InputCommandType::Tab;
  387. return true;
  388. case 0x41: // A
  389. if(isCtrlPressed)
  390. {
  391. command = InputCommandType::SelectAll;
  392. return true;
  393. }
  394. case 0x43: // C
  395. if(isCtrlPressed)
  396. {
  397. command = InputCommandType::Copy;
  398. return true;
  399. }
  400. case 0x56: // V
  401. if(isCtrlPressed)
  402. {
  403. command = InputCommandType::Paste;
  404. return true;
  405. }
  406. case 0x58: // X
  407. if(isCtrlPressed)
  408. {
  409. command = InputCommandType::Cut;
  410. return true;
  411. }
  412. case 0x5A: // Z
  413. if(isCtrlPressed)
  414. {
  415. command = InputCommandType::Undo;
  416. return true;
  417. }
  418. case 0x59: // Y
  419. if(isCtrlPressed)
  420. {
  421. command = InputCommandType::Redo;
  422. return true;
  423. }
  424. }
  425. return false;
  426. }
  427. }