z_mirror/EditorExtensions
git clone https://git.raiguard.me/z_mirror/EditorExtensions
468b48b
hidden-items
1local gui =require ( "__flib__/gui-lite" ) 2local math =require ( "__flib__/math" ) 3local table =require ( "__flib__/table" ) 4 5local util =require ( "__EditorExtensions__/scripts/util" ) 6 7--- @param entity LuaEntity 8--- @param speed double 9local function set_speed ( entity , speed ) 10entity . fluidbox [ 2 ] ={ 11name ="ee-super-pump-speed-fluid" , 12amount =100000000000 , 13temperature =speed + 0.01 , -- avoid floating point imprecision 14} 15end 16 17--- @param entity LuaEntity 18local function get_speed ( entity ) 19return math . floor ( entity . fluidbox [ 2 ]. temperature ) 20end 21 22local slider_to_temperature ={ 23[ 0 ] =0 , 24[ 1 ] =100 , 25[ 2 ] =200 , 26[ 3 ] =300 , 27[ 4 ] =400 , 28[ 5 ] =500 , 29[ 6 ] =600 , 30[ 7 ] =700 , 31[ 8 ] =800 , 32[ 9 ] =900 , 33[ 10 ] =1000 , 34[ 11 ] =2000 , 35[ 12 ] =3000 , 36[ 13 ] =4000 , 37[ 14 ] =5000 , 38[ 15 ] =6000 , 39[ 16 ] =7000 , 40[ 17 ] =8000 , 41[ 18 ] =9000 , 42[ 19 ] =10000 , 43[ 20 ] =15000 , 44[ 21 ] =20000 , 45[ 22 ] =25000 , 46[ 23 ] =30000 , 47} 48local temperature_to_slider =table . invert ( slider_to_temperature ) 49 50--- @param speed double 51--- @return double 52local function to_slider_value ( speed ) 53local index 54if speed == 0 then 55index =0 56elseif speed < 1000 then 57index =math . floor ( speed / 100 ) * 100 58elseif speed < 10000 then 59index =math . floor ( speed / 1000 ) * 1000 60elseif speed < 30000 then 61index =math . floor ( speed / 5000 ) * 5000 62else 63index =30000 64end 65return temperature_to_slider [ index ] 66end 67 68--- @param value double 69--- @return double 70local function from_slider_value ( value ) 71return slider_to_temperature [ value ] 72end 73 74--- @param entity LuaEntity 75--- @param flow LuaGuiElement 76local function update_gui ( entity , flow ) 77local speed =get_speed ( entity ) 78 79local textfield =flow . speed_textfield --[[@as LuaGuiElement]] 80textfield . style ="ee_slider_textfield" 81textfield . text =tostring ( speed ) 82 83local slider =flow . speed_slider --[[@as LuaGuiElement]] 84slider . slider_value =to_slider_value ( speed ) 85end 86 87local handlers ={ 88--- @param entity LuaEntity 89--- @param e EventData.on_gui_value_changed 90on_pump_speed_slider_value_changed =function ( entity , e ) 91local new_speed =from_slider_value ( e . element . slider_value ) 92set_speed ( entity , new_speed ) 93update_gui ( entity , e . element . parent ) 94end , 95 96--- @param entity LuaEntity 97--- @param e EventData.on_gui_text_changed 98on_pump_speed_textfield_changed =function ( entity , e ) 99local textfield =e . element 100local text =textfield . text 101local speed =tonumber ( text ) 102 103if not speed or speed > 600000 then 104textfield . style ="ee_invalid_slider_textfield" 105return 106end 107 108set_speed ( entity , speed ) 109update_gui ( entity , e . element . parent ) 110end , 111} 112 113gui . add_handlers ( handlers , function ( e , handler ) 114local player =game . get_player ( e . player_index ) 115if not player or player . opened_gui_type ~= defines . gui_type . entity then 116return 117end 118local entity =player . opened 119if not entity or not entity . valid then 120return 121end 122if entity . name ~= "ee-super-pump" then 123return 124end 125handler ( entity , e ) 126end ) 127 128--- @param player LuaPlayer 129local function create_gui ( player ) 130local existing =player . gui . relative . ee_super_pump_window 131if existing then 132existing . destroy () 133end 134 135gui . add ( player . gui . relative , { 136type ="frame" , 137name ="ee_super_pump_window" , 138style ="frame_with_even_paddings" , 139style_mods ={ width =448 } , 140anchor ={ 141gui =defines . relative_gui_type . entity_with_energy_source_gui , 142position =defines . relative_gui_position . bottom , 143name ="ee-super-pump" , 144} , 145{ 146type ="frame" , 147name ="inner_frame" , 148style ="inside_shallow_frame_with_padding" , 149{ 150type ="flow" , 151name ="inner_flow" , 152style ="centering_horizontal_flow" , 153{ type ="label" , caption ={ "gui.ee-speed" } , tooltip ={ "gui.ee-speed-tooltip" } } , 154{ 155type ="slider" , 156name ="speed_slider" , 157style_mods ={ horizontally_stretchable =true , margin ={ 0 , 8 , 0 , 8 } } , 158minimum_value =0 , 159maximum_value =23 , 160value =0 , 161handler ={ [ defines . events . on_gui_value_changed ] =handlers . on_pump_speed_slider_value_changed } , 162} , 163{ 164type ="textfield" , 165name ="speed_textfield" , 166style ="ee_slider_textfield" , 167numeric =true , 168clear_and_focus_on_right_click =true , 169handler ={ [ defines . events . on_gui_text_changed ] =handlers . on_pump_speed_textfield_changed } , 170} , 171} , 172} , 173} ) 174end 175 176--- @param e BuiltEvent 177local function on_entity_built ( e ) 178local entity =e . created_entity or e . entity or e . destination 179if not entity or not entity . valid or entity . name ~= "ee-super-pump" then 180return 181end 182local speed =12000 183local tags =e . tags 184if tags and tags . EditorExtensions then 185speed =tags . EditorExtensions . speed --[[@as double]] 186end 187set_speed ( entity , speed ) 188end 189 190--- @param e EventData.on_player_setup_blueprint 191local function on_player_setup_blueprint ( e ) 192local blueprint =util . get_blueprint ( e ) 193if not blueprint then 194return 195end 196 197local entities =blueprint . get_blueprint_entities () 198if not entities then 199return 200end 201for i , entity in pairs ( entities ) do 202--- @cast i uint 203if entity . name ~= "ee-super-pump" then 204goto continue 205end 206local real_entity =e . surface . find_entity ( entity . name , entity . position ) 207if not real_entity then 208goto continue 209end 210blueprint . set_blueprint_entity_tag ( i , "EditorExtensions" , { speed =get_speed ( real_entity ) } ) 211:: continue :: 212end 213end 214 215local function on_entity_settings_pasted ( e ) 216local source , destination =e . source , e . destination 217if not source . valid or not destination . valid then 218return 219end 220if source . name ~= "ee-super-pump" or destination . name ~= "ee-super-pump" then 221return 222end 223set_speed ( destination , get_speed ( source )) 224end 225 226--- @param e EventData.on_gui_opened 227local function on_gui_opened ( e ) 228local player =game . get_player ( e . player_index ) 229if not player or player . opened_gui_type ~= defines . gui_type . entity then 230return 231end 232local entity =e . entity 233if not entity or not entity . valid then 234return 235end 236if entity . name ~= "ee-super-pump" then 237return 238end 239 240update_gui ( entity , player . gui . relative . ee_super_pump_window . inner_frame . inner_flow ) 241end 242 243--- @param e EventData.on_player_created 244local function on_player_created ( e ) 245local player =game . get_player ( e . player_index ) 246if not player then 247return 248end 249 250create_gui ( player ) 251end 252 253local super_pump ={} 254 255super_pump . on_configuration_changed =function () 256for _ , player in pairs ( game . players ) do 257create_gui ( player ) 258end 259end 260 261super_pump . events ={ 262[ defines . events . on_built_entity ] =on_entity_built , 263[ defines . events . on_entity_cloned ] =on_entity_built , 264[ defines . events . on_entity_settings_pasted ] =on_entity_settings_pasted , 265[ defines . events . on_gui_opened ] =on_gui_opened , 266[ defines . events . on_player_created ] =on_player_created , 267[ defines . events . on_player_setup_blueprint ] =on_player_setup_blueprint , 268[ defines . events . on_robot_built_entity ] =on_entity_built , 269[ defines . events . script_raised_built ] =on_entity_built , 270[ defines . events . script_raised_revive ] =on_entity_built , 271} 272 273return super_pump