From 7bf672fd0c6211909d94078b448032b1bd0916ef Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 23 Oct 2021 18:19:18 +0800 Subject: *misc --- Editor/GUI/EditorWindows.h | 3 ++- Editor/GUI/GUIWindow.cpp | 11 ++++++++-- Editor/Scripting/EditorGUI/GUIWindow.bind.cpp | 9 ++++++++ Examples/Hello/Setting.lua | 8 ------- .../DefaultContent/Libraries/GameLab/Class.lua | 2 +- .../Libraries/GameLab/Editor/GUI/EditorWindow.lua | 7 +++++- Resources/Scripts/Editor/AssetBrowser.lua | 16 ++++++++++++++ Resources/Scripts/Editor/ProjectWindow.lua | 20 ----------------- Resources/Scripts/EditorApplication.lua | 13 ++++++----- .../Scripts/EditorGUI/EditorWindowManager.lua | 5 +++++ Runtime/Lua/LuaBind/LuaBindClass.hpp | 25 ++++++++++++++++------ Runtime/Lua/LuaBind/LuaBindHelper.h | 1 + Runtime/Lua/LuaBind/LuaBindRef.h | 3 ++- Runtime/Lua/LuaBind/LuaBindState.cpp | 5 +++++ Runtime/Lua/LuaBind/LuaBindState.h | 3 ++- Runtime/Lua/LuaBind/LuaBindVM.cpp | 10 +++++++++ Runtime/Lua/LuaBind/LuaBindVM.h | 7 ++++-- Runtime/Lua/LuaHelper.cpp | 5 +++++ Runtime/Lua/LuaHelper.h | 3 +++ 19 files changed, 108 insertions(+), 48 deletions(-) delete mode 100644 Examples/Hello/Setting.lua create mode 100644 Resources/Scripts/Editor/AssetBrowser.lua delete mode 100644 Resources/Scripts/Editor/ProjectWindow.lua create mode 100644 Resources/Scripts/EditorGUI/EditorWindowManager.lua diff --git a/Editor/GUI/EditorWindows.h b/Editor/GUI/EditorWindows.h index a25b612..3b84b13 100644 --- a/Editor/GUI/EditorWindows.h +++ b/Editor/GUI/EditorWindows.h @@ -145,6 +145,7 @@ public: void OnFocus(); void OnLostFocus(); + void OnPaint(); GET_SET(std::string, Name, m_Name); GET(HDC, DC, m_DC); @@ -161,7 +162,7 @@ private: HDC m_DC; HGLRC m_RC; - LuaBind::MemberRef m_Instance; // EditorWindow脚本 + LuaBind::MemberRef m_Script; // EditorWindow脚本 LUA_BIND_DECL_CLASS(GUIWindow); diff --git a/Editor/GUI/GUIWindow.cpp b/Editor/GUI/GUIWindow.cpp index 2127232..fb23a59 100644 --- a/Editor/GUI/GUIWindow.cpp +++ b/Editor/GUI/GUIWindow.cpp @@ -45,7 +45,7 @@ LRESULT CALLBACK GUIWindow::GUIViewWndProc(HWND hWnd, UINT message, WPARAM wPara { log_info("WndProc", "WM_PAINT"); self->SetAsRenderContext(); - + self->OnPaint(); BeginPaint(self->m_Handle, &ps); EndPaint(self->m_Handle, &ps); UpdateWindow(self->m_Handle); @@ -150,6 +150,7 @@ LRESULT CALLBACK GUIWindow::GUIViewWndProc(HWND hWnd, UINT message, WPARAM wPara } case WM_SETFOCUS: //获得焦点 + self->OnFocus(); return 0; case WM_KILLFOCUS: //失去焦点 return 0; @@ -176,7 +177,7 @@ void GUIWindow::RepaintAll() GUIWindow::GUIWindow(LuaBind::VM* vm) : LuaBind::NativeClass(vm) - , m_Instance() + , m_Script() { } @@ -313,12 +314,18 @@ void GUIWindow::DoPaint() void GUIWindow::OnFocus() { + InvokeLuaCallback(m_Script, "OnFocus"); } void GUIWindow::OnLostFocus() { } +void GUIWindow::OnPaint() +{ + InvokeLuaCallback(m_Script, "OnPaint"); +} + void GUIWindow::SetPosition(Rectf position) { log_info("GUIWindow::SetPosition()"); diff --git a/Editor/Scripting/EditorGUI/GUIWindow.bind.cpp b/Editor/Scripting/EditorGUI/GUIWindow.bind.cpp index 075bce5..9ecc209 100644 --- a/Editor/Scripting/EditorGUI/GUIWindow.bind.cpp +++ b/Editor/Scripting/EditorGUI/GUIWindow.bind.cpp @@ -7,6 +7,7 @@ LUA_BIND_REGISTRY(GUIWindow) { "Focus", _Focus }, { "SetContainerWindow", _SetContainerWindow }, { "SetPosition", _SetPosition }, + { "SetInstance", _SetInstance}, { "New", _New } ); } @@ -37,6 +38,14 @@ LUA_BIND_IMPL_METHOD(GUIWindow, _SetContainerWindow) return 0; } +// SetInstance(self, editorWindow) +LUA_BIND_IMPL_METHOD(GUIWindow, _SetInstance) +{ + LUA_BIND_PREPARE(L, GUIWindow); + self->SetMemberRef(state,self->m_Script, -1); + return 0; +} + // GUIWindow.SetPosition(self, {x, y, width, height}) LUA_BIND_IMPL_METHOD(GUIWindow, _SetPosition) { diff --git a/Examples/Hello/Setting.lua b/Examples/Hello/Setting.lua deleted file mode 100644 index 2ab1597..0000000 --- a/Examples/Hello/Setting.lua +++ /dev/null @@ -1,8 +0,0 @@ -local setting = {} - -setting.libraries = { -"Framework", -"luasocks" -} - -return setting \ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/GameLab/Class.lua b/Resources/DefaultContent/Libraries/GameLab/Class.lua index 8f74d10..cb08449 100644 --- a/Resources/DefaultContent/Libraries/GameLab/Class.lua +++ b/Resources/DefaultContent/Libraries/GameLab/Class.lua @@ -21,7 +21,7 @@ local Class = function(className, pkg) local child = _class(childName, childPkg) if cls then setmetatable(child, cls) - child._base = cls + child.base = cls end return child end diff --git a/Resources/Libraries/GameLab/Editor/GUI/EditorWindow.lua b/Resources/Libraries/GameLab/Editor/GUI/EditorWindow.lua index 2ec7f95..5b1b140 100644 --- a/Resources/Libraries/GameLab/Editor/GUI/EditorWindow.lua +++ b/Resources/Libraries/GameLab/Editor/GUI/EditorWindow.lua @@ -1,6 +1,8 @@ local EditorWindow = GameLab.Class("EditorWindow", "GameLab.Editor.GUI") -EditorWindow.Ctor = function(self) +EditorWindow.Ctor = function(self, title) + self.title = title + self.guiWindow = nil end EditorWindow.OnGUI = function(self) @@ -15,4 +17,7 @@ end EditorWindow.OnStop = function(self) end +EditorWindow.OnFocus = function(self) +end + return EditorWindow \ No newline at end of file diff --git a/Resources/Scripts/Editor/AssetBrowser.lua b/Resources/Scripts/Editor/AssetBrowser.lua new file mode 100644 index 0000000..07cead4 --- /dev/null +++ b/Resources/Scripts/Editor/AssetBrowser.lua @@ -0,0 +1,16 @@ +local Debug = GameLab.Debug +local AssetBrowser = GameLab.Editor.GUI.EditorWindow.Extend("AssetBrowser", "GameLab.Editor") + +AssetBrowser.Ctor = function(self) + self.base.Ctor(self, "AssetBrowser") +end + +AssetBrowser.OnGUI = function(self) + Debug.Log("AssetBrowser.OnGUI()" .. self.title) +end + +AssetBrowser.OnFocus = function(self) + Debug.Log("AssetBrowser.OnFocus()" .. self.title) +end + +return AssetBrowser \ No newline at end of file diff --git a/Resources/Scripts/Editor/ProjectWindow.lua b/Resources/Scripts/Editor/ProjectWindow.lua deleted file mode 100644 index ffa9ef8..0000000 --- a/Resources/Scripts/Editor/ProjectWindow.lua +++ /dev/null @@ -1,20 +0,0 @@ -local GUI = GameLab.Editor.GUI -local GUILayout = GameLab.Editor.GUILayout - -local ProjectWindow = { - ["name"] = "Project", -} - -ProjectWindow.OnGUI = function(self) - if GUILayout.Button("click") then - - end -end - -ProjectWindow.OnUpdate = function(self) - -end - -GUI.RegisterEditorWindow("Project", ProjectWindow, "Custom/Project") - - diff --git a/Resources/Scripts/EditorApplication.lua b/Resources/Scripts/EditorApplication.lua index f09a197..8a175db 100644 --- a/Resources/Scripts/EditorApplication.lua +++ b/Resources/Scripts/EditorApplication.lua @@ -1,5 +1,7 @@ local json = require "LiteJson.json" local inspect = require "inspect" +local AssetBrowser = require "./Scripts/Editor/AssetBrowser" +local EditorWindowManager = require "./Scripts/EditorGUI/EditorWindowManager" local Debug = GameLab.Debug local GUI = GameLab.Editor.GUI @@ -17,10 +19,8 @@ mainWindow:SetIcon("./Icon/GameLab.ico") app:SetMainWindow(mainWindow) local guiWindow = GUI.GUIWindow.New() ---guiWindow:SetContainerWindow(mainWindow) ---guiWindow:SetPosition({0,0, 500, 400}) -guiWindow.a = 10 -guiWindow = nil +guiWindow:SetContainerWindow(mainWindow) +guiWindow:SetPosition({0,0, 500, 400}) collectgarbage() @@ -28,14 +28,17 @@ Debug.Log(GameLab.Path.GetRootDirectory()) Debug.Log(inspect{foo=1,2,3,4}) -local wnd = GUI.EditorWindow.New() +local wnd = AssetBrowser.New() Debug.Log(inspect(mainWindow._type)) +guiWindow:SetInstance(wnd) local v = GameLab.Engine.Math.Vector4.New(1,2,3,4) Debug.Log(inspect(v)) local V4 = GameLab.Engine.Math.Vector4.Extend("V4", "GameLab.Engine.Math") +Debug.Log(EditorWindowManager.name) + while true do app:PullMessage() diff --git a/Resources/Scripts/EditorGUI/EditorWindowManager.lua b/Resources/Scripts/EditorGUI/EditorWindowManager.lua new file mode 100644 index 0000000..707038b --- /dev/null +++ b/Resources/Scripts/EditorGUI/EditorWindowManager.lua @@ -0,0 +1,5 @@ +local EditorWindowManager = {} + +EditorWindowManager.name = "asd" + +return EditorWindowManager \ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindClass.hpp b/Runtime/Lua/LuaBind/LuaBindClass.hpp index c112c03..eb028c2 100644 --- a/Runtime/Lua/LuaBind/LuaBindClass.hpp +++ b/Runtime/Lua/LuaBind/LuaBindClass.hpp @@ -44,11 +44,6 @@ namespace LuaBind }; - // TODO: 将公共部分提取出来,不要重复生成代码 - //class NativeClassBase - //{ - //} - // 需要暴露给lua的native class需要继承此类。通过lua管理的实例要确保引用计数的正确性,在多个线程中需要确 // 定不会误释放。 template @@ -90,11 +85,16 @@ namespace LuaBind NativeClass(LuaBind::VM* vm); virtual ~NativeClass(); + VM* GetVM() { return mOwner; } + // 成员引用管理,在实例的ref table里。设置、取、清除 void SetMemberRef(State& state, MemberRef& memRef, int idx); bool PushMemberRef(State& state, MemberRef& memRef); void ClearMemberRef(State& state, MemberRef& memRef); + // 调用回调函数 + void InvokeLuaCallback(LuaBind::MemberRef script, cc8* method); + private: friend class State; @@ -348,7 +348,6 @@ namespace LuaBind state.PushPtrUserdata(p); lua_newtable(state); // ref table,无法在lua处访问,用来管理C对象的生命周期 - //lua_newtable(state); // member table,lua中创建的对象成员都保存在这里 PushClassTable(state); // class table // stack: @@ -405,6 +404,20 @@ namespace LuaBind } } + template + void NativeClass::InvokeLuaCallback(LuaBind::MemberRef script, cc8* method) + { + if (!script) + return; + LuaBind::State state = GetVM()->GetCurThread(); + int top = state.GetTop(); + PushMemberRef(state, script); + state.GetField(-1, method); + PushMemberRef(state, script); + state.Call(1, 0); + state.SetTop(top); + } + template bool NativeClass::PushMemberRef(State& state, MemberRef& memRef) { diff --git a/Runtime/Lua/LuaBind/LuaBindHelper.h b/Runtime/Lua/LuaBind/LuaBindHelper.h index f095add..86fcb13 100644 --- a/Runtime/Lua/LuaBind/LuaBindHelper.h +++ b/Runtime/Lua/LuaBind/LuaBindHelper.h @@ -13,4 +13,5 @@ namespace LuaBind int m_Cur; }; }; + #endif \ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindRef.h b/Runtime/Lua/LuaBind/LuaBindRef.h index cd2fad1..c7b7ab3 100644 --- a/Runtime/Lua/LuaBind/LuaBindRef.h +++ b/Runtime/Lua/LuaBind/LuaBindRef.h @@ -7,7 +7,8 @@ namespace LuaBind { - // 全局引用,保存在LUA_REGISTRYINDEX下面的两个表里 + // 全局引用,保存在LUA_REGISTRYINDEX下面的两个表里,生命周期手动控制 + // 如果要用局部引用,用MemberRef,会保存在UserData的RefTable里,生命周期和UserData一致 class Ref { public: diff --git a/Runtime/Lua/LuaBind/LuaBindState.cpp b/Runtime/Lua/LuaBind/LuaBindState.cpp index 3d26dc3..c9183ff 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.cpp +++ b/Runtime/Lua/LuaBind/LuaBindState.cpp @@ -323,6 +323,11 @@ namespace LuaBind return lua_gettop(mState); } + void State::SetTop(int top) + { + lua_settop(mState, top); + } + bool State::HasField(int idx, cc8* name) { lua_getfield(mState, idx, name); diff --git a/Runtime/Lua/LuaBind/LuaBindState.h b/Runtime/Lua/LuaBind/LuaBindState.h index aa77c26..60e05be 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.h +++ b/Runtime/Lua/LuaBind/LuaBindState.h @@ -222,7 +222,8 @@ namespace LuaBind if(!state.CheckParams(1, params)) return 0 #define LUA_BIND_STATE(L) \ - LuaBind::State state(L) + LuaBind::State state(L);\ + state.GetVM()->SetCurThread(L) //--------------------------------------------------------------------------------// diff --git a/Runtime/Lua/LuaBind/LuaBindVM.cpp b/Runtime/Lua/LuaBind/LuaBindVM.cpp index 6b39288..e454929 100644 --- a/Runtime/Lua/LuaBind/LuaBindVM.cpp +++ b/Runtime/Lua/LuaBind/LuaBindVM.cpp @@ -60,6 +60,16 @@ namespace LuaBind return mMainThread; } + void VM::SetCurThread(lua_State* cur) + { + mCurThread = cur; + } + + lua_State* VM::GetCurThread() + { + return mCurThread; + } + State VM::GetMainState() { return mMainThread; diff --git a/Runtime/Lua/LuaBind/LuaBindVM.h b/Runtime/Lua/LuaBind/LuaBindVM.h index a53a707..f3a9a30 100644 --- a/Runtime/Lua/LuaBind/LuaBindVM.h +++ b/Runtime/Lua/LuaBind/LuaBindVM.h @@ -30,6 +30,8 @@ namespace LuaBind void OpenLibs(); lua_State* GetMainThread(); + void SetCurThread(lua_State* cur); + lua_State* GetCurThread(); lua_State* CreateThread(); State GetMainState(); @@ -42,8 +44,9 @@ namespace LuaBind private: static std::map VMs; // 通过global_State索引虚拟机,为了方便 - global_State* mGlobalState; // 虚拟机的global_State,由当前虚拟机的所有线程共享,保存注册表、管理GC - lua_State* mMainThread; // 主线程 + global_State* mGlobalState; // 虚拟机的global_State,由当前虚拟机的所有线程共享,保存注册表、管理GC + lua_State* mMainThread; // 主线程 + lua_State* mCurThread; // 当前线程,并不是绝对准确的,在调用C函数时会设置,见LUA_BIND_STATE宏 RefTable mStrongRefTable; // 全局强引用表 RefTable mWeakRefTable; // 全局弱引用表 diff --git a/Runtime/Lua/LuaHelper.cpp b/Runtime/Lua/LuaHelper.cpp index 289f0f6..53bea3f 100644 --- a/Runtime/Lua/LuaHelper.cpp +++ b/Runtime/Lua/LuaHelper.cpp @@ -21,3 +21,8 @@ Vector2f State::GetValue < Vector2f >(int idx, const Vector2f value) v2.y = GetField(idx, 2, 0); return v2; } + +int LuaHelper::Call(const char* func, const char* params, ...) +{ + return 1; +} \ No newline at end of file diff --git a/Runtime/Lua/LuaHelper.h b/Runtime/Lua/LuaHelper.h index 5e9bf90..4ab0d6a 100644 --- a/Runtime/Lua/LuaHelper.h +++ b/Runtime/Lua/LuaHelper.h @@ -6,5 +6,8 @@ class LuaHelper { public: + static int Call(const char* func, const char* params, ...); }; + + -- cgit v1.1-26-g67d0