first stab at bringing lua bindings back

This commit is contained in:
Hartmut Seichter 2024-07-25 00:19:36 +02:00
parent 058f7ca23d
commit 49f8fbf187
3 changed files with 74 additions and 60 deletions

View file

@ -8,7 +8,7 @@ add_subdirectory(core)
# add_subdirectory(io)
#add_subdirectory(ui)
# add_subdirectory(binding)
add_subdirectory(binding)
add_subdirectory(visual)
# add_subdirectory(geometry)

View file

@ -6,10 +6,10 @@ set(hdrs
set(srcs
src/script.cpp
src/script_core.cpp
src/script_system.cpp
src/script_io.cpp
src/script_scene.cpp
src/script_visual.cpp
# src/script_system.cpp
# src/script_io.cpp
# src/script_scene.cpp
# src/script_visual.cpp
src/runtime_lua.hpp
src/runtime_lua.cpp
)

View file

@ -1,39 +1,34 @@
#include "pw/core/vector.hpp"
#include "pw/core/quaternion.hpp"
#include "pw/core/axisangle.hpp"
#include "pw/core/color.hpp"
#include "pw/core/debug.hpp"
#include "pw/core/size.hpp"
#include "pw/core/point.hpp"
#include "pw/core/time.hpp"
#include "pw/core/geometry.hpp"
#include "pw/core/image.hpp"
#include "pw/core/matrix_transform.hpp"
#include "pw/core/point.hpp"
#include "pw/core/primitives.hpp"
#include "pw/core/quaternion.hpp"
#include "pw/core/rectangle.hpp"
#include "pw/core/color.hpp"
#include "pw/core/size.hpp"
#include "pw/core/time.hpp"
#include "pw/core/vector.hpp"
#include "runtime_lua.hpp"
// seems CRTP magic doesnt work with SOL
namespace sol {
template <> struct is_automagical<pw::matrix4x4> : std::false_type {};
template <> struct is_automagical<pw::vector3> : std::false_type {};
template <> struct is_automagical<pw::vector2> : std::false_type {};
template <> struct is_automagical<pw::vector4> : std::false_type {};
template <> struct is_automagical<pw::quaternion> : std::false_type {};
template <> struct is_automagical<pw::rectangle> : std::false_type {};
// template <> struct is_automagical<pw::matrix4x4> : std::false_type {};
// template <> struct is_automagical<pw::vector3> : std::false_type {};
// template <> struct is_automagical<pw::vector2> : std::false_type {};
// template <> struct is_automagical<pw::vector4> : std::false_type {};
// template <> struct is_automagical<pw::quaternion> : std::false_type {};
// template <> struct is_automagical<pw::rectangle> : std::false_type {};
}
namespace pw {
vector3 table_to_vector3(sol::table t)
{
return vector3(t[0],t[1],t[2]);
}
// vector3 table_to_vector3(sol::table t) { return vector3(t[0], t[1], t[2]); }
template <typename elementType>
std::vector<elementType> convert_sequence(sol::state& lua,sol::table t)
{
std::vector<elementType> convert_sequence(sol::state& lua, sol::table t) {
const std::size_t sz = t.size();
std::vector<elementType> res(sz);
for (std::size_t i = 1; i <= sz; i++) {
@ -42,45 +37,64 @@ std::vector<elementType> convert_sequence(sol::state& lua,sol::table t)
return res;
}
void register_core_function(sol::state& lua, sol::table& ns) {
ns.set("pi", pw::pi<float>());
void register_core_function(sol::state& lua,sol::table& ns)
{
// clang-format off
ns.set("pi",pw::pi<real_t>());
ns.new_usertype<color>(
"color", sol::call_constructor,sol::constructors<color()>(),
"rgba", &color::rgba,
"data", sol::property([](color& c) { return c.rgba.data(); }),
"table",
sol::property(
[](const color& c) {
return sol::as_table(
std::array<vector4f::value_type, 4>{
c.rgba.x(), c.rgba.y(), c.rgba.z(), c.rgba.w()});
},
[](color& c,const sol::table& t) {
c = color{.rgba = vector4f{t[0], t[1], t[2], t[3]}};
})
ns.new_usertype<color>("color",
sol::call_constructor,sol::constructors<color(),color(real_t,real_t,real_t,real_t)>(),
"rgba",&color::rgba,
"data",sol::property([](color& c) { return std::ref(c.rgba.data);} ),
"table",sol::property([](const color& c){ return sol::as_table(std::array<vector4::value_type,4>{c.rgba.x(),c.rgba.y(),c.rgba.z(),c.rgba.w()}); },
[](color& c,const sol::table& t) { c = color((real_t)t[0],t[1],t[2],t[3]);})
);
ns.new_usertype<matrix4x4>("matrix4x4",
sol::call_constructor,sol::constructors<matrix4x4()>(),
"row",&matrix4x4::row,
"column",&matrix4x4::column,
"inverse", sol::readonly_property(&matrix4x4::inverse),
"identity",sol::readonly_property(&matrix4x4::identity),
sol::meta_function::multiplication,[](const matrix4x4& a,const vector4& b) { return vector4(a * b); }
);
ns.new_usertype<vector2>("vector2",
sol::call_constructor,sol::constructors<vector2(),vector2(real_t,real_t)>(),
"x", sol::property(sol::resolve<const vector2::value_type&() const>(&vector2::x), [](vector2& v,vector2::value_type val){ v.x() = val;}),
"y", sol::property(sol::resolve<const vector2::value_type&() const>(&vector2::y), [](vector2& v,vector2::value_type val){ v.y() = val;}),
"dot",&vector2::dot,
sol::meta_function::addition,[](const vector2& a,const vector2& b){ return vector2(a + b); },
sol::meta_function::subtraction,[](const vector2& a,const vector2& b){ return vector2(a - b); },
"data",sol::property([](vector2& s) { return std::ref(s.data);} ),
"table",sol::property([](const vector2& s){ return sol::as_table(std::array<vector2::value_type,2>{s.x(),s.y()}); },
[](vector2& s,const sol::table& t) { s.data[0] = t[1]; s.data[1] = t[2];} )
);
// clang-format on
#if 0
ns.new_usertype<matrix4x4>(
"matrix4x4", sol::call_constructor, sol::constructors<matrix4x4()>(),
"row", &matrix4x4::row, "column", &matrix4x4::column, "inverse",
sol::readonly_property(&matrix4x4::inverse), "identity",
sol::readonly_property(&matrix4x4::identity),
sol::meta_function::multiplication,
[](const matrix4x4& a, const vector4& b) { return vector4(a * b); });
ns.new_usertype<vector2f>(
"vector2", sol::call_constructor,
sol::constructors<vector2(), vector2(real_t, real_t)>(),
"x",
sol::property(
sol::resolve<const vector2::value_type&() const>(&vector2::x),
[](vector2& v, vector2::value_type val) { v.x() = val; }),
"y",
sol::property(
sol::resolve<const vector2::value_type&() const>(&vector2::y),
[](vector2& v, vector2::value_type val) { v.y() = val; }),
"dot", &vector2::dot, sol::meta_function::addition,
[](const vector2& a, const vector2& b) { return vector2(a + b); },
sol::meta_function::subtraction,
[](const vector2& a, const vector2& b) { return vector2(a - b); },
"data", sol::property([](vector2& s) { return std::ref(s.data); }),
"table", sol::property([](const vector2& s) {
return sol::as_table(std::array<vector2::value_type, 2>{s.x(), s.y()});
}, [](vector2& s, const sol::table& t) {
s.data[0] = t[1];
s.data[1] = t[2];
}));
ns.new_usertype<vector3>("vector3"
,sol::call_constructor,sol::constructors<vector3(),vector3(real_t,real_t,real_t)>()
@ -258,9 +272,9 @@ void register_core_function(sol::state& lua,sol::table& ns)
ns.create_named("mathf")
.set_function("ping_pong",ping_pong<real_t>);
#endif
}
PW_REGISTER_LUA(core)
}
} // namespace pw