From 142c075ca3d8e8fe73e494e42ee9c19ad571613f Mon Sep 17 00:00:00 2001 From: Lveyou <1339484752@qq.com> Date: Thu, 9 Jul 2026 18:08:26 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E5=A2=9E=E5=8A=A0=E8=A3=81=E5=88=87?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dl/base/dl_color.h | 2 +- dl/render/dl_image.cpp | 9 ++ dl/render/dl_image.h | 4 +- dl/script/dl_lua_register.cpp | 156 +++++++++++++++++++++++++--------- dl/script/dl_lua_register.h | 1 + dl/tool/dl_tool_ctrl_2d.h | 1 + 6 files changed, 129 insertions(+), 44 deletions(-) diff --git a/dl/base/dl_color.h b/dl/base/dl_color.h index 3e0f674c..03c04bb4 100644 --- a/dl/base/dl_color.h +++ b/dl/base/dl_color.h @@ -318,7 +318,7 @@ namespace ColorDef constexpr Color PURPLE = { 1.0f, 0.0f, 1.0f, 1.0f }; constexpr Color CYAN = { 0.0f, 1.0f, 1.0f, 1.0f }; - constexpr Color ORAGEN = { 1.0f, 0.5f, 0.0f, 1.0f }; + constexpr Color ORANGE = { 1.0f, 0.5f, 0.0f, 1.0f }; constexpr Color GREEN_DARK = { 0.0f, 100.0f / 255.0f, 0.0f, 1.0f }; diff --git a/dl/render/dl_image.cpp b/dl/render/dl_image.cpp index 3a7d0e3f..14ab0d79 100644 --- a/dl/render/dl_image.cpp +++ b/dl/render/dl_image.cpp @@ -141,6 +141,15 @@ void CalcLeastRect(const Image* img, RectU& rect) { using namespace Align; assert(img); + + if (Every::Zero(rect)) + rect = createRectU(img->GetSize()); + + if (!Math::IsRectValid(rect)) + { + log_err("不是有效的矩形:{}", rect); + return; + } //如果此行、或此列全是透明像素则移动 //移动p1.y while (GetTop(rect) < GetButtom(rect)) diff --git a/dl/render/dl_image.h b/dl/render/dl_image.h index 71511f19..b3c54838 100644 --- a/dl/render/dl_image.h +++ b/dl/render/dl_image.h @@ -94,7 +94,9 @@ bool Save(const Image* img, std::string_view path_name); /** * @brief 计算返回最小包围盒(去除边缘的全透明像素) -* @param[in,out] rect 传入包围盒,返回缩小后的,可能返回空矩形(全透明) +* @param[in] img 图片 +* @param[in,out] rect 包围盒,返回缩小后的,可能返回空矩形(全透明) +* @note 传入空矩阵则使用图片大小 */ void CalcLeastRect(const Image* img, RectU& rect); diff --git a/dl/script/dl_lua_register.cpp b/dl/script/dl_lua_register.cpp index 28f5067f..8aedf75a 100644 --- a/dl/script/dl_lua_register.cpp +++ b/dl/script/dl_lua_register.cpp @@ -144,6 +144,21 @@ namespace dl return {}; } + template + static Array lua_Array_op_add(const Array& a, const Array& b) + { + return a + b; + } + template + static Array lua_Array_op_sub(const Array& a, const Array& b) + { + return a - b; + } + template + static Array lua_Array_op_nag(const Array& a) + { + return -a; + } void lua_register_base(lua_State* L) { @@ -161,51 +176,68 @@ namespace dl luabridge::getGlobalNamespace(L) .beginClass("Position") + .addConstructor() .addPropertyReadWrite("x", &Position::x) .addPropertyReadWrite("y", &Position::y); luabridge::getGlobalNamespace(L) .beginClass("Position4") + .addConstructor() .addPropertyReadWrite("x", &Position4::x) .addPropertyReadWrite("y", &Position4::y) .addPropertyReadWrite("z", &Position4::z) .addPropertyReadWrite("w", &Position4::w); + luabridge::getGlobalNamespace(L) + .beginClass("Point") + .addConstructor() + .addFunction("__add", lua_Array_op_add) + .addFunction("__sub", lua_Array_op_sub) + .addFunction("__unm", lua_Array_op_nag) + .addPropertyReadWrite("x", &Point::x) + .addPropertyReadWrite("y", &Point::y); + + luabridge::getGlobalNamespace(L) + .beginClass("Size") + .addConstructor() + .addPropertyReadWrite("w", &Size::w) + .addPropertyReadWrite("h", &Size::h); + + luabridge::getGlobalNamespace(L) + .beginClass("PointU") + .addConstructor() + .addPropertyReadWrite("x", &PointU::x) + .addPropertyReadWrite("y", &PointU::y); + + luabridge::getGlobalNamespace(L) + .beginClass("RectU") + .addConstructor() + .addPropertyReadWrite("lt", &RectU::lt) + .addPropertyReadWrite("rt", &RectU::rt) + .addPropertyReadWrite("rb", &RectU::rb) + .addPropertyReadWrite("lb", &RectU::lb); + + luabridge::getGlobalNamespace(L) + .beginNamespace("Align") + .addFunction("GetSize", Align::GetSize) + .addFunction("GetLT", Align::GetLT) + .endNamespace(); + + luabridge::getGlobalNamespace(L) + .addFunction("toPoint", toPoint); + + luabridge::getGlobalNamespace(L) .beginNamespace("String") .addFunction("ErasePath", String::ErasePath) .addFunction("GetFileStem", String::GetFileStem) .addFunction("toPosition2", String::toPosition2) + .addFunction("toPoint2", String::toPoint2) .addFunction("ReplaceExtension", luabridge::overload(String::ReplaceExtension)) .addFunction("EraseFilename", luabridge::overload(String::EraseFilename)) .endNamespace(); - - luabridge::getGlobalNamespace(L) - .beginNamespace("Img") - .addVariable("REGEX_FORMAT", Img::REGEX_FORMAT) - //.addFunction("Resize", - // luabridge::overload(Img::Resize), - // luabridge::overload(Img::Resize)) - .addFunction("SetAlphaZero", - luabridge::overload(Img::SetAlphaZero), - luabridge::overload(Img::SetAlphaZero)) - /*.addFunction("Create", - luabridge::overload(Img::Create), - luabridge::overload(Img::Create))*/ - .addFunction("Save", Img::Save) - .endNamespace(); - - luabridge::getGlobalNamespace(L) - .beginClass("Image") - .endClass(); - - luabridge::getGlobalNamespace(L) - .beginNamespace("ColorDef") - .addVariable("NONE", ColorDef::NONE) - .addVariable("RED", ColorDef::RED) - .endNamespace(); } void lua_register_system(lua_State* L) @@ -321,23 +353,6 @@ namespace dl auto result = luabridge::push(L, g_factory); lua_setglobal(L, "g_factory"); - luabridge::getGlobalNamespace(L) - .beginNamespace("Img") - .addFunction("Create", - luabridge::overload(&Img::Create), - luabridge::overload(&Img::Create)) - .addFunction("CreateSvg", &Img::CreateSvg) - .addFunction("CreateWebp", - luabridge::overload(&Img::CreateWebp), - luabridge::overload(&Img::CreateWebp)) - .addFunction("Save", &Img::Save) - .addFunction("SetAlphaZero", - luabridge::overload(&Img::SetAlphaZero), - luabridge::overload(&Img::SetAlphaZero)) - .addFunction("Resize", - luabridge::overload(&Img::Resize), - luabridge::overload(&Img::Resize)) - .endNamespace(); } void lua_register_sprite(lua_State* L) @@ -550,6 +565,62 @@ namespace dl .endClass(); } + void lua_register_image(lua_State* L) + { + luabridge::getGlobalNamespace(L) + .beginNamespace("Img") + .addVariable("REGEX_FORMAT", Img::REGEX_FORMAT) + .addFunction("Create", + luabridge::overload(&Img::Create), + luabridge::overload(&Img::Create)) + .addFunction("CreateSvg", &Img::CreateSvg) + .addFunction("CreateWebp", + luabridge::overload(&Img::CreateWebp), + luabridge::overload(&Img::CreateWebp)) + .addFunction("Save", &Img::Save) + .addFunction("CalcLeastRect", &Img::CalcLeastRect) + .addFunction("SetAlphaZero", + luabridge::overload(&Img::SetAlphaZero), + luabridge::overload(&Img::SetAlphaZero)) + .addFunction("Resize", + luabridge::overload(&Img::Resize), + luabridge::overload(&Img::Resize)) + .addFunction("AddRect", Img::AddRect) + .addFunction("SetAlphaZero", + luabridge::overload(Img::SetAlphaZero), + luabridge::overload(Img::SetAlphaZero)) + .addFunction("Save", Img::Save) + .endNamespace(); + luabridge::getGlobalNamespace(L) + .beginClass("Image") + .endClass(); + + luabridge::getGlobalNamespace(L) + .beginNamespace("ColorDef") + .addVariable("NONE", ColorDef::NONE) + .addVariable("WHITE", ColorDef::WHITE) + .addVariable("BLACK", ColorDef::BLACK) + .addVariable("RED", ColorDef::RED) + .addVariable("GREEN", ColorDef::GREEN) + .addVariable("BLUE", ColorDef::BLUE) + .addVariable("YELLOW", ColorDef::YELLOW) + .addVariable("PURPLE", ColorDef::PURPLE) + .addVariable("CYAN", ColorDef::CYAN) + .addVariable("ORANGE", ColorDef::ORANGE) + .addVariable("GREEN_DARK", ColorDef::GREEN_DARK) + .addVariable("DISABLE", ColorDef::DISABLE) + .addVariable("TEXT_BG", ColorDef::TEXT_BG) + .addVariable("FACTOR_GRAY", ColorDef::FACTOR_GRAY) + .addVariable("GRAY", ColorDef::GRAY) + .addVariable("GRAY_LIGHT", ColorDef::GRAY_LIGHT) + .addVariable("GRAY_DARK", ColorDef::GRAY_DARK) + .addVariable("CLEAR_SCREEN", ColorDef::CLEAR_SCREEN) + .addVariable("AXIS_X", ColorDef::AXIS_X) + .addVariable("AXIS_Y", ColorDef::AXIS_Y) + .addVariable("AXIS_Z", ColorDef::AXIS_Z) + .endNamespace(); + } + #if DL_PLATFORM == DL_PLATFORM_WIN32 #define WIN32_LEAN_AND_MEAN #include @@ -574,6 +645,7 @@ namespace dl lua_register_memory(L); lua_register_other(L); lua_register_json(L); + lua_register_image(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 117fef2c..386b25b1 100644 --- a/dl/script/dl_lua_register.h +++ b/dl/script/dl_lua_register.h @@ -24,6 +24,7 @@ namespace dl void lua_register_memory(lua_State* L); void lua_register_json(lua_State* L); void lua_register_other(lua_State* L); + void lua_register_image(lua_State* L); void lua_register_all(lua_State* L); } \ No newline at end of file diff --git a/dl/tool/dl_tool_ctrl_2d.h b/dl/tool/dl_tool_ctrl_2d.h index 1a42e0f1..8800f9a5 100644 --- a/dl/tool/dl_tool_ctrl_2d.h +++ b/dl/tool/dl_tool_ctrl_2d.h @@ -85,6 +85,7 @@ public: _drag._key = _keyDrag; _drag.Update(coor, true); } + // 位置限制 } private: Cpt::Drag _drag;