263 lines
6.3 KiB
C++
263 lines
6.3 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 <LuaBridge/Array.h>
|
|
#include <LuaBridge/Vector.h>
|
|
|
|
|
|
// Array 绑定
|
|
namespace luabridge {
|
|
|
|
//=================================================================================================
|
|
/**
|
|
* @brief Stack specialization for `dl::Array`.
|
|
*/
|
|
template <class T, std::size_t Size>
|
|
struct Stack<dl::Array<T, Size>>
|
|
{
|
|
using Type = dl::Array<T, Size>;
|
|
|
|
[[nodiscard]] static Result push(lua_State* L, const Type& array)
|
|
{
|
|
#if LUABRIDGE_SAFE_STACK_CHECKS
|
|
if (!lua_checkstack(L, 3))
|
|
return makeErrorCode(ErrorCode::LuaStackOverflow);
|
|
#endif
|
|
|
|
StackRestore stackRestore(L);
|
|
|
|
lua_createtable(L, static_cast<int>(Size), 0);
|
|
|
|
for (std::size_t i = 0; i < Size; ++i)
|
|
{
|
|
lua_pushinteger(L, static_cast<lua_Integer>(i + 1));
|
|
|
|
auto result = Stack<T>::push(L, array[i]);
|
|
if (!result)
|
|
return result;
|
|
|
|
lua_settable(L, -3);
|
|
}
|
|
|
|
stackRestore.reset();
|
|
return {};
|
|
}
|
|
|
|
[[nodiscard]] static TypeResult<Type> get(lua_State* L, int index)
|
|
{
|
|
if (!lua_istable(L, index))
|
|
return makeErrorCode(ErrorCode::InvalidTypeCast);
|
|
|
|
if (get_length(L, index) != Size)
|
|
return makeErrorCode(ErrorCode::InvalidTableSizeInCast);
|
|
|
|
const StackRestore stackRestore(L);
|
|
|
|
Type array;
|
|
|
|
int absIndex = lua_absindex(L, index);
|
|
lua_pushnil(L);
|
|
|
|
int arrayIndex = 0;
|
|
while (lua_next(L, absIndex) != 0)
|
|
{
|
|
auto item = Stack<T>::get(L, -1);
|
|
if (!item)
|
|
return makeErrorCode(ErrorCode::InvalidTypeCast);
|
|
|
|
array[arrayIndex++] = *item;
|
|
lua_pop(L, 1);
|
|
}
|
|
|
|
return array;
|
|
}
|
|
|
|
[[nodiscard]] static bool isInstance(lua_State* L, int index)
|
|
{
|
|
return lua_istable(L, index) && get_length(L, index) == Size;
|
|
}
|
|
};
|
|
|
|
} // namespace luabridge
|
|
|
|
|
|
// 枚举类绑定
|
|
|
|
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;
|
|
}
|
|
|
|
void lua_register_base(lua_State* L)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.addFunction("log", lua_log);
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.addFunction("delete", lua_delete);
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginNamespace("String")
|
|
.addFunction("ErasePath", String::ErasePath)
|
|
.addFunction("ReplaceExtension",
|
|
luabridge::overload<std::string_view, std::string_view>(String::ReplaceExtension))
|
|
.endNamespace();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginNamespace("Img")
|
|
.addVariable("REGEX_FORMAT", Img::REGEX_FORMAT)
|
|
.addFunction("ResizeImage",
|
|
luabridge::overload<const Image*,float>(Img::ResizeImage),
|
|
luabridge::overload<const Image*, const Size&>(Img::ResizeImage))
|
|
.endNamespace();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<Image>("Image")
|
|
.endClass();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.addFunction("createImage",
|
|
luabridge::overload<std::string_view>(&createImage),
|
|
luabridge::overload<const Size&, const Color&>(&createImage))
|
|
.addFunction("saveImage", &saveImage);
|
|
}
|
|
|
|
void lua_register_system(lua_State* L)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<ParamWindow>("ParamWindow")
|
|
.addConstructor<void (*)()>() // 忽略这个构造会出错……
|
|
.addProperty("title", &ParamWindow::_strTitle)
|
|
.endClass();
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<System>("System")
|
|
.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");
|
|
}
|
|
|
|
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("Rename", &File::Rename)
|
|
.addFunction("Copy", &File::Copy)
|
|
.addFunction("GetFilePath", &File::GetFilePath)
|
|
.addFunction("GetFileName", &File::GetFileName)
|
|
.endNamespace();
|
|
}
|
|
|
|
void lua_register_gfx(lua_State* L)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<Graphics>("Graphics")
|
|
.addFunction("Rectangle2D", &Graphics::Rectangle2D)
|
|
.endClass();
|
|
|
|
auto result = luabridge::push(L, g_gfx);
|
|
lua_setglobal(L, "g_gfx");
|
|
}
|
|
|
|
void lua_register_ui(lua_State* L)
|
|
{
|
|
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::CreatePanel)
|
|
.addFunction("CreateText", &Manager::CreateText)
|
|
.addFunction("CreateTextDefault", &Manager::CreateTextDefault)
|
|
.endClass();
|
|
|
|
auto result = luabridge::push(L, g_ui);
|
|
lua_setglobal(L, "g_ui");
|
|
}
|
|
|
|
void lua_register_factory(lua_State* L)
|
|
{
|
|
/*luabridge::getGlobalNamespace(L)
|
|
.beginClass<Factory>("Factory")
|
|
.addFunction("CreateText", &Factory::CreateText)
|
|
.endClass();
|
|
|
|
auto result = luabridge::push(L, g_factory);
|
|
lua_setglobal(L, "g_factory");*/
|
|
}
|
|
|
|
#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);
|
|
|
|
#if DL_PLATFORM == DL_PLATFORM_WIN32
|
|
lua_register_win32(L);
|
|
#endif
|
|
}
|
|
|
|
} |