z_mirror/TaskList

git clone https://git.raiguard.me/z_mirror/TaskList

Caleb HeuerAdd legacy flib gui module5843c65

trunk
8.0 KiB332 linesraw
1if ... ~= "gui" then
2  return require("gui")
3end
4
5--- @diagnostic disable
6
7local mod_name = script.mod_name
8local gui_event_defines = {}
9
10local event_id_to_string_mapping = {}
11for name, id in pairs(defines.events) do
12  if string.find(name, "^on_gui") then
13    gui_event_defines[name] = id
14    event_id_to_string_mapping[id] = string.gsub(name, "^on_gui", "on")
15  end
16end
17
18--- @deprecated use `gui-lite` instead
19local flib_gui = {}
20
21--- @deprecated use `gui-lite` instead
22function flib_gui.hook_events(callback)
23  local on_event = script.on_event
24  for _, id in pairs(gui_event_defines) do
25    on_event(id, callback)
26  end
27end
28
29--- @deprecated use `gui-lite` instead
30function flib_gui.read_action(event_data)
31  local elem = event_data.element
32  if not elem or not elem.valid then
33    return
34  end
35
36  local mod_tags = elem.tags[mod_name]
37  if not mod_tags then
38    return
39  end
40
41  local elem_actions = mod_tags.flib
42  if not elem_actions then
43    return
44  end
45
46  local event_name = event_id_to_string_mapping[event_data.name]
47  local msg = elem_actions[event_name]
48
49  return msg
50end
51
52--- @deprecated use `gui-lite` instead
53local function recursive_build(parent, structure, refs)
54  -- If the structure has no type, just ignore it
55  -- This is to make it possible to pass unit types `{}` to represent "no element" without breaking things
56  if not structure.type then
57    return
58  end
59
60  -- Prepare tags
61  local original_tags = structure.tags
62  local tags = original_tags or {}
63  local actions = structure.actions
64  local tags_flib = tags.flib
65  tags.flib = actions
66  structure.tags = { [mod_name] = tags }
67
68  -- Make the game not convert these into a property tree for no reason
69  structure.actions = nil
70  -- Substructures can be defined in special tables or as the array portion of this structure
71  local substructures
72  local substructures_len = #structure
73  if substructures_len > 0 then
74    if structure.children or structure.tabs then
75      error("Children or tab-and-content pairs must ALL be in the array portion, or a subtable. Not both at once!")
76    end
77    substructures = {}
78    for i = 1, substructures_len do
79      substructures[i] = structure[i]
80      structure[i] = nil
81    end
82  else
83    substructures = structure.children or structure.tabs
84    structure.children = nil
85    structure.tabs = nil
86  end
87
88  -- Create element
89  local elem = parent.add(structure)
90
91  -- Restore structure
92  structure.tags = original_tags
93  structure.actions = actions
94  tags.flib = tags_flib
95
96  local style_mods = structure.style_mods
97  if style_mods then
98    for k, v in pairs(style_mods) do
99      elem.style[k] = v
100    end
101  end
102
103  local elem_mods = structure.elem_mods
104  if elem_mods then
105    for k, v in pairs(elem_mods) do
106      elem[k] = v
107    end
108  end
109
110  local ref = structure.ref
111  if ref then
112    -- Recursively create tables as needed
113    local prev = refs
114    local ref_length = #ref
115    for i = 1, ref_length - 1 do
116      local current_key = ref[i]
117      local current = prev[current_key]
118      if not current then
119        current = {}
120        prev[current_key] = current
121      end
122      prev = current
123    end
124    prev[ref[ref_length]] = elem
125  end
126
127  -- Substructures
128  if substructures then
129    if structure.type == "tabbed-pane" then
130      local add_tab = elem.add_tab
131      for i = 1, #substructures do
132        local tab_and_content = substructures[i]
133        if not (tab_and_content.tab and tab_and_content.content) then
134          error("TabAndContent must have `tab` and `content` fields")
135        end
136        local tab = recursive_build(elem, tab_and_content.tab, refs)
137        local content = recursive_build(elem, tab_and_content.content, refs)
138        add_tab(tab, content)
139      end
140    else
141      for i = 1, #substructures do
142        recursive_build(elem, substructures[i], refs)
143      end
144    end
145  end
146
147  return elem
148end
149
150--- @deprecated use `gui-lite` instead
151function flib_gui.build(parent, structures)
152  local refs = {}
153  for i = 1, #structures do
154    recursive_build(parent, structures[i], refs)
155  end
156  return refs
157end
158
159--- @deprecated use `gui-lite` instead
160function flib_gui.add(parent, structure)
161  -- Just in case they had a ref in the structure already, extract it
162  local previous_ref = structure.ref
163  -- Put in a known ref that we can use later
164  structure.ref = { "FLIB_ADD_ROOT" }
165  -- Build the element
166  local refs = {}
167  recursive_build(parent, structure, refs)
168  -- Restore the previous ref
169  structure.ref = previous_ref
170  -- Return the element
171  return refs.FLIB_ADD_ROOT
172end
173
174--- @deprecated use `gui-lite` instead
175local function recursive_update(elem, updates)
176  if updates.cb then
177    updates.cb(elem)
178  end
179
180  if updates.style then
181    elem.style = updates.style
182  end
183
184  if updates.style_mods then
185    for key, value in pairs(updates.style_mods) do
186      elem.style[key] = value
187    end
188  end
189
190  if updates.elem_mods then
191    for key, value in pairs(updates.elem_mods) do
192      elem[key] = value
193    end
194  end
195
196  if updates.tags then
197    flib_gui.update_tags(elem, updates.tags)
198  end
199
200  -- TODO: This could be a lot better
201  if updates.actions then
202    for event_name, payload in pairs(updates.actions) do
203      flib_gui.set_action(elem, event_name, payload)
204    end
205  end
206
207  local substructures
208  local substructures_len = #updates
209  if substructures_len > 0 then
210    if updates.children or updates.tabs then
211      error("Children or tab-and-content pairs must ALL be in the array portion, or a subtable. Not both at once!")
212    end
213    substructures = {}
214    for i = 1, substructures_len do
215      substructures[i] = updates[i]
216      updates[i] = nil
217    end
218  else
219    substructures = updates.children or updates.tabs
220    updates.children = nil
221    updates.tabs = nil
222  end
223  local subelements
224  if elem.type == "tabbed-pane" then
225    subelements = elem.tabs
226  else
227    subelements = elem.children
228  end
229
230  if substructures then
231    for i, substructure in pairs(substructures) do
232      if substructure.tab or substructure.content then
233        local elem_tab_and_content = subelements[i]
234        if elem_tab_and_content then
235          local tab = elem_tab_and_content.tab
236          local tab_updates = substructures.tab
237          if tab and tab_updates then
238            recursive_update(tab, tab_updates)
239          end
240          local content = elem_tab_and_content.content
241          local content_updates = substructures.content
242          if content and content_updates then
243            recursive_update(content, content_updates)
244          end
245        end
246      elseif subelements[i] then
247        recursive_update(subelements[i], substructure)
248      end
249    end
250  end
251end
252
253--- @deprecated use `gui-lite` instead
254function flib_gui.update(elem, updates)
255  recursive_update(elem, updates)
256end
257
258--- @deprecated use `gui-lite` instead
259function flib_gui.get_tags(elem)
260  return elem.tags[mod_name] or {}
261end
262
263--- @deprecated use `gui-lite` instead
264function flib_gui.set_tags(elem, tags)
265  local elem_tags = elem.tags
266  elem_tags[mod_name] = tags
267  elem.tags = elem_tags
268end
269
270--- @deprecated use `gui-lite` instead
271function flib_gui.delete_tags(elem)
272  local elem_tags = elem.tags
273  elem_tags[mod_name] = nil
274  elem.tags = elem_tags
275end
276
277--- @deprecated use `gui-lite` instead
278function flib_gui.update_tags(elem, updates)
279  local elem_tags = elem.tags
280  local existing = elem_tags[mod_name]
281
282  if not existing then
283    existing = {}
284    elem_tags[mod_name] = existing
285  end
286
287  for k, v in pairs(updates) do
288    existing[k] = v
289  end
290
291  elem.tags = elem_tags
292end
293
294--- @deprecated use `gui-lite` instead
295function flib_gui.set_action(elem, event_name, msg)
296  local elem_tags = elem.tags
297  local existing = elem_tags[mod_name]
298
299  if not existing then
300    existing = {}
301    elem_tags[mod_name] = existing
302  end
303
304  local actions = existing.flib
305  if not actions then
306    actions = {}
307    existing.flib = actions
308  end
309
310  actions[event_name] = msg or nil
311
312  elem.tags = elem_tags
313end
314
315--- @deprecated use `gui-lite` instead
316function flib_gui.get_action(elem, event_name)
317  local elem_tags = elem.tags
318  local existing = elem_tags[mod_name]
319
320  if not existing then
321    return
322  end
323
324  local actions = existing.flib
325  if not actions then
326    return
327  end
328
329  return actions[event_name]
330end
331
332return flib_gui