298 lines
8.5 KiB
C++
298 lines
8.5 KiB
C++
#include "dl_lua_register.h"
|
|
|
|
#include "dl_io.h"
|
|
#include "render/dl_graphics.h"
|
|
#include "system/dl_system.h"
|
|
#include "script/dl_lua.h"
|
|
#include "ui/dl_ui_manager.h"
|
|
#include "render/dl_image.h"
|
|
#include "script/dl_lua_register_container.h"
|
|
|
|
// 枚举类绑定
|
|
template <>
|
|
struct luabridge::Stack<dl::File::CopyMode> : luabridge::Enum<dl::File::CopyMode,
|
|
dl::File::CopyMode::NONE,
|
|
dl::File::CopyMode::OVERRIDE,
|
|
dl::File::CopyMode::SKIP>
|
|
{
|
|
};
|
|
|
|
namespace dl
|
|
{
|
|
void lua_log(std::string_view str)
|
|
{
|
|
log(str);
|
|
}
|
|
|
|
void lua_delete(Image* img)
|
|
{
|
|
delete img;
|
|
}
|
|
|
|
std::string lua_to_string(const Position2& pos)
|
|
{
|
|
return to_string(pos);
|
|
}
|
|
|
|
void lua_register_base(lua_State* L)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.addFunction("log", lua_log)
|
|
.addFunction("delete", lua_delete)
|
|
.addFunction("to_string",
|
|
luabridge::overload<const Position2&>(lua_to_string));
|
|
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginNamespace("String")
|
|
.addFunction("ErasePath", String::ErasePath)
|
|
.addFunction("GetFileStem", String::GetFileStem)
|
|
.addFunction("toPosition2", String::toPosition2)
|
|
.addFunction("ReplaceExtension",
|
|
luabridge::overload<std::string_view, std::string_view>(String::ReplaceExtension))
|
|
.addFunction("EraseFilename",
|
|
luabridge::overload<std::string_view>(String::EraseFilename))
|
|
.endNamespace();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginNamespace("Img")
|
|
.addVariable("REGEX_FORMAT", Img::REGEX_FORMAT)
|
|
.addFunction("Resize",
|
|
luabridge::overload<const Image*, float>(Img::Resize),
|
|
luabridge::overload<const Image*, const Size&>(Img::Resize))
|
|
.addFunction("SetAlphaZero",
|
|
luabridge::overload<Image*>(Img::SetAlphaZero),
|
|
luabridge::overload<Image*, uint8_t, uint32_t>(Img::SetAlphaZero))
|
|
.addFunction("Create",
|
|
luabridge::overload<std::string_view>(Img::Create),
|
|
luabridge::overload<const Size&, const Color&>(Img::Create))
|
|
.addFunction("Save", Img::Save)
|
|
.endNamespace();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<Image>("Image")
|
|
.endClass();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginNamespace("ColorDef")
|
|
.addVariable("NONE", ColorDef::NONE)
|
|
.addVariable("RED", ColorDef::RED)
|
|
.endNamespace();
|
|
}
|
|
|
|
void lua_register_system(lua_State* L)
|
|
{
|
|
#ifndef DL_DISABLE_RENDER
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<ParamWindow>("ParamWindow")
|
|
.addConstructor<void (*)()>() // 忽略这个构造会出错……
|
|
.addProperty("title", &ParamWindow::_strTitle)
|
|
.endClass();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<System>("System")
|
|
.addFunction("SetFuncInit", &System::lua_SetFuncInit)
|
|
.addFunction("SetFuncRun", &System::lua_SetFuncUpdate)
|
|
.addFunction("Start", (void(System::*)(const ParamWindow&))&System::Start)
|
|
.endClass();
|
|
|
|
auto result = luabridge::push(L, g_system);
|
|
lua_setglobal(L, "g_system");
|
|
#endif
|
|
}
|
|
|
|
void lua_register_file(lua_State* L)
|
|
{
|
|
using namespace File::Lua;
|
|
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginNamespace("g_file")
|
|
.beginNamespace("CopyMode")
|
|
.addVariable("NONE", File::CopyMode::NONE)
|
|
.addVariable("OVERRIDE", File::CopyMode::OVERRIDE)
|
|
.addVariable("SKIP",File::CopyMode::SKIP)
|
|
.endNamespace()
|
|
.endNamespace();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginNamespace("g_file")
|
|
.addFunction("GetPathFileAll", lua_GetPathFileAll_3, lua_GetPathFileAll_2, lua_GetPathFileAll_1)
|
|
.addFunction("GetAbsolute", File::GetAbsolute)
|
|
.addFunction("GetRelative", File::GetRelative)
|
|
.addFunction("CreateFolder", File::CreateFolder)
|
|
.addFunction("DeleteContent", File::DeleteContent)
|
|
.addFunction("Rename", File::Rename)
|
|
.addFunction("Copy", File::Copy)
|
|
.addFunction("GetFilePath", File::GetFilePath)
|
|
.addFunction("GetFileName", File::GetFileName)
|
|
.addFunction("GetString", File::GetString)
|
|
.addFunction("SaveString",
|
|
luabridge::overload<std::string_view, std::string_view>(File::SaveString))
|
|
.endNamespace();
|
|
}
|
|
|
|
void lua_register_gfx(lua_State* L)
|
|
{
|
|
#ifndef DL_DISABLE_RENDER
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<Graphics>("Graphics")
|
|
.addFunction("Rectangle2D", &Graphics::Rectangle2D)
|
|
.endClass();
|
|
|
|
auto result = luabridge::push(L, g_gfx);
|
|
lua_setglobal(L, "g_gfx");
|
|
#endif
|
|
}
|
|
|
|
void lua_register_ui(lua_State* L)
|
|
{
|
|
#ifndef DL_DISABLE_RENDER
|
|
using namespace UI;
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<Panel>("UIPanel")
|
|
.addFunction("Add", (void(Panel::*)(UI::Text*))&Panel::Add)
|
|
.endClass();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<UI::Text>("UIText")
|
|
.addFunction("SetString", &UI::Text::SetString)
|
|
.addFunction("SetLang", &UI::Text::SetLang)
|
|
.endClass();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<Manager>("UIManager")
|
|
.addFunction("CreatePanel", (Panel * (Manager::*)())&Manager::lua_CreatePanel)
|
|
.addFunction("CreateText", &Manager::CreateText)
|
|
.addFunction("CreateTextDefault", &Manager::CreateTextDefault)
|
|
.endClass();
|
|
|
|
auto result = luabridge::push(L, g_ui);
|
|
lua_setglobal(L, "g_ui");
|
|
#endif
|
|
}
|
|
|
|
void lua_register_factory(lua_State* L)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<Factory>("Factory")
|
|
.addFunction("CreateText", &Factory::CreateText)
|
|
.addFunction("CreateTextDefault", &Factory::CreateTextDefault)
|
|
.addFunction("CreateSprite",
|
|
luabridge::overload<std::string_view, size_t>(&Factory::CreateSprite),
|
|
luabridge::overload<const SpriteID&>(&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>("Sprite")
|
|
.addFunction("Render", &Sprite::Render)
|
|
.addFunction("SetColor",
|
|
luabridge::overload<const Color&>(&Sprite::SetColor),
|
|
luabridge::overload<const Color&, size_t>(&Sprite::SetColor))
|
|
.endClass();
|
|
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<gcSprite>("gcSprite")
|
|
.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();
|
|
|
|
// 注册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>("Text")
|
|
.addFunction("Render", &Text::Render)
|
|
.addFunction("SetString", &Text::SetString)
|
|
.endClass();
|
|
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<gcText>("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
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#include <Windows.h>
|
|
void lua_register_win32(lua_State* L)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.addFunction("system", &system);
|
|
}
|
|
|
|
#endif
|
|
|
|
void lua_register_all(lua_State* L)
|
|
{
|
|
lua_register_base(L);
|
|
lua_register_system(L);
|
|
lua_register_file(L);
|
|
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);
|
|
#endif
|
|
}
|
|
|
|
} |