z_mirror/TaskList

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

raiguardMostly hook up task details GUI (#7)fa77a7d

task-details-gui
4.6 KiB156 linesraw
1--[[
2  DESIGN GOALS:
3  - Extremely simple to-do list
4  - Compact and full versions of the UI
5    - Compact for reference, full for manipulation
6  - Opt-in advanced features
7    - Timed reminders (interval, absolute time)
8    - Notifications for reminders
9    - Task assignment
10    - Maybe projects?
11  - Current task must be easily visible in some way
12    - Consider using the bottom-right of the screen for this
13  - "Private" and "public" tasks
14    - Private is visible only to you, public is visible to your force
15  - An infinite amount of subtask nesting
16  - Import and export TODOs
17
18  DESIGN NOTES:
19  - Each task will have an entirely unique ID
20    - Store the next ID in the root of `global`
21  - Tasks will be stored in a one-dimensional table keyed by task ID
22  - Tasks can be owned by the force, or by the player
23    - Player tasks will be shown separately from force tasks
24  - A task can have infinite subtasks
25    - Subtasks are just pointers to other tasks, so they support the full feature suite, including more subtasks
26    - Subtasks may have a different assignee from their parent
27  - Import and export will use the standard method
28  - Each owner (force, player, or task) will have a `tasks` array containing the task IDs to show, in order
29    - Subtasks are also stored in the same manner, but are in a `subtasks` table instead
30  - When a task is completed, it is added to its owners `completed_tasks` array at the front
31    - Completed tasks will be listed after active tasks, in the order that they were completed
32    - Store completion time?
33]]
34
35local event = require("__flib__.event")
36local gui = require("__flib__.gui")
37local migration = require("__flib__.migration")
38
39local migrations = require("scripts.migrations")
40local edit_task_gui = require("scripts.gui.edit-task.index")
41local player_data = require("scripts.player-data")
42local task = require("scripts.task")
43local tasks_gui = require("scripts.gui.tasks.index")
44local util = require("scripts.util")
45
46-- BOOTSTRAP
47
48event.on_init(function()
49  global.forces = {}
50  global.next_task_id = 1
51  global.players = {}
52  global.tasks = {}
53
54  for _, force in pairs(game.forces) do
55    global.forces[force.index] = {
56      completed_tasks = {},
57      tasks = {},
58    }
59  end
60  for _, player in pairs(game.players) do
61    player_data.init(player)
62  end
63
64  migrations.generic()
65end)
66
67event.on_load(function()
68  for _, Task in pairs(global.tasks) do
69    task.load(Task)
70  end
71
72  for _, player_table in pairs(global.players) do
73    tasks_gui.load(player_table.guis.tasks)
74    if player_table.guis.edit_task then
75      edit_task_gui.load(player_table.guis.edit_task)
76    end
77  end
78end)
79
80event.on_configuration_changed(function(e)
81  if migration.on_config_changed(e, migrations.versions) then
82    migrations.generic()
83  end
84end)
85
86-- INTERACTION
87
88gui.hook_events(function(e)
89  local msg = gui.read_action(e)
90  if msg then
91    local Gui = util.get_gui(e.player_index, msg.gui)
92    if Gui then
93      Gui:dispatch(msg, e)
94    end
95  end
96end)
97
98event.register("tlst-linked-confirm-gui", function(e)
99  --- @type EditTaskGui
100  local EditTaskGui = util.get_gui(e.player_index, "edit_task")
101  if EditTaskGui then
102    EditTaskGui:dispatch({ action = "confirm" })
103    game.get_player(e.player_index).play_sound({ path = "utility/confirm" })
104  end
105end)
106
107--- @param player_index number
108local function toggle_new_task(player_index)
109  local EditTaskGui = util.get_gui(player_index, "edit_task")
110  if EditTaskGui then
111    EditTaskGui:destroy()
112  else
113    local player = game.get_player(player_index)
114    local player_table = global.players[player_index]
115    edit_task_gui.new(player, player_table, { standalone = true })
116  end
117end
118
119event.register("tlst-new-task", function(e)
120  toggle_new_task(e.player_index)
121end)
122
123event.register({ "tlst-toggle-gui", defines.events.on_lua_shortcut }, function(e)
124  if (e.input_name or e.prototype_name) == "tlst-toggle-gui" then
125    local player_table = global.players[e.player_index]
126    if player_table and player_table.guis.tasks then
127      player_table.guis.tasks:toggle()
128    end
129  elseif e.prototype_name == "tlst-new-task" then
130    toggle_new_task(e.player_index)
131  end
132end)
133
134-- PLAYER
135
136event.on_player_created(function(e)
137  local player = game.get_player(e.player_index)
138
139  player_data.init(player)
140  player_data.refresh(player, global.players[e.player_index])
141end)
142
143event.on_player_removed(function(e)
144  -- Remove all player tasks
145  local player_table = global.players[e.player_index]
146  for _, task_ids in pairs({ player_table.completed_tasks, player_table.tasks }) do
147    for _, task_id in pairs(task_ids) do
148      local Task = global.tasks[task_id]
149      if Task then
150        Task:delete()
151      end
152    end
153  end
154
155  global.players[e.player_index] = nil
156end)