diff options
author | chai <chaifix@163.com> | 2021-11-09 19:22:13 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-11-09 19:22:13 +0800 |
commit | d8417b03b9c2a820d3d3be0dfa80841b4d1f4c04 (patch) | |
tree | 7d036d283bd7a626d56c5c5a725733df439c8368 /Runtime/Events/InputEvent.cpp | |
parent | 13f477664c07826c92eac774f0035994c460c057 (diff) |
*misc
Diffstat (limited to 'Runtime/Events/InputEvent.cpp')
-rw-r--r-- | Runtime/Events/InputEvent.cpp | 262 |
1 files changed, 260 insertions, 2 deletions
diff --git a/Runtime/Events/InputEvent.cpp b/Runtime/Events/InputEvent.cpp index e062bc2..c5994ef 100644 --- a/Runtime/Events/InputEvent.cpp +++ b/Runtime/Events/InputEvent.cpp @@ -1,11 +1,246 @@ #include "InputEvent.h" +#ifdef GAMELAB_EDITOR +#include "Editor/GUI/WindowUtil.h" +#endif -InputEvent::InputEvent(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +static Vector2f GetInputMousePosition(HWND window) { +#if GAMELAB_EDITOR + POINT mousePos; + ::GetCursorPos(&mousePos); + ::ScreenToClient(window, &mousePos); + int offsetX = 0, offsetY = 0; + HWND offsetWindow; + WindowUtil::GetEditorMouseOffset(&offsetX, &offsetY, &offsetWindow); + if (offsetWindow == window) + { + mousePos.x += offsetX; + mousePos.y += offsetY; + } + return Vector2f(mousePos.x, mousePos.y); +#else + //return GetInputManager().GetMousePosition(); + +#endif } -void InputEvent::CastToTable(LuaBind::State& state) +InputEvent::InputEvent(UINT message, WPARAM wParam, LPARAM lParam, HWND window) +{ + static Vector2f s_LastMousePos(0.0f, 0.0f); + + isMouse = false, isKey = false; + + switch (message) { + case WM_LBUTTONDOWN: + type = InputEvent_MouseDown; + isMouse = true; + button = Mouse_LeftButton; + clickCount = 1; + break; + case WM_RBUTTONDOWN: + type = InputEvent_MouseDown; + isMouse = true; + button = Mouse_RightButton; + clickCount = 1; + break; + case WM_MBUTTONDOWN: + type = InputEvent_MouseDown; + isMouse = true; + button = Mouse_MiddleButton; + clickCount = 1; + break; + case WM_LBUTTONUP: + type = InputEvent_MouseUp; + isMouse = true; + button = Mouse_LeftButton; + clickCount = 1; + break; + case WM_RBUTTONUP: + type = InputEvent_MouseUp; + isMouse = true; + button = Mouse_RightButton; + clickCount = 1; + break; + case WM_MBUTTONUP: + type = InputEvent_MouseUp; + isMouse = true; + button = Mouse_MiddleButton; + clickCount = 1; + break; + case WM_LBUTTONDBLCLK: + type = InputEvent_MouseDown; + isMouse = true; + button = Mouse_LeftButton; + clickCount = 2; + break; + case WM_RBUTTONDBLCLK: + type = InputEvent_MouseDown; + isMouse = true; + button = Mouse_RightButton; + clickCount = 2; + break; + case WM_MBUTTONDBLCLK: + type = InputEvent_MouseDown; + isMouse = true; + button = Mouse_MiddleButton; + clickCount = 2; + break; + case WM_MOUSEMOVE: + type = InputEvent_MouseMove; + isMouse = true; + button = 0; + if (wParam & MK_LBUTTON) + { + type = InputEvent_MouseDrag; + button |= Mouse_LeftButton; + } + if (wParam & MK_RBUTTON) + { + type = InputEvent_MouseDrag; + button |= Mouse_RightButton; + } + if (wParam & MK_MBUTTON) + { + type = InputEvent_MouseDrag; + button |= Mouse_MiddleButton; + } + break; + case WM_KEYUP: + type = InputEvent_KeyUp; + isKey = true; + break; + case WM_KEYDOWN: + type = InputEvent_KeyDown; + isKey = true; + break; + case WM_SYSKEYUP: + type = InputEvent_KeyUp; + isKey = true; + break; + case WM_SYSKEYDOWN: + type = InputEvent_KeyDown; + isKey = true; + break; + case WM_CHAR: + if (!(lParam & (1 << 31))) // key is pressed + { + type = InputEvent_KeyDown; + isKey = true; + } + break; + case WM_MOUSEWHEEL: + mouseDelta.y = -GET_WHEEL_DELTA_WPARAM(wParam) / float(WHEEL_DELTA) * 3.0f; + type = InputEvent_ScrollWheel; + isMouse = true; + button = 0; + break; + case WM_MOUSEHWHEEL: + mouseDelta.x = GET_WHEEL_DELTA_WPARAM(wParam) / float(WHEEL_DELTA) * 3.0f; + type = InputEvent_ScrollWheel; + isMouse = true; + button = 0; + break; + default: + type = InputEvent_Ignore; + break; + } + + // Handle modifiers + modifiers = 0; + if (type != InputEvent_Ignore) + { + if (GetKeyState(VK_SHIFT) < 0) + modifiers |= Modifier_Shift; + if (GetKeyState(VK_CONTROL) < 0) + modifiers |= Modifier_Control; + if (GetKeyState(VK_MENU) < 0) + modifiers |= Modifier_Alt; + if (GetKeyState(VK_LWIN) < 0 || GetKeyState(VK_RWIN) < 0) + modifiers |= Modifier_Command; + if (GetKeyState(VK_CAPITAL) < 0) + modifiers |= Modifier_CapsLock; + } + + if (isMouse) + { + Vector2f p = GetInputMousePosition(window); + mousePosition = p; + + pressure = 1.0f; // TODO ? + + // mouse wheel events already captured scroll distance in delta + if (type == InputEvent_ScrollWheel && message == WM_MOUSEWHEEL) + { + // nothing + } + else + { + mouseDelta = mousePosition - s_LastMousePos; + + //if (type == InputEvent_MouseDrag && s_wantsMouseJumping) + // DoMouseJumpingTroughScreenEdges(window, mousePosition, s_LastMousePos); + //else + s_LastMousePos = mousePosition; + + // We get quite a lot of move/drag events that have delta of zero. Filter those + // out. + if ((type == InputEvent_MouseMove || type == InputEvent_MouseDrag) && mouseDelta.x == 0.0f && mouseDelta.y == 0.0f) + type = InputEvent_Ignore; + + } + } + else + { + Vector2f p = GetInputMousePosition(window); + + button = 0; + pressure = 0.0f; + mouseDelta = Vector2f(0.0f, 0.0f); + clickCount = 0; + mousePosition = p; + } + + // handle keyboard + character = keycode = 0; + + if (isKey) + { + //if (message == WM_CHAR) + //{ + // character = wParam; + // if (character == 127 || character < ' ' && character != 13 && character != 10 && character != 9) // ignore control characters other than Enter or Tab + // type = InputEvent_Ignore; + //} + //else + //{ + // if (wParam == VK_CONTROL) + // { + // wParam = (lParam & (1 << 24)) ? VK_RCONTROL : VK_LCONTROL; + // } + // if (wParam == VK_MENU) + // { + // wParam = (lParam & (1 << 24)) ? VK_RMENU : VK_LMENU; + // } + // keycode = InputKeycodeFromVKEY(wParam); + // if (keycode == SDLK_UNKNOWN) + // type = InputEvent::kIgnore; + // if (((wParam >= VK_F1) || (wParam >= VK_PRIOR && wParam <= VK_HELP)) && (wParam < VK_OEM_1 || wParam > VK_OEM_8)) + // modifiers |= InputEvent::kFunctionKey; + //} + + //// The unity convention is that function keys are anything that requires special handling + //// when dealing with a text entry field. Hence, backspace is + //if (keycode == SDLK_BACKSPACE) + // modifiers |= InputEvent::kFunctionKey; + + //// "Return" gives an ASCII-13 (Carriage return). For sanity, we turn that into ASCII-10(linefeed, \n) + //if (character == 13) + // character = 10; + } +} + +void InputEvent::CastToTable(LuaBind::State& state) const { lua_newtable(state); int table = state.GetTop(); @@ -15,5 +250,28 @@ void InputEvent::CastToTable(LuaBind::State& state) lua_setfield(state, table, "type"); // "mousePosition" + state.PushLuaObject(mousePosition); + lua_setfield(state, table, "mousePosition"); + + if (isMouse) + { + // "mouseDelta" + state.PushLuaObject(mouseDelta); + lua_setfield(state, table, "mouseDelta"); + + // "button" + lua_pushnumber(state, button); + lua_setfield(state, table, "button"); + + // "clickCount" + lua_pushnumber(state, clickCount); + lua_setfield(state, table, "clickCount"); + } + + if (isKey) + { + + } + state.SetTop(table); }
\ No newline at end of file |