BsPlatformWndProc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "Win32/BsPlatformWndProc.h"
  5. #include "BsRenderWindow.h"
  6. #include "BsCoreApplication.h"
  7. #include "BsInput.h"
  8. #include "BsDebug.h"
  9. #include "BsRenderWindowManager.h"
  10. namespace BansheeEngine
  11. {
  12. bool PlatformWndProc::isShiftPressed = false;
  13. bool PlatformWndProc::isCtrlPressed = false;
  14. LRESULT CALLBACK PlatformWndProc::_win32WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  15. {
  16. if (uMsg == WM_CREATE)
  17. { // Store pointer to Win32Window in user data area
  18. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)(((LPCREATESTRUCT)lParam)->lpCreateParams));
  19. RenderWindow* newWindow = (RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  20. if(newWindow->isModal())
  21. {
  22. if(!mModalWindowStack.empty())
  23. {
  24. RenderWindow* curModalWindow = mModalWindowStack.top();
  25. HWND curHwnd;
  26. curModalWindow->getCustomAttribute("WINDOW", &curHwnd);
  27. EnableWindow(curHwnd, FALSE);
  28. }
  29. else
  30. {
  31. Vector<RenderWindow*> renderWindows = RenderWindowManager::instance().getRenderWindows();
  32. for(auto& renderWindow : renderWindows)
  33. {
  34. if(renderWindow == newWindow)
  35. continue;
  36. HWND curHwnd;
  37. renderWindow->getCustomAttribute("WINDOW", &curHwnd);
  38. EnableWindow(curHwnd, FALSE);
  39. }
  40. }
  41. mModalWindowStack.push(newWindow);
  42. }
  43. return 0;
  44. }
  45. // look up window instance
  46. // note: it is possible to get a WM_SIZE before WM_CREATE
  47. RenderWindow* win = (RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  48. if (!win)
  49. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  50. switch( uMsg )
  51. {
  52. case WM_ACTIVATE:
  53. {
  54. bool active = (LOWORD(wParam) != WA_INACTIVE);
  55. if( active )
  56. win->setActive(true);
  57. break;
  58. }
  59. case WM_DESTROY:
  60. {
  61. bool reenableWindows = false;
  62. if(!mModalWindowStack.empty())
  63. {
  64. if(mModalWindowStack.top() == win) // This is the most common case, top-most modal was closed
  65. {
  66. mModalWindowStack.pop();
  67. }
  68. else // Possibly some other window was closed somehow, see if it was modal and remove from stack if it is
  69. {
  70. Stack<RenderWindow*> newStack;
  71. while(!mModalWindowStack.empty())
  72. {
  73. RenderWindow* curWindow = mModalWindowStack.top();
  74. mModalWindowStack.pop();
  75. if(curWindow == win)
  76. continue;
  77. newStack.push(curWindow);
  78. }
  79. mModalWindowStack = newStack;
  80. }
  81. if(!mModalWindowStack.empty()) // Enable next modal window
  82. {
  83. RenderWindow* curModalWindow = mModalWindowStack.top();
  84. HWND curHwnd;
  85. curModalWindow->getCustomAttribute("WINDOW", &curHwnd);
  86. EnableWindow(curHwnd, TRUE);
  87. }
  88. else
  89. reenableWindows = true; // No more modal windows, re-enable any remaining window
  90. }
  91. if(reenableWindows)
  92. {
  93. Vector<RenderWindow*> renderWindows = RenderWindowManager::instance().getRenderWindows();
  94. for(auto& renderWindow : renderWindows)
  95. {
  96. HWND curHwnd;
  97. renderWindow->getCustomAttribute("WINDOW", &curHwnd);
  98. EnableWindow(curHwnd, TRUE);
  99. }
  100. }
  101. break;
  102. }
  103. case WM_SETFOCUS:
  104. {
  105. if(!win->hasFocus())
  106. windowFocusReceived(win);
  107. break;
  108. }
  109. case WM_KILLFOCUS:
  110. {
  111. if(win->hasFocus())
  112. windowFocusLost(win);
  113. break;
  114. }
  115. case WM_SYSCHAR:
  116. // return zero to bypass defProc and signal we processed the message, unless it's an ALT-space
  117. if (wParam != VK_SPACE)
  118. return 0;
  119. break;
  120. case WM_MOVE:
  121. windowMovedOrResized(win);
  122. break;
  123. case WM_DISPLAYCHANGE:
  124. windowMovedOrResized(win);
  125. break;
  126. case WM_SIZE:
  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<RectI>& 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. mMouseLeftWindows.push_back(win);
  208. }
  209. break;
  210. case WM_LBUTTONUP:
  211. {
  212. ReleaseCapture();
  213. Vector2I intMousePos;
  214. OSPointerButtonStates btnStates;
  215. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  216. if(!onCursorButtonReleased.empty())
  217. onCursorButtonReleased(intMousePos, OSMouseButton::Left, btnStates);
  218. }
  219. break;
  220. case WM_MBUTTONUP:
  221. {
  222. ReleaseCapture();
  223. Vector2I intMousePos;
  224. OSPointerButtonStates btnStates;
  225. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  226. if(!onCursorButtonReleased.empty())
  227. onCursorButtonReleased(intMousePos, OSMouseButton::Middle, btnStates);
  228. }
  229. break;
  230. case WM_RBUTTONUP:
  231. {
  232. ReleaseCapture();
  233. Vector2I intMousePos;
  234. OSPointerButtonStates btnStates;
  235. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  236. if(!onCursorButtonReleased.empty())
  237. onCursorButtonReleased(intMousePos, OSMouseButton::Right, btnStates);
  238. }
  239. break;
  240. case WM_LBUTTONDOWN:
  241. {
  242. SetCapture(hWnd);
  243. Vector2I intMousePos;
  244. OSPointerButtonStates btnStates;
  245. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  246. if(!onCursorButtonPressed.empty())
  247. onCursorButtonPressed(intMousePos, OSMouseButton::Left, btnStates);
  248. }
  249. break;
  250. case WM_MBUTTONDOWN:
  251. {
  252. SetCapture(hWnd);
  253. Vector2I intMousePos;
  254. OSPointerButtonStates btnStates;
  255. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  256. if(!onCursorButtonPressed.empty())
  257. onCursorButtonPressed(intMousePos, OSMouseButton::Middle, btnStates);
  258. }
  259. break;
  260. case WM_RBUTTONDOWN:
  261. {
  262. SetCapture(hWnd);
  263. Vector2I intMousePos;
  264. OSPointerButtonStates btnStates;
  265. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  266. if(!onCursorButtonPressed.empty())
  267. onCursorButtonPressed(intMousePos, OSMouseButton::Right, btnStates);
  268. }
  269. break;
  270. case WM_LBUTTONDBLCLK:
  271. {
  272. Vector2I intMousePos;
  273. OSPointerButtonStates btnStates;
  274. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  275. if(!onCursorDoubleClick.empty())
  276. onCursorDoubleClick(intMousePos, btnStates);
  277. }
  278. break;
  279. case WM_NCMOUSEMOVE:
  280. case WM_MOUSEMOVE:
  281. {
  282. // Set up tracking so we get notified when mouse leaves the window
  283. if(!mIsTrackingMouse)
  284. {
  285. TRACKMOUSEEVENT tme = { sizeof(tme) };
  286. tme.dwFlags = TME_LEAVE;
  287. tme.hwndTrack = hWnd;
  288. TrackMouseEvent(&tme);
  289. mIsTrackingMouse = true;
  290. }
  291. if(uMsg == WM_NCMOUSEMOVE)
  292. return true;
  293. Vector2I intMousePos;
  294. OSPointerButtonStates btnStates;
  295. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  296. if(!onCursorMoved.empty())
  297. onCursorMoved(intMousePos, btnStates);
  298. return true;
  299. }
  300. case WM_MOUSEWHEEL:
  301. {
  302. INT16 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  303. float wheelDeltaFlt = wheelDelta / (float)WHEEL_DELTA;
  304. if(!onMouseWheelScrolled.empty())
  305. onMouseWheelScrolled(wheelDeltaFlt);
  306. return true;
  307. }
  308. case WM_SYSKEYDOWN:
  309. case WM_KEYDOWN:
  310. {
  311. if(wParam == VK_SHIFT)
  312. {
  313. isShiftPressed = true;
  314. break;
  315. }
  316. if(wParam == VK_CONTROL)
  317. {
  318. isCtrlPressed = true;
  319. break;
  320. }
  321. InputCommandType command = InputCommandType::Backspace;
  322. if(getCommand((unsigned int)wParam, command))
  323. {
  324. if(!onInputCommand.empty())
  325. onInputCommand(command);
  326. return 0;
  327. }
  328. break;
  329. }
  330. case WM_SYSKEYUP:
  331. case WM_KEYUP:
  332. {
  333. if(wParam == VK_SHIFT)
  334. {
  335. isShiftPressed = false;
  336. }
  337. if(wParam == VK_CONTROL)
  338. {
  339. isCtrlPressed = false;
  340. }
  341. break;
  342. }
  343. case WM_CHAR:
  344. {
  345. // TODO - Not handling IME input
  346. switch (wParam)
  347. {
  348. case VK_BACK:
  349. case 0x0A: // linefeed
  350. case 0x0D: // carriage return
  351. case VK_ESCAPE:
  352. case VK_TAB:
  353. break;
  354. default: // displayable character
  355. {
  356. UINT8 scanCode = (lParam >> 16) & 0xFF;
  357. BYTE keyState[256];
  358. HKL layout = GetKeyboardLayout(0);
  359. if(GetKeyboardState(keyState) == 0)
  360. return 0;
  361. unsigned int vk = MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, layout);
  362. if(vk == 0)
  363. return 0;
  364. InputCommandType command = InputCommandType::Backspace;
  365. if(getCommand(vk, command)) // We ignore character combinations that are special commands
  366. return 0;
  367. UINT32 finalChar = (UINT32)wParam;
  368. if(!onCharInput.empty())
  369. onCharInput(finalChar);
  370. return 0;
  371. }
  372. }
  373. break;
  374. }
  375. case WM_BS_SETCAPTURE:
  376. SetCapture(hWnd);
  377. break;
  378. case WM_BS_RELEASECAPTURE:
  379. ReleaseCapture();
  380. break;
  381. case WM_CAPTURECHANGED:
  382. if(!onMouseCaptureChanged.empty())
  383. onMouseCaptureChanged();
  384. break;
  385. }
  386. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  387. }
  388. LRESULT PlatformWndProc::translateNonClientAreaType(NonClientAreaBorderType type)
  389. {
  390. LRESULT dir = HTCLIENT;
  391. switch(type)
  392. {
  393. case NonClientAreaBorderType::Left:
  394. dir = HTLEFT;
  395. break;
  396. case NonClientAreaBorderType::TopLeft:
  397. dir = HTTOPLEFT;
  398. break;
  399. case NonClientAreaBorderType::Top:
  400. dir = HTTOP;
  401. break;
  402. case NonClientAreaBorderType::TopRight:
  403. dir = HTTOPRIGHT;
  404. break;
  405. case NonClientAreaBorderType::Right:
  406. dir = HTRIGHT;
  407. break;
  408. case NonClientAreaBorderType::BottomRight:
  409. dir = HTBOTTOMRIGHT;
  410. break;
  411. case NonClientAreaBorderType::Bottom:
  412. dir = HTBOTTOM;
  413. break;
  414. case NonClientAreaBorderType::BottomLeft:
  415. dir = HTBOTTOMLEFT;
  416. break;
  417. }
  418. return dir;
  419. }
  420. void PlatformWndProc::getMouseData(HWND hWnd, WPARAM wParam, LPARAM lParam, Vector2I& mousePos, OSPointerButtonStates& btnStates)
  421. {
  422. POINT clientPoint;
  423. clientPoint.x = GET_X_LPARAM(lParam);
  424. clientPoint.y = GET_Y_LPARAM(lParam);
  425. ClientToScreen(hWnd, &clientPoint);
  426. mousePos.x = clientPoint.x;
  427. mousePos.y = clientPoint.y;
  428. btnStates.mouseButtons[0] = (wParam & MK_LBUTTON) != 0;
  429. btnStates.mouseButtons[1] = (wParam & MK_MBUTTON) != 0;
  430. btnStates.mouseButtons[2] = (wParam & MK_RBUTTON) != 0;
  431. btnStates.shift = (wParam & MK_SHIFT) != 0;
  432. btnStates.ctrl = (wParam & MK_CONTROL) != 0;
  433. }
  434. bool PlatformWndProc::getCommand(unsigned int virtualKeyCode, InputCommandType& command)
  435. {
  436. switch (virtualKeyCode)
  437. {
  438. case VK_LEFT:
  439. command = isShiftPressed ? InputCommandType::SelectLeft : InputCommandType::CursorMoveLeft;
  440. return true;
  441. case VK_RIGHT:
  442. command = isShiftPressed ? InputCommandType::SelectRight : InputCommandType::CursorMoveRight;
  443. return true;
  444. case VK_UP:
  445. command = isShiftPressed ? InputCommandType::SelectUp : InputCommandType::CursorMoveUp;
  446. return true;
  447. case VK_DOWN:
  448. command = isShiftPressed ? InputCommandType::SelectDown : InputCommandType::CursorMoveDown;
  449. return true;
  450. case VK_ESCAPE:
  451. command = InputCommandType::Escape;
  452. return true;
  453. case VK_RETURN:
  454. command = InputCommandType::Return;
  455. return true;
  456. case VK_BACK:
  457. command = InputCommandType::Backspace;
  458. return true;
  459. case VK_DELETE:
  460. command = InputCommandType::Delete;
  461. return true;
  462. }
  463. return false;
  464. }
  465. }