From 398a5655f42378176a1e596af6d399a180ffb733 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Mon, 3 Jul 2023 08:51:42 +1000 Subject: Add Drawable <-> Lua conversion to LuaGameConverters --- source/game/scripting/StarLuaGameConverters.cpp | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'source/game/scripting/StarLuaGameConverters.cpp') diff --git a/source/game/scripting/StarLuaGameConverters.cpp b/source/game/scripting/StarLuaGameConverters.cpp index e2fd516..070048e 100644 --- a/source/game/scripting/StarLuaGameConverters.cpp +++ b/source/game/scripting/StarLuaGameConverters.cpp @@ -417,6 +417,60 @@ Maybe LuaConverter::to(LuaEngine& engine, LuaValue con return {}; } +LuaValue LuaConverter::from(LuaEngine& engine, Drawable const& v) { + auto table = engine.createTable(); + if (auto line = v.part.ptr()) { + table.set("line", line->line); + table.set("width", line->width); + } else if (auto poly = v.part.ptr()) { + table.set("poly", poly->poly); + } else if (auto image = v.part.ptr()) { + table.set("image", AssetPath::join(image->image)); + table.set("transformation", image->transformation); + } + + table.set("position", v.position); + table.set("color", v.color); + table.set("fullbright", v.fullbright); + + return table; +} + +Maybe LuaConverter::to(LuaEngine& engine, LuaValue const& v) { + if (auto table = v.ptr()) { + Maybe result; + result.emplace(); + Drawable& drawable = result.get(); + + Color color = table->get>("color").value(Color::White); + + if (auto line = table->get>("line")) + drawable = Drawable::makeLine(line.take(), table->get("width"), color); + else if (auto poly = table->get>("poly")) + drawable = Drawable::makePoly(poly.take(), color); + else if (auto image = table->get>("image")) + drawable = Drawable::makeImage(image.take(), 1.0f, table->get>("centered").value(true), Vec2F(), color); + else + return {}; // throw LuaAnimationComponentException("Drawable table must have 'line', 'poly', or 'image'"); + + if (auto transformation = table->get>("transformation")) + drawable.transform(*transformation); + if (auto rotation = table->get>("rotation")) + drawable.rotate(*rotation); + if (table->get("mirrored")) + drawable.scale(Vec2F(-1, 1)); + if (auto scale = table->get>("scale")) + drawable.scale(*scale); + if (auto position = table->get>("position")) + drawable.translate(*position); + + drawable.fullbright = table->get("fullbright"); + + return result; + } + return {}; +} + LuaValue LuaConverter::from(LuaEngine& engine, Collection const& c) { auto table = engine.createTable(); table.set("name", c.name); -- cgit v1.2.3