From 71ed09a6f70901c9097973a44b24d6a6ced2834f Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 14 Nov 2023 13:20:04 +1100 Subject: Add two-way databinding for lua, and flesh out the lua statusbar --- src/lua/include/bridge.hpp | 9 ++++++++ src/lua/include/lua_thread.hpp | 2 ++ src/lua/include/property.hpp | 47 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/lua/include/property.hpp (limited to 'src/lua/include') diff --git a/src/lua/include/bridge.hpp b/src/lua/include/bridge.hpp index 059d0604..26401d14 100644 --- a/src/lua/include/bridge.hpp +++ b/src/lua/include/bridge.hpp @@ -11,19 +11,28 @@ #include "lua.hpp" #include "lvgl.h" +#include "property.hpp" #include "service_locator.hpp" namespace lua { class Bridge { public: + static auto Get(lua_State* state) -> Bridge*; + Bridge(system_fsm::ServiceLocator&, lua_State& s); + auto AddPropertyModule( + const std::string&, + std::vector>>) -> void; + system_fsm::ServiceLocator& services() { return services_; } + PropertyBindings& bindings() { return bindings_; } private: system_fsm::ServiceLocator& services_; lua_State& state_; + PropertyBindings bindings_; }; } // namespace lua diff --git a/src/lua/include/lua_thread.hpp b/src/lua/include/lua_thread.hpp index 381b1bdb..939d0cda 100644 --- a/src/lua/include/lua_thread.hpp +++ b/src/lua/include/lua_thread.hpp @@ -27,6 +27,8 @@ class LuaThread { auto RunScript(const std::string& path) -> bool; + auto bridge() -> Bridge& { return *bridge_; } + private: LuaThread(std::unique_ptr&, std::unique_ptr&, lua_State*); diff --git a/src/lua/include/property.hpp b/src/lua/include/property.hpp new file mode 100644 index 00000000..b6b4718f --- /dev/null +++ b/src/lua/include/property.hpp @@ -0,0 +1,47 @@ +/* + * Copyright 2023 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include +#include + +#include "lua.hpp" +#include "lvgl.h" +#include "service_locator.hpp" + +namespace lua { + +using LuaValue = std::variant; + +class Property { + public: + Property() : Property(std::monostate{}) {} + Property(const LuaValue&); + Property(const LuaValue&, std::function); + + auto IsTwoWay() -> bool { return cb_.has_value(); } + + auto PushValue(lua_State& s) -> int; + auto PopValue(lua_State& s) -> bool; + auto Update(const LuaValue& new_val) -> void; + + auto AddLuaBinding(lua_State*, int ref) -> void; + + private: + LuaValue value_; + std::optional> cb_; + std::vector> bindings_; +}; + +class PropertyBindings { + public: + PropertyBindings(lua_State&); + + auto Register(lua_State*, Property*) -> void; +}; + +} // namespace lua -- cgit v1.2.3