z_mirror/TaskList
git clone https://git.raiguard.me/z_mirror/TaskList
fa77a7d
task-details-gui
1--[[ 2DESIGN 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 18DESIGN 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 () 49global . forces ={} 50global . next_task_id =1 51global . players ={} 52global . tasks ={} 53 54for _ , force in pairs ( game . forces ) do 55global . forces [ force . index ] ={ 56completed_tasks ={} , 57tasks ={} , 58} 59end 60for _ , player in pairs ( game . players ) do 61player_data . init ( player ) 62end 63 64migrations . generic () 65end ) 66 67event . on_load ( function () 68for _ , Task in pairs ( global . tasks ) do 69task . load ( Task ) 70end 71 72for _ , player_table in pairs ( global . players ) do 73tasks_gui . load ( player_table . guis . tasks ) 74if player_table . guis . edit_task then 75edit_task_gui . load ( player_table . guis . edit_task ) 76end 77end 78end ) 79 80event . on_configuration_changed ( function ( e ) 81if migration . on_config_changed ( e , migrations . versions ) then 82migrations . generic () 83end 84end ) 85 86-- INTERACTION 87 88gui . hook_events ( function ( e ) 89local msg =gui . read_action ( e ) 90if msg then 91local Gui =util . get_gui ( e . player_index , msg . gui ) 92if Gui then 93Gui : dispatch ( msg , e ) 94end 95end 96end ) 97 98event . register ( "tlst-linked-confirm-gui" , function ( e ) 99--- @type EditTaskGui 100local EditTaskGui =util . get_gui ( e . player_index , "edit_task" ) 101if EditTaskGui then 102EditTaskGui : dispatch ( { action ="confirm" } ) 103game . get_player ( e . player_index ). play_sound ( { path ="utility/confirm" } ) 104end 105end ) 106 107--- @param player_index number 108local function toggle_new_task ( player_index ) 109local EditTaskGui =util . get_gui ( player_index , "edit_task" ) 110if EditTaskGui then 111EditTaskGui : destroy () 112else 113local player =game . get_player ( player_index ) 114local player_table =global . players [ player_index ] 115edit_task_gui . new ( player , player_table , { standalone =true } ) 116end 117end 118 119event . register ( "tlst-new-task" , function ( e ) 120toggle_new_task ( e . player_index ) 121end ) 122 123event . register ( { "tlst-toggle-gui" , defines . events . on_lua_shortcut } , function ( e ) 124if ( e . input_name or e . prototype_name ) == "tlst-toggle-gui" then 125local player_table =global . players [ e . player_index ] 126if player_table and player_table . guis . tasks then 127player_table . guis . tasks : toggle () 128end 129elseif e . prototype_name == "tlst-new-task" then 130toggle_new_task ( e . player_index ) 131end 132end ) 133 134-- PLAYER 135 136event . on_player_created ( function ( e ) 137local player =game . get_player ( e . player_index ) 138 139player_data . init ( player ) 140player_data . refresh ( player , global . players [ e . player_index ]) 141end ) 142 143event . on_player_removed ( function ( e ) 144-- Remove all player tasks 145local player_table =global . players [ e . player_index ] 146for _ , task_ids in pairs ( { player_table . completed_tasks , player_table . tasks } ) do 147for _ , task_id in pairs ( task_ids ) do 148local Task =global . tasks [ task_id ] 149if Task then 150Task : delete () 151end 152end 153end 154 155global . players [ e . player_index ] =nil 156end )