File: content.lua

package info (click to toggle)
openmw 0.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,076 kB
  • sloc: cpp: 380,958; xml: 2,192; sh: 1,449; python: 911; makefile: 26; javascript: 5
file content (142 lines) | stat: -rw-r--r-- 3,601 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
        validateContentChild(v)
        result[i] = v
        if type(v.name) == 'string' then
            result.__nameIndex[v.name] = i
        end
    end
    return setmetatable(result, M)
end
local function validateIndex(self, index)
    if type(index) ~= 'number' then
        error('Unexpected Content key: ' .. tostring(index))
    end
    if index < 1 or (#self + 1) < index then
        error('Invalid Content index: ' .. tostring(index))
    end
end

local function getIndexFromKey(self, key)
    local index = key
    if type(key) == 'string' then
        index = self.__nameIndex[key]
        if not index then
            error('Unexpected content key:' .. key)
        end
    end
    validateIndex(self, index)
    return index
end

local methods = {
    insert = function(self, index, value)
        validateIndex(self, index)
        validateContentChild(value)
        for i = #self, index, -1 do
            rawset(self, i + 1, rawget(self, i))
            local name = rawget(self, i + 1)
            if name then
                self.__nameIndex[name] = i + 1
            end
        end
        rawset(self, index, value)
        if value.name then
            self.__nameIndex[value.name] = index
        end
    end,
    indexOf = function(self, value)
        if type(value) == 'string' then
            return self.__nameIndex[value]
        else
            for i = 1, #self do
                if rawget(self, i) == value then
                    return i
                end
            end
        end
        return nil
    end,
    add = function(self, value)
        self:insert(#self + 1, value)
        return #self
    end,
}
M.__index = function(self, key)
    if methods[key] then return methods[key] end
    local index = getIndexFromKey(self, key)
    return rawget(self, index)
end
local function nameAt(self, index)
    local v = rawget(self, index)
    return v and type(v.name) == 'string' and v.name
end

local function remove(self, index)
    local oldName = nameAt(self, index)
    if oldName then
        self.__nameIndex[oldName] = nil
    end
    if index > #self then
        error('Invalid Content index:' .. tostring(index))
    end
    for i = index, #self - 1 do
        local v = rawget(self, i + 1)
        rawset(self, i, v)
        if type(v.name) == 'string' then
            self.__nameIndex[v.name] = i
        end
    end
    rawset(self, #self, nil)
end

local function assign(self, index, value)
    local oldName = nameAt(self, index)
    if oldName then
        self.__nameIndex[oldName] = nil
    end
    rawset(self, index, value)
    if value.name then
        self.__nameIndex[value.name] = index
    end
end

M.__newindex = function(self, key, value)
    local index = getIndexFromKey(self, key)
    if value == nil then
        remove(self, index)
    else
        validateContentChild(value)
        assign(self, index, value)
    end
end
M.__tostring = function(self)
    return ('UiContent{%d layouts}'):format(#self)
end
local function next(self, index)
    local v = rawget(self, index)
    if v then
        return index + 1, v
    else
        return nil, nil
    end
end

M.__pairs = function(self)
    return next, self, 1
end
M.__ipairs = M.__pairs
M.__metatable = false

return M