somewhat better working registration system for the lua runtime

This commit is contained in:
Hartmut Seichter 2019-01-24 15:52:03 +01:00
parent 6ee3732994
commit baa209ceea
13 changed files with 141 additions and 91 deletions

View file

@ -16,6 +16,4 @@ runtime_lua::runtime_lua()
{
}
}

View file

@ -29,7 +29,14 @@ protected:
};
void register_core_lua();
#define PW_REGISTER_LUA(name) void register_##name##_lua() { runtime_lua::get().add(#name,register_##name##_function); }
#define PW_REGISTER_DECL_LUA(name) void register_##name##_lua();
#define PW_REGISTER_USE_LUA(name) register_##name##_lua();
}

View file

@ -5,9 +5,14 @@
#include "pw/core/debug.hpp"
namespace pw {
PW_REGISTER_DECL_LUA(core)
PW_REGISTER_DECL_LUA(system)
PW_REGISTER_DECL_LUA(scene)
PW_REGISTER_DECL_LUA(visual)
struct script::state {
sol::state _state;
@ -41,7 +46,7 @@ script::state::state()
// add the script as a user type
_namespace.new_usertype<state>("script",
"initialize",&state::load_all,
"load_all",&state::load_all,
"load",&state::load
);
@ -58,6 +63,7 @@ void script::state::load_all() {
//
for (auto m : runtime_lua::get().register_functions()) {
m.second(_state,_namespace);
debug::d() << "loading module " << m.first;
}
}
@ -81,7 +87,12 @@ int script::eval(const std::string &s)
void script::initialize()
{
register_core_lua();
PW_REGISTER_USE_LUA(core)
PW_REGISTER_USE_LUA(system)
PW_REGISTER_USE_LUA(scene)
PW_REGISTER_USE_LUA(visual)
}

View file

@ -9,46 +9,52 @@
#include "runtime_lua.hpp"
namespace pw {
// 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::quaternion> : std::false_type {};
}
namespace pw {
void register_core_function(sol::state& lua,sol::table& ns)
{
debug::d() << "registering ";
typedef real_t Scalar;
ns.set("pi",pw::pi<Scalar>());
ns.new_usertype<vector3>
(
"vector3",
sol::constructors<vector3(),vector3(vector3::value_type,vector3::value_type,vector3::value_type)>(),
"x", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;}),
"y", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;}),
"z", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;}),
"cross",&vector3::cross,
"lerp",&vector3::lerp
);
ns.new_usertype<matrix4x4>("matrix4x4"
, sol::constructors<matrix4x4()>()
, "row",&matrix4x4::row
);
ns.new_usertype<vector3>("vector3"
,sol::constructors<vector3(),vector3(Scalar,Scalar,Scalar)>()
,"x", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;})
,"y", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;})
,"z", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;})
,"cross",&vector3::cross
,"transposed",&vector3::transposed
,"lerp",&vector3::lerp
);
ns.new_usertype<quaternion>
(
"quaternion",
sol::constructors<quaternion(), quaternion(quaternion::value_type,quaternion::value_type,quaternion::value_type,quaternion::value_type)>(),
"x", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;}),
"y", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;}),
"z", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;}),
"w", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;}),
"identity",sol::readonly_property(&quaternion::identity),
"dot",&quaternion::dot,
"inverse",sol::readonly_property(&quaternion::inverse),
"normalized",&quaternion::normalized,
"lerp",&quaternion::lerp,
"slerp",&quaternion::slerp
);
ns.new_usertype<quaternion>("quaternion"
, sol::constructors<quaternion(), quaternion(quaternion::value_type,quaternion::value_type,quaternion::value_type,quaternion::value_type)>()
,"x", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;})
,"y", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;})
,"z", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;})
,"w", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;})
,"identity",sol::readonly_property(&quaternion::identity)
,"dot",&quaternion::dot
,"inverse",sol::readonly_property(&quaternion::inverse)
,"normalized",&quaternion::normalized
,"lerp",&quaternion::lerp
,"slerp",&quaternion::slerp
);
ns.new_usertype<axisangle>
("axisangle",
@ -86,23 +92,22 @@ void register_core_function(sol::state& lua,sol::table& ns)
"reset",&timer::reset
);
ns.new_usertype<mesh>
(
"mesh",
sol::constructors<mesh()>()
);
auto mesh_type = ns.new_usertype<mesh>("mesh"
, sol::constructors<mesh()>()
, "topology", sol::property(&mesh::topology,&mesh::set_topology)
, "vertices", sol::property(&mesh::vertices,&mesh::set_vertices)
, "indices", sol::property(&mesh::indices,&mesh::set_indices)
);
mesh_type.new_enum("topology"
, "points", mesh::topology_type::points
, "lines", mesh::topology_type::lines
, "line_strip", mesh::topology_type::line_strip
);
// lua["mesh"].new_enum()
// (
// "mesh",
// sol::constructors<mesh()>()
// );
}
PW_REGISTER_LUA(core)
void register_core_lua()
{
runtime_lua::get().add("core",register_core_function);
}
}

View file

@ -5,7 +5,7 @@
namespace pw {
void lua_register_scene(sol::state& lua,sol::table &ns)
void register_scene_function(sol::state&,sol::table &ns)
{
ns.new_usertype<node>("node",
@ -21,4 +21,6 @@ void lua_register_scene(sol::state& lua,sol::table &ns)
);
}
PW_REGISTER_LUA(scene)
}

View file

@ -10,7 +10,7 @@
namespace pw {
void lua_register_system(sol::table &ns)
void register_system_function(sol::state&, sol::table &ns)
{
ns.new_usertype<window>("window",
"update",&window::update,
@ -40,4 +40,10 @@ void lua_register_system(sol::table &ns)
}
PW_REGISTER_LUA(system)
}

View file

@ -6,7 +6,7 @@
namespace pw {
void lua_register_visual(sol::table &ns)
void register_visual_function(sol::state&,sol::table &ns)
{
ns.new_usertype<pipeline>("pipeline",
@ -16,4 +16,6 @@ void lua_register_visual(sol::table &ns)
}
PW_REGISTER_LUA(visual)
}