From 7186b8febc7a5c822d35e7c66369b548d9339fa5 Mon Sep 17 00:00:00 2001 From: Cody Glassman Date: Thu, 1 Aug 2024 07:52:09 -0700 Subject: [PATCH] generate all swizzle bindings at API initialization --- .../components_tests/lua/test_utilpackage.cpp | 8 -- components/lua/utilpackage.cpp | 110 +++++++----------- 2 files changed, 40 insertions(+), 78 deletions(-) diff --git a/apps/components_tests/lua/test_utilpackage.cpp b/apps/components_tests/lua/test_utilpackage.cpp index 2b562b5b7f..559b69a699 100644 --- a/apps/components_tests/lua/test_utilpackage.cpp +++ b/apps/components_tests/lua/test_utilpackage.cpp @@ -51,9 +51,6 @@ namespace EXPECT_TRUE(get(lua, "util.vector2(4, 6):ediv(util.vector2(2, 3)) == util.vector2(2, 2)")); lua.safe_script("swizzle = util.vector2(1, 2)"); EXPECT_TRUE(get(lua, "swizzle.xx == util.vector2(1, 1) and swizzle.yy == util.vector2(2, 2)")); - EXPECT_ERROR(lua.safe_script("v = util.vector2(1, 2).xp"), "unrecognized swizzle index"); - EXPECT_ERROR(lua.safe_script("v = util.vector2(1, 2).xxxxx"), "invalid swizzle length"); - EXPECT_ERROR(lua.safe_script("v = util.vector2(1, 2).zw"), "swizzle index out of range"); } TEST(LuaUtilPackageTest, Vector3) @@ -90,9 +87,6 @@ namespace lua.safe_script("swizzle = util.vector3(1, 2, 3)"); EXPECT_TRUE(get(lua, "swizzle.xxx == util.vector3(1, 1, 1)")); EXPECT_TRUE(get(lua, "swizzle.xyz == swizzle.zyx.zyx")); - EXPECT_ERROR(lua.safe_script("v = util.vector3(1, 2, 3).xyp"), "unrecognized swizzle index"); - EXPECT_ERROR(lua.safe_script("v = util.vector3(1, 2, 3).xxxxx"), "invalid swizzle length"); - EXPECT_ERROR(lua.safe_script("v = util.vector3(1, 2, 3).xxxw"), "swizzle index out of range"); } TEST(LuaUtilPackageTest, Vector4) @@ -131,8 +125,6 @@ namespace EXPECT_TRUE(get(lua, "swizzle.wwww == util.vector4(4, 4, 4, 4)")); EXPECT_TRUE(get(lua, "swizzle.xyzw == util.vector4(1, 2, 3, 4)")); EXPECT_TRUE(get(lua, "swizzle.xyzw == swizzle.wzyx.wzyx")); - EXPECT_ERROR(lua.safe_script("v = util.vector4(1, 2, 3, 4).xyp"), "unrecognized swizzle index"); - EXPECT_ERROR(lua.safe_script("v = util.vector4(1, 2, 3, 4).xxxxx"), "invalid swizzle length"); } TEST(LuaUtilPackageTest, Color) diff --git a/components/lua/utilpackage.cpp b/components/lua/utilpackage.cpp index 979cb1c1ac..852144576a 100644 --- a/components/lua/utilpackage.cpp +++ b/components/lua/utilpackage.cpp @@ -14,60 +14,6 @@ #include "shapes/box.hpp" -namespace -{ - int swizzleIndex(char c) - { - switch (c) - { - case 'x': - return 0; - case 'y': - return 1; - case 'z': - return 2; - case 'w': - return 3; - default: - throw std::runtime_error("unrecognized swizzle index"); - } - } - template - sol::object swizzle(sol::state_view lua, const T& vec, std::string_view key) - { - if (key.length() <= 1 || key.length() > LuaUtil::Vec4::num_components) - { - throw std::runtime_error("invalid swizzle length"); - } - - std::array components; - size_t aindex = 0; - - for (char c : key) - { - size_t sindex = swizzleIndex(c); - if (sindex >= T::num_components) - { - throw std::runtime_error("swizzle index out of range"); - } - components[aindex++] = vec[sindex]; - } - - switch (key.length()) - { - case 2: - return sol::make_object(lua, { components[0], components[1] }); - case 3: - return sol::make_object(lua, { components[0], components[1], components[2] }); - case 4: - return sol::make_object( - lua, { components[0], components[1], components[2], components[3] }); - default: - throw std::runtime_error("fatal error"); - } - } -} - namespace sol { template <> @@ -110,6 +56,44 @@ namespace LuaUtil { namespace { + template + void swizzle(sol::usertype& type, Indices... indices) + { + constexpr std::array components = { 'x', 'y', 'z', 'w' }; + + std::string field = { components[indices]... }; + type[field] = sol::readonly_property([=](const T& v) -> TNew { return { v[indices]... }; }); + } + + // Creates bindings for all possible permutations (repetition allowed) of x,y,z,w fields + template + void addSwizzleFields(sol::usertype& type) + { + for (size_t a = 0; a < T::num_components; ++a) + { + // Single component swizzles + swizzle(type, a); + + for (size_t b = 0; b < T::num_components; ++b) + { + // Two component swizzles + swizzle(type, a, b); + + for (size_t c = 0; c < T::num_components; ++c) + { + // Three component swizzles + swizzle(type, a, b, c); + + for (size_t d = 0; d < T::num_components; ++d) + { + // Four component swizzles + swizzle(type, a, b, c, d); + } + } + } + } + } + template void addVectorMethods(sol::usertype& vectorType) { @@ -151,6 +135,8 @@ namespace LuaUtil ss << ")"; return ss.str(); }; + + addSwizzleFields(vectorType); } } @@ -162,21 +148,12 @@ namespace LuaUtil // Lua bindings for Vec2 util["vector2"] = [](float x, float y) { return Vec2(x, y); }; sol::usertype vec2Type = lua.new_usertype("Vec2"); - vec2Type["x"] = sol::readonly_property([](const Vec2& v) -> float { return v.x(); }); - vec2Type["y"] = sol::readonly_property([](const Vec2& v) -> float { return v.y(); }); - vec2Type[sol::meta_function::index] - = [lua](const Vec2& v, std::string_view key) { return swizzle(lua, v, key); }; addVectorMethods(vec2Type); vec2Type["rotate"] = &Misc::rotateVec2f; // Lua bindings for Vec3 util["vector3"] = [](float x, float y, float z) { return Vec3(x, y, z); }; sol::usertype vec3Type = lua.new_usertype("Vec3"); - vec3Type["x"] = sol::readonly_property([](const Vec3& v) -> float { return v.x(); }); - vec3Type["y"] = sol::readonly_property([](const Vec3& v) -> float { return v.y(); }); - vec3Type["z"] = sol::readonly_property([](const Vec3& v) -> float { return v.z(); }); - vec3Type[sol::meta_function::index] - = [lua](const Vec3& v, std::string_view key) { return swizzle(lua, v, key); }; addVectorMethods(vec3Type); vec3Type[sol::meta_function::involution] = [](const Vec3& a, const Vec3& b) { return a ^ b; }; vec3Type["cross"] = [](const Vec3& a, const Vec3& b) { return a ^ b; }; @@ -184,12 +161,6 @@ namespace LuaUtil // Lua bindings for Vec4 util["vector4"] = [](float x, float y, float z, float w) { return Vec4(x, y, z, w); }; sol::usertype vec4Type = lua.new_usertype("Vec4"); - vec4Type["x"] = sol::readonly_property([](const Vec4& v) -> float { return v.x(); }); - vec4Type["y"] = sol::readonly_property([](const Vec4& v) -> float { return v.y(); }); - vec4Type["z"] = sol::readonly_property([](const Vec4& v) -> float { return v.z(); }); - vec4Type["w"] = sol::readonly_property([](const Vec4& v) -> float { return v.w(); }); - vec4Type[sol::meta_function::index] - = [lua](const Vec4& v, std::string_view key) { return swizzle(lua, v, key); }; addVectorMethods(vec4Type); // Lua bindings for Box @@ -394,5 +365,4 @@ namespace LuaUtil return util; } - }