z_mirror/EditorExtensions
git clone https://git.raiguard.me/z_mirror/EditorExtensions
8a6b6a9
trunk
1--- @class Util 2local util ={} 3 4local coreutil =require ( "__core__.lualib.util" ) 5util . parse_energy =coreutil . parse_energy 6 7--- @param handler flib.GuiElemHandler 8function util . close_button ( handler ) 9return { 10type ="sprite-button" , 11style ="frame_action_button" , 12sprite ="utility/close" , 13-- hovered_sprite = "utility/close_black", 14-- clicked_sprite = "utility/close_black", 15tooltip ={ "gui.close-instruction" } , 16mouse_button_filter ={ "left" } , 17handler ={ [ defines . events . on_gui_click ] =handler } , 18} 19end 20 21--- @param mode string 22--- @param handler flib.GuiElemHandler 23--- @return flib.GuiElemDef 24function util . mode_radio_button ( mode , handler ) 25return { 26type ="radiobutton" , 27name ="mode_radio_button_" .. string . gsub ( mode , "%-" , "_" ), 28caption ={ "gui-infinity-container." .. mode } , 29tooltip ={ "gui-infinity-pipe." .. mode .. "-tooltip" } , 30state =false , 31tags ={ mode =mode } , 32handler ={ [ defines . events . on_gui_checked_state_changed ] =handler } , 33} 34end 35 36function util . pusher () 37return { type ="empty-widget" , style ="flib_horizontal_pusher" , ignored_by_interaction =true } 38end 39 40--- @return boolean 41function util . in_debug_world () 42if not settings . global [ "ee-override-debug-world" ]. value then 43return false 44end 45if script . level . mod_name ~= "base" or script . level . level_name ~= "freeplay" then 46return false 47end 48local nauvis =game . get_surface ( "nauvis" ) 49if not nauvis then 50return false 51end 52local mps =nauvis . map_gen_settings 53return mps . height == 50 and mps . width == 50 54end 55 56--- @return boolean 57function util . in_testing_scenario () 58return script . level . mod_name == "EditorExtensions" and script . level . level_name == "testing" 59end 60 61--- @param player LuaPlayer 62--- @param message LocalisedString 63--- @param play_sound boolean? 64--- @param position MapPosition? 65function util . flying_text ( player , message , play_sound , position ) 66player . create_local_flying_text ( { 67text =message , 68create_at_cursor =not position , 69position =position , 70} ) 71if play_sound then 72player . play_sound ( { path ="utility/cannot_build" } ) 73end 74end 75 76--- @param player LuaPlayer 77--- @param to_state boolean? 78--- @return boolean 79function util . player_can_use_editor ( player , to_state ) 80local can_use_editor =to_state 81if can_use_editor == nil then 82local permission_group =player . permission_group 83if permission_group then 84can_use_editor =permission_group . allows_action ( defines . input_action . toggle_map_editor ) 85if not can_use_editor and player . controller_type == defines . controllers . editor then 86player . ( { "message.ee-cannot-use-map-editor" } ) 87-- XXX: We need to re-enable the capability in order to get them out of the editor 88permission_group . set_allows_action ( defines . input_action . toggle_map_editor , true ) 89player . toggle_map_editor () 90permission_group . set_allows_action ( defines . input_action . toggle_map_editor , false ) 91end 92end 93end 94player . set_shortcut_available ( "ee-toggle-map-editor" , can_use_editor or false ) 95return can_use_editor and player . admin or false 96end 97 98return util