| 183 | } |
| 184 | |
| 185 | sol::table initUtilPackage(lua_State* state) |
| 186 | { |
| 187 | sol::state_view lua(state); |
| 188 | sol::table util(lua, sol::create); |
| 189 | |
| 190 | // Lua bindings for Vec2 |
| 191 | util["vector2"] = [](float x, float y) { return Vec2(x, y); }; |
| 192 | sol::usertype<Vec2> vec2Type = lua.new_usertype<Vec2>("Vec2"); |
| 193 | addVectorMethods<Vec2>(vec2Type); |
| 194 | vec2Type["rotate"] = &Misc::rotateVec2f; |
| 195 | |
| 196 | // Lua bindings for Vec3 |
| 197 | util["vector3"] = [](float x, float y, float z) { return Vec3(x, y, z); }; |
| 198 | sol::usertype<Vec3> vec3Type = lua.new_usertype<Vec3>("Vec3"); |
| 199 | addVectorMethods<Vec3>(vec3Type); |
| 200 | vec3Type[sol::meta_function::involution] = [](const Vec3& a, const Vec3& b) { return a ^ b; }; |
| 201 | vec3Type["cross"] = [](const Vec3& a, const Vec3& b) { return a ^ b; }; |
| 202 | |
| 203 | // Lua bindings for Vec4 |
| 204 | util["vector4"] = [](float x, float y, float z, float w) { return Vec4(x, y, z, w); }; |
| 205 | sol::usertype<Vec4> vec4Type = lua.new_usertype<Vec4>("Vec4"); |
| 206 | addVectorMethods<Vec4>(vec4Type); |
| 207 | |
| 208 | // Lua bindings for Box |
| 209 | util["box"] = sol::overload([](const Vec3& center, const Vec3& halfSize) { return Box(center, halfSize); }, |
| 210 | [](const TransformM& transform) { return Box(transform.mM); }, |
| 211 | [](const TransformQ& transform) { return Box(Vec3(), Vec3(1, 1, 1), transform.mQ); }); |
| 212 | sol::usertype<Box> boxType = lua.new_usertype<Box>("Box"); |
| 213 | boxType["center"] = sol::readonly_property([](const Box& b) { return b.mCenter; }); |
| 214 | boxType["halfSize"] = sol::readonly_property([](const Box& b) { return b.mHalfSize; }); |
| 215 | boxType["transform"] = sol::readonly_property([](const Box& b) { return TransformM{ b.asTransform() }; }); |
| 216 | boxType["vertices"] = sol::readonly_property([lua](const Box& b) { |
| 217 | sol::table table(lua, sol::create); |
| 218 | const auto vertices = b.vertices(); |
| 219 | for (size_t i = 0; i < vertices.size(); ++i) |
| 220 | table[toLuaIndex(i)] = vertices[i]; |
| 221 | return table; |
| 222 | }); |
| 223 | boxType[sol::meta_function::equal_to] = [](const Box& a, const Box& b) { return a == b; }; |
| 224 | boxType[sol::meta_function::to_string] = [](const Box& b) { |
| 225 | std::stringstream ss; |
| 226 | ss << "Box{ "; |
| 227 | ss << "center(" << b.mCenter.x() << ", " << b.mCenter.y() << ", " << b.mCenter.z() << ") "; |
| 228 | ss << "halfSize(" << b.mHalfSize.x() << ", " << b.mHalfSize.y() << ", " << b.mHalfSize.z() << ")"; |
| 229 | ss << " }"; |
| 230 | return ss.str(); |
| 231 | }; |
| 232 | |
| 233 | // Lua bindings for Color |
| 234 | sol::usertype<Misc::Color> colorType = lua.new_usertype<Misc::Color>("Color"); |
| 235 | colorType["r"] = sol::readonly_property([](const Misc::Color& c) { return c.r(); }); |
| 236 | colorType["g"] = sol::readonly_property([](const Misc::Color& c) { return c.g(); }); |
| 237 | colorType["b"] = sol::readonly_property([](const Misc::Color& c) { return c.b(); }); |
| 238 | colorType["a"] = sol::readonly_property([](const Misc::Color& c) { return c.a(); }); |
| 239 | colorType[sol::meta_function::to_string] = [](const Misc::Color& c) { return c.toString(); }; |
| 240 | colorType["asRgba"] = [](const Misc::Color& c) { return Vec4(c.r(), c.g(), c.b(), c.a()); }; |
| 241 | colorType["asRgb"] = [](const Misc::Color& c) { return Vec3(c.r(), c.g(), c.b()); }; |
| 242 | colorType["asHex"] = [](const Misc::Color& c) { return c.toHex(); }; |