BsPlatformWndProc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. HWND curHwnd;
  23. curModalWindow->getCustomAttribute("WINDOW", &curHwnd);
  24. EnableWindow(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. HWND curHwnd;
  34. renderWindow->getCustomAttribute("WINDOW", &curHwnd);
  35. EnableWindow(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. HWND curHwnd;
  82. curModalWindow->getCustomAttribute("WINDOW", &curHwnd);
  83. EnableWindow(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. windowMovedOrResized(win);
  119. break;
  120. case WM_DISPLAYCHANGE:
  121. windowMovedOrResized(win);
  122. break;
  123. case WM_SIZE:
  124. windowMovedOrResized(win);
  125. break;
  126. case WM_SETCURSOR:
  127. if(isCursorHidden())
  128. win32HideCursor();
  129. else
  130. {
  131. switch (LOWORD(lParam))
  132. {
  133. case HTTOPLEFT:
  134. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  135. return 0;
  136. case HTTOP:
  137. SetCursor(LoadCursor(0, IDC_SIZENS));
  138. return 0;
  139. case HTTOPRIGHT:
  140. SetCursor(LoadCursor(0, IDC_SIZENESW));
  141. return 0;
  142. case HTLEFT:
  143. SetCursor(LoadCursor(0, IDC_SIZEWE));
  144. return 0;
  145. case HTRIGHT:
  146. SetCursor(LoadCursor(0, IDC_SIZEWE));
  147. return 0;
  148. case HTBOTTOMLEFT:
  149. SetCursor(LoadCursor(0, IDC_SIZENESW));
  150. return 0;
  151. case HTBOTTOM:
  152. SetCursor(LoadCursor(0, IDC_SIZENS));
  153. return 0;
  154. case HTBOTTOMRIGHT:
  155. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  156. return 0;
  157. }
  158. win32ShowCursor();
  159. }
  160. return true;
  161. case WM_GETMINMAXINFO:
  162. // Prevent the window from going smaller than some minimu size
  163. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
  164. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
  165. break;
  166. case WM_CLOSE:
  167. {
  168. // TODO - Only stop main loop if primary window is closed!!
  169. gCoreApplication().stopMainLoop();
  170. return 0;
  171. }
  172. case WM_NCHITTEST:
  173. {
  174. auto iterFind = mNonClientAreas.find(win);
  175. if(iterFind == mNonClientAreas.end())
  176. break;
  177. POINT mousePos;
  178. mousePos.x = GET_X_LPARAM(lParam);
  179. mousePos.y = GET_Y_LPARAM(lParam);
  180. ScreenToClient(hWnd, &mousePos);
  181. Vector2I mousePosInt;
  182. mousePosInt.x = mousePos.x;
  183. mousePosInt.y = mousePos.y;
  184. Vector<NonClientResizeArea>& resizeAreasPerWindow = iterFind->second.resizeAreas;
  185. for(auto area : resizeAreasPerWindow)
  186. {
  187. if(area.area.contains(mousePosInt))
  188. return translateNonClientAreaType(area.type);
  189. }
  190. Vector<Rect2I>& moveAreasPerWindow = iterFind->second.moveAreas;
  191. for(auto area : moveAreasPerWindow)
  192. {
  193. if(area.contains(mousePosInt))
  194. return HTCAPTION;
  195. }
  196. return HTCLIENT;
  197. }
  198. case WM_MOUSELEAVE:
  199. {
  200. // Note: Right now I track only mouse leaving client area. So it's possible for the "mouse left window" callback
  201. // to trigger, while the mouse is still in the non-client area of the window.
  202. mIsTrackingMouse = false; // TrackMouseEvent ends when this message is received and needs to be re-applied
  203. BS_LOCK_MUTEX(mSync);
  204. if (!onMouseLeftWindow.empty())
  205. onMouseLeftWindow(win);
  206. }
  207. break;
  208. case WM_LBUTTONUP:
  209. {
  210. ReleaseCapture();
  211. Vector2I intMousePos;
  212. OSPointerButtonStates btnStates;
  213. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  214. if(!onCursorButtonReleased.empty())
  215. onCursorButtonReleased(intMousePos, OSMouseButton::Left, btnStates);
  216. }
  217. break;
  218. case WM_MBUTTONUP:
  219. {
  220. ReleaseCapture();
  221. Vector2I intMousePos;
  222. OSPointerButtonStates btnStates;
  223. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  224. if(!onCursorButtonReleased.empty())
  225. onCursorButtonReleased(intMousePos, OSMouseButton::Middle, btnStates);
  226. }
  227. break;
  228. case WM_RBUTTONUP:
  229. {
  230. ReleaseCapture();
  231. Vector2I intMousePos;
  232. OSPointerButtonStates btnStates;
  233. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  234. if(!onCursorButtonReleased.empty())
  235. onCursorButtonReleased(intMousePos, OSMouseButton::Right, btnStates);
  236. }
  237. break;
  238. case WM_LBUTTONDOWN:
  239. {
  240. SetCapture(hWnd);
  241. Vector2I intMousePos;
  242. OSPointerButtonStates btnStates;
  243. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  244. if(!onCursorButtonPressed.empty())
  245. onCursorButtonPressed(intMousePos, OSMouseButton::Left, btnStates);
  246. }
  247. break;
  248. case WM_MBUTTONDOWN:
  249. {
  250. SetCapture(hWnd);
  251. Vector2I intMousePos;
  252. OSPointerButtonStates btnStates;
  253. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  254. if(!onCursorButtonPressed.empty())
  255. onCursorButtonPressed(intMousePos, OSMouseButton::Middle, btnStates);
  256. }
  257. break;
  258. case WM_RBUTTONDOWN:
  259. {
  260. SetCapture(hWnd);
  261. Vector2I intMousePos;
  262. OSPointerButtonStates btnStates;
  263. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  264. if(!onCursorButtonPressed.empty())
  265. onCursorButtonPressed(intMousePos, OSMouseButton::Right, btnStates);
  266. }
  267. break;
  268. case WM_LBUTTONDBLCLK:
  269. {
  270. Vector2I intMousePos;
  271. OSPointerButtonStates btnStates;
  272. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  273. if(!onCursorDoubleClick.empty())
  274. onCursorDoubleClick(intMousePos, btnStates);
  275. }
  276. break;
  277. case WM_NCMOUSEMOVE:
  278. case WM_MOUSEMOVE:
  279. {
  280. // Set up tracking so we get notified when mouse leaves the window
  281. if(!mIsTrackingMouse)
  282. {
  283. TRACKMOUSEEVENT tme = { sizeof(tme) };
  284. tme.dwFlags = TME_LEAVE;
  285. tme.hwndTrack = hWnd;
  286. TrackMouseEvent(&tme);
  287. mIsTrackingMouse = true;
  288. }
  289. if(uMsg == WM_NCMOUSEMOVE)
  290. return true;
  291. Vector2I intMousePos;
  292. OSPointerButtonStates btnStates;
  293. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  294. if(!onCursorMoved.empty())
  295. onCursorMoved(intMousePos, btnStates);
  296. return true;
  297. }
  298. case WM_MOUSEWHEEL:
  299. {
  300. INT16 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  301. float wheelDeltaFlt = wheelDelta / (float)WHEEL_DELTA;
  302. if(!onMouseWheelScrolled.empty())
  303. onMouseWheelScrolled(wheelDeltaFlt);
  304. return true;
  305. }
  306. case WM_SYSKEYDOWN:
  307. case WM_KEYDOWN:
  308. {
  309. if(wParam == VK_SHIFT)
  310. {
  311. isShiftPressed = true;
  312. break;
  313. }
  314. if(wParam == VK_CONTROL)
  315. {
  316. isCtrlPressed = true;
  317. break;
  318. }
  319. InputCommandType command = InputCommandType::Backspace;
  320. if(getCommand((unsigned int)wParam, command))
  321. {
  322. if(!onInputCommand.empty())
  323. onInputCommand(command);
  324. return 0;
  325. }
  326. break;
  327. }
  328. case WM_SYSKEYUP:
  329. case WM_KEYUP:
  330. {
  331. if(wParam == VK_SHIFT)
  332. {
  333. isShiftPressed = false;
  334. }
  335. if(wParam == VK_CONTROL)
  336. {
  337. isCtrlPressed = false;
  338. }
  339. break;
  340. }
  341. case WM_CHAR:
  342. {
  343. // TODO - Not handling IME input
  344. switch (wParam)
  345. {
  346. case VK_BACK:
  347. case 0x0A: // linefeed
  348. case 0x0D: // carriage return
  349. case VK_ESCAPE:
  350. case VK_TAB:
  351. break;
  352. default: // displayable character
  353. {
  354. UINT8 scanCode = (lParam >> 16) & 0xFF;
  355. BYTE keyState[256];
  356. HKL layout = GetKeyboardLayout(0);
  357. if(GetKeyboardState(keyState) == 0)
  358. return 0;
  359. unsigned int vk = MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, layout);
  360. if(vk == 0)
  361. return 0;
  362. InputCommandType command = InputCommandType::Backspace;
  363. if(getCommand(vk, command)) // We ignore character combinations that are special commands
  364. return 0;
  365. UINT32 finalChar = (UINT32)wParam;
  366. if(!onCharInput.empty())
  367. onCharInput(finalChar);
  368. return 0;
  369. }
  370. }
  371. break;
  372. }
  373. case WM_BS_SETCAPTURE:
  374. SetCapture(hWnd);
  375. break;
  376. case WM_BS_RELEASECAPTURE:
  377. ReleaseCapture();
  378. break;
  379. case WM_CAPTURECHANGED:
  380. if(!onMouseCaptureChanged.empty())
  381. onMouseCaptureChanged();
  382. break;
  383. }
  384. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  385. }
  386. LRESULT PlatformWndProc::translateNonClientAreaType(NonClientAreaBorderType type)
  387. {
  388. LRESULT dir = HTCLIENT;
  389. switch(type)
  390. {
  391. case NonClientAreaBorderType::Left:
  392. dir = HTLEFT;
  393. break;
  394. case NonClientAreaBorderType::TopLeft:
  395. dir = HTTOPLEFT;
  396. break;
  397. case NonClientAreaBorderType::Top:
  398. dir = HTTOP;
  399. break;
  400. case NonClientAreaBorderType::TopRight:
  401. dir = HTTOPRIGHT;
  402. break;
  403. case NonClientAreaBorderType::Right:
  404. dir = HTRIGHT;
  405. break;
  406. case NonClientAreaBorderType::BottomRight:
  407. dir = HTBOTTOMRIGHT;
  408. break;
  409. case NonClientAreaBorderType::Bottom:
  410. dir = HTBOTTOM;
  411. break;
  412. case NonClientAreaBorderType::BottomLeft:
  413. dir = HTBOTTOMLEFT;
  414. break;
  415. }
  416. return dir;
  417. }
  418. void PlatformWndProc::getMouseData(HWND hWnd, WPARAM wParam, LPARAM lParam, Vector2I& mousePos, OSPointerButtonStates& btnStates)
  419. {
  420. POINT clientPoint;
  421. clientPoint.x = GET_X_LPARAM(lParam);
  422. clientPoint.y = GET_Y_LPARAM(lParam);
  423. ClientToScreen(hWnd, &clientPoint);
  424. mousePos.x = clientPoint.x;
  425. mousePos.y = clientPoint.y;
  426. btnStates.mouseButtons[0] = (wParam & MK_LBUTTON) != 0;
  427. btnStates.mouseButtons[1] = (wParam & MK_MBUTTON) != 0;
  428. btnStates.mouseButtons[2] = (wParam & MK_RBUTTON) != 0;
  429. btnStates.shift = (wParam & MK_SHIFT) != 0;
  430. btnStates.ctrl = (wParam & MK_CONTROL) != 0;
  431. }
  432. bool PlatformWndProc::getCommand(unsigned int virtualKeyCode, InputCommandType& command)
  433. {
  434. switch (virtualKeyCode)
  435. {
  436. case VK_LEFT:
  437. command = isShiftPressed ? InputCommandType::SelectLeft : InputCommandType::CursorMoveLeft;
  438. return true;
  439. case VK_RIGHT:
  440. command = isShiftPressed ? InputCommandType::SelectRight : InputCommandType::CursorMoveRight;
  441. return true;
  442. case VK_UP:
  443. command = isShiftPressed ? InputCommandType::SelectUp : InputCommandType::CursorMoveUp;
  444. return true;
  445. case VK_DOWN:
  446. command = isShiftPressed ? InputCommandType::SelectDown : InputCommandType::CursorMoveDown;
  447. return true;
  448. case VK_ESCAPE:
  449. command = InputCommandType::Escape;
  450. return true;
  451. case VK_RETURN:
  452. command = InputCommandType::Return;
  453. return true;
  454. case VK_BACK:
  455. command = InputCommandType::Backspace;
  456. return true;
  457. case VK_DELETE:
  458. command = InputCommandType::Delete;
  459. return true;
  460. }
  461. return false;
  462. }
  463. }