From f037dc814d5447efee884328d83398d56ee59260 Mon Sep 17 00:00:00 2001 From: uramer Date: Sat, 11 Nov 2023 11:35:58 +0100 Subject: [PATCH] Allow UI Elements in UI Content --- components/lua_ui/content.cpp | 3 +++ components/lua_ui/content.hpp | 2 +- components/lua_ui/content.lua | 22 ++++++++++++---------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/components/lua_ui/content.cpp b/components/lua_ui/content.cpp index 2e1d4ca0c4..dd169a9291 100644 --- a/components/lua_ui/content.cpp +++ b/components/lua_ui/content.cpp @@ -1,4 +1,5 @@ #include "content.hpp" +#include "element.hpp" namespace LuaUi { @@ -14,6 +15,8 @@ namespace LuaUi bool isValidContent(const sol::object& object) { + if (object.is()) + return true; if (object.get_type() != sol::type::table) return false; sol::table table = object; diff --git a/components/lua_ui/content.hpp b/components/lua_ui/content.hpp index c8bb82ecf3..945f833b48 100644 --- a/components/lua_ui/content.hpp +++ b/components/lua_ui/content.hpp @@ -22,7 +22,7 @@ namespace LuaUi : mTable(std::move(table)) { if (!isValidContent(mTable)) - throw std::domain_error("Expected a Content table"); + throw std::domain_error("Invalid UI Content"); } size_t size() const { return mTable.size(); } diff --git a/components/lua_ui/content.lua b/components/lua_ui/content.lua index fbd39d5f68..99fdb86b70 100644 --- a/components/lua_ui/content.lua +++ b/components/lua_ui/content.lua @@ -1,12 +1,17 @@ local M = {} M.__Content = true + +function validateContentChild(v) + if not (type(v) == 'table' or v.__type and v.__type.name == 'LuaUi::Element') then + error('Content can only contain tables and Elements') + end +end + M.new = function(source) local result = {} result.__nameIndex = {} for i, v in ipairs(source) do - if type(v) ~= 'table' then - error('Content can only contain tables') - end + validateContentChild(v) result[i] = v if type(v.name) == 'string' then result.__nameIndex[v.name] = i @@ -38,9 +43,7 @@ end local methods = { insert = function(self, index, value) validateIndex(self, index) - if type(value) ~= 'table' then - error('Content can only contain tables') - end + validateContentChild(value) for i = #self, index, -1 do rawset(self, i + 1, rawget(self, i)) local name = rawget(self, i + 1) @@ -56,7 +59,7 @@ local methods = { indexOf = function(self, value) if type(value) == 'string' then return self.__nameIndex[value] - elseif type(value) == 'table' then + else for i = 1, #self do if rawget(self, i) == value then return i @@ -113,10 +116,9 @@ M.__newindex = function(self, key, value) local index = getIndexFromKey(self, key) if value == nil then remove(self, index) - elseif type(value) == 'table' then - assign(self, index, value) else - error('Content can only contain tables') + validateContentChild(value) + assign(self, index, value) end end M.__tostring = function(self)