z_mirror/TaskList
git clone https://git.raiguard.me/z_mirror/TaskList
155b98f
trunk
1local gui =require ( "gui" ) 2 3local constants =require ( "constants" ) 4local util =require ( "scripts.util" ) 5 6local actions =require ( "scripts.gui.edit-task.actions" ) 7 8-- GUI 9 10--- @class EditTaskGuiRefs 11--- @field window LuaGuiElement 12--- @field titlebar_flow LuaGuiElement 13--- @field title_textfield LuaGuiElement 14--- @field description_textfield LuaGuiElement 15--- @field add_to_top_checkbox LuaGuiElement 16--- @field private_checkbox LuaGuiElement 17--- @field assignee_dropdown LuaGuiElement 18--- @field status_dropdown LuaGuiElement 19--- @field footer_flow LuaGuiElement 20 21--- @class EditTaskGui 22local EditTaskGui ={} 23 24EditTaskGui . actions =actions 25 26function EditTaskGui : destroy () 27local window =self . refs . window 28if window and window . valid then 29self . refs . window . destroy () 30end 31self . player_table . guis . edit_task =nil 32 33if self . parent and self . parent . refs . window . valid and not self . parent . state . pinned then 34self . player . opened =self . parent . refs . window --- @diagnostic disable-line 35end 36 37if self . state . standalone then 38self . player . set_shortcut_toggled ( "tlst-new-task" , false ) 39end 40end 41 42function EditTaskGui : dispatch ( msg , e ) 43local transform =msg . transform 44if transform then 45if transform == "handle_titlebar_click" and e . button == defines . mouse_button_type . middle then 46msg . action ="recenter" 47end 48end 49 50if msg . action then 51local handler =self . actions [ msg . action ] 52 53-- Icon selector confirmations have these properties and should be ignored 54-- to prevent selecting an icon from adding a new task 55if e . element == nil and e . input_name == "tlst-linked-confirm-gui" and e . in_gui == true then 56return 57end 58 59if handler then 60handler ( self , msg or {} , e or {} ) 61end 62end 63end 64 65-- BOOTSTRAP 66 67local index ={} 68 69--- @class NewTaskOptions 70--- @field parent_gui TasksGui? 71--- @field task Task? 72--- @field parent_task Task? 73--- @field set_private boolean? 74--- @field standalone boolean? 75--- @field ignore_close boolean? 76 77--- @param player LuaPlayer 78--- @param player_table PlayerTable 79--- @param options NewTaskOptions 80function index . new ( player , player_table , options ) 81options =options or {} 82local Task =options . task 83local Parent =options . parent_gui 84local ParentTask =options . parent_task 85 86local players ={ { "gui.tlst-unassigned" } } 87local force =util . get_force ( player ) 88local assignee_selection_index =1 89local player_selection_index =0 90 91local title_caption 92if Task then 93title_caption ={ "gui.tlst-edit-task" } 94else 95title_caption ={ "gui.tlst-new-task" } 96end 97 98Task =Task or {} 99 100local assignable =true 101local owner =Task . owner or ParentTask or {} 102local assignee_index =0 103while owner . object_name == "Task" do 104owner =owner . owner 105end 106if owner . object_name == "LuaPlayer" then 107assignable =false 108assignee_index =ParentTask and ParentTask . assignee and ParentTask . assignee . index or 0 109else 110assignee_index =Task . assignee and Task . assignee . index or 0 111end 112 113for player_index , other_player in pairs ( game . players ) do 114if util . get_force ( other_player ) == force then 115table . insert ( players , other_player . name ) 116if player_index == player . index then 117player_selection_index =# players 118end 119if assignee_index == player_index then 120assignee_selection_index =# players 121end 122end 123end 124 125local default_task =player . mod_settings [ "tlst-new-tasks-in-progress" ]. value and "in_progress" or "not_started" 126local status_items ={} 127local selected_status_index =constants . task_status [ default_task ]. index 128for status_name , status_info in pairs ( constants . task_status ) do 129if status_name == Task . status then 130selected_status_index =status_info . index 131end 132table . insert ( status_items , { "" , "[img=flib_indicator_" .. status_info . color .. "] " , status_info . label } ) 133end 134 135--- @type EditTaskGuiRefs 136local refs =gui . build ( player . gui . screen , { 137{ 138type ="frame" , 139name ="tlst_edit_task_window" , 140style_mods ={ width =448 } , 141direction ="vertical" , 142ref ={ "window" } , 143actions ={ 144on_closed ={ gui ="edit_task" , action ="close" } , 145} , 146{ 147type ="flow" , 148style ="flib_titlebar_flow" , 149ref ={ "titlebar_flow" } , 150actions ={ 151on_click ={ gui ="edit_task" , transform ="handle_titlebar_click" } , 152} , 153{ type ="label" , style ="frame_title" , caption =title_caption , ignored_by_interaction =true } , 154{ type ="empty-widget" , style ="flib_dialog_titlebar_drag_handle" , ignored_by_interaction =true } , 155} , 156{ 157type ="frame" , 158style ="inside_shallow_frame" , 159direction ="vertical" , 160{ 161type ="frame" , 162style ="subheader_frame" , 163style_mods ={ horizontally_stretchable =true } , 164visible =ParentTask and true or false , 165{ 166type ="label" , 167style ="bold_label" , 168style_mods ={ left_margin =8 } , 169caption =ParentTask and { "gui.tlst-subtask-of" , ParentTask . title } or nil , 170} , 171} , 172{ 173type ="flow" , 174style_mods ={ padding =12 , vertical_spacing =8 } , 175direction ="vertical" , 176{ type ="label" , caption ={ "gui.tlst-title" } } , 177{ 178type ="textfield" , 179style ="flib_widthless_textfield" , 180style_mods ={ horizontally_stretchable =true } , 181text =Task . title , 182icon_selector =true , 183ref ={ "title_textfield" } , 184actions ={ 185on_confirmed ={ gui ="edit_task" , action ="confirm" } , 186} , 187} , 188{ type ="label" , caption ={ "gui.tlst-description" } } , 189{ 190type ="text-box" , 191style_mods ={ height =200 , width =400 } , 192text =Task . description , 193icon_selector =true , 194elem_mods ={ word_wrap =true } , 195ref ={ "description_textfield" } , 196actions ={ 197on_confirmed ={ gui ="edit_task" , action ="confirm" } , 198} , 199} , 200{ 201type ="flow" , 202visible =not Task . title , 203{ 204type ="checkbox" , 205caption ={ "gui.tlst-add-to-top" } , 206state =player . mod_settings [ "tlst-new-tasks-at-top" ]. value , 207ref ={ "add_to_top_checkbox" } , 208} , 209{ type ="empty-widget" , style ="flib_horizontal_pusher" } , 210{ 211type ="checkbox" , 212caption ={ "" , { "gui.tlst-private" } , " [img=info]" } , 213tooltip ={ "gui.tlst-private-description" } , 214state =owner and owner . object_name == "LuaPlayer" or false , 215enabled =not ParentTask , 216ref ={ "private_checkbox" } , 217actions ={ 218on_checked_state_changed ={ gui ="edit_task" , action ="update_assignee_dropdown" } , 219} , 220} , 221} , 222{ 223type ="flow" , 224style_mods ={ vertical_align ="center" } , 225{ type ="label" , caption ={ "gui.tlst-assignee" } } , 226{ type ="empty-widget" , style ="flib_horizontal_pusher" } , 227{ 228type ="drop-down" , 229items =players , 230selected_index =assignee_selection_index , 231enabled =assignable , 232ref ={ "assignee_dropdown" } , 233} , 234} , 235{ 236type ="flow" , 237style_mods ={ vertical_align ="center" } , 238{ 239type ="label" , 240caption ={ "" , { "gui.tlst-status" } , " [img=info]" } , 241tooltip ={ "gui.tlst-status-description" } , 242} , 243{ type ="empty-widget" , style ="flib_horizontal_pusher" } , 244{ 245type ="drop-down" , 246items =status_items , 247selected_index =selected_status_index , 248ref ={ "status_dropdown" } , 249} , 250} , 251} , 252} , 253{ 254type ="flow" , 255style ="dialog_buttons_horizontal_flow" , 256actions ={ on_click ={ gui ="edit_task" , transform ="handle_titlebar_click" } } , 257ref ={ "footer_flow" } , 258{ 259type ="button" , 260style ="back_button" , 261caption ={ "gui.cancel" } , 262actions ={ 263on_click ={ gui ="edit_task" , action ="close" } , 264} , 265} , 266{ type ="empty-widget" , style ="flib_dialog_footer_drag_handle" , ignored_by_interaction =true } , 267Task . title and { 268type ="button" , 269style ="tlst_red_dialog_button" , 270caption ={ "gui.delete" } , 271actions ={ 272on_click ={ gui ="edit_task" , action ="delete" } , 273} , 274} or {} , 275Task . title and { 276type ="empty-widget" , 277style ="flib_dialog_footer_drag_handle" , 278ignored_by_interaction =true , 279} or {} , 280{ 281type ="button" , 282style ="confirm_button" , 283caption ={ "gui.confirm" } , 284actions ={ 285on_click ={ gui ="edit_task" , action ="confirm" } , 286} , 287} , 288} , 289} , 290} ) 291 292refs . window . force_auto_center () 293refs . titlebar_flow . drag_target =refs . window 294refs . footer_flow . drag_target =refs . window 295 296if Parent then 297if not Parent . state . pinned then 298Parent . state . ignore_close =true 299end 300player . opened =refs . window 301elseif options . standalone then 302player . opened =refs . window 303player . set_shortcut_toggled ( "tlst-new-task" , true ) 304end 305 306--- @class EditTaskGui 307local self ={ 308parent =Parent , 309player =player , 310player_table =player_table , 311refs =refs , 312state ={ 313just_confirmed =options . ignore_close and true or false , 314parent_task =ParentTask , 315player_selection_index =player_selection_index , 316standalone =options . standalone , 317--- @type Task|nil 318task =Task . title and Task or nil , 319} , 320} 321 322setmetatable ( self , { __index =EditTaskGui } ) 323 324player_table . guis . edit_task =self 325 326if options . set_private then 327self . refs . private_checkbox . state =true 328self . actions . update_assignee_dropdown ( self ) 329end 330 331self . refs . title_textfield . select_all () 332self . refs . title_textfield . focus () 333end 334 335--- @param Gui EditTaskGui 336function index . load ( Gui ) 337setmetatable ( Gui , { __index =EditTaskGui } ) 338end 339 340return index