z_mirror/TaskList
git clone https://git.raiguard.me/z_mirror/TaskList
5843c65
austinrobichaux18/Twiggy/AddPriorityAndArea
1if ... ~= "gui" then 2return require ( "gui" ) 3end 4 5--- @diagnostic disable 6 7local mod_name =script . mod_name 8local gui_event_defines ={} 9 10local event_id_to_string_mapping ={} 11for name , id in pairs ( defines . events ) do 12if string . find ( name , "^on_gui" ) then 13gui_event_defines [ name ] =id 14event_id_to_string_mapping [ id ] =string . gsub ( name , "^on_gui" , "on" ) 15end 16end 17 18--- @deprecated use `gui-lite` instead 19local flib_gui ={} 20 21--- @deprecated use `gui-lite` instead 22function flib_gui . hook_events ( callback ) 23local on_event =script . on_event 24for _ , id in pairs ( gui_event_defines ) do 25on_event ( id , callback ) 26end 27end 28 29--- @deprecated use `gui-lite` instead 30function flib_gui . read_action ( event_data ) 31local elem =event_data . element 32if not elem or not elem . valid then 33return 34end 35 36local mod_tags =elem . tags [ mod_name ] 37if not mod_tags then 38return 39end 40 41local elem_actions =mod_tags . flib 42if not elem_actions then 43return 44end 45 46local event_name =event_id_to_string_mapping [ event_data . name ] 47local msg =elem_actions [ event_name ] 48 49return msg 50end 51 52--- @deprecated use `gui-lite` instead 53local function recursive_build ( parent , structure , refs ) 54-- If the structure has no type, just ignore it 55-- This is to make it possible to pass unit types `{}` to represent "no element" without breaking things 56if not structure . type then 57return 58end 59 60-- Prepare tags 61local original_tags =structure . tags 62local tags =original_tags or {} 63local actions =structure . actions 64local tags_flib =tags . flib 65tags . flib =actions 66structure . tags ={ [ mod_name ] =tags } 67 68-- Make the game not convert these into a property tree for no reason 69structure . actions =nil 70-- Substructures can be defined in special tables or as the array portion of this structure 71local substructures 72local substructures_len =# structure 73if substructures_len > 0 then 74if structure . children or structure . tabs then 75error ( "Children or tab-and-content pairs must ALL be in the array portion, or a subtable. Not both at once!" ) 76end 77substructures ={} 78for i =1 , substructures_len do 79substructures [ i ] =structure [ i ] 80structure [ i ] =nil 81end 82else 83substructures =structure . children or structure . tabs 84structure . children =nil 85structure . tabs =nil 86end 87 88-- Create element 89local elem =parent . add ( structure ) 90 91-- Restore structure 92structure . tags =original_tags 93structure . actions =actions 94tags . flib =tags_flib 95 96local style_mods =structure . style_mods 97if style_mods then 98for k , v in pairs ( style_mods ) do 99elem . style [ k ] =v 100end 101end 102 103local elem_mods =structure . elem_mods 104if elem_mods then 105for k , v in pairs ( elem_mods ) do 106elem [ k ] =v 107end 108end 109 110local ref =structure . ref 111if ref then 112-- Recursively create tables as needed 113local prev =refs 114local ref_length =# ref 115for i =1 , ref_length - 1 do 116local current_key =ref [ i ] 117local current =prev [ current_key ] 118if not current then 119current ={} 120prev [ current_key ] =current 121end 122prev =current 123end 124prev [ ref [ ref_length ]] =elem 125end 126 127-- Substructures 128if substructures then 129if structure . type == "tabbed-pane" then 130local add_tab =elem . add_tab 131for i =1 , # substructures do 132local tab_and_content =substructures [ i ] 133if not ( tab_and_content . tab and tab_and_content . content ) then 134error ( "TabAndContent must have `tab` and `content` fields" ) 135end 136local tab =recursive_build ( elem , tab_and_content . tab , refs ) 137local content =recursive_build ( elem , tab_and_content . content , refs ) 138add_tab ( tab , content ) 139end 140else 141for i =1 , # substructures do 142recursive_build ( elem , substructures [ i ], refs ) 143end 144end 145end 146 147return elem 148end 149 150--- @deprecated use `gui-lite` instead 151function flib_gui . build ( parent , structures ) 152local refs ={} 153for i =1 , # structures do 154recursive_build ( parent , structures [ i ], refs ) 155end 156return refs 157end 158 159--- @deprecated use `gui-lite` instead 160function flib_gui . add ( parent , structure ) 161-- Just in case they had a ref in the structure already, extract it 162local previous_ref =structure . ref 163-- Put in a known ref that we can use later 164structure . ref ={ "FLIB_ADD_ROOT" } 165-- Build the element 166local refs ={} 167recursive_build ( parent , structure , refs ) 168-- Restore the previous ref 169structure . ref =previous_ref 170-- Return the element 171return refs . FLIB_ADD_ROOT 172end 173 174--- @deprecated use `gui-lite` instead 175local function recursive_update ( elem , updates ) 176if updates . cb then 177updates . cb ( elem ) 178end 179 180if updates . style then 181elem . style =updates . style 182end 183 184if updates . style_mods then 185for key , value in pairs ( updates . style_mods ) do 186elem . style [ key ] =value 187end 188end 189 190if updates . elem_mods then 191for key , value in pairs ( updates . elem_mods ) do 192elem [ key ] =value 193end 194end 195 196if updates . tags then 197flib_gui . update_tags ( elem , updates . tags ) 198end 199 200-- TODO: This could be a lot better 201if updates . actions then 202for event_name , payload in pairs ( updates . actions ) do 203flib_gui . set_action ( elem , event_name , payload ) 204end 205end 206 207local substructures 208local substructures_len =# updates 209if substructures_len > 0 then 210if updates . children or updates . tabs then 211error ( "Children or tab-and-content pairs must ALL be in the array portion, or a subtable. Not both at once!" ) 212end 213substructures ={} 214for i =1 , substructures_len do 215substructures [ i ] =updates [ i ] 216updates [ i ] =nil 217end 218else 219substructures =updates . children or updates . tabs 220updates . children =nil 221updates . tabs =nil 222end 223local subelements 224if elem . type == "tabbed-pane" then 225subelements =elem . tabs 226else 227subelements =elem . children 228end 229 230if substructures then 231for i , substructure in pairs ( substructures ) do 232if substructure . tab or substructure . content then 233local elem_tab_and_content =subelements [ i ] 234if elem_tab_and_content then 235local tab =elem_tab_and_content . tab 236local tab_updates =substructures . tab 237if tab and tab_updates then 238recursive_update ( tab , tab_updates ) 239end 240local content =elem_tab_and_content . content 241local content_updates =substructures . content 242if content and content_updates then 243recursive_update ( content , content_updates ) 244end 245end 246elseif subelements [ i ] then 247recursive_update ( subelements [ i ], substructure ) 248end 249end 250end 251end 252 253--- @deprecated use `gui-lite` instead 254function flib_gui . update ( elem , updates ) 255recursive_update ( elem , updates ) 256end 257 258--- @deprecated use `gui-lite` instead 259function flib_gui . get_tags ( elem ) 260return elem . tags [ mod_name ] or {} 261end 262 263--- @deprecated use `gui-lite` instead 264function flib_gui . set_tags ( elem , tags ) 265local elem_tags =elem . tags 266elem_tags [ mod_name ] =tags 267elem . tags =elem_tags 268end 269 270--- @deprecated use `gui-lite` instead 271function flib_gui . delete_tags ( elem ) 272local elem_tags =elem . tags 273elem_tags [ mod_name ] =nil 274elem . tags =elem_tags 275end 276 277--- @deprecated use `gui-lite` instead 278function flib_gui . update_tags ( elem , updates ) 279local elem_tags =elem . tags 280local existing =elem_tags [ mod_name ] 281 282if not existing then 283existing ={} 284elem_tags [ mod_name ] =existing 285end 286 287for k , v in pairs ( updates ) do 288existing [ k ] =v 289end 290 291elem . tags =elem_tags 292end 293 294--- @deprecated use `gui-lite` instead 295function flib_gui . set_action ( elem , event_name , msg ) 296local elem_tags =elem . tags 297local existing =elem_tags [ mod_name ] 298 299if not existing then 300existing ={} 301elem_tags [ mod_name ] =existing 302end 303 304local actions =existing . flib 305if not actions then 306actions ={} 307existing . flib =actions 308end 309 310actions [ event_name ] =msg or nil 311 312elem . tags =elem_tags 313end 314 315--- @deprecated use `gui-lite` instead 316function flib_gui . get_action ( elem , event_name ) 317local elem_tags =elem . tags 318local existing =elem_tags [ mod_name ] 319 320if not existing then 321return 322end 323 324local actions =existing . flib 325if not actions then 326return 327end 328 329return actions [ event_name ] 330end 331 332return flib_gui