-
Notifications
You must be signed in to change notification settings - Fork 0
/
EZGUI.lua
193 lines (171 loc) · 5.77 KB
/
EZGUI.lua
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
--[[
EZGUI v0.1.4
]]
local _ModTextFileGetContent = ModTextFileGetContent
function shallow_copy(t)
local t2 = {}
for k,v in pairs(t) do
t2[k] = v
end
return t2
end
-- Thanks to dextercd#7326 on Discord for helping me debug this and coming up with the final working version
local function make_observable(t, key, prev_keys)
if type(t) ~= "table" or getmetatable(t) then
return
end
local prev_keys = prev_keys or {}
local _data = {}
if key then
table.insert(prev_keys, key)
end
for k, v in pairs(t) do
_data[k] = v
t[k] = nil
make_observable(v, k, shallow_copy(prev_keys))
end
setmetatable(t, {
__index = function(self, key)
return _data[key]
end,
__newindex = function(self, key, value)
if type(value) == "table" then
make_observable(value, key, shallow_copy(prev_keys))
end
_data[key] = value
path = table.concat(prev_keys, ".")
if path ~= '' then
path = path .. '.'
end
path = path .. key
-- print(path .. " changed!")
end
})
end
return {
init = function(self_path)
if self_path then
-- Remove trailing / at the end and add it again to allow both versions
self_path = self_path:gsub("/$", "") .. "/"
local files = {
("%scss_props.lua"):format(self_path),
("%scss.lua"):format(self_path),
("%soop.lua"):format(self_path),
("%sparsing_functions.lua"):format(self_path),
("%sstring_buffer.lua"):format(self_path),
("%sutils.lua"):format(self_path),
("%selements/DOMElement.lua"):format(self_path),
("%selements/Layout.lua"):format(self_path),
("%selements/Text.lua"):format(self_path),
("%selements/Input.lua"):format(self_path),
("%selements/Button.lua"):format(self_path),
("%selements/Image.lua"):format(self_path),
("%selements/Slider.lua"):format(self_path),
}
for i, filepath in ipairs(files) do
local content = ModTextFileGetContent(filepath)
local s = content:gsub("%%PATH%%", self_path)
ModTextFileSetContent(filepath, s)
end
end
self_path = self_path and self_path or ""
local pretty = dofile_once(self_path .. "lib/pretty.lua")
local Layout = dofile_once(self_path .. "elements/Layout.lua")
local Text = dofile_once(self_path .. "elements/Text.lua")
local Input = dofile_once(self_path .. "elements/Input.lua")
local Button = dofile_once(self_path .. "elements/Button.lua")
local Image = dofile_once(self_path .. "elements/Image.lua")
local Slider = dofile_once(self_path .. "elements/Slider.lua")
local nxml = dofile_once(self_path .. "lib/nxml.lua")
local utils = dofile_once(self_path .. "utils.lua")
local css = dofile_once(self_path .. "css.lua")
local parser = dofile_once(self_path .. "parsing_functions.lua")
local DOM_Elements = {
Layout = Layout,
Text = Text,
Button = Button,
Image = Image,
Slider = Slider,
Input = Input,
}
local dom_cache = {}
local function make_dom_from_nxml_table(xml, xml_content, data_context)
local root_layout, style_element
for i, element in ipairs(xml) do
if element.name == "Layout" then
root_layout = element
elseif element.name == "Style" then
style_element = element
end
end
if not root_layout then
error("No root 'Layout' found", 2)
end
local rulesets = {}
if style_element then
local text, text_pos = style_element:text()
text = parser.convert_css_comments_to_spaces(text)
rulesets = style_element and parse_style_rulesets(text, function(str, msg, pos, lvl)
utils.throw_error(xml_content, msg, text_pos + pos, 2)
end)
end
local function make_dom(element)
if not DOM_Elements[element.name] then
error("Unknown element type: '" .. element.name .. "'", 4)
end
local dom_element = DOM_Elements[element.name](element, data_context)
-- TODO: Check if the element can even contain children / valid children types
for i, child in ipairs(element.children) do
local child_dom = make_dom(child)
dom_element:AddChild(child_dom)
end
return dom_element
end
local dom = make_dom(root_layout)
local function apply_rules(dom_element)
css.apply_matching_rules_to_element(dom_element, rulesets)
for i, child in ipairs(dom_element.children) do
apply_rules(child)
end
end
apply_rules(dom)
return dom
end
local function make_dom_from_xml_file(xml_path, data_context)
local xml_content = _ModTextFileGetContent(xml_path)
local xml = nxml.parse_many(xml_content)
return make_dom_from_nxml_table(xml, xml_content, data_context)
end
local function create_id_generator()
local id = 1
return function()
id = id + 1
return id
end
end
local _gui = GuiCreate()
local observing = {}
return function(x, y, content, data, gui)
if not gui then
GuiStartFrame(_gui)
end
if not observing[data] then
make_observable(data)
observing[data] = true
end
gui = gui or _gui
if not dom_cache[content] then
if type(content) == "string" then
dom_cache[content] = make_dom_from_xml_file(content, data)
else
dom_cache[content] = make_dom_from_nxml_table(content.xml, content.xml_string, data)
end
dom_cache[content].style.margin_left = x
dom_cache[content].style.margin_top = y
end
local new_id = create_id_generator()
local root_layout = dom_cache[content]
return root_layout:Render(gui, new_id, data)
end
end
}