-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold_index.lua
More file actions
370 lines (335 loc) · 9.8 KB
/
Copy pathold_index.lua
File metadata and controls
370 lines (335 loc) · 9.8 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
SUI = setmetatable({
UIT = {
CHILDREN_MARKER = 1001,
CHILDREN_KEY_MARKER = 1002,
T_STYLE = 1100,
FRAGMENT = 1101,
CHILDREN_KEY_PROVIDER = 1102,
},
UIT_NO_RENDER = {},
can_be_rendered = function(element)
return element.n and not SUI.UIT_NO_RENDER[element.n]
end,
assign = function(target, ...)
for i = 1, select("#", ...) do
local source = select(i, ...)
if type(source) == "table" then
for k, v in pairs(source) do
target[k] = v
end
end
end
return target
end,
array_assign = function(target, ...)
for i = 1, select("#", ...) do
local source = select(i, ...)
if type(source) == "table" then
for _, v in ipairs(source) do
table.insert(target, v)
end
end
end
return target
end,
array_assign_nodes = function(parent, new_nodes, deep_search)
local result = {}
local marker_found = false
local function search_for_marker(element, insert)
element.v_nodes = element.v_nodes or {}
local nodes = element.v_nodes
for k, v in pairs(nodes) do
local skip = false
if not marker_found then
if v.n == SUI.UIT.CHILDREN_MARKER then
marker_found = true
skip = true
-- table.remove(nodes, k)
local new_k = k - 1
for _, new_node in pairs(new_nodes) do
table.insert(nodes, new_k, new_node)
new_node.v_parent = element
new_k = new_k + 1
end
end
-- if not marker_found and deep_search and v.v_nodes then
-- search_for_marker(v.v_nodes)
-- end
end
if insert and not skip then
result[#result + 1] = v
end
end
end
search_for_marker(parent, true)
if not marker_found then
for _, new_node in pairs(new_nodes) do
table.insert(result, new_node)
end
end
return result
end,
resolve_props_and_nodes = function(props, nodes)
props = props or {}
if not nodes and #props > 0 then
return {}, props
else
return props, nodes
end
end,
set_element_v_nodes = function(element, new_nodes, old_nodes)
local result_v_nodes = {}
new_nodes = new_nodes or {}
local nodes_inserted = false
local insert_new_nodes = function()
nodes_inserted = true
for _, node in pairs(new_nodes or {}) do
local cloned_node = (type(node.clone) == "function" and node.clone())
or SUI.wrap_balatro_ui(node.n, node.config or {}, node.nodes or {})
or node
cloned_node.v_parent = element
result_v_nodes[#result_v_nodes + 1] = cloned_node
end
end
local function search_for_marker(e)
local nodes = e.v_nodes or {}
e.v_nodes = nodes
for k, v in pairs(nodes) do
if v.n == SUI.UIT.CHILDREN_MARKER then
nodes_inserted = true
table.remove(nodes, k)
local new_k = k
for _, new_node in pairs(new_nodes) do
table.insert(nodes, new_k, new_node)
new_node.v_parent = e
new_k = new_k + 1
end
break
end
if not nodes_inserted and v.v_nodes then
search_for_marker(v)
end
end
end
for _, node in pairs(old_nodes or {}) do
local cloned_node = (type(node.clone) == "function" and node.clone())
or SUI.wrap_balatro_ui(node.n, node.config or {}, node.nodes or {})
or node
cloned_node.v_parent = element
if not nodes_inserted and node.n == SUI.UIT.CHILDREN_MARKER then
insert_new_nodes()
else
result_v_nodes[#result_v_nodes + 1] = cloned_node
if element.v_config.deep_children and not nodes_inserted then
search_for_marker(cloned_node)
end
end
end
if not nodes_inserted then
insert_new_nodes()
end
element.v_nodes = result_v_nodes
return element
end,
set_element_v_config = function(element, config)
element.v_config = config
return element
end,
set_element_v_context = function(element)
element.v_context = {}
if element.n == SUI.UIT.T_STYLE then
element.v_context.t_style = SUI.assign({}, element.v_config)
elseif element.n == SUI.UIT.CHILDREN_KEY_PROVIDER then
element.v_context.children_key_provider = SUI.assign({}, element.v_config)
end
return element
end,
calculate_element_config = function(element)
element.config = element.v_config
return element
end,
calculate_element_context = function(element)
element.context = SUI.assign({}, element.v_parent and element.v_parent.context or {})
if element.n == SUI.UIT.T_STYLE then
element.context.t_style = SUI.assign({}, element.context.t_style or {}, element.v_context.t_style)
elseif element.n == G.UIT.T and not element.v_config.no_t_style then
element.config = SUI.assign({}, element.context.t_style or {}, element.v_config)
elseif element.n == SUI.UIT.CHILDREN_KEY_PROVIDER then
element.context.children_key_provider =
SUI.assign({}, element.context.children_key_provider or {}, element.v_context.children_key_provider)
elseif element.n == SUI.UIT.CHILDREN_KEY_MARKER then
local new_elements = {}
for _, node in
pairs((element.context.children_key_provider or {})[element.config.children_marker_key or ""] or {})
do
local clone = node.clone()
clone.v_parent = element
new_elements[#new_elements + 1] = clone
end
element.v_nodes = new_elements
end
return element
end,
render_element = function(element, parent, result_nodes)
local is_extend = not not result_nodes
result_nodes = result_nodes or {}
for _, node in pairs(element.v_nodes or {}) do
if node.n then
SUI.calculate_element_config(node)
SUI.calculate_element_context(node)
if SUI.can_be_rendered(node) then
node.parent = parent or element
SUI.render_element(node)
result_nodes[#result_nodes + 1] = node
else
SUI.render_element(node, element, result_nodes)
end
end
end
if not is_extend then
element.nodes = result_nodes
end
return element
end,
render = function(element)
if not element then
return
end
SUI.calculate_element_config(element)
SUI.calculate_element_context(element)
SUI.render_element(element)
return element
end,
init_balatro_element = function(n, config, nodes, old_nodes)
local element = {
n = n,
v_config = {},
config = {},
v_nodes = {},
nodes = {},
v_parent = nil,
parent = nil,
v_context = {},
context = {},
}
element.clone = function()
return SUI.wrap_balatro_ui(element.n, element.v_config or {}, element.v_nodes)
end
element.clone_func = function()
return function()
return element.clone()
end
end
element.extend = function(new_props, new_nodes)
new_props, new_nodes = SUI.resolve_props_and_nodes(new_props, new_nodes)
return SUI.wrap_balatro_ui(
element.n,
SUI.assign({}, element.v_config or {}, new_props or {}),
new_nodes or {},
element.v_nodes or {}
)
end
element.render = function()
return SUI.render(element)
end
element.render_func = function()
return function()
return element.render()
end
end
element.clone_and_render_func = function()
return function()
return element.clone().render()
end
end
element.get_rendered_topology = function(indent)
indent = indent or ""
local element_line = string.format("%s[%s]", indent, element.n or "x")
if element.n == G.UIT.T and element.config then
local text = element.config.ref_table and element.config.ref_table[element.v_config.ref_value]
or element.config.config
or ""
element_line = element_line .. " " .. text
end
indent = indent .. " "
for _, node in pairs(element.nodes or {}) do
element_line = element_line .. "\n" .. node.get_rendered_topology(indent)
end
return element_line
end
element.get_topology = function(indent)
indent = indent or ""
local element_line = string.format("%s[%s]", indent, element.n or "x")
if element.n == G.UIT.T and element.v_config then
local text = element.v_config.ref_table and element.v_config.ref_table[element.v_config.ref_value]
or element.v_config.text
or ""
element_line = element_line .. " " .. text
end
indent = indent .. " "
for _, node in pairs(element.v_nodes or {}) do
element_line = element_line .. "\n" .. node.get_topology(indent)
end
return element_line
end
element.print_topology = function()
return print("\n" .. element.get_topology())
end
element.print_rendered_topology = function()
return print("\n" .. element.get_rendered_topology())
end
element = setmetatable(element, {
__call = function(t, new_props, new_nodes)
return element.extend(new_props, new_nodes)
end,
})
SUI.set_element_v_config(element, config)
SUI.set_element_v_nodes(element, nodes, old_nodes)
SUI.set_element_v_context(element)
return element
end,
wrap_balatro_ui = function(n, props, nodes, old_nodes)
if not n then
return nil
end
props, nodes = SUI.resolve_props_and_nodes(props, nodes)
return SUI.init_balatro_element(n, props, nodes, old_nodes)
end,
R = function(props, nodes)
return SUI.wrap_balatro_ui(G.UIT.R, props, nodes)
end,
C = function(props, nodes)
return SUI.wrap_balatro_ui(G.UIT.C, props, nodes)
end,
T = function(props)
return SUI.wrap_balatro_ui(G.UIT.T, props)
end,
T_STYLE = function(props, nodes)
return SUI.wrap_balatro_ui(SUI.UIT.T_STYLE, props, nodes)
end,
B = function(props)
return SUI.wrap_balatro_ui(G.UIT.B, props)
end,
ROOT = function(props, nodes)
return SUI.wrap_balatro_ui(G.UIT.ROOT, props, nodes)
end,
CHILDREN_MARKER = function()
return SUI.wrap_balatro_ui(SUI.UIT.CHILDREN_MARKER)
end,
CHILDREN_KEY_MARKER = function(key)
return SUI.wrap_balatro_ui(SUI.UIT.CHILDREN_KEY_MARKER, { children_marker_key = key })
end,
FRAGMENT = function(props, nodes)
return SUI.wrap_balatro_ui(SUI.UIT.FRAGMENT, props, nodes)
end,
CHILDREN_KEY_PROVIDER = function(props, nodes)
return SUI.wrap_balatro_ui(SUI.UIT.CHILDREN_KEY_PROVIDER, props, nodes)
end,
}, {})
SUI.UIT_NO_RENDER = {
[SUI.UIT.CHILDREN_MARKER] = true,
[SUI.UIT.CHILDREN_KEY_MARKER] = true,
[SUI.UIT.T_STYLE] = true,
[SUI.UIT.FRAGMENT] = true,
[SUI.UIT.CHILDREN_KEY_PROVIDER] = true,
}