z_mirror/BlueprintTools
git clone https://git.raiguard.me/z_mirror/BlueprintTools
2d9f200
clipboards
1local event =require ( "__flib__.event" ) 2local gui =require ( "__flib__.gui-beta" ) 3local migration =require ( "__flib__.migration" ) 4 5local constants =require ( "constants" ) 6 7local buttons_gui =require ( "scripts.gui.buttons" ) 8local global_data =require ( "scripts.global-data" ) 9local migrations =require ( "scripts.migrations" ) 10local player_data =require ( "scripts.player-data" ) 11local util =require ( "scripts.util" ) 12 13local quick_grid =require ( "scripts.processors.quick-grid" ) 14local set_tiles_gui =require ( "scripts.gui.set-tiles" ) 15local swap_wire_colors =require ( "scripts.processors.swap-wire-colors" ) 16 17-- ----------------------------------------------------------------------------- 18-- EVENT HANDLERS 19 20-- BOOTSTRAP 21 22event . on_init ( function () 23global_data . init () 24 25for i , player in pairs ( game . players ) do 26player_data . init ( i ) 27player_data . refresh ( player , global . players [ i ]) 28end 29end ) 30 31event . on_load ( function () 32 33end ) 34 35event . on_configuration_changed ( function ( e ) 36if migration . on_config_changed ( e , migrations ) then 37for i , player in pairs ( game . players ) do 38local player_table =global . players [ i ] 39player_data . refresh ( player , player_table ) 40end 41end 42end ) 43 44-- CUSTOM INPUTS 45 46event . register ( "bpt-swap-wire-colors" , function ( e ) 47swap_wire_colors ( game . get_player ( e . player_index )) 48end ) 49 50event . register ( "bpt-set-tiles" , function ( e ) 51local player =game . get_player ( e . player_index ) 52local player_table =global . players [ e . player_index ] 53local cursor_stack =player . cursor_stack 54if cursor_stack and cursor_stack . valid_for_read then 55local blueprint =util . get_blueprint ( cursor_stack ) 56if blueprint and blueprint . is_blueprint_setup () then 57set_tiles_gui . build ( player , player_table ) 58end 59end 60end ) 61 62event . register ( "bpt-quick-grid" , function ( e ) 63quick_grid ( game . get_player ( e . player_index )) 64end ) 65 66event . register ( "bpt-configure" , function ( e ) 67local player =game . get_player ( e . player_index ) 68local cursor_stack =player . cursor_stack 69if cursor_stack and cursor_stack . valid_for_read then 70local blueprint =util . get_blueprint ( cursor_stack ) 71if 72( blueprint and blueprint . is_blueprint_setup ()) 73or ( cursor_stack . is_upgrade_item or cursor_stack . is_deconstruction_item ) 74then 75player . opened =cursor_stack 76end 77end 78end ) 79 80event . register ( { "bpt-activate-deconstruction-clipboard" , "bpt-activate-upgrade-clipboard" } , function ( e ) 81local _ , _ , type =string . find ( e . input_name , "bpt%-activate%-(.*)%-clipboard" ) 82 83local player =game . get_player ( e . player_index ) 84local player_table =global . players [ e . player_index ] 85 86local cursor_stack =player . cursor_stack 87if cursor_stack then 88local clipboard_book =player_table . clipboards [ constants . clipboard_indices [ type ]] 89local clipboard_inventory =clipboard_book . get_inventory ( defines . inventory . item_main ) 90if not clipboard_inventory . is_empty () and player . clear_cursor () then 91cursor_stack . set_stack ( clipboard_book ) 92end 93end 94end ) 95 96event . register ( "bpt-linked-confirm-gui" , function ( e ) 97local player =game . get_player ( e . player_index ) 98local player_table =global . players [ e . player_index ] 99local gui_data =player_table . guis . set_tiles 100if gui_data and gui_data . refs . confirm_button . enabled then 101set_tiles_gui . handle_action ( e , { action ="confirm" } ) 102player . play_sound { path ="utility/confirm" } 103end 104end ) 105 106-- event.register({"bpt-linked-cycle-blueprint-backwards", "bpt-linked-cycle-blueprint-forwards"}, function(e) 107-- local direction = e.input_name == "bpt-linked-cycle-blueprint-backwards" and -1 or 1 108-- end) 109 110event . register ( "bpt-linked-clear-cursor" , function ( e ) 111end ) 112 113-- GUI 114 115gui . hook_events ( function ( e ) 116local player =game . get_player ( e . player_index ) 117local player_table =global . players [ e . player_index ] 118local action =gui . read_action ( e ) 119 120if action then 121if action . gui == "buttons" then 122if action . action == "swap_wire_colors" then 123swap_wire_colors ( player ) 124elseif action . action == "set_tiles" then 125set_tiles_gui . build ( player , player_table ) 126elseif action . action == "quick_grid" then 127quick_grid ( player ) 128elseif action . action == "configure" then 129player . opened =player . cursor_stack 130end 131elseif action . gui == "set_tiles" then 132set_tiles_gui . handle_action ( e , action ) 133end 134end 135end ) 136 137-- PLAYER 138 139event . on_player_created ( function ( e ) 140player_data . init ( e . player_index ) 141player_data . refresh ( game . get_player ( e . player_index ), global . players [ e . player_index ]) 142end ) 143 144event . on_player_removed ( function ( e ) 145global . players [ e . player_index ] =nil 146end ) 147 148event . on_player_cursor_stack_changed ( function ( e ) 149local player =game . get_player ( e . player_index ) 150local player_table =global . players [ e . player_index ] 151local cursor_stack =player . cursor_stack 152 153local blueprint =util . get_blueprint ( cursor_stack ) 154local blueprint_buttons_shown =player_table . flags . blueprint_buttons_shown 155 156if blueprint then 157local is_setup =blueprint . is_blueprint_setup () 158if is_setup and not blueprint_buttons_shown then 159buttons_gui . show ( player_table , "blueprint" ) 160elseif not is_setup and blueprint_buttons_shown then 161buttons_gui . hide ( player_table , "blueprint" ) 162end 163elseif blueprint_buttons_shown then 164buttons_gui . hide ( player_table , "blueprint" ) 165end 166 167if cursor_stack . is_upgrade_item then 168buttons_gui . show ( player_table , "upgrade_planner" ) 169elseif player_table . flags . upgrade_planner_buttons_shown then 170buttons_gui . hide ( player_table , "upgrade_planner" ) 171end 172 173if cursor_stack . is_deconstruction_item then 174buttons_gui . show ( player_table , "deconstruction_planner" ) 175elseif player_table . flags . deconstruction_planner_buttons_shown then 176buttons_gui . hide ( player_table , "deconstruction_planner" ) 177end 178 179-- if the set tiles GUI is open and the cursor stack changes in any way, close it 180if player_table . guis . set_tiles then 181set_tiles_gui . destroy ( player_table ) 182end 183end )