z_mirror/TaskList

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

raiguardMostly hook up task details GUI (#7)fa77a7d

task-details-gui
3.2 KiB128 linesraw
1local constants = require("constants")
2
3local edit_task_gui = require("scripts.gui.edit-task.index")
4local util = require("scripts.util")
5
6local actions = {}
7
8--- @param Gui TaskGui
9function actions.close(Gui)
10  Gui:destroy()
11end
12
13--- @param Gui TaskGui
14--- @param e on_gui_click
15function actions.recenter(Gui, _, e)
16  if e.button == defines.mouse_button_type.middle then
17    Gui.refs.window.force_auto_center()
18  end
19end
20
21--- @param Gui TasksGui
22--- @param e on_gui_checked_state_changed
23function actions.toggle_show_completed(Gui, _, e)
24  local state = Gui.state
25  state.show_completed = not state.show_completed
26  e.element.state = state.show_completed
27
28  Gui:update_show_completed()
29end
30
31--- @param Gui TasksGui
32function actions.edit_task(Gui, msg)
33  if not util.get_gui(Gui.player.index, "edit_task") then
34    local Task = msg.task_id and global.tasks[msg.task_id] or nil
35    local ParentTask = msg.parent_task_id and global.tasks[msg.parent_task_id] or nil
36    edit_task_gui.new(Gui.player, Gui.player_table, {
37      parent_gui = Gui,
38      task = Task,
39      parent_task = ParentTask,
40    })
41  end
42end
43
44--- @param Gui TasksGui
45--- @param msg table
46--- @param e on_gui_checked_state_changed
47function actions.toggle_task_completed(Gui, msg, e)
48  local task_id = msg.task_id
49
50  local Task = global.tasks[task_id]
51  if Task then
52    Task:toggle_completed()
53  end
54end
55
56--- @param Gui TasksGui
57--- @param e on_gui_click
58function actions.expand_task(Gui, _, e)
59  local elem = e.element
60  local details_flow = elem.parent.parent.details_flow
61
62  if e.control or e.shift then
63    local our_index = elem.parent.parent.get_index_in_parent()
64    local delta = e.shift and 1 or -1
65    elem.parent.parent.parent.swap_children(our_index + delta, our_index)
66    return
67  end
68
69  details_flow.visible = not details_flow.visible
70  if details_flow.visible then
71    elem.sprite = "tlst_arrow_down"
72  else
73    elem.sprite = "tlst_arrow_right"
74  end
75end
76
77--- @param Gui TasksGui
78--- @param e on_gui_switch_state_changed
79function actions.toggle_tasks_mode(Gui, _, e)
80  local visible = e.element.switch_state == "left" and "force" or "private"
81
82  Gui.refs.force_flow.visible = visible == "force"
83  Gui.refs.private_flow.visible = visible == "private"
84end
85
86--- @param Gui TasksGui
87--- @param msg table
88--- @param e on_gui_click
89function actions.move_task(Gui, msg, e)
90  local delta = msg.delta
91  local task_id = msg.task_id
92
93  local Task = global.tasks[task_id]
94  if Task then
95    Task:move(delta)
96  end
97end
98
99--- @param Gui TasksGui
100--- @param msg table
101function actions.cycle_task_status(Gui, msg)
102  local Task = global.tasks[msg.task_id]
103  if Task then
104    local current = Task.status
105    local next = next(constants.task_status, current) or next(constants.task_status)
106    Task.status = next
107
108    Task:update_guis(function(Gui)
109      Gui:update_task(Task)
110    end)
111  end
112end
113
114--- @param Gui TasksGui
115--- @param msg table
116function actions.open_task_gui(Gui, msg)
117  local Task = global.tasks[msg.task_id]
118  if Task then
119    local TaskGui = Gui.player_table.guis.task[msg.task_id]
120    if not TaskGui then
121      game.print("REQUIRE LOOP BABY")
122      -- TaskGui = task_gui.new(Gui.player, Gui.player_table, Task)
123    end
124    -- TODO:
125  end
126end
127
128return actions