z_mirror/BlueprintTools
git clone https://git.raiguard.me/z_mirror/BlueprintTools
bf73278
trunk
1local gui =require ( "gui" ) 2 3local buttons_gui =require ( "scripts.gui.buttons" ) 4local import_string_gui =require ( "scripts.gui.import-string" ) 5local library_shortcuts_gui =require ( "scripts.gui.library-shortcuts" ) 6local migrations =require ( "scripts.migrations" ) 7local player_data =require ( "scripts.player-data" ) 8local util =require ( "scripts.util" ) 9 10local planner_pipette =require ( "scripts.processors.planner-pipette" ) 11local set_tiles_gui =require ( "scripts.gui.set-tiles" ) 12local swap_wire_colors =require ( "scripts.processors.swap-wire-colors" ) 13 14-- ----------------------------------------------------------------------------- 15-- EVENT HANDLERS 16 17-- BOOTSTRAP 18 19script . on_init ( function () 20storage . players ={} 21planner_pipette . cache_downgrades () 22 23for i , player in pairs ( game . players ) do 24player_data . init ( i ) 25player_data . refresh ( player , storage . players [ i ]) 26end 27end ) 28 29script . on_configuration_changed ( function ( e ) 30planner_pipette . cache_downgrades () 31 32local old_version =e . mod_changes [ script . mod_name ] and e . mod_changes [ script . mod_name ]. old_version 33if not old_version then 34return 35end 36 37for version , migration in pairs ( migrations ) do 38if helpers . compare_versions ( version , old_version ) > 0 then 39migration () 40end 41end 42 43for i , player in pairs ( game . players ) do 44local player_table =storage . players [ i ] 45player_data . refresh ( player , player_table ) 46end 47end ) 48 49-- CUSTOM INPUTS 50 51script . on_event ( "bpt-import-string" , function ( e ) 52local player =game . get_player ( e . player_index ) 53import_string_gui . build ( player ) 54end ) 55 56script . on_event ( "bpt-swap-wire-colors" , function ( e ) 57swap_wire_colors ( game . get_player ( e . player_index )) 58end ) 59 60script . on_event ( "bpt-set-tiles" , function ( e ) 61local player =game . get_player ( e . player_index ) 62if not player then 63return 64end 65local player_table =storage . players [ e . player_index ] 66local cursor_stack =player . cursor_stack 67if cursor_stack and cursor_stack . valid_for_read then 68local blueprint =util . get_blueprint ( cursor_stack ) 69if blueprint then 70set_tiles_gui . build ( player , player_table ) 71end 72end 73end ) 74 75script . on_event ( "bpt-configure" , function ( e ) 76local player =game . get_player ( e . player_index ) 77if not player then 78return 79end 80local cursor_stack =player . cursor_stack 81if cursor_stack and cursor_stack . valid_for_read then 82local blueprint =util . get_blueprint ( cursor_stack ) 83if blueprint or ( cursor_stack . is_upgrade_item or cursor_stack . is_deconstruction_item ) then 84player . opened =cursor_stack 85end 86end 87end ) 88 89script . on_event ( "bpt-linked-confirm-gui" , function ( e ) 90local player =game . get_player ( e . player_index ) 91if not player then 92return 93end 94local player_table =storage . players [ e . player_index ] 95local gui_data =player_table . guis . set_tiles 96if gui_data and gui_data . refs . confirm_button . enabled then 97set_tiles_gui . handle_action ( e , { action ="confirm" } ) 98player . play_sound ( { path ="utility/confirm" } ) 99end 100end ) 101 102script . on_event ( "bpt-linked-clear-cursor" , function ( e ) 103local player =game . get_player ( e . player_index ) 104if not player then 105return 106end 107local player_table =storage . players [ e . player_index ] 108local cursor_stack =player . cursor_stack 109if player_table . flags . holding_temporary_item and cursor_stack . valid_for_read then 110local item_type =cursor_stack . type 111if item_type == "blueprint" and not cursor_stack . is_blueprint_setup () then 112cursor_stack . clear () 113elseif item_type == "blueprint-book" or item_type == "upgrade-item" or item_type == "deconstruction-item" then 114cursor_stack . clear () 115end 116end 117end ) 118 119-- GUI 120 121gui . hook_events ( function ( e ) 122local player =game . get_player ( e . player_index ) 123if not player then 124return 125end 126local player_table =storage . players [ e . player_index ] 127local action =gui . read_action ( e ) 128 129if action then 130if action . gui == "buttons" then 131if action . action == "swap_wire_colors" then 132swap_wire_colors ( player ) 133elseif action . action == "set_tiles" then 134set_tiles_gui . build ( player , player_table ) 135elseif action . action == "configure" then 136player . opened =player . cursor_stack 137end 138elseif action . gui == "set_tiles" then 139set_tiles_gui . handle_action ( e , action ) 140elseif action . gui == "library_shortcuts" then 141library_shortcuts_gui . handle_action ( player , player_table , action ) 142elseif action . gui == "import_string" then 143import_string_gui . handle_action ( player , action ) 144end 145end 146end ) 147 148-- PLAYER 149 150script . on_event ( defines . events . on_player_created , function ( e ) 151player_data . init ( e . player_index ) 152player_data . refresh ( game . get_player ( e . player_index ), storage . players [ e . player_index ]) 153end ) 154 155script . on_event ( defines . events . on_player_removed , function ( e ) 156storage . players [ e . player_index ] =nil 157end ) 158 159script . on_event ( defines . events . on_player_cursor_stack_changed , function ( e ) 160local player =game . get_player ( e . player_index ) 161if not player then 162return 163end 164local player_table =storage . players [ e . player_index ] 165local cursor_stack =player . cursor_stack 166 167if player_table . flags . setting_temporary_item then 168player_table . flags . setting_temporary_item =false 169elseif player_table . flags . holding_temporary_item then 170player_table . flags . holding_temporary_item =false 171end 172 173local blueprint =util . get_blueprint ( cursor_stack ) 174local blueprint_buttons_shown =player_table . flags . blueprint_buttons_shown 175 176if blueprint and not blueprint_buttons_shown then 177buttons_gui . show ( player_table , "blueprint" ) 178elseif not blueprint and blueprint_buttons_shown then 179buttons_gui . hide ( player_table , "blueprint" ) 180end 181 182if cursor_stack . is_upgrade_item then 183buttons_gui . show ( player_table , "upgrade_planner" ) 184elseif player_table . flags . upgrade_planner_buttons_shown then 185buttons_gui . hide ( player_table , "upgrade_planner" ) 186end 187 188if cursor_stack . is_deconstruction_item then 189buttons_gui . show ( player_table , "deconstruction_planner" ) 190elseif player_table . flags . deconstruction_planner_buttons_shown then 191buttons_gui . hide ( player_table , "deconstruction_planner" ) 192end 193 194-- if the set tiles GUI is open and the cursor stack changes in any way, close it 195if player_table . guis . set_tiles then 196set_tiles_gui . destroy ( player_table ) 197end 198end ) 199 200-- SETTINGS 201 202script . on_event ( defines . events . on_runtime_mod_setting_changed , function ( e ) 203if string . sub ( e . setting , 1 , 4 ) == "bpt-" then 204local player =game . get_player ( e . player_index ) 205if not player then 206return 207end 208local player_table =storage . players [ e . player_index ] 209player_data . update_settings ( player , player_table ) 210if e . setting == "bpt-show-library-shortcuts" then 211library_shortcuts_gui . refresh ( player , player_table ) 212else 213buttons_gui . refresh ( player , player_table ) 214local flags =player_table . flags 215if flags . blueprint_buttons_shown then 216buttons_gui . show ( player_table , "blueprint" ) 217end 218if flags . upgrade_planner_buttons_shown then 219buttons_gui . show ( player_table , "upgrade_planner" ) 220end 221if flags . deconstruction_planner_buttons_shown then 222buttons_gui . show ( player_table , "deconstruction_planner" ) 223end 224end 225end 226end )