z_mirror/Krastorio2
git clone https://git.raiguard.me/z_mirror/Krastorio2
45624855
santichu95/patch-1
1local flib_gui =require ( "__flib__.gui" ) 2 3local constants =require ( "scripts.constants" ) 4local util =require ( "scripts.util" ) 5 6-- GUI 7 8--- @param player LuaPlayer 9--- @param entity LuaEntity 10local function update_gui ( player , entity ) 11local player_gui =storage . roboport_guis [ player . index ] 12if not player_gui or not player_gui . valid then 13return 14end 15local mode =string . match ( entity . name , "^.*%-(%w*)%-mode" ) or "normal" 16 17for _ , button in pairs ( player_gui . inner_frame . children ) do 18if button . name == mode then 19button . state =true 20else 21button . state =false 22end 23end 24end 25 26--- @param entity LuaEntity 27--- @param player LuaPlayer 28--- @param to_mode string? 29local function change_mode ( entity , player , to_mode ) 30local matched , _ , base_name , suffix =string . find ( entity . name , "^(.-)(%-%w-%-mode)$" ) 31if not matched then 32base_name =entity . name 33suffix ="" 34end 35 36local new_mode_data 37if to_mode then 38new_mode_data =constants . roboport_modes [ to_mode ] 39else 40new_mode_data =constants . roboport_modes [ constants . next_roboport_mode [ suffix ]] 41end 42 43local new_name =base_name .. new_mode_data . suffix 44 45if not prototypes . entity [ new_name ] then 46util . flying_text_with_sound ( player , { "message.kr-roboport-modes-not-supported" } , { position =entity . position } ) 47return 48end 49 50local energy =entity . energy 51local health =entity . health 52 53-- Find all players that have this roboport open 54-- This must be done before the roboport is swapped 55local guis_to_update ={} 56for player_index in pairs ( storage . roboport_guis ) do 57local gui_player =game . get_player ( player_index ) 58if gui_player and gui_player . opened_gui_type == defines . gui_type . entity then 59local opened =gui_player . opened 60if opened and opened == entity then 61table . insert ( guis_to_update , gui_player ) 62end 63end 64end 65 66local new_entity =entity . surface . create_entity ( { 67name =new_name , 68position =entity . position , 69force =entity . force , 70direction =entity . direction , 71fast_replace =true , 72raise_built =true , 73spill =false , 74create_build_effect_smoke =false , 75} ) 76 77if new_entity then 78new_entity . energy =energy 79new_entity . health =health 80new_entity . last_user =player 81 82util . change_mode_fx ( new_entity , { "message.kr-" .. new_mode_data . text .. "-mode" } , new_mode_data . color ) 83 84-- Re-open GUI for all players who had it open and update the radio buttons 85for _ , gui_player in pairs ( guis_to_update ) do 86gui_player . opened =new_entity 87update_gui ( gui_player , new_entity ) 88end 89end 90end 91 92--- @param e EventData.on_gui_checked_state_changed 93local function on_mode_radio_button_checked ( e ) 94local player =game . get_player ( e . player_index ) 95if not player or player . opened_gui_type ~= defines . gui_type . entity then 96return 97end 98local entity =player . opened --[[@as LuaEntity]] 99if not entity . valid or entity . type ~= "roboport" then 100return 101end 102change_mode ( entity , player , e . element . name ) 103end 104 105--- @param mode string 106local function radio_button ( mode ) 107return { 108type ="radiobutton" , 109name =mode , 110caption ={ "message.kr-" .. mode .. "-mode" } , 111state =false , 112handler ={ [ defines . events . on_gui_checked_state_changed ] =on_mode_radio_button_checked } , 113} 114end 115 116--- @param player_index uint 117local function destroy_gui ( player_index ) 118local player_gui =storage . roboport_guis [ player_index ] 119if not player_gui then 120return 121end 122if player_gui . valid then 123player_gui . destroy () 124end 125storage . roboport_guis [ player_index ] =nil 126end 127 128--- @param player LuaPlayer 129local function refresh_gui ( player ) 130destroy_gui ( player . index ) 131 132_ , storage . roboport_guis [ player . index ] =flib_gui . add ( player . gui . relative , { 133type ="frame" , 134name ="kr_roboport_mode_window" , 135caption ={ "gui.kr-change-mode" } , 136anchor ={ 137gui =defines . relative_gui_type . roboport_gui , 138position =defines . relative_gui_position . right , 139names =storage . roboport_has_variants_gui , 140} , 141{ 142type ="frame" , 143name ="inner_frame" , 144style ="inside_shallow_frame_with_padding" , 145direction ="vertical" , 146radio_button ( "normal" ), 147radio_button ( "construction" ), 148radio_button ( "logistic" ), 149} , 150} ) 151end 152 153local function find_variants () 154-- Find all compatible roboports 155local has_variants_gui ={} 156for _ , entity in pairs ( prototypes . get_entity_filtered ( { { filter ="type" , type ="roboport" } } )) do 157local matched , _ , base_name =string . find ( entity . name , "^(.-)%-%w*%-mode" ) 158if matched then 159has_variants_gui [ base_name ] =true 160has_variants_gui [ entity . name ] =true 161end 162end 163 164-- Convert to a format usable by GuiAnchor 165local output ={} 166local i =0 167for entity in pairs ( has_variants_gui ) do 168i =i + 1 169output [ i ] =entity 170end 171 172storage . roboport_has_variants_gui =output 173end 174 175--- @param e EventData.on_gui_opened 176local function on_gui_opened ( e ) 177local entity =e . entity 178if not entity or not entity . valid then 179return 180end 181local player =game . get_player ( e . player_index ) 182if not player then 183return 184end 185if entity . type == "roboport" then 186update_gui ( player , entity ) 187end 188end 189 190--- @param e EventData.on_player_created 191local function on_player_created ( e ) 192local player =game . get_player ( e . player_index ) 193if player then 194refresh_gui ( player ) 195end 196end 197 198--- @param e EventData.on_player_removed 199local function on_player_removed ( e ) 200destroy_gui ( e . player_index ) 201end 202 203--- @param e EventData.CustomInputEvent 204local function on_change_roboport_mode_hotkey ( e ) 205local player =game . get_player ( e . player_index ) 206if not player then 207return 208end 209local selected =player . selected 210if selected and selected . valid and selected . type == "roboport" then 211change_mode ( selected , player ) 212end 213end 214 215local function on_configuration_changed () 216find_variants () 217for _ , player in pairs ( game . players ) do 218refresh_gui ( player ) 219end 220end 221 222local function on_init () 223--- @type table<uint, LuaGuiElement> 224storage . roboport_guis ={} 225on_configuration_changed () 226end 227 228local roboport ={} 229 230roboport . on_init =on_init 231roboport . on_configuration_changed =on_configuration_changed 232 233roboport . events ={ 234[ defines . events . on_gui_opened ] =on_gui_opened , 235[ defines . events . on_player_created ] =on_player_created , 236[ defines . events . on_player_removed ] =on_player_removed , 237[ "kr-change-roboport-state" ] =on_change_roboport_mode_hotkey , 238} 239 240flib_gui . add_handlers ( { roboport_on_mode_radio_button_checked =on_mode_radio_button_checked } ) 241 242return roboport