From 00926fdce68605b1a46b4b1e41296a295c63869e Mon Sep 17 00:00:00 2001 From: Xein Date: Mon, 23 Nov 2026 09:34:28 +0100 Subject: [PATCH 1/2] UI support for array binds --- .../src/toast/scripting/ui_bind_convert.cpp | 159 ++++++++++++++++++ .../src/toast/scripting/ui_bind_convert.hpp | 28 +++ engine/src/toast/scripting/ui_binds_proxy.cpp | 59 ++++++- engine/src/toast/ui/bind_definitions.cpp | 101 +++++++++++ engine/src/toast/ui/bind_definitions.hpp | 26 +++ engine/src/toast/ui/bind_value.hpp | 120 +++++++++++++ engine/src/toast/ui/document_preprocess.cpp | 81 ++++++++- engine/src/toast/ui/document_preprocess.hpp | 2 + engine/src/toast/ui/ui_binds.cpp | 84 +++++++-- engine/src/toast/ui/ui_binds.hpp | 18 +- 10 files changed, 656 insertions(+), 22 deletions(-) create mode 100644 engine/src/toast/scripting/ui_bind_convert.cpp create mode 100644 engine/src/toast/scripting/ui_bind_convert.hpp create mode 100644 engine/src/toast/ui/bind_definitions.cpp create mode 100644 engine/src/toast/ui/bind_definitions.hpp create mode 100644 engine/src/toast/ui/bind_value.hpp diff --git a/engine/src/toast/scripting/ui_bind_convert.cpp b/engine/src/toast/scripting/ui_bind_convert.cpp new file mode 100644 index 000000000..c10df735d --- /dev/null +++ b/engine/src/toast/scripting/ui_bind_convert.cpp @@ -0,0 +1,159 @@ +#include "ui_bind_convert.hpp" + +#include +#include + +namespace scripting { + +namespace { + +constexpr int k_max_depth = 8; + +using Result = std::expected; + +auto where(const std::string& path) -> std::string { + return path.empty() ? std::string("the value") : path; +} + +auto scalarFromLua(lua_State* l, int index, const std::string& path) -> Result { + switch (lua_type(l, index)) { + case LUA_TNIL: return ui::BindValue::scalar(Rml::Variant()); + case LUA_TBOOLEAN: return ui::BindValue::scalar(Rml::Variant(lua_toboolean(l, index) != 0)); + case LUA_TNUMBER: + if (lua_isinteger(l, index) != 0) { + const lua_Integer v = lua_tointeger(l, index); + if (v >= INT_MIN && v <= INT_MAX) { + return ui::BindValue::scalar(Rml::Variant(static_cast(v))); + } + return ui::BindValue::scalar(Rml::Variant(static_cast(v))); + } + return ui::BindValue::scalar(Rml::Variant(static_cast(lua_tonumber(l, index)))); + case LUA_TSTRING: { + size_t length = 0; + const char* text = lua_tolstring(l, index, &length); + return ui::BindValue::scalar(Rml::Variant(Rml::String(text, length))); + } + default: return std::unexpected(std::format("unsupported {} value at {}", luaL_typename(l, index), where(path))); + } +} + +auto convert(lua_State* l, int index, BindKindHint hint, int depth, const std::string& path) -> Result { + if (lua_type(l, index) != LUA_TTABLE) { + return scalarFromLua(l, index, path); + } + if (depth > k_max_depth) { + return std::unexpected(std::format("table nested deeper than {} levels (cyclic?) at {}", k_max_depth, where(path))); + } + + index = lua_absindex(l, index); + + // classify the keys without running metamethods + size_t count = 0; + size_t integer_keys = 0; + size_t string_keys = 0; + lua_Integer max_index = 0; + lua_pushnil(l); + while (lua_next(l, index) != 0) { + lua_pop(l, 1); // keep the key for lua_next + ++count; + if (lua_type(l, -1) == LUA_TSTRING) { + ++string_keys; + } else if (lua_type(l, -1) == LUA_TNUMBER && lua_isinteger(l, -1) != 0 && lua_tointeger(l, -1) >= 1) { + ++integer_keys; + max_index = std::max(max_index, lua_tointeger(l, -1)); + } else { + lua_pop(l, 1); + return std::unexpected(std::format("table key at {} must be a string or an integer from 1", where(path))); + } + } + + const bool is_array = count == 0 ? hint != BindKindHint::object : string_keys == 0; + if (count > 0 && string_keys > 0 && integer_keys > 0) { + return std::unexpected(std::format("mixed array/object table at {}", where(path))); + } + if (is_array && count > 0 && static_cast(max_index) != integer_keys) { + return std::unexpected(std::format("sparse array at {}", where(path))); + } + + if (is_array) { + ui::BindValue out = ui::BindValue::array(); + out.items().reserve(count); + for (lua_Integer i = 1; i <= static_cast(count); ++i) { + lua_rawgeti(l, index, i); + auto child = convert(l, -1, BindKindHint::any, depth + 1, std::format("{}[{}]", path, i)); + lua_pop(l, 1); + if (!child) { + return child; + } + out.push(std::move(*child)); + } + return out; + } + + ui::BindValue out = ui::BindValue::object(); + lua_pushnil(l); + while (lua_next(l, index) != 0) { + // key at -2 and value at -1 + // the key stays a string because the conversion never touches it + size_t length = 0; + const char* key_text = lua_tolstring(l, -2, &length); + const std::string key(key_text, length); + auto child = convert(l, -1, BindKindHint::any, depth + 1, path.empty() ? key : std::format("{}.{}", path, key)); + if (!child) { + lua_pop(l, 2); + return child; + } + out.set(key, std::move(*child)); + lua_pop(l, 1); + } + return out; +} + +void pushVariant(lua_State* l, const Rml::Variant& v) { + switch (v.GetType()) { + case Rml::Variant::BOOL: lua_pushboolean(l, v.Get() ? 1 : 0); break; + case Rml::Variant::BYTE: + case Rml::Variant::CHAR: + case Rml::Variant::INT: lua_pushinteger(l, static_cast(v.Get())); break; + case Rml::Variant::INT64: + case Rml::Variant::UINT: + case Rml::Variant::UINT64: lua_pushinteger(l, static_cast(v.Get())); break; + case Rml::Variant::FLOAT: + case Rml::Variant::DOUBLE: lua_pushnumber(l, static_cast(v.Get())); break; + case Rml::Variant::NONE: lua_pushnil(l); break; + default: { + const Rml::String text = v.Get(); + lua_pushlstring(l, text.data(), text.size()); + } + } +} + +} + +auto luaToBindValue(lua_State* l, int index, BindKindHint hint) -> std::expected { + return convert(l, index, hint, 1, {}); +} + +void pushBindValue(lua_State* l, const ui::BindValue& value) { + if (value.isScalar()) { + pushVariant(l, value.value()); + return; + } + luaL_checkstack(l, 4, "ui_binds"); + if (value.isArray()) { + lua_createtable(l, static_cast(value.items().size()), 0); + lua_Integer i = 1; + for (const auto& item : value.items()) { + pushBindValue(l, item); + lua_rawseti(l, -2, i++); + } + return; + } + lua_createtable(l, 0, static_cast(value.members().size())); + for (const auto& [name, member] : value.members()) { + pushBindValue(l, member); + lua_setfield(l, -2, name.c_str()); + } +} + +} diff --git a/engine/src/toast/scripting/ui_bind_convert.hpp b/engine/src/toast/scripting/ui_bind_convert.hpp new file mode 100644 index 000000000..458d6afa6 --- /dev/null +++ b/engine/src/toast/scripting/ui_bind_convert.hpp @@ -0,0 +1,28 @@ +/** + * @file ui_bind_convert.hpp + * @author Xein + * @date 22 Nov 2026 + */ + +#pragma once +#include +#include +#include +#include + +struct lua_State; + +namespace scripting { + +enum class BindKindHint { + any, + array, + object +}; + +[[nodiscard]] +auto TOAST_API luaToBindValue(lua_State* l, int index, BindKindHint hint) -> std::expected; + +void TOAST_API pushBindValue(lua_State* l, const ui::BindValue& value); + +} diff --git a/engine/src/toast/scripting/ui_binds_proxy.cpp b/engine/src/toast/scripting/ui_binds_proxy.cpp index 689645ac8..f02caedb8 100644 --- a/engine/src/toast/scripting/ui_binds_proxy.cpp +++ b/engine/src/toast/scripting/ui_binds_proxy.cpp @@ -1,5 +1,7 @@ #include "ui_binds_proxy.hpp" +#include "ui_bind_convert.hpp" + #include #include #include @@ -46,6 +48,50 @@ auto luaRefToVariant(lua_State* l, const luabridge::LuaRef& v) -> Rml::Variant { return {}; } +auto bindToLuaRef(lua_State* l, ui::UIBinds& binds, std::string_view name) -> luabridge::LuaRef { + const ui::BindValue* tree = binds.getValue(name); + if (tree == nullptr) { + return {l}; + } + if (tree->isScalar()) { + return variantToLuaRef(l, tree->value()); + } + pushBindValue(l, *tree); + auto ref = luabridge::LuaRef::fromStack(l, -1); + lua_pop(l, 1); + return ref; +} + +auto assignBind(lua_State* l, ui::UIBinds& binds, const std::string& name, const luabridge::LuaRef& value) -> std::string { + const ui::BindValue* current = binds.getValue(name); + const bool wants_array = current != nullptr && current->isArray(); + const bool wants_object = current != nullptr && current->isObject(); + + if (!wants_array && !wants_object && !value.isTable()) { + binds.set(name, luaRefToVariant(l, value)); + return {}; + } + + value.push(l); + const BindKindHint hint = wants_array ? BindKindHint::array : wants_object ? BindKindHint::object : BindKindHint::any; + auto converted = luaToBindValue(l, -1, hint); + lua_pop(l, 1); + if (!converted) { + return converted.error(); + } + if (wants_array && !converted->isArray()) { + return "expects an array (a list used by data-for)"; + } + if (wants_object && !converted->isObject()) { + return "expects an object (a table with string keys)"; + } + if (!wants_array && !wants_object) { + return "is a plain value in the document, it cannot hold a table (use data-for or {{name.field}} to make it one)"; + } + binds.setValue(name, std::move(*converted)); + return {}; +} + auto resolveBinds(const toast::Node* node) -> ui::UIBinds* { return node != nullptr ? ui::UIBinds::forNode(node) : nullptr; } @@ -72,7 +118,7 @@ auto uiBindsProxyIndex(UIBindsProxy& proxy, const luabridge::LuaRef& key, lua_St if (binds == nullptr || !binds->has(name)) { return {l}; } - return variantToLuaRef(l, binds->get(name)); + return bindToLuaRef(l, *binds, name); } auto uiBindsProxyNewindex(UIBindsProxy& proxy, const luabridge::LuaRef& key, const luabridge::LuaRef& value, lua_State* l) @@ -98,7 +144,9 @@ auto uiBindsProxyNewindex(UIBindsProxy& proxy, const luabridge::LuaRef& key, con return {l}; } - binds->set(name, luaRefToVariant(l, value)); + if (const std::string error = assignBind(l, *binds, name, value); !error.empty()) { + luaL_error(l, "ui_binds: '%s' %s", name.c_str(), error.c_str()); + } return {l}; } @@ -112,7 +160,7 @@ auto uiBindsGet(const toast::Node* node, std::string_view name, lua_State* l) -> if (binds == nullptr || !binds->has(name)) { return {l}; } - return variantToLuaRef(l, binds->get(name)); + return bindToLuaRef(l, *binds, name); } auto uiBindsSet(toast::Node* node, std::string_view name, const luabridge::LuaRef& value, lua_State* l) -> bool { @@ -120,7 +168,10 @@ auto uiBindsSet(toast::Node* node, std::string_view name, const luabridge::LuaRe if (binds == nullptr || !binds->has(name)) { return false; } - binds->set(name, luaRefToVariant(l, value)); + const std::string key(name); + if (const std::string error = assignBind(l, *binds, key, value); !error.empty()) { + luaL_error(l, "ui_binds: '%s' %s", key.c_str(), error.c_str()); + } return true; } diff --git a/engine/src/toast/ui/bind_definitions.cpp b/engine/src/toast/ui/bind_definitions.cpp new file mode 100644 index 000000000..29e6834dd --- /dev/null +++ b/engine/src/toast/ui/bind_definitions.cpp @@ -0,0 +1,101 @@ +#include "bind_definitions.hpp" + +namespace ui { + +namespace { + +auto asValue(void* ptr) -> BindValue* { + return static_cast(ptr); +} + +auto childOf(BindValue& value) -> Rml::DataVariable { + return Rml::DataVariable(bindDefinitionFor(value), &value); +} + +class ScalarDefinition final : public Rml::VariableDefinition { +public: + ScalarDefinition() : VariableDefinition(Rml::DataVariableType::Scalar) { } + + auto Get(void* ptr, Rml::Variant& variant) -> bool override { + const BindValue* value = asValue(ptr); + if (value == nullptr || !value->isScalar()) { + return false; + } + variant = value->value(); + return true; + } + + auto Set(void* ptr, const Rml::Variant& variant) -> bool override { + BindValue* value = asValue(ptr); + if (value == nullptr || !value->isScalar()) { + return false; + } + *value = BindValue::scalar(variant); + return true; + } +}; + +class ArrayDefinition final : public Rml::VariableDefinition { +public: + ArrayDefinition() : VariableDefinition(Rml::DataVariableType::Array) { } + + auto Size(void* ptr) -> int override { + const BindValue* value = asValue(ptr); + return value != nullptr && value->isArray() ? static_cast(value->items().size()) : 0; + } + + auto Child(void* ptr, const Rml::DataAddressEntry& address) -> Rml::DataVariable override { + BindValue* value = asValue(ptr); + if (value == nullptr || !value->isArray()) { + return {}; + } + auto& items = value->items(); + if (address.index < 0) { + // same as native Rml arrays: `list.size` + return address.name == "size" ? Rml::MakeLiteralIntVariable(static_cast(items.size())) : Rml::DataVariable {}; + } + if (static_cast(address.index) >= items.size()) { + return {}; + } + return childOf(items[static_cast(address.index)]); + } +}; + +class ObjectDefinition final : public Rml::VariableDefinition { +public: + ObjectDefinition() : VariableDefinition(Rml::DataVariableType::Struct) { } + + auto Child(void* ptr, const Rml::DataAddressEntry& address) -> Rml::DataVariable override { + BindValue* value = asValue(ptr); + if (value == nullptr || address.index >= 0) { + return {}; + } + BindValue* member = value->find(address.name); + return member != nullptr ? childOf(*member) : Rml::DataVariable {}; + } +}; + +} + +auto bindArrayDefinition() -> Rml::VariableDefinition* { + static ArrayDefinition definition; + return &definition; +} + +auto bindObjectDefinition() -> Rml::VariableDefinition* { + static ObjectDefinition definition; + return &definition; +} + +auto bindDefinitionFor(const BindValue& value) -> Rml::VariableDefinition* { + static ScalarDefinition scalar; + if (value.isArray()) { + return bindArrayDefinition(); + } + if (value.isObject()) { + return bindObjectDefinition(); + } + return &scalar; +} + +} diff --git a/engine/src/toast/ui/bind_definitions.hpp b/engine/src/toast/ui/bind_definitions.hpp new file mode 100644 index 000000000..abbe93454 --- /dev/null +++ b/engine/src/toast/ui/bind_definitions.hpp @@ -0,0 +1,26 @@ +/** + * @file bind_definitions.hpp + * @author Xein + * @date 22 Nov 2026 + * + * @brief RmlUi data-model definitions that expose BindValue trees (arrays and objects) to `data-for` and `{{a.b}}` + */ + +#pragma once +#include "bind_value.hpp" + +#include +#include + +namespace ui { + +[[nodiscard]] +auto TOAST_API bindDefinitionFor(const BindValue& value) -> Rml::VariableDefinition*; + +[[nodiscard]] +auto TOAST_API bindArrayDefinition() -> Rml::VariableDefinition*; + +[[nodiscard]] +auto TOAST_API bindObjectDefinition() -> Rml::VariableDefinition*; + +} diff --git a/engine/src/toast/ui/bind_value.hpp b/engine/src/toast/ui/bind_value.hpp new file mode 100644 index 000000000..894b9b17f --- /dev/null +++ b/engine/src/toast/ui/bind_value.hpp @@ -0,0 +1,120 @@ +/** + * @file bind_value.hpp + * @author Xein + * @date 22 Nov 2026 + * + * @brief Tree value behind a Panel bind + */ + +#pragma once +#include +#include +#include +#include +#include +#include +#include + +namespace ui { + +class BindValue { +public: + using Array = std::vector; + using Object = std::vector>; + + BindValue() = default; + + [[nodiscard]] + static auto scalar(Rml::Variant value) -> BindValue { + BindValue v; + v.m_data = std::move(value); + return v; + } + + [[nodiscard]] + static auto array() -> BindValue { + BindValue v; + v.m_data = Array {}; + return v; + } + + [[nodiscard]] + static auto object() -> BindValue { + BindValue v; + v.m_data = Object {}; + return v; + } + + [[nodiscard]] + auto isScalar() const -> bool { + return std::holds_alternative(m_data); + } + + [[nodiscard]] + auto isArray() const -> bool { + return std::holds_alternative(m_data); + } + + [[nodiscard]] + auto isObject() const -> bool { + return std::holds_alternative(m_data); + } + + [[nodiscard]] + auto value() const -> const Rml::Variant& { + return std::get(m_data); + } + + [[nodiscard]] + auto items() -> Array& { + return std::get(m_data); + } + + [[nodiscard]] + auto items() const -> const Array& { + return std::get(m_data); + } + + [[nodiscard]] + auto members() -> Object& { + return std::get(m_data); + } + + [[nodiscard]] + auto members() const -> const Object& { + return std::get(m_data); + } + + [[nodiscard]] + auto find(std::string_view key) -> BindValue* { + if (!isObject()) { + return nullptr; + } + for (auto& [name, value] : members()) { + if (name == key) { + return &value; + } + } + return nullptr; + } + + [[nodiscard]] + auto find(std::string_view key) const -> const BindValue* { + return const_cast(this)->find(key); // NOLINT(cppcoreguidelines-pro-type-const-cast) + } + + void set(std::string_view key, BindValue value) { + if (BindValue* existing = find(key)) { + *existing = std::move(value); + return; + } + members().emplace_back(std::string(key), std::move(value)); + } + + void push(BindValue value) { items().push_back(std::move(value)); } + +private: + std::variant m_data; +}; + +} diff --git a/engine/src/toast/ui/document_preprocess.cpp b/engine/src/toast/ui/document_preprocess.cpp index a9c7b7a05..3cc3d3bd8 100644 --- a/engine/src/toast/ui/document_preprocess.cpp +++ b/engine/src/toast/ui/document_preprocess.cpp @@ -13,7 +13,9 @@ auto isIdentChar(char c) -> bool { } auto isExpressionKeyword(std::string_view word) -> bool { - static constexpr std::array keywords {"true", "false", "and", "or", "not", "it", "it_index", "ev"}; + static constexpr std::array keywords { + "true", "false", "and", "or", "not", "it", "it_index", "it_size", "it_first", "it_last", "ev" + }; return std::ranges::find(keywords, word) != keywords.end(); } @@ -23,7 +25,47 @@ void pushUnique(std::vector& out, std::string value) { } } -void collectIdentifiers(std::string_view expression, std::vector& out) { +void parseDataFor(std::string_view value, std::vector& aliases, std::string_view& container) { + const size_t colon = value.find(':'); + if (colon == std::string_view::npos) { + container = {}; + return; + } + container = value.substr(colon + 1); + + std::string_view head = value.substr(0, colon); + while (!head.empty()) { + const size_t comma = head.find(','); + std::string_view part = head.substr(0, comma); + while (!part.empty() && (std::isspace(static_cast(part.front())) != 0)) { + part.remove_prefix(1); + } + while (!part.empty() && (std::isspace(static_cast(part.back())) != 0)) { + part.remove_suffix(1); + } + if (!part.empty()) { + aliases.emplace_back(part); + } + head = comma == std::string_view::npos ? std::string_view {} : head.substr(comma + 1); + } +} + +auto asPlainIdentifier(std::string_view expression) -> std::string_view { + while (!expression.empty() && (std::isspace(static_cast(expression.front())) != 0)) { + expression.remove_prefix(1); + } + while (!expression.empty() && (std::isspace(static_cast(expression.back())) != 0)) { + expression.remove_suffix(1); + } + if (expression.empty() || (std::isalpha(static_cast(expression.front())) == 0 && expression.front() != '_')) { + return {}; + } + return std::ranges::all_of(expression, isIdentChar) ? expression : std::string_view {}; +} + +void collectIdentifiers( + std::string_view expression, std::vector& out, std::vector* member_roots = nullptr +) { size_t i = 0; while (i < expression.size()) { const char c = expression[i]; @@ -49,6 +91,9 @@ void collectIdentifiers(std::string_view expression, std::vector& o } if (!isExpressionKeyword(word) && (std::isdigit(static_cast(word[0])) == 0)) { pushUnique(out, std::string(word)); + if (member_roots != nullptr && i < expression.size() && (expression[i] == '.' || expression[i] == '[')) { + pushUnique(*member_roots, std::string(word)); + } } // skip the member chain while (i < expression.size() && (isIdentChar(expression[i]) || expression[i] == '.')) { @@ -183,6 +228,9 @@ auto resolveColorReferences(std::string_view source, const ColorResolver& resolv auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> DocumentScan { ZoneScoped; DocumentScan scan; + std::vector aliases; // data-for loop variables, locals of the document + std::vector member_roots; // identifiers read through . or [ + std::vector array_candidates; // plain containers of data-for // can attributes and {{ }} expressions size_t i = 0; @@ -198,7 +246,7 @@ auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> D if (end == std::string_view::npos) { break; } - collectIdentifiers(rml.substr(i + 2, end - i - 2), scan.binds); + collectIdentifiers(rml.substr(i + 2, end - i - 2), scan.binds, &member_roots); i = end + 2; continue; } @@ -257,12 +305,16 @@ auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> D collectEventName(value, scan.events); } else if (name == "data-for") { // "item : items" or "item, index : items" binds the container - const size_t colon = value.find(':'); - if (colon != std::string_view::npos) { - collectIdentifiers(value.substr(colon + 1), scan.binds); + std::string_view container; + parseDataFor(value, aliases, container); + if (!container.empty()) { + collectIdentifiers(container, scan.binds, &member_roots); + if (const auto plain = asPlainIdentifier(container); !plain.empty()) { + pushUnique(array_candidates, std::string(plain)); + } } } else if (name.starts_with("data-") && name != "data-model" && !name.starts_with("data-loc-")) { - collectIdentifiers(value, scan.binds); + collectIdentifiers(value, scan.binds, &member_roots); if (name == "data-checked") { collectIdentifiers(value, scan.bool_binds); } @@ -276,6 +328,21 @@ auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> D i++; } + // loop variables are locals of their `data-for` + const auto is_alias = [&](const std::string& name) { return std::ranges::find(aliases, name) != aliases.end(); }; + std::erase_if(scan.binds, is_alias); + std::erase_if(scan.bool_binds, is_alias); + for (const auto& name : array_candidates) { + if (!is_alias(name)) { + pushUnique(scan.array_binds, name); + } + } + for (const auto& name : member_roots) { + if (!is_alias(name) && std::ranges::find(scan.array_binds, name) == scan.array_binds.end()) { + pushUnique(scan.object_binds, name); + } + } + // inject style links and the data model std::string result = normalizeStylesheetLinks(resolveColorReferences(rml, ctx.color_resolver)); diff --git a/engine/src/toast/ui/document_preprocess.hpp b/engine/src/toast/ui/document_preprocess.hpp index 43f0660a7..44a5d5400 100644 --- a/engine/src/toast/ui/document_preprocess.hpp +++ b/engine/src/toast/ui/document_preprocess.hpp @@ -28,6 +28,8 @@ struct DocumentScan { std::string transformed_rml; std::vector binds; std::vector bool_binds; + std::vector array_binds; + std::vector object_binds; std::vector events; }; diff --git a/engine/src/toast/ui/ui_binds.cpp b/engine/src/toast/ui/ui_binds.cpp index 7c7abc5bd..9dc9fe367 100644 --- a/engine/src/toast/ui/ui_binds.cpp +++ b/engine/src/toast/ui/ui_binds.cpp @@ -1,5 +1,6 @@ #include "ui_binds.hpp" +#include "bind_definitions.hpp" #include "document_preprocess.hpp" #include @@ -20,11 +21,37 @@ void UIBindStore::reconcile(const DocumentScan& scan) { std::erase_if(m_values, [&](const auto& entry) { return !active.contains(entry.first); }); + const auto contains = [](const std::vector& list, const std::string& name) { + return std::ranges::find(list, name) != list.end(); + }; + for (const auto& name : scan.binds) { - const bool is_bool = std::ranges::find(scan.bool_binds, name) != scan.bool_binds.end(); - auto [it, inserted] = m_values.try_emplace(name, is_bool ? Rml::Variant(false) : Rml::Variant(Rml::String())); - if (!inserted && is_bool && it->second.GetType() != Rml::Variant::BOOL) { - it->second = Rml::Variant(false); + const bool is_bool = contains(scan.bool_binds, name); + const bool is_array = contains(scan.array_binds, name); + const bool is_object = !is_array && contains(scan.object_binds, name); + + auto make_default = [&]() { + if (is_array) { + return BindValue::array(); + } + if (is_object) { + return BindValue::object(); + } + return BindValue::scalar(is_bool ? Rml::Variant(false) : Rml::Variant(Rml::String())); + }; + + auto [it, inserted] = m_values.try_emplace(name, make_default()); + if (inserted) { + continue; + } + + // a bind that changed kind between reloads + const BindValue& current = it->second; + const bool kind_matches = is_array ? current.isArray() : is_object ? current.isObject() : current.isScalar(); + const bool bool_mismatch = + is_bool && !is_array && !is_object && current.isScalar() && current.value().GetType() != Rml::Variant::BOOL; + if (!kind_matches || bool_mismatch) { + it->second = make_default(); } } } @@ -35,10 +62,27 @@ auto UIBindStore::has(std::string_view name) const -> bool { auto UIBindStore::get(std::string_view name) const -> Rml::Variant { const auto it = m_values.find(std::string(name)); - return it != m_values.end() ? it->second : Rml::Variant(); + if (it == m_values.end() || !it->second.isScalar()) { + return {}; + } + return it->second.value(); } void UIBindStore::set(std::string_view name, Rml::Variant value) { + m_values[std::string(name)] = BindValue::scalar(std::move(value)); +} + +auto UIBindStore::getValue(std::string_view name) -> BindValue* { + const auto it = m_values.find(std::string(name)); + return it != m_values.end() ? &it->second : nullptr; +} + +auto UIBindStore::getValue(std::string_view name) const -> const BindValue* { + const auto it = m_values.find(std::string(name)); + return it != m_values.end() ? &it->second : nullptr; +} + +void UIBindStore::setValue(std::string_view name, BindValue value) { m_values[std::string(name)] = std::move(value); } @@ -75,11 +119,21 @@ UIBinds::UIBinds(Rml::Context* context, toast::Node* owner, const DocumentScan& // Two-way value binds for (const auto& name : scan.binds) { - constructor.BindFunc( - name, - [this, name](Rml::Variant& out) { out = m_store.get(name); }, - [this, name](const Rml::Variant& in) { set(name, in); } - ); + const bool is_array = std::ranges::find(scan.array_binds, name) != scan.array_binds.end(); + const bool is_object = !is_array && std::ranges::find(scan.object_binds, name) != scan.object_binds.end(); + BindValue* tree = (is_array || is_object) ? m_store.getValue(name) : nullptr; + if (tree != nullptr) { + // arrays and objects are read in place from the store + constructor.BindCustomDataVariable( + name, Rml::DataVariable(is_array ? bindArrayDefinition() : bindObjectDefinition(), tree) + ); + } else { + constructor.BindFunc( + name, + [this, name](Rml::Variant& out) { out = m_store.get(name); }, + [this, name](const Rml::Variant& in) { set(name, in); } + ); + } m_dirty_names.emplace(name); } @@ -115,6 +169,16 @@ void UIBinds::set(std::string_view name, Rml::Variant value) { m_dirty_names.emplace(key); } +auto UIBinds::getValue(std::string_view name) const -> const BindValue* { + return m_store.getValue(name); +} + +void UIBinds::setValue(std::string_view name, BindValue value) { + const std::string key(name); + m_store.setValue(key, std::move(value)); + m_dirty_names.emplace(key); +} + void UIBinds::flushDirty() { if (m_handle) { for (const auto& name : m_dirty_names) { diff --git a/engine/src/toast/ui/ui_binds.hpp b/engine/src/toast/ui/ui_binds.hpp index 4c1bc111f..ea22129a5 100644 --- a/engine/src/toast/ui/ui_binds.hpp +++ b/engine/src/toast/ui/ui_binds.hpp @@ -7,6 +7,8 @@ */ #pragma once +#include "bind_value.hpp" + #include #include #include @@ -45,8 +47,15 @@ class TOAST_API UIBindStore { void set(std::string_view name, Rml::Variant value); + [[nodiscard]] + auto getValue(std::string_view name) -> BindValue*; + [[nodiscard]] + auto getValue(std::string_view name) const -> const BindValue*; + + void setValue(std::string_view name, BindValue value); + private: - std::unordered_map m_values; + std::unordered_map m_values; }; class UIBinds { @@ -75,6 +84,13 @@ class UIBinds { /// Writes `name` from script and notifies the data model to refresh the UI void set(std::string_view name, Rml::Variant value); + /// @returns the value tree of `name` + [[nodiscard]] + auto getValue(std::string_view name) const -> const BindValue*; + + /// Writes a whole array/object/scalar tree + void setValue(std::string_view name, BindValue value); + void flushDirty(); static void flushAllDirty(); From 7e3a67ea5df5040ef4362eecc512108a0e975519 Mon Sep 17 00:00:00 2001 From: Xein Date: Mon, 23 Nov 2026 10:20:53 +0100 Subject: [PATCH 2/2] clang-tidy --- engine/src/toast/scripting/ui_bind_convert.cpp | 5 +++-- engine/src/toast/scripting/ui_bind_convert.hpp | 2 +- engine/src/toast/scripting/ui_binds_proxy.cpp | 7 ++++++- engine/src/toast/ui/bind_definitions.cpp | 2 +- engine/src/toast/ui/ui_binds.cpp | 12 ++++++++++-- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/engine/src/toast/scripting/ui_bind_convert.cpp b/engine/src/toast/scripting/ui_bind_convert.cpp index c10df735d..4cf2475ac 100644 --- a/engine/src/toast/scripting/ui_bind_convert.cpp +++ b/engine/src/toast/scripting/ui_bind_convert.cpp @@ -2,6 +2,7 @@ #include #include +#include namespace scripting { @@ -71,14 +72,14 @@ auto convert(lua_State* l, int index, BindKindHint hint, int depth, const std::s if (count > 0 && string_keys > 0 && integer_keys > 0) { return std::unexpected(std::format("mixed array/object table at {}", where(path))); } - if (is_array && count > 0 && static_cast(max_index) != integer_keys) { + if (is_array && count > 0 && std::cmp_not_equal(max_index, integer_keys)) { return std::unexpected(std::format("sparse array at {}", where(path))); } if (is_array) { ui::BindValue out = ui::BindValue::array(); out.items().reserve(count); - for (lua_Integer i = 1; i <= static_cast(count); ++i) { + for (lua_Integer i = 1; std::cmp_less_equal(i, count); ++i) { lua_rawgeti(l, index, i); auto child = convert(l, -1, BindKindHint::any, depth + 1, std::format("{}[{}]", path, i)); lua_pop(l, 1); diff --git a/engine/src/toast/scripting/ui_bind_convert.hpp b/engine/src/toast/scripting/ui_bind_convert.hpp index 458d6afa6..b869a370b 100644 --- a/engine/src/toast/scripting/ui_bind_convert.hpp +++ b/engine/src/toast/scripting/ui_bind_convert.hpp @@ -14,7 +14,7 @@ struct lua_State; namespace scripting { -enum class BindKindHint { +enum class BindKindHint : uint8_t { any, array, object diff --git a/engine/src/toast/scripting/ui_binds_proxy.cpp b/engine/src/toast/scripting/ui_binds_proxy.cpp index f02caedb8..8628f6617 100644 --- a/engine/src/toast/scripting/ui_binds_proxy.cpp +++ b/engine/src/toast/scripting/ui_binds_proxy.cpp @@ -73,7 +73,12 @@ auto assignBind(lua_State* l, ui::UIBinds& binds, const std::string& name, const } value.push(l); - const BindKindHint hint = wants_array ? BindKindHint::array : wants_object ? BindKindHint::object : BindKindHint::any; + BindKindHint hint = BindKindHint::any; + if (wants_array) { + hint = BindKindHint::array; + } else if (wants_object) { + hint = BindKindHint::object; + } auto converted = luaToBindValue(l, -1, hint); lua_pop(l, 1); if (!converted) { diff --git a/engine/src/toast/ui/bind_definitions.cpp b/engine/src/toast/ui/bind_definitions.cpp index 29e6834dd..06e007ea0 100644 --- a/engine/src/toast/ui/bind_definitions.cpp +++ b/engine/src/toast/ui/bind_definitions.cpp @@ -9,7 +9,7 @@ auto asValue(void* ptr) -> BindValue* { } auto childOf(BindValue& value) -> Rml::DataVariable { - return Rml::DataVariable(bindDefinitionFor(value), &value); + return {bindDefinitionFor(value), &value}; } class ScalarDefinition final : public Rml::VariableDefinition { diff --git a/engine/src/toast/ui/ui_binds.cpp b/engine/src/toast/ui/ui_binds.cpp index 9dc9fe367..263670b15 100644 --- a/engine/src/toast/ui/ui_binds.cpp +++ b/engine/src/toast/ui/ui_binds.cpp @@ -47,8 +47,16 @@ void UIBindStore::reconcile(const DocumentScan& scan) { // a bind that changed kind between reloads const BindValue& current = it->second; - const bool kind_matches = is_array ? current.isArray() : is_object ? current.isObject() : current.isScalar(); - const bool bool_mismatch = + bool kind_matches = false; + if (is_array) { + kind_matches = current.isArray(); + } else if (is_object) { + kind_matches = current.isObject(); + } else { + kind_matches = current.isScalar(); + } + + bool bool_mismatch = is_bool && !is_array && !is_object && current.isScalar() && current.value().GetType() != Rml::Variant::BOOL; if (!kind_matches || bool_mismatch) { it->second = make_default();