worked out a simple way to call a lua function as a method based on the overridable_function_members.cpp

example
This commit is contained in:
Hartmut Seichter 2019-01-14 22:47:57 +01:00
parent e267a0d2ed
commit 29da543ed7
2 changed files with 37 additions and 15 deletions

View file

@ -4,21 +4,38 @@
#include "pw/system/input.hpp"
#include "pw/system/display.hpp"
#include "pw/core/debug.hpp"
// hijacking
//#include "pw/visual/pipeline.hpp"
namespace pw {
struct window_lua : public window {
// overridable function
sol::function lua_update;
window_lua(sol::this_state L)
// default is an empty callback
: lua_update(sol::make_reference<sol::function>(L.lua_state(), [](){}))
{
// set this internal callback of the window to the lua callback
_on_update = [this](window&){this->lua_update();};
}
};
void script_system::load(sol::table &ns)
{
ns.new_usertype<window>("window",
"update",&window::update,
"set_on_update",&window::set_on_update,
"title",sol::writeonly_property(&window::set_title),
"size",sol::property(&window::size,&window::set_size),
"position",sol::property(&window::position,&window::set_position),
"fullscreen",sol::property(&window::fullscreen,&window::set_fullscreen)
);
ns.new_usertype<window_lua>("window",
sol::constructors<window_lua(sol::this_state)>(),
"on_update",&window_lua::lua_update,
"update",&window::update,
"title",sol::writeonly_property(&window::set_title),
"size",sol::property(&window::size,&window::set_size),
"position",sol::property(&window::position,&window::set_position),
"fullscreen",sol::property(&window::fullscreen,&window::set_fullscreen)
);
ns.new_usertype<input>("input",
"new", sol::no_constructor,
@ -35,15 +52,15 @@ void script_system::load(sol::table &ns)
);
// ns.set_function("my_class_func_2", &my_class::func);
// ns.set_function("my_class_func_2", &my_class::func);
//
// // hijack part
// //
// ns.new_usertype<pipeline>("pipeline",
// "create",&pipeline::create
// );
//
// // hijack part
// //
// ns.new_usertype<pipeline>("pipeline",
// "create",&pipeline::create
// );
}
}

View file

@ -70,6 +70,11 @@ local w = pw.window.new()
-- set title
w.title = "pixwerx 0.1"
-- setup a lua callback function
w.on_update = function(self)
print("test on update",w.position.x,w.position.y)
end
-- set size
w.size = pw.size.new(800,600)
-- move window