Add Drawable <-> Lua conversion to LuaGameConverters

This commit is contained in:
Kae 2023-07-03 08:51:42 +10:00
parent d7ba113688
commit 398a5655f4
8 changed files with 74 additions and 37 deletions

View File

@ -464,10 +464,10 @@ LuaString LuaEngine::createString(char const* str) {
return LuaString(LuaDetail::LuaHandle(RefPtr<LuaEngine>(this), popHandle(m_state))); return LuaString(LuaDetail::LuaHandle(RefPtr<LuaEngine>(this), popHandle(m_state)));
} }
LuaTable LuaEngine::createTable() { LuaTable LuaEngine::createTable(int narr, int nrec) {
lua_checkstack(m_state, 1); lua_checkstack(m_state, 1);
lua_newtable(m_state); lua_createtable(m_state, narr, nrec);
return LuaTable(LuaDetail::LuaHandle(RefPtr<LuaEngine>(this), popHandle(m_state))); return LuaTable(LuaDetail::LuaHandle(RefPtr<LuaEngine>(this), popHandle(m_state)));
} }

View File

@ -505,7 +505,7 @@ public:
LuaString createString(String const& str); LuaString createString(String const& str);
LuaString createString(char const* str); LuaString createString(char const* str);
LuaTable createTable(); LuaTable createTable(int narr = 0, int nrec = 0);
template <typename Container> template <typename Container>
LuaTable createTable(Container const& map); LuaTable createTable(Container const& map);
@ -1890,7 +1890,7 @@ T LuaEngine::luaTo(LuaValue const& v) {
template <typename Container> template <typename Container>
LuaTable LuaEngine::createTable(Container const& map) { LuaTable LuaEngine::createTable(Container const& map) {
auto table = createTable(); auto table = createTable(0, map.size());
for (auto const& p : map) for (auto const& p : map)
table.set(p.first, p.second); table.set(p.first, p.second);
return table; return table;
@ -1898,7 +1898,7 @@ LuaTable LuaEngine::createTable(Container const& map) {
template <typename Container> template <typename Container>
LuaTable LuaEngine::createArrayTable(Container const& array) { LuaTable LuaEngine::createArrayTable(Container const& array) {
auto table = createTable(); auto table = createTable(array.size(), 0);
int i = 1; int i = 1;
for (auto const& elem : array) { for (auto const& elem : array) {
table.set(LuaInt(i), elem); table.set(LuaInt(i), elem);

View File

@ -63,10 +63,10 @@ struct LuaConverter<Vector<T, N>> {
template <typename T> template <typename T>
struct LuaConverter<Matrix3<T>> { struct LuaConverter<Matrix3<T>> {
static LuaValue from(LuaEngine& engine, Matrix3<T> const& m) { static LuaValue from(LuaEngine& engine, Matrix3<T> const& m) {
auto table = engine.createTable(); auto table = engine.createTable(3, 0);
table.set(1, luaFrom(engine, m.row1)); table.set(1, m[0]);
table.set(2, luaFrom(engine, m.row2)); table.set(2, m[1]);
table.set(3, luaFrom(engine, m.row3)); table.set(3, m[2]);
return table; return table;
} }

View File

@ -212,7 +212,6 @@ public:
template <size_t P = N> template <size_t P = N>
Enable4DOrHigher<P> setW(T const& t); Enable4DOrHigher<P> setW(T const& t);
private:
using Base::size; using Base::size;
using Base::empty; using Base::empty;
}; };

View File

@ -95,7 +95,8 @@ Drawable Drawable::makeImage(AssetPath image, float pixelSize, bool centered, Ve
transformation.translate(-imageSize / 2); transformation.translate(-imageSize / 2);
} }
transformation.scale(pixelSize); if (pixelSize != 1.0f)
transformation.scale(pixelSize);
drawable.part = ImagePart{move(image), move(transformation)}; drawable.part = ImagePart{move(image), move(transformation)};
drawable.position = position; drawable.position = position;

View File

@ -61,36 +61,12 @@ LuaAnimationComponent<Base>::LuaAnimationComponent() {
animationCallbacks.registerCallback("clearDrawables", [this]() { animationCallbacks.registerCallback("clearDrawables", [this]() {
m_drawables.clear(); m_drawables.clear();
}); });
animationCallbacks.registerCallback("addDrawable", [this](LuaTable const& drawableTable, Maybe<String> renderLayerName) { animationCallbacks.registerCallback("addDrawable", [this](Drawable drawable, Maybe<String> renderLayerName) {
Maybe<EntityRenderLayer> renderLayer; Maybe<EntityRenderLayer> renderLayer;
if (renderLayerName) if (renderLayerName)
renderLayer = parseRenderLayer(*renderLayerName); renderLayer = parseRenderLayer(*renderLayerName);
Color color = drawableTable.get<Maybe<Color>>("color").value(Color::White); drawable.scale(1.0f / TilePixels);
Drawable drawable;
if (auto line = drawableTable.get<Maybe<Line2F>>("line"))
drawable = Drawable::makeLine(line.take(), drawableTable.get<float>("width"), color);
else if (auto poly = drawableTable.get<Maybe<PolyF>>("poly"))
drawable = Drawable::makePoly(poly.take(), color);
else if (auto image = drawableTable.get<Maybe<String>>("image"))
drawable = Drawable::makeImage(image.take(), 1.0f / TilePixels, drawableTable.get<Maybe<bool>>("centered").value(true), Vec2F(), color);
else
throw LuaAnimationComponentException("Drawable table must have 'line', 'poly', or 'image'");
if (auto transformation = drawableTable.get<Maybe<Mat3F>>("transformation"))
drawable.transform(*transformation);
if (auto rotation = drawableTable.get<Maybe<float>>("rotation"))
drawable.rotate(*rotation);
if (drawableTable.get<bool>("mirrored"))
drawable.scale(Vec2F(-1, 1));
if (auto scale = drawableTable.get<Maybe<float>>("scale"))
drawable.scale(*scale);
if (auto position = drawableTable.get<Maybe<Vec2F>>("position"))
drawable.translate(*position);
drawable.fullbright = drawableTable.get<bool>("fullbright");
m_drawables.append({move(drawable), renderLayer}); m_drawables.append({move(drawable), renderLayer});
}); });
animationCallbacks.registerCallback("clearLightSources", [this]() { animationCallbacks.registerCallback("clearLightSources", [this]() {

View File

@ -417,6 +417,60 @@ Maybe<LiquidLevel> LuaConverter<LiquidLevel>::to(LuaEngine& engine, LuaValue con
return {}; return {};
} }
LuaValue LuaConverter<Drawable>::from(LuaEngine& engine, Drawable const& v) {
auto table = engine.createTable();
if (auto line = v.part.ptr<Drawable::LinePart>()) {
table.set("line", line->line);
table.set("width", line->width);
} else if (auto poly = v.part.ptr<Drawable::PolyPart>()) {
table.set("poly", poly->poly);
} else if (auto image = v.part.ptr<Drawable::ImagePart>()) {
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<Drawable> LuaConverter<Drawable>::to(LuaEngine& engine, LuaValue const& v) {
if (auto table = v.ptr<LuaTable>()) {
Maybe<Drawable> result;
result.emplace();
Drawable& drawable = result.get();
Color color = table->get<Maybe<Color>>("color").value(Color::White);
if (auto line = table->get<Maybe<Line2F>>("line"))
drawable = Drawable::makeLine(line.take(), table->get<float>("width"), color);
else if (auto poly = table->get<Maybe<PolyF>>("poly"))
drawable = Drawable::makePoly(poly.take(), color);
else if (auto image = table->get<Maybe<String>>("image"))
drawable = Drawable::makeImage(image.take(), 1.0f, table->get<Maybe<bool>>("centered").value(true), Vec2F(), color);
else
return {}; // throw LuaAnimationComponentException("Drawable table must have 'line', 'poly', or 'image'");
if (auto transformation = table->get<Maybe<Mat3F>>("transformation"))
drawable.transform(*transformation);
if (auto rotation = table->get<Maybe<float>>("rotation"))
drawable.rotate(*rotation);
if (table->get<bool>("mirrored"))
drawable.scale(Vec2F(-1, 1));
if (auto scale = table->get<Maybe<float>>("scale"))
drawable.scale(*scale);
if (auto position = table->get<Maybe<Vec2F>>("position"))
drawable.translate(*position);
drawable.fullbright = table->get<bool>("fullbright");
return result;
}
return {};
}
LuaValue LuaConverter<Collection>::from(LuaEngine& engine, Collection const& c) { LuaValue LuaConverter<Collection>::from(LuaEngine& engine, Collection const& c) {
auto table = engine.createTable(); auto table = engine.createTable();
table.set("name", c.name); table.set("name", c.name);

View File

@ -9,6 +9,7 @@
#include "StarCollectionDatabase.hpp" #include "StarCollectionDatabase.hpp"
#include "StarBehaviorState.hpp" #include "StarBehaviorState.hpp"
#include "StarSystemWorld.hpp" #include "StarSystemWorld.hpp"
#include "StarDrawable.hpp"
namespace Star { namespace Star {
@ -98,6 +99,12 @@ struct LuaConverter<LiquidLevel> {
static Maybe<LiquidLevel> to(LuaEngine& engine, LuaValue const& v); static Maybe<LiquidLevel> to(LuaEngine& engine, LuaValue const& v);
}; };
template <>
struct LuaConverter<Drawable> {
static LuaValue from(LuaEngine& engine, Drawable const& v);
static Maybe<Drawable> to(LuaEngine& engine, LuaValue const& v);
};
template <typename T> template <typename T>
LuaMethods<RpcPromise<T>> LuaUserDataMethods<RpcPromise<T>>::make() { LuaMethods<RpcPromise<T>> LuaUserDataMethods<RpcPromise<T>>::make() {
LuaMethods<RpcPromise<T>> methods; LuaMethods<RpcPromise<T>> methods;