z_mirror/TaskList

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

akxcv[feature] Filter active tasks: Assigned to me (#27)125cb06

fix/blueprint-sandboxes-force
2.1 KiB80 linesraw
1local gui = require("__flib__/gui")
2local mod_gui = require("__core__/lualib/mod-gui")
3
4local active_task_button = {}
5
6--- @param player LuaPlayer
7--- @param player_table PlayerTable
8function active_task_button.build(player, player_table)
9  local button = player_table.guis.active_task_button
10  if not button or not button.valid then
11    button = gui.add(mod_gui.get_button_flow(player), {
12      type = "button",
13      style = mod_gui.button_style,
14      style_mods = { left_padding = 8, right_padding = 8 },
15      actions = {
16        on_click = { gui = "tasks", action = "toggle" },
17      },
18    })
19
20    player_table.guis.active_task_button = button
21  end
22
23  active_task_button.update(player, player_table)
24end
25
26--- @param player LuaPlayer
27--- @param Task Task
28--- @return boolean
29local function can_show_task(player, Task)
30  if not Task then
31    return false
32  end
33  if Task.status ~= "in_progress" then
34    return false
35  end
36  if player.mod_settings["tlst-active-filter-assigned"].value then
37    if Task.assignee == nil then
38      return false
39    end
40    return Task.assignee.index == player.index
41  end
42
43  return true
44end
45
46--- @param player LuaPlayer
47--- @param player_table PlayerTable
48function active_task_button.update(player, player_table)
49  local button = player_table.guis.active_task_button
50  if button and button.valid then
51    local tasks
52    if player.mod_settings["tlst-show-active-task"].value == "force" then
53      tasks = global.forces[player.force.index].tasks
54    else
55      tasks = player_table.tasks
56    end
57    -- The "active" task is the first top-level active task we come across
58    for _, task_id in pairs(tasks) do
59      local Task = global.tasks[task_id]
60      if can_show_task(player, Task) then
61        button.caption = Task.title
62        return
63      end
64    end
65    button.caption = { "gui.tlst-no-active-task" }
66  end
67end
68
69--- @param player_table PlayerTable
70function active_task_button.destroy(player_table)
71  local button = player_table.guis.active_task_button
72  if button then
73    if button.valid then
74      button.destroy()
75    end
76    player_table.guis.active_task_button = nil
77  end
78end
79
80return active_task_button