mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-04-28 21:07:59 +03: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 "content.hpp"
|
||||||
|
#include "element.hpp"
|
||||||
|
|
||||||
namespace LuaUi
|
namespace LuaUi
|
||||||
{
|
{
|
||||||
|
@ -14,6 +15,8 @@ namespace LuaUi
|
||||||
|
|
||||||
bool isValidContent(const sol::object& object)
|
bool isValidContent(const sol::object& object)
|
||||||
{
|
{
|
||||||
|
if (object.is<Element>())
|
||||||
|
return true;
|
||||||
if (object.get_type() != sol::type::table)
|
if (object.get_type() != sol::type::table)
|
||||||
return false;
|
return false;
|
||||||
sol::table table = object;
|
sol::table table = object;
|
||||||
|
|
|
@ -22,7 +22,7 @@ namespace LuaUi
|
||||||
: mTable(std::move(table))
|
: mTable(std::move(table))
|
||||||
{
|
{
|
||||||
if (!isValidContent(mTable))
|
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(); }
|
size_t size() const { return mTable.size(); }
|
||||||
|
|
|
@ -1,12 +1,17 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
M.__Content = true
|
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)
|
M.new = function(source)
|
||||||
local result = {}
|
local result = {}
|
||||||
result.__nameIndex = {}
|
result.__nameIndex = {}
|
||||||
for i, v in ipairs(source) do
|
for i, v in ipairs(source) do
|
||||||
if type(v) ~= 'table' then
|
validateContentChild(v)
|
||||||
error('Content can only contain tables')
|
|
||||||
end
|
|
||||||
result[i] = v
|
result[i] = v
|
||||||
if type(v.name) == 'string' then
|
if type(v.name) == 'string' then
|
||||||
result.__nameIndex[v.name] = i
|
result.__nameIndex[v.name] = i
|
||||||
|
@ -38,9 +43,7 @@ end
|
||||||
local methods = {
|
local methods = {
|
||||||
insert = function(self, index, value)
|
insert = function(self, index, value)
|
||||||
validateIndex(self, index)
|
validateIndex(self, index)
|
||||||
if type(value) ~= 'table' then
|
validateContentChild(value)
|
||||||
error('Content can only contain tables')
|
|
||||||
end
|
|
||||||
for i = #self, index, -1 do
|
for i = #self, index, -1 do
|
||||||
rawset(self, i + 1, rawget(self, i))
|
rawset(self, i + 1, rawget(self, i))
|
||||||
local name = rawget(self, i + 1)
|
local name = rawget(self, i + 1)
|
||||||
|
@ -56,7 +59,7 @@ local methods = {
|
||||||
indexOf = function(self, value)
|
indexOf = function(self, value)
|
||||||
if type(value) == 'string' then
|
if type(value) == 'string' then
|
||||||
return self.__nameIndex[value]
|
return self.__nameIndex[value]
|
||||||
elseif type(value) == 'table' then
|
else
|
||||||
for i = 1, #self do
|
for i = 1, #self do
|
||||||
if rawget(self, i) == value then
|
if rawget(self, i) == value then
|
||||||
return i
|
return i
|
||||||
|
@ -113,10 +116,9 @@ M.__newindex = function(self, key, value)
|
||||||
local index = getIndexFromKey(self, key)
|
local index = getIndexFromKey(self, key)
|
||||||
if value == nil then
|
if value == nil then
|
||||||
remove(self, index)
|
remove(self, index)
|
||||||
elseif type(value) == 'table' then
|
|
||||||
assign(self, index, value)
|
|
||||||
else
|
else
|
||||||
error('Content can only contain tables')
|
validateContentChild(value)
|
||||||
|
assign(self, index, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
M.__tostring = function(self)
|
M.__tostring = function(self)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue