From 49f8fbf187798194202922e24816edd1806eb95f Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Thu, 25 Jul 2024 00:19:36 +0200 Subject: [PATCH] first stab at bringing lua bindings back --- src/CMakeLists.txt | 2 +- src/binding/CMakeLists.txt | 8 +-- src/binding/src/script_core.cpp | 124 ++++++++++++++++++-------------- 3 files changed, 74 insertions(+), 60 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c232a6b..f627034 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) diff --git a/src/binding/CMakeLists.txt b/src/binding/CMakeLists.txt index acb882a..4c8b598 100644 --- a/src/binding/CMakeLists.txt +++ b/src/binding/CMakeLists.txt @@ -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 ) diff --git a/src/binding/src/script_core.cpp b/src/binding/src/script_core.cpp index 9cd9343..84541de 100644 --- a/src/binding/src/script_core.cpp +++ b/src/binding/src/script_core.cpp @@ -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 : std::false_type {}; -template <> struct is_automagical : std::false_type {}; -template <> struct is_automagical : std::false_type {}; -template <> struct is_automagical : std::false_type {}; -template <> struct is_automagical : std::false_type {}; -template <> struct is_automagical : std::false_type {}; +// template <> struct is_automagical : std::false_type {}; +// template <> struct is_automagical : std::false_type {}; +// template <> struct is_automagical : std::false_type {}; +// template <> struct is_automagical : std::false_type {}; +// template <> struct is_automagical : std::false_type {}; +// template <> struct is_automagical : 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 -std::vector convert_sequence(sol::state& lua,sol::table t) -{ +std::vector convert_sequence(sol::state& lua, sol::table t) { const std::size_t sz = t.size(); std::vector res(sz); for (std::size_t i = 1; i <= sz; i++) { @@ -42,45 +37,64 @@ std::vector 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()); -void register_core_function(sol::state& lua,sol::table& ns) -{ + // clang-format off - ns.set("pi",pw::pi()); + ns.new_usertype( + "color", sol::call_constructor,sol::constructors(), + "rgba", &color::rgba, + "data", sol::property([](color& c) { return c.rgba.data(); }), + "table", + sol::property( + [](const color& c) { + return sol::as_table( + std::array{ + 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", - sol::call_constructor,sol::constructors(), - "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{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", - sol::call_constructor,sol::constructors(), - "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", - sol::call_constructor,sol::constructors(), - "x", sol::property(sol::resolve(&vector2::x), [](vector2& v,vector2::value_type val){ v.x() = val;}), - "y", sol::property(sol::resolve(&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{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", sol::call_constructor, sol::constructors(), + "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", sol::call_constructor, + sol::constructors(), + "x", + sol::property( + sol::resolve(&vector2::x), + [](vector2& v, vector2::value_type val) { v.x() = val; }), + "y", + sol::property( + sol::resolve(&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{s.x(), s.y()}); + }, [](vector2& s, const sol::table& t) { + s.data[0] = t[1]; + s.data[1] = t[2]; + })); + ns.new_usertype("vector3" ,sol::call_constructor,sol::constructors() @@ -258,9 +272,9 @@ void register_core_function(sol::state& lua,sol::table& ns) ns.create_named("mathf") .set_function("ping_pong",ping_pong); +#endif } - PW_REGISTER_LUA(core) -} +} // namespace pw