summaryrefslogtreecommitdiff
path: root/Runtime
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-11-09 19:22:13 +0800
committerchai <chaifix@163.com>2021-11-09 19:22:13 +0800
commitd8417b03b9c2a820d3d3be0dfa80841b4d1f4c04 (patch)
tree7d036d283bd7a626d56c5c5a725733df439c8368 /Runtime
parent13f477664c07826c92eac774f0035994c460c057 (diff)
*misc
Diffstat (limited to 'Runtime')
-rw-r--r--Runtime/Events/InputEvent.cpp262
-rw-r--r--Runtime/Events/InputEvent.h23
-rw-r--r--Runtime/Lua/LuaBind/LuaBind.h1
-rw-r--r--Runtime/Lua/LuaBind/LuaBindInvoker.cpp8
-rw-r--r--Runtime/Lua/LuaBind/LuaBindInvoker.h3
-rw-r--r--Runtime/Lua/LuaBind/LuaBindLClass.h16
-rw-r--r--Runtime/Lua/LuaBind/LuaBindState.cpp7
-rw-r--r--Runtime/Lua/LuaBind/LuaBindState.h4
-rw-r--r--Runtime/Lua/LuaBind/LuaBindTable.h2
-rw-r--r--Runtime/Lua/LuaHelper.cpp29
-rw-r--r--Runtime/Lua/LuaHelper.h7
-rw-r--r--Runtime/Math/Vector2.h27
-rw-r--r--Runtime/Scripting/Resource/Resource.bind.cpp1
13 files changed, 377 insertions, 13 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
diff --git a/Runtime/Events/InputEvent.h b/Runtime/Events/InputEvent.h
index 6069c85..164bfff 100644
--- a/Runtime/Events/InputEvent.h
+++ b/Runtime/Events/InputEvent.h
@@ -30,6 +30,22 @@ enum EInputEventType
InputEvent_RotateGesture = 1002
};
+enum EModifiers {
+ Modifier_Shift = 1 << 0,
+ Modifier_Control = 1 << 1,
+ Modifier_Alt = 1 << 2,
+ Modifier_Command = 1 << 3,
+ Modifier_Numeric = 1 << 4,
+ Modifier_CapsLock = 1 << 5,
+ Modifier_FunctionKey = 1 << 6
+};
+
+enum EMouseButton {
+ Mouse_LeftButton = 0,
+ Mouse_RightButton = 1,
+ Mouse_MiddleButton = 2
+};
+
// 输入事件
struct InputEvent : public LuaBind::INativeTable
{
@@ -50,8 +66,11 @@ struct InputEvent : public LuaBind::INativeTable
bool use;
- InputEvent(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
+ InputEvent(UINT message, WPARAM wParam, LPARAM lParam, HWND window);
+
+ void CastToTable(LuaBind::State& state) const override;
- void CastToTable(LuaBind::State& state) override;
+private:
+ bool isMouse, isKey;
}; \ No newline at end of file
diff --git a/Runtime/Lua/LuaBind/LuaBind.h b/Runtime/Lua/LuaBind/LuaBind.h
index b6c5870..2a73c98 100644
--- a/Runtime/Lua/LuaBind/LuaBind.h
+++ b/Runtime/Lua/LuaBind/LuaBind.h
@@ -12,5 +12,6 @@
#include "LuaBindState.inc"
#include "LuaBindInvoker.h"
#include "LuaBindTable.h"
+#include "LuaBindLClass.h"
#endif \ No newline at end of file
diff --git a/Runtime/Lua/LuaBind/LuaBindInvoker.cpp b/Runtime/Lua/LuaBind/LuaBindInvoker.cpp
index debebf8..a95ef5c 100644
--- a/Runtime/Lua/LuaBind/LuaBindInvoker.cpp
+++ b/Runtime/Lua/LuaBind/LuaBindInvoker.cpp
@@ -76,12 +76,18 @@ namespace LuaBind
++argc;
}
- void MemberInvoker::AddTable(INativeTable& tb)
+ void MemberInvoker::AddTable(const INativeTable& tb)
{
tb.CastToTable(state);
++argc;
}
+ void MemberInvoker::AddLuaObject(const ILuaClass& obj)
+ {
+ obj.CastToLuaObject(state);
+ ++argc;
+ }
+
void MemberInvoker::AddMember(MemberRef member)
{
owner->PushMemberRef(state, member);
diff --git a/Runtime/Lua/LuaBind/LuaBindInvoker.h b/Runtime/Lua/LuaBind/LuaBindInvoker.h
index 8ea57a2..edb0725 100644
--- a/Runtime/Lua/LuaBind/LuaBindInvoker.h
+++ b/Runtime/Lua/LuaBind/LuaBindInvoker.h
@@ -56,7 +56,8 @@ namespace LuaBind
void AddNil();
void AddBool(bool b);
void AddString(const char* str);
- void AddTable(INativeTable& tb);
+ void AddTable(const INativeTable& tb);
+ void AddLuaObject(const ILuaClass& obj);
template<class T>
void AddUserdata(NativeClass<T>& udata) {
udata.PushUserdata(state);
diff --git a/Runtime/Lua/LuaBind/LuaBindLClass.h b/Runtime/Lua/LuaBind/LuaBindLClass.h
new file mode 100644
index 0000000..9f4c959
--- /dev/null
+++ b/Runtime/Lua/LuaBind/LuaBindLClass.h
@@ -0,0 +1,16 @@
+#pragma once
+
+namespace LuaBind
+{
+
+ class State;
+
+ // 需要转成lua class对象的实现这个接口,创建一个lua的对象,放在栈顶
+ class ILuaClass
+ {
+ public:
+ virtual void CastToLuaObject(State&) const = 0 ;
+
+ };
+
+}
diff --git a/Runtime/Lua/LuaBind/LuaBindState.cpp b/Runtime/Lua/LuaBind/LuaBindState.cpp
index 676c19c..92e46cd 100644
--- a/Runtime/Lua/LuaBind/LuaBindState.cpp
+++ b/Runtime/Lua/LuaBind/LuaBindState.cpp
@@ -196,7 +196,12 @@ namespace LuaBind
}
}
- void State::PushTable(INativeTable& tb)
+ void State::PushLuaObject(const ILuaClass& lc)
+ {
+ lc.CastToLuaObject(*this);
+ }
+
+ void State::PushTable(const INativeTable& tb)
{
tb.CastToTable(*this);
}
diff --git a/Runtime/Lua/LuaBind/LuaBindState.h b/Runtime/Lua/LuaBind/LuaBindState.h
index 24bd092..aaad952 100644
--- a/Runtime/Lua/LuaBind/LuaBindState.h
+++ b/Runtime/Lua/LuaBind/LuaBindState.h
@@ -7,6 +7,7 @@
#include "LuaBindRefTable.h"
#include "LuaBindGlobalState.h"
#include "LuaBindTable.h"
+#include "LuaBindLClass.h"
namespace LuaBind
{
@@ -101,7 +102,8 @@ namespace LuaBind
bool HasField(int idx, int name, int type);
bool HasKeys(int idx);
- void PushTable(INativeTable& tb);
+ void PushLuaObject(const ILuaClass& lc);
+ void PushTable(const INativeTable& tb);
void PushNil();
void Push(bool value);
void Push(cc8* value);
diff --git a/Runtime/Lua/LuaBind/LuaBindTable.h b/Runtime/Lua/LuaBind/LuaBindTable.h
index 3a6ec6c..a3822d3 100644
--- a/Runtime/Lua/LuaBind/LuaBindTable.h
+++ b/Runtime/Lua/LuaBind/LuaBindTable.h
@@ -9,7 +9,7 @@ namespace LuaBind
struct INativeTable
{
// 将结构转换为table并留在栈顶
- virtual void CastToTable(State& state) = 0;
+ virtual void CastToTable(State& state) const = 0;
};
} \ No newline at end of file
diff --git a/Runtime/Lua/LuaHelper.cpp b/Runtime/Lua/LuaHelper.cpp
index f1dc349..71fe782 100644
--- a/Runtime/Lua/LuaHelper.cpp
+++ b/Runtime/Lua/LuaHelper.cpp
@@ -5,6 +5,7 @@
#include "Runtime/Graphics/Color.h"
using namespace LuaBind;
+using namespace std;
template <>
Rect State::GetValue < Rect >(int idx, const Rect value)
@@ -207,4 +208,32 @@ bool LuaHelper::IsType(LuaBind::State& state, const char* typeName, int idx)
bool LuaHelper::InstantiateClass(LuaBind::State& state, const char* classFullName)
{
return false;
+}
+
+bool LuaHelper::GetLuaClass(LuaBind::State& state, const char* fullName)
+{
+ if (fullName == NULL || strlen(fullName) == 0)
+ return false;
+ int top = state.GetTop();
+ lua_pushvalue(state, LUA_GLOBALSINDEX);
+ string name = fullName;
+ while (name.size() > 0)
+ {
+ int dot = name.find('.');
+ dot = dot != string::npos ? dot : name.size();
+ string pkg = name.substr(0, dot);
+ if (dot < name.size())
+ name = name.substr(dot + 1, name.size() - dot - 1);
+ else
+ name = "";
+ lua_getfield(state, -1, pkg.c_str());
+ if (lua_isnil(state, -1))
+ {
+ lua_settop(state, top);
+ return false;
+ }
+ }
+ lua_replace(state, top + 1);
+ lua_settop(state, top + 1);
+ return true;
} \ No newline at end of file
diff --git a/Runtime/Lua/LuaHelper.h b/Runtime/Lua/LuaHelper.h
index c3cd70e..79cab29 100644
--- a/Runtime/Lua/LuaHelper.h
+++ b/Runtime/Lua/LuaHelper.h
@@ -1,6 +1,4 @@
#pragma once
-#include "Runtime/Math/Rect.h"
-#include "Runtime/Math/Vector2.h"
#include "./LuaBind/LuaBind.h"
// lua 5.1 doc: https://www.lua.org/manual/5.1/
@@ -14,5 +12,8 @@ public:
static void OnRegisterNativeClass(LuaBind::State& state, int cls, std::string clsName, std::string pkgName);
// 创建lua类的实例并留在栈顶,如果失败返回false且不压栈
static bool InstantiateClass(LuaBind::State& state, const char* classFullName);
-
+
+ // 找到类table,并留在栈顶,如果返回false表示没有找到,栈顶无值
+ static bool GetLuaClass(LuaBind::State& state, const char* fullName);
+
}; \ No newline at end of file
diff --git a/Runtime/Math/Vector2.h b/Runtime/Math/Vector2.h
index 7c519b8..31098ab 100644
--- a/Runtime/Math/Vector2.h
+++ b/Runtime/Math/Vector2.h
@@ -1,11 +1,12 @@
#pragma once
#include "MathHelper.h"
#include "Runtime/Utilities/Assert.h"
+#include "Runtime/Lua/LuaHelper.h"
namespace Internal
{
template<typename T>
- struct Vector2T
+ struct Vector2T : public LuaBind::ILuaClass
{
Vector2T(T x = 0, T y = 0)
{
@@ -43,6 +44,30 @@ namespace Internal
return v.x != x || v.y != y;
}
+ Vector2T<T> operator - (const Vector2T& v) const
+ {
+ Vector2T<T> res = Vector2T<T>(x - v.x, y - v.y);
+ return res;
+ }
+
+ void CastToLuaObject(LuaBind::State& state) const override
+ {
+ int top = state.GetTop();
+ if (LuaHelper::GetLuaClass(state, "GameLab.Engine.Math.Vector2"))
+ {
+ lua_getfield(state, -1, "New");
+ lua_pushnumber(state, (float)x);
+ lua_pushnumber(state, (float)y);
+ state.Call(2, 1);
+ lua_replace(state, top + 1);
+ lua_settop(state, top + 1);
+ }
+ else
+ {
+ lua_pushnil(state);
+ }
+ }
+
T x, y;
static Vector2T<T> zero;
diff --git a/Runtime/Scripting/Resource/Resource.bind.cpp b/Runtime/Scripting/Resource/Resource.bind.cpp
index a10ed69..2f63951 100644
--- a/Runtime/Scripting/Resource/Resource.bind.cpp
+++ b/Runtime/Scripting/Resource/Resource.bind.cpp
@@ -4,6 +4,7 @@
#include "Runtime/Graphics/ImageData.h"
#define STB_IMAGE_IMPLEMENTATION
#include "ThirdParty/stb/stb_image.h"
+#include "Runtime/Utilities/Assert.h"
using namespace LuaBind;