From 9b1f8214eea0c86d41f903a5feba9aac78603df1 Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 12 Nov 2021 19:13:03 +0800 Subject: *misc --- Runtime/Events/InputEvent.cpp | 88 +++++++++++++++++++++++--------- Runtime/Events/InputEvent.h | 17 +++--- Runtime/Lua/LuaBind/LuaBindLClass.h | 1 + Runtime/Lua/LuaBind/LuaBindTable.h | 1 + Runtime/Math/Vector2.h | 6 +++ Runtime/Scripting/Events/Events.bind.cpp | 65 +++++++++++++++++++++++ 6 files changed, 149 insertions(+), 29 deletions(-) create mode 100644 Runtime/Scripting/Events/Events.bind.cpp (limited to 'Runtime') diff --git a/Runtime/Events/InputEvent.cpp b/Runtime/Events/InputEvent.cpp index 07d8778..e741511 100644 --- a/Runtime/Events/InputEvent.cpp +++ b/Runtime/Events/InputEvent.cpp @@ -3,6 +3,34 @@ #include "Editor/GUI/WindowUtil.h" #endif +namespace Events +{ + bool IsMouseEvent(EInputEventType type) + { + switch (type) + { + case InputEvent_MouseDown: + case InputEvent_MouseUp: + case InputEvent_MouseMove: + case InputEvent_MouseDrag: + case InputEvent_ScrollWheel: + return true; + } + return false; + } + + bool IsKeyEvent(EInputEventType type) + { + switch (type) + { + case InputEvent_KeyDown: + case InputEvent_KeyUp: + return true; + } + return false; + } +} + static Vector2f GetInputMousePosition(HWND window) { #if GAMELAB_EDITOR @@ -25,70 +53,64 @@ static Vector2f GetInputMousePosition(HWND window) #endif } +InputEvent::InputEvent() +{ +} + InputEvent::InputEvent(UINT message, WPARAM wParam, LPARAM lParam, HWND window) { static Vector2f s_LastMousePos(0.0f, 0.0f); - isMouse = false, isKey = false; + clickCount = 0; 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) { @@ -108,37 +130,30 @@ InputEvent::InputEvent(UINT message, WPARAM wParam, LPARAM lParam, HWND window) 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: @@ -162,7 +177,7 @@ InputEvent::InputEvent(UINT message, WPARAM wParam, LPARAM lParam, HWND window) modifiers |= Modifier_CapsLock; } - if (isMouse) + if (Events::IsMouseEvent(type)) { Vector2f p = GetInputMousePosition(window); mousePosition = p; @@ -204,7 +219,7 @@ InputEvent::InputEvent(UINT message, WPARAM wParam, LPARAM lParam, HWND window) // handle keyboard character = keycode = 0; - if (isKey) + if (Events::IsKeyEvent(type)) { //if (message == WM_CHAR) //{ @@ -249,7 +264,7 @@ void InputEvent::CastToTable(LuaBind::State& state) const lua_pushnumber(state, type); lua_setfield(state, table, "type"); - // "used" + // "use" lua_pushboolean(state, use); lua_setfield(state, table, "use"); @@ -257,7 +272,7 @@ void InputEvent::CastToTable(LuaBind::State& state) const state.PushLuaObject(mousePosition); lua_setfield(state, table, "mousePosition"); - if (isMouse) + if (Events::IsMouseEvent(type)) { // "mouseDelta" state.PushLuaObject(mouseDelta); @@ -272,10 +287,37 @@ void InputEvent::CastToTable(LuaBind::State& state) const lua_setfield(state, table, "clickCount"); } - if (isKey) + if (Events::IsKeyEvent(type)) { } state.SetTop(table); +} + +void InputEvent::RestoreFromTable(LuaBind::State& state, int idx) +{ + int top = state.GetTop(); + int index = state.AbsIndex(idx); + type = (EInputEventType) state.GetField(index, "type", InputEvent_Ignore); + use = state.GetField(index, "use", false); + + lua_getfield(state, index, "mousePosition"); + mousePosition.RestoreFromLuaObject(state, -1); + + if (Events::IsMouseEvent(type)) + { + lua_getfield(state, index, "mouseDelta"); + mouseDelta.RestoreFromLuaObject(state, -1); + + button = state.GetField(index, "button", 0); + clickCount = state.GetField(index, "clickCount", 0); + } + + if (Events::IsKeyEvent(type)) + { + + } + + state.SetTop(top); } \ No newline at end of file diff --git a/Runtime/Events/InputEvent.h b/Runtime/Events/InputEvent.h index 164bfff..d19fbea 100644 --- a/Runtime/Events/InputEvent.h +++ b/Runtime/Events/InputEvent.h @@ -41,9 +41,9 @@ enum EModifiers { }; enum EMouseButton { - Mouse_LeftButton = 0, - Mouse_RightButton = 1, - Mouse_MiddleButton = 2 + Mouse_LeftButton = 1, + Mouse_RightButton = 2, + Mouse_MiddleButton = 3 }; // 输入事件 @@ -66,11 +66,16 @@ struct InputEvent : public LuaBind::INativeTable bool use; + InputEvent(); InputEvent(UINT message, WPARAM wParam, LPARAM lParam, HWND window); void CastToTable(LuaBind::State& state) const override; + void RestoreFromTable(LuaBind::State& state, int index) override; -private: - bool isMouse, isKey; +}; -}; \ No newline at end of file +namespace Events +{ + bool IsMouseEvent(EInputEventType type); + bool IsKeyEvent(EInputEventType type); +} \ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindLClass.h b/Runtime/Lua/LuaBind/LuaBindLClass.h index 9f4c959..296d6a1 100644 --- a/Runtime/Lua/LuaBind/LuaBindLClass.h +++ b/Runtime/Lua/LuaBind/LuaBindLClass.h @@ -10,6 +10,7 @@ namespace LuaBind { public: virtual void CastToLuaObject(State&) const = 0 ; + virtual void RestoreFromLuaObject(State& state, int index) = 0; }; diff --git a/Runtime/Lua/LuaBind/LuaBindTable.h b/Runtime/Lua/LuaBind/LuaBindTable.h index a3822d3..b3028f8 100644 --- a/Runtime/Lua/LuaBind/LuaBindTable.h +++ b/Runtime/Lua/LuaBind/LuaBindTable.h @@ -10,6 +10,7 @@ namespace LuaBind { // 将结构转换为table并留在栈顶 virtual void CastToTable(State& state) const = 0; + virtual void RestoreFromTable(State& state, int index) = 0; }; } \ No newline at end of file diff --git a/Runtime/Math/Vector2.h b/Runtime/Math/Vector2.h index 31098ab..aba4262 100644 --- a/Runtime/Math/Vector2.h +++ b/Runtime/Math/Vector2.h @@ -68,6 +68,12 @@ namespace Internal } } + void RestoreFromLuaObject(LuaBind::State& state, int index) override + { + + } + + T x, y; static Vector2T zero; diff --git a/Runtime/Scripting/Events/Events.bind.cpp b/Runtime/Scripting/Events/Events.bind.cpp new file mode 100644 index 0000000..40ea544 --- /dev/null +++ b/Runtime/Scripting/Events/Events.bind.cpp @@ -0,0 +1,65 @@ +#include "Runtime/Events/InputEvent.h" +#include "Runtime/Lua/LuaHelper.h" +#include "Runtime/Debug/Log.h" + +// Events.CopyEvent(e) +int CopyEvent(lua_State* L) +{ + LUA_BIND_STATE(L); + + InputEvent e; + e.RestoreFromTable(state, -1); + state.PushTable(e); + + return 1; +} + +static luaL_Reg funcs[] = { + {"CopyEvent", CopyEvent}, + {0, 0} +}; + +int luaopen_GameLab_Events(lua_State* L) +{ + log_info_tag("Scripting", "luaopen_GameLab_Events()"); + + LUA_BIND_STATE(L); + state.PushGlobalNamespace(); + state.PushNamespace("GameLab"); + state.PushNamespace("Events"); + + state.RegisterMethods(funcs); + + LUA_BIND_REGISTER_ENUM(state, "EEventType", + { "MouseDown", InputEvent_MouseDown }, + { "MouseUp", InputEvent_MouseUp }, + { "MouseMove", InputEvent_MouseMove }, + { "MouseDrag", InputEvent_MouseDrag }, + { "KeyDown", InputEvent_KeyDown }, + { "KeyUp", InputEvent_KeyUp }, + { "ScrollWheel", InputEvent_ScrollWheel }, + { "Repaint", InputEvent_Repaint }, + { "Layout", InputEvent_Layout }, + { "DragUpdated", InputEvent_DragUpdated }, + { "DragPerform", InputEvent_DragPerform }, + { "DragExited", InputEvent_DragExited }, + { "Ignore", InputEvent_Ignore }, + { "Used", InputEvent_Used }, + { "ValidateCommand", InputEvent_ValidateCommand }, + { "ExecuteCommand", InputEvent_ExecuteCommand }, + { "ContextClick", InputEvent_ContextClick }, + { "MouseEnterWindow", InputEvent_MouseEnterWindow }, + { "MouseLeaveWindow", InputEvent_MouseLeaveWindow }, + { "MagnifyGesture", InputEvent_MagnifyGesture }, + { "SwipeGesture", InputEvent_SwipeGesture }, + { "RotateGesture", InputEvent_RotateGesture } + ); + + LUA_BIND_REGISTER_ENUM(state, "EMouseButton", + { "LeftButton", Mouse_LeftButton }, + { "RightButton", Mouse_RightButton }, + { "MiddleButton", Mouse_MiddleButton } + ); + + return 1; +} \ No newline at end of file -- cgit v1.1-26-g67d0