mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-30 07:15:34 +00:00
Allow UI Elements in UI Content
This commit is contained in:
parent
47d176e6ed
commit
f037dc814d
3 changed files with 16 additions and 11 deletions
|
@ -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<Element>())
|
||||
return true;
|
||||
if (object.get_type() != sol::type::table)
|
||||
return false;
|
||||
sol::table table = object;
|
||||
|
|
|
@ -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(); }
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue