slowly getting tracktion again
This commit is contained in:
parent
a5dea1ede1
commit
dd23fa811a
29 changed files with 685 additions and 380 deletions
|
@ -1 +1,32 @@
|
|||
add_subdirectory(src)
|
||||
#add_subdirectory(src)
|
||||
|
||||
|
||||
set(hdrs
|
||||
include/pw/scripting/script.hpp
|
||||
include/pw/scripting/scripting.hpp
|
||||
)
|
||||
|
||||
set(srcs
|
||||
src/script.cpp
|
||||
)
|
||||
|
||||
add_library(pwscripting
|
||||
STATIC
|
||||
${hdrs}
|
||||
${srcs}
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
pwscripting
|
||||
PUBLIC
|
||||
include
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
pwscripting
|
||||
PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src/deps/lua-5.3.4/src
|
||||
${CMAKE_SOURCE_DIR}/src/deps/sol
|
||||
)
|
||||
|
||||
target_link_libraries(pwscripting lualib pwcore pwui pwscene)
|
||||
|
|
|
@ -28,6 +28,4 @@ protected:
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
|
||||
set(hdrs
|
||||
../include/pw/scripting/script.hpp
|
||||
../include/pw/scripting/scripting.hpp
|
||||
)
|
||||
|
||||
set(srcs
|
||||
script.cpp
|
||||
)
|
||||
|
||||
add_library(pwscripting
|
||||
STATIC
|
||||
${hdrs}
|
||||
${srcs}
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
pwscripting
|
||||
PUBLIC
|
||||
../include
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
pwscripting
|
||||
PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src/deps/lua-5.3.4/src
|
||||
${CMAKE_SOURCE_DIR}/src/deps/sol
|
||||
)
|
||||
|
||||
target_link_libraries(pwscripting lua pwcore)
|
|
@ -6,75 +6,118 @@
|
|||
#include "pw/core/quaternion.hpp"
|
||||
#include "pw/core/axisangle.hpp"
|
||||
|
||||
#include "pw/scene/node.hpp"
|
||||
|
||||
#include "pw/ui/window.hpp"
|
||||
|
||||
|
||||
struct lua_state : public pw::script::state {
|
||||
|
||||
pw::scripting::state _state;
|
||||
pw::scripting::table _namespace;
|
||||
pw::scripting::state _state;
|
||||
pw::scripting::table _namespace;
|
||||
|
||||
void load_modules();
|
||||
void load_modules();
|
||||
|
||||
lua_state() {
|
||||
lua_state() {
|
||||
|
||||
// create global pw namespace
|
||||
_namespace = _state.create_named_table("pw");
|
||||
// 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
|
||||
);
|
||||
// add the script as a user type
|
||||
_namespace.new_usertype<lua_state>("script",
|
||||
"initialize",&lua_state::load_modules
|
||||
);
|
||||
|
||||
// add this instance as "script"
|
||||
_namespace.set("script",this);
|
||||
}
|
||||
// add this instance as "script"
|
||||
_namespace.set("script",this);
|
||||
}
|
||||
|
||||
virtual int run(const std::string &s);
|
||||
virtual int run(const std::string &s);
|
||||
|
||||
};
|
||||
|
||||
int lua_state::run(const std::string &s)
|
||||
{
|
||||
return _state.script(s.c_str()).get<int>();
|
||||
return _state.script(s.c_str()).get<int>();
|
||||
}
|
||||
|
||||
void lua_state::load_modules() {
|
||||
|
||||
// open all libraries
|
||||
_state.open_libraries();
|
||||
// open all libraries
|
||||
_state.open_libraries();
|
||||
|
||||
typedef double Scalar;
|
||||
typedef double Scalar;
|
||||
|
||||
_namespace.set("pi",pw::pi<Scalar>());
|
||||
_namespace.set("pi",pw::pi<Scalar>());
|
||||
|
||||
using namespace pw;
|
||||
using namespace pw;
|
||||
|
||||
_namespace.new_usertype<vector3d>("vector3",
|
||||
"set",&vector3d::set,
|
||||
"x", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::x), &vector3d::set_x),
|
||||
"y", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::y), &vector3d::set_y),
|
||||
"z", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::z), &vector3d::set_z),
|
||||
// "v",&vector3d::values,
|
||||
"clone",&vector3d::clone
|
||||
);
|
||||
_namespace.new_usertype<vector3d>("vector3",
|
||||
sol::constructors<vector3d(), vector3d(Scalar,Scalar,Scalar)>(),
|
||||
"set",&vector3d::set,
|
||||
"x", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::x), &vector3d::set_x),
|
||||
"y", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::y), &vector3d::set_y),
|
||||
"z", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::z), &vector3d::set_z),
|
||||
"norm",&vector3d::norm,
|
||||
"cross",&vector3d::cross,
|
||||
"dot",&vector3d::dot,
|
||||
// sol::meta_function::addition, sol::resolve<vector3d(const vector3d&, const vector3d&)>(::operator+),
|
||||
// sol::meta_function::subtraction, &vector3d::operator-
|
||||
// "v",&vector3d::values,
|
||||
"clone",&vector3d::clone
|
||||
);
|
||||
|
||||
_namespace.new_usertype<quaterniond>("quaternion",
|
||||
"set",&quaterniond::set,
|
||||
"x", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::x), &quaterniond::set_x),
|
||||
"y", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::y), &quaterniond::set_y),
|
||||
"z", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::z), &quaterniond::set_z),
|
||||
"w", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::w), &quaterniond::set_w),
|
||||
"dot",&quaterniond::dot,
|
||||
"inverse",&quaterniond::inverse,
|
||||
"normalized",&quaterniond::normalized,
|
||||
"lerp",&quaterniond::lerp
|
||||
// "v",&vector3d::values,
|
||||
// "clone",&vector3d::clone
|
||||
);
|
||||
_namespace.new_usertype<quaterniond>("quaternion",
|
||||
sol::constructors<quaterniond(), vector3d(Scalar,Scalar,Scalar,Scalar)>(),
|
||||
"set",&quaterniond::set,
|
||||
"x", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::x), &quaterniond::set_x),
|
||||
"y", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::y), &quaterniond::set_y),
|
||||
"z", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::z), &quaterniond::set_z),
|
||||
"w", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::w), &quaterniond::set_w),
|
||||
"dot",&quaterniond::dot,
|
||||
"inverse",&quaterniond::inverse,
|
||||
"normalized",&quaterniond::normalized,
|
||||
"lerp",&quaterniond::lerp
|
||||
// "v",&vector3d::values,
|
||||
// "clone",&vector3d::clone
|
||||
);
|
||||
|
||||
_namespace.new_usertype<axisangled>("axisangle",
|
||||
"axis",scripting::property(&axisangled::axis,&axisangled::set_axis),
|
||||
"angle",scripting::property(&axisangled::angle,&axisangled::set_angle)
|
||||
);
|
||||
_namespace.new_usertype<axisangled>("axisangle",
|
||||
sol::constructors<axisangled(), axisangled(vector3d,Scalar)>(),
|
||||
"axis",scripting::property(&axisangled::axis,&axisangled::set_axis),
|
||||
"angle",scripting::property(&axisangled::angle,&axisangled::set_angle)
|
||||
);
|
||||
|
||||
|
||||
|
||||
_namespace.new_usertype<window>("window",
|
||||
"update",&window::update,
|
||||
"set_title",&window::set_title,
|
||||
"set_size",&window::set_size
|
||||
);
|
||||
|
||||
|
||||
_namespace.new_usertype<node>("node",
|
||||
sol::constructors<node(), node(std::string)>(),
|
||||
"add_child",&node::add_child,
|
||||
"shared",&node::shared,
|
||||
"children",&node::children,
|
||||
"child_count",&node::child_count,
|
||||
"create", []() -> std::shared_ptr<node> {
|
||||
auto ptr = std::make_shared<node>();
|
||||
return ptr;
|
||||
},
|
||||
// "share",scripting::property(scripting::resolve<std::shared_ptr<node>(std::make_shared<node>))
|
||||
"name",scripting::property(&node::name,&node::set_name)
|
||||
);
|
||||
|
||||
|
||||
|
||||
// _namespace.set("something", std::shared_ptr<node>(new node()));
|
||||
|
||||
_namespace["my_func"] = []() -> std::shared_ptr<node> {
|
||||
return std::make_shared<node>();
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
@ -82,17 +125,17 @@ void lua_state::load_modules() {
|
|||
|
||||
pw::script::script()
|
||||
{
|
||||
_state.reset(new lua_state());
|
||||
_state.reset(new lua_state());
|
||||
}
|
||||
|
||||
pw::script::~script()
|
||||
{
|
||||
_state = nullptr;
|
||||
_state = nullptr;
|
||||
}
|
||||
|
||||
int pw::script::run(const std::string &s)
|
||||
{
|
||||
return _state->run(s);
|
||||
return _state->run(s);
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue