reworking the scripting module
This commit is contained in:
parent
803af8c37a
commit
a497ac1f7d
11 changed files with 77 additions and 46 deletions
|
@ -15,13 +15,9 @@ public:
|
|||
|
||||
int run(const std::string& s);
|
||||
|
||||
struct state {
|
||||
virtual int run(const std::string& script) = 0;
|
||||
virtual ~state() = default;
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
struct state;
|
||||
std::unique_ptr<state> _state;
|
||||
|
||||
};
|
||||
|
|
|
@ -2,59 +2,76 @@
|
|||
|
||||
#include "scripting.hpp"
|
||||
|
||||
#include "script_core.hpp"
|
||||
#include "script_system.hpp"
|
||||
#include "script_scene.hpp"
|
||||
#include "script_visual.hpp"
|
||||
//#include "script_core.hpp"
|
||||
//#include "script_system.hpp"
|
||||
//#include "script_scene.hpp"
|
||||
//#include "script_visual.hpp"
|
||||
|
||||
#include "pw/core/debug.hpp"
|
||||
|
||||
namespace pw {
|
||||
|
||||
struct lua_state : public script::state {
|
||||
struct script::state {
|
||||
|
||||
scripting::state _state;
|
||||
scripting::table _namespace;
|
||||
sol::state _state;
|
||||
sol::table _namespace;
|
||||
|
||||
void load_modules();
|
||||
void load(const std::string& name);
|
||||
|
||||
lua_state() {
|
||||
state() {
|
||||
|
||||
// create global pw namespace
|
||||
_namespace = _state.create_named_table("pw");
|
||||
|
||||
// add the script as a user type
|
||||
_namespace.new_usertype<lua_state>("script",
|
||||
"initialize",&lua_state::load_modules
|
||||
_namespace.new_usertype<state>("script",
|
||||
"initialize",&state::load_modules,
|
||||
"load",&state::load
|
||||
);
|
||||
|
||||
// add this instance as "script"
|
||||
_namespace.set("script",this);
|
||||
}
|
||||
|
||||
virtual int run(const std::string &s);
|
||||
int run(const std::string &s);
|
||||
|
||||
};
|
||||
|
||||
int lua_state::run(const std::string &s)
|
||||
int script::state::run(const std::string &s)
|
||||
{
|
||||
return _state.script(s.c_str()).get<int>();
|
||||
}
|
||||
|
||||
void lua_state::load_modules() {
|
||||
void script::state::load(const std::string& name)
|
||||
{
|
||||
lua_runtime::register_function_list[name](_state,_namespace);
|
||||
}
|
||||
|
||||
void script::state::load_modules() {
|
||||
|
||||
// open all libraries
|
||||
_state.open_libraries();
|
||||
|
||||
script_core::load(_namespace);
|
||||
script_system::load(_namespace);
|
||||
script_scene::load(_namespace);
|
||||
script_visual::load(_namespace);
|
||||
for (auto lib_reg : lua_runtime::register_function_list) {
|
||||
lib_reg.second(_state,_namespace);
|
||||
debug::d() << lib_reg.first;
|
||||
}
|
||||
|
||||
// script_core::load(_namespace);
|
||||
// script_system::load(_namespace);
|
||||
// script_scene::load(_namespace);
|
||||
// script_visual::load(_namespace);
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
script::script()
|
||||
: _state(make_unique<script::state>())
|
||||
{
|
||||
_state.reset(new lua_state());
|
||||
}
|
||||
|
||||
script::~script()
|
||||
|
|
|
@ -9,9 +9,11 @@
|
|||
#include "pw/core/timer.hpp"
|
||||
#include "pw/core/mesh.hpp"
|
||||
|
||||
#include "scripting.hpp"
|
||||
|
||||
namespace pw {
|
||||
|
||||
void script_core::load(sol::table &ns)
|
||||
void lua_register_core(sol::state& lua,sol::table& ns)
|
||||
{
|
||||
|
||||
typedef real_t Scalar;
|
||||
|
@ -42,7 +44,7 @@ void script_core::load(sol::table &ns)
|
|||
"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",scripting::readonly_property(&quaternion::inverse),
|
||||
"inverse",sol::readonly_property(&quaternion::inverse),
|
||||
"normalized",&quaternion::normalized,
|
||||
"lerp",&quaternion::lerp,
|
||||
"slerp",&quaternion::slerp
|
||||
|
@ -51,8 +53,8 @@ void script_core::load(sol::table &ns)
|
|||
ns.new_usertype<axisangle>
|
||||
("axisangle",
|
||||
sol::constructors<axisangle(), axisangle(vector3,Scalar)>(),
|
||||
"axis",scripting::property(&axisangle::axis,&axisangle::set_axis),
|
||||
"angle",scripting::property(&axisangle::angle,&axisangle::set_angle)
|
||||
"axis",sol::property(&axisangle::axis,&axisangle::set_axis),
|
||||
"angle",sol::property(&axisangle::angle,&axisangle::set_angle)
|
||||
);
|
||||
|
||||
|
||||
|
@ -90,13 +92,21 @@ void script_core::load(sol::table &ns)
|
|||
sol::constructors<mesh()>()
|
||||
);
|
||||
|
||||
// ns.new_enum<mesh::topology_type>
|
||||
// lua["mesh"].new_enum()
|
||||
// (
|
||||
// "mesh",
|
||||
// sol::constructors<mesh()>()
|
||||
// );
|
||||
}
|
||||
|
||||
struct _register_core {
|
||||
static _register_core& get() {
|
||||
lua_runtime::register_function_list["core"] = lua_register_core;
|
||||
static _register_core instance;
|
||||
return instance;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace pw {
|
|||
|
||||
struct script_core {
|
||||
|
||||
static void load(scripting::table& ns);
|
||||
static void load(sol::table& ns);
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace pw {
|
||||
|
||||
void script_scene::load(sol::table &ns)
|
||||
void lua_register_scene(sol::table &ns)
|
||||
{
|
||||
|
||||
ns.new_usertype<node>("node",
|
||||
|
@ -16,7 +16,7 @@ void script_scene::load(sol::table &ns)
|
|||
"is_leaf", sol::readonly_property(&node::is_leaf),
|
||||
"is_root", sol::readonly_property(&node::is_root),
|
||||
// "share",scripting::property(scripting::resolve<std::shared_ptr<node>(std::make_shared<node>))
|
||||
"name",scripting::property(&node::name,&node::set_name)
|
||||
"name",sol::property(&node::name,&node::set_name)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,11 +5,11 @@
|
|||
|
||||
namespace pw {
|
||||
|
||||
struct script_scene {
|
||||
//struct script_scene {
|
||||
|
||||
static void load(scripting::table& ns);
|
||||
// static void load(scripting::table& ns);
|
||||
|
||||
};
|
||||
//};
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
namespace pw {
|
||||
|
||||
void script_system::load(sol::table &ns)
|
||||
void lua_register_system(sol::table &ns)
|
||||
{
|
||||
ns.new_usertype<window>("window",
|
||||
"update",&window::update,
|
||||
|
|
|
@ -5,11 +5,11 @@
|
|||
|
||||
namespace pw {
|
||||
|
||||
struct script_system {
|
||||
//struct script_system {
|
||||
|
||||
static void load(scripting::table& ns);
|
||||
// static void load(scripting::table& ns);
|
||||
|
||||
};
|
||||
//};
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
namespace pw {
|
||||
|
||||
void script_visual::load(sol::table &ns)
|
||||
void lua_register_visual(sol::table &ns)
|
||||
{
|
||||
|
||||
ns.new_usertype<pipeline>("pipeline",
|
||||
|
|
|
@ -5,11 +5,11 @@
|
|||
|
||||
namespace pw {
|
||||
|
||||
struct script_visual {
|
||||
//struct script_visual {
|
||||
|
||||
static void load(scripting::table& ns);
|
||||
// static void load(scripting::table& ns);
|
||||
|
||||
};
|
||||
//};
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -3,11 +3,19 @@
|
|||
|
||||
#include "sol/sol.hpp"
|
||||
#include <lua.hpp>
|
||||
#include <map>
|
||||
|
||||
namespace pw {
|
||||
|
||||
// include external namespace of sol
|
||||
namespace scripting = sol;
|
||||
struct lua_runtime {
|
||||
|
||||
typedef std::function<void(sol::state&,sol::table&)> register_function_t;
|
||||
typedef std::map<std::string,register_function_t> register_func_list_t;
|
||||
static register_func_list_t register_function_list;
|
||||
|
||||
};
|
||||
|
||||
lua_runtime::register_func_list_t lua_runtime::register_function_list = lua_runtime::register_func_list_t();
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue