BsPlatformWndProc.cpp 13 KB

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