diff --git a/dl/script/dl_lua.cpp b/dl/script/dl_lua.cpp index 0c88e295..46d9d56d 100644 --- a/dl/script/dl_lua.cpp +++ b/dl/script/dl_lua.cpp @@ -22,20 +22,36 @@ namespace dl lua_close(L); } - bool LuaScript::DoFile(std::string_view path_name) + bool LuaScript::DoString(std::string_view str) { - Buffer buf = File::GetBuffer(path_name); - - int ret = luaL_loadbuffer(L, (const char*)buf.data(), buf.size(), path_name.data()); + int ret = luaL_loadstring(L, str.data()); if (ret) { - log_err("lua 执行(加载)失败:[{}]:{}", path_name, lua_tostring(L, -1)); + log_err("lua 解析失败[{}]:\n{}", lua_tostring(L, -1), str); return false; } ret = lua_pcall(L, 0, 0, 0); if (ret) { - log_err("lua 执行失败:[{}]:{}", path_name, lua_tostring(L, -1)); + log_err("lua 执行失败[{}]:\n{}", lua_tostring(L, -1), str); + return false; + } + return true; + } + + bool LuaScript::DoFile(std::string_view path_name) + { + Buffer buf = File::GetBuffer(path_name); + int ret = luaL_loadbuffer(L, (const char*)buf.data(), buf.size(), path_name.data()); + if (ret) + { + log_err("lua 解析(加载)失败[{}]:{}", path_name, lua_tostring(L, -1)); + return false; + } + ret = lua_pcall(L, 0, 0, 0); + if (ret) + { + log_err("lua 执行失败[{}]:{}", path_name, lua_tostring(L, -1)); return false; } return true; diff --git a/dl/script/dl_lua.h b/dl/script/dl_lua.h index 309957cf..a5f91a04 100644 --- a/dl/script/dl_lua.h +++ b/dl/script/dl_lua.h @@ -44,6 +44,7 @@ public: void SetPathRoot(std::string_view str) { _pathRoot = std::string{ str }; } + bool DoString(std::string_view str); bool DoFile(std::string_view path_name); bool ExcuteFunction(std::string_view name) diff --git a/dl/script/dl_lua_register.cpp b/dl/script/dl_lua_register.cpp index f7c1c13e..00081a89 100644 --- a/dl/script/dl_lua_register.cpp +++ b/dl/script/dl_lua_register.cpp @@ -172,76 +172,100 @@ namespace dl #endif } - /*luabridge::LuaRef g_spr_proxy{ nullptr }; - - - void lua_proxy_call(gcSprite& spr, - luabridge::LuaRef p1, - luabridge::LuaRef p2, - luabridge::LuaRef p3) - { - auto s1 = p1.tostring(); - auto s2 = p2.tostring(); - auto s3 = p3.tostring(); - g_spr_proxy(spr.GetPointer(), p1, p2, p3); - }*/ - void lua_register_factory(lua_State* L) { luabridge::getGlobalNamespace(L) .beginClass("Factory") .addFunction("CreateText", &Factory::CreateText) + .addFunction("CreateTextDefault", &Factory::CreateTextDefault) .addFunction("CreateSprite", luabridge::overload(&Factory::CreateSprite), luabridge::overload(&Factory::CreateSprite)) .addFunction("SetPath", &Factory::SetPath) .endClass(); + auto result = luabridge::push(L, g_factory); + lua_setglobal(L, "g_factory"); + } + + void lua_register_sprite(lua_State* L) + { luabridge::getGlobalNamespace(L) .beginClass("Sprite") .addFunction("Render", &Sprite::Render) - .addFunction("SetColor", + .addFunction("SetColor", luabridge::overload(&Sprite::SetColor), luabridge::overload(&Sprite::SetColor)) .endClass(); - //luabridge::getGlobalNamespace(L) - // .addFunction("lua_proxy_call", lua_proxy_call); luabridge::getGlobalNamespace(L) .beginClass("gcSprite") - .addIndexMetaMethod([](gcSprite& self, const luabridge::LuaRef& key, lua_State* L) - { - /*luabridge::LuaRef realObj = lua_ref(L, self.GetPointer()); - luabridge::LuaRef realMethod = realObj[methodName]; - self = spr.GetPointer();*/ - if (!luabridge::push(L, self.GetPointer())) - lua_pushnil(L); - return luabridge::LuaRef::fromStack(L); - }) - //.addIndexMetaMethod([](gcSprite& self, const luabridge::LuaRef& key, lua_State* L) -> luabridge::LuaRef - // { - // if (!self) - // return luabridge::LuaRef(L, luabridge::LuaNil()); - // luabridge::LuaRef ref_class = luabridge::getGlobal(L, "Sprite"); - // auto is = ref_class.tostring(); - // auto is1 = key.tostring(); - // luabridge::LuaRef func = ref_class[key]; - // //lua_remove(L, 1); - // //luabridge::push(L, self.GetPointer()); - // //// 把当前所有参数(已替换)传给原函数 - // //lua_insert(L, 1) - // //func(self.GetPointer()); - // g_spr_proxy = func; - // luabridge::LuaRef f = luabridge::getGlobal(L, "lua_proxy_call"); - // auto is2 = f.tostring(); - // return luabridge::LuaRef(L, f); - // }) .addFunction("GetPointer", &gcSprite::GetPointer) + .addIndexMetaMethod([](gcSprite& self, const luabridge::LuaRef& key, lua_State* L) -> luabridge::LuaRef + { + if (key.tostring() == "GetPointer") + {// 不清楚为何存在 GetPointer,还是会触发元函数 + luabridge::LuaRef ref_class = luabridge::getGlobal(L, "gcSprite"); + luabridge::LuaRef ref_func = ref_class[key]; + return ref_func; + } + // 获取到原Sprite函数 + luabridge::LuaRef ref_class = luabridge::getGlobal(L, "Sprite"); + luabridge::LuaRef ref_func = ref_class[key]; + luabridge::setGlobal(L, ref_func, "__dl_sprite_func"); + // 调用代理函数 + luabridge::LuaRef ref_proxy = luabridge::getGlobal(L, "__dl_sprite_proxy"); + return ref_proxy; + }) .endClass(); - auto result = luabridge::push(L, g_factory); - lua_setglobal(L, "g_factory"); + // 注册sprite代理函数 + g_lua->DoString(R"( +__dl_sprite_func = nil +__dl_sprite_proxy = function(obj, ...) + __dl_sprite_func(obj:GetPointer(), ...) +end +)"); + } + + void lua_register_text(lua_State* L) + { + luabridge::getGlobalNamespace(L) + .beginClass("Text") + .addFunction("Render", &Text::Render) + .addFunction("SetString", &Text::SetString) + .endClass(); + + + luabridge::getGlobalNamespace(L) + .beginClass("gcText") + .addFunction("GetPointer", &gcText::GetPointer) + .addIndexMetaMethod([](gcText& self, const luabridge::LuaRef& key, lua_State* L) -> luabridge::LuaRef + { + if (key.tostring() == "GetPointer") + {// 不清楚为何存在 GetPointer,还是会触发元函数 + luabridge::LuaRef ref_class = luabridge::getGlobal(L, "gcText"); + luabridge::LuaRef ref_func = ref_class[key]; + return ref_func; + } + // 获取到原Text函数 + luabridge::LuaRef ref_class = luabridge::getGlobal(L, "Text"); + luabridge::LuaRef ref_func = ref_class[key]; + luabridge::setGlobal(L, ref_func, "__dl_text_func"); + // 调用代理函数 + luabridge::LuaRef ref_proxy = luabridge::getGlobal(L, "__dl_text_proxy"); + return ref_proxy; + }) + .endClass(); + + // 注册sprite代理函数 + g_lua->DoString(R"( +__dl_text_func = nil +__dl_text_proxy = function(obj, ...) + __dl_text_func(obj:GetPointer(), ...) +end +)"); } #if DL_PLATFORM == DL_PLATFORM_WIN32 @@ -263,6 +287,8 @@ namespace dl lua_register_gfx(L); lua_register_ui(L); lua_register_factory(L); + lua_register_sprite(L); + lua_register_text(L); #if DL_PLATFORM == DL_PLATFORM_WIN32 lua_register_win32(L); diff --git a/dl/script/dl_lua_register.h b/dl/script/dl_lua_register.h index 6e05ea03..04a57cec 100644 --- a/dl/script/dl_lua_register.h +++ b/dl/script/dl_lua_register.h @@ -19,6 +19,8 @@ namespace dl void lua_register_gfx(lua_State* L); void lua_register_ui(lua_State* L); void lua_register_factory(lua_State* L); + void lua_register_sprite(lua_State* L); + void lua_register_text(lua_State* L); void lua_register_all(lua_State* L); } \ No newline at end of file diff --git a/project/dl_console/res/窗口示例.lua b/project/dl_console/res/窗口示例.lua index 1ee056f7..7d03be23 100644 --- a/project/dl_console/res/窗口示例.lua +++ b/project/dl_console/res/窗口示例.lua @@ -1,15 +1,20 @@ local spr +local txt function Init() spr = g_factory:CreateSprite("apple", 0) + txt = g_factory:CreateTextDefault() + txt:SetString("文本绘制测试") end function Update() -- log("额 hello dl! 22") - g_gfx:Rectangle2D({100, 100}, {500, 500}) + g_gfx:Rectangle2D({50, 100}, {950, 1000}) - --spr:SetColor(ColorDef.NONE, 0) + spr:SetColor(ColorDef.NONE, 0) spr:Render() + + txt:Render() end log("xxxx")