CmPlatformWndProc.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "CmPlatformWndProc.h"
  2. #include "CmRenderWindow.h"
  3. #include "CmApplication.h"
  4. namespace CamelotFramework
  5. {
  6. LRESULT CALLBACK PlatformWndProc::_win32WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  7. {
  8. if (uMsg == WM_CREATE)
  9. { // Store pointer to Win32Window in user data area
  10. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)(((LPCREATESTRUCT)lParam)->lpCreateParams));
  11. return 0;
  12. }
  13. // look up window instance
  14. // note: it is possible to get a WM_SIZE before WM_CREATE
  15. RenderWindow* win = (RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  16. if (!win)
  17. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  18. switch( uMsg )
  19. {
  20. case WM_ACTIVATE:
  21. {
  22. bool active = (LOWORD(wParam) != WA_INACTIVE);
  23. if( active )
  24. {
  25. win->setActive(true);
  26. if(!win->hasFocus())
  27. windowFocusReceived(win);
  28. }
  29. else
  30. {
  31. if(win->hasFocus())
  32. windowFocusLost(win);
  33. }
  34. break;
  35. }
  36. case WM_SYSKEYDOWN:
  37. switch( wParam )
  38. {
  39. case VK_CONTROL:
  40. case VK_SHIFT:
  41. case VK_MENU: //ALT
  42. //return zero to bypass defProc and signal we processed the message
  43. return 0;
  44. }
  45. break;
  46. case WM_SYSKEYUP:
  47. switch( wParam )
  48. {
  49. case VK_CONTROL:
  50. case VK_SHIFT:
  51. case VK_MENU: //ALT
  52. case VK_F10:
  53. //return zero to bypass defProc and signal we processed the message
  54. return 0;
  55. }
  56. break;
  57. case WM_SYSCHAR:
  58. // return zero to bypass defProc and signal we processed the message, unless it's an ALT-space
  59. if (wParam != VK_SPACE)
  60. return 0;
  61. break;
  62. case WM_ENTERSIZEMOVE:
  63. break;
  64. case WM_EXITSIZEMOVE:
  65. break;
  66. case WM_MOVE:
  67. windowMovedOrResized(win);
  68. break;
  69. case WM_DISPLAYCHANGE:
  70. windowMovedOrResized(win);
  71. break;
  72. case WM_SIZE:
  73. windowMovedOrResized(win);
  74. break;
  75. case WM_SETCURSOR:
  76. if(isCursorHidden())
  77. win32HideCursor();
  78. else
  79. win32ShowCursor();
  80. return true;
  81. case WM_GETMINMAXINFO:
  82. // Prevent the window from going smaller than some minimu size
  83. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
  84. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
  85. break;
  86. case WM_CLOSE:
  87. {
  88. // TODO - Only stop main loop if primary window is closed!!
  89. gApplication().stopMainLoop();
  90. return 0;
  91. }
  92. case WM_NCLBUTTONUP:
  93. {
  94. break;
  95. }
  96. case WM_LBUTTONUP:
  97. {
  98. break;
  99. }
  100. case WM_MOUSEMOVE:
  101. {
  102. POINT mousePos;
  103. mousePos.x = GET_X_LPARAM(lParam);
  104. mousePos.y = GET_Y_LPARAM(lParam);
  105. ClientToScreen(hWnd, &mousePos);
  106. if(!onMouseMoved.empty())
  107. onMouseMoved(Int2(mousePos.x, mousePos.y));
  108. return true;
  109. }
  110. case WM_MOUSEWHEEL:
  111. {
  112. INT16 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  113. float wheelDeltaFlt = wheelDelta / (float)WHEEL_DELTA;
  114. if(!onMouseWheelScrolled.empty())
  115. onMouseWheelScrolled(wheelDeltaFlt);
  116. return true;
  117. }
  118. case WM_DEADCHAR:
  119. case WM_CHAR:
  120. {
  121. switch (wParam)
  122. {
  123. case VK_BACK:
  124. case 0x0A: // linefeed
  125. case 0x0D: // carriage return
  126. case VK_ESCAPE:
  127. case VK_TAB:
  128. break;
  129. default: // displayable character
  130. {
  131. UINT8 scanCode = (lParam >> 16) & 0xFF;
  132. BYTE keyState[256];
  133. HKL layout = GetKeyboardLayout(0);
  134. if(GetKeyboardState(keyState) == 0)
  135. return 0;
  136. unsigned int vk = MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, layout);
  137. if(vk == 0)
  138. return 0;
  139. bool isDeadKey = (MapVirtualKeyEx(vk, MAPVK_VK_TO_CHAR, layout) & (1 << 31)) != 0;
  140. if(isDeadKey)
  141. return 0;
  142. wchar_t buff[3] = {0};
  143. int numChars = ToUnicodeEx(vk, scanCode, keyState, buff, 3, 0, layout);
  144. // TODO - I am ignoring dead keys here - primarily because I haven't found a good way of retrieving non-combined dead key
  145. // value. ToUnicodeEx and MapVirtualKeyEx only return precombined (i.e. spacing) versions, which can't be combined using other characters.
  146. // I need non-combined version so I can use it with FoldString to apply to a certain character.
  147. UINT32 finalChar = 0;
  148. if(numChars == 1)
  149. finalChar = buff[0];
  150. else
  151. return 0;
  152. if(!onCharInput.empty())
  153. onCharInput(finalChar);
  154. return 0;
  155. }
  156. }
  157. break;
  158. }
  159. }
  160. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  161. }
  162. }