z_mirror/TaskList

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

raiguardMostly hook up task details GUI (#7)fa77a7d

task-details-gui
1.7 KiB60 linesraw
1local table = require("__flib__.table")
2
3local util = {}
4
5--- Create a local flying text with an error sound.
6--- @param player LuaPlayer
7--- @param text LocalisedString
8function util.error_text(player, text)
9  player.create_local_flying_text({ create_at_cursor = true, text = text })
10  player.play_sound({ path = "utility/cannot_build" })
11end
12
13--- @class GuiIdent
14--- @field name string
15--- @field id string|number
16
17--- Safely retrieve the GUI for the given player.
18--- @param player_index number
19--- @param gui_ident string|GuiIdent
20--- @return TasksGui
21function util.get_gui(player_index, gui_ident)
22  local player_table = global.players[player_index]
23  if player_table then
24    local Gui
25    if type(gui_ident) == "table" then
26      Gui = player_table.guis[gui_ident.name][gui_ident.id]
27    else
28      Gui = player_table.guis[gui_ident]
29    end
30    if Gui and Gui.refs.window.valid then
31      return Gui
32    end
33  end
34end
35
36--- Update completed visibility for all subtasks
37--- @param flow LuaGuiElement
38--- @param to_state boolean
39function util.recursive_show_completed(flow, to_state)
40  for _, row in pairs(flow.incompleted.children) do --- @diagnostic disable-line
41    util.recursive_show_completed(row.details_flow.subtasks_flow, to_state)
42  end
43
44  local completed_flow = flow.completed --- @diagnostic disable-line
45  if to_state and #completed_flow.children > 0 then
46    completed_flow.visible = true
47
48    for _, row in pairs(completed_flow.children) do
49      util.recursive_show_completed(row.details_flow.subtasks_flow, to_state)
50    end
51  else
52    completed_flow.visible = false
53  end
54end
55
56function util.remove_task(source, task_id)
57  table.remove(source, table.find(source, task_id))
58end
59
60return util