z_mirror/StatsGui
git clone https://git.raiguard.me/z_mirror/StatsGui
57acba4
trunk
1local sensors =require ( "scripts.sensors" ) 2 3local stats_gui ={} 4 5function stats_gui . build ( player , player_table ) 6local single_line =player_table . settings . single_line 7local style ="statsgui_frame" 8if player_table . settings . adjust_for_clock then 9style =style .. "_clock" 10end 11if not player_table . settings . adjust_for_ups then 12style =style .. "_no_ups" 13end 14 15local window =player . gui . screen . add ( { 16type ="frame" , 17style =style , 18direction =single_line and "horizontal" or "vertical" , 19ignored_by_interaction =true , 20visible =player . controller_type ~= defines . controllers . cutscene , 21} ) 22 23player_table . stats_window =window 24 25stats_gui . update ( player , player_table ) 26end 27 28function stats_gui . destroy ( player_table ) 29local stats_window =player_table . stats_window 30if stats_window and stats_window . valid then 31stats_window . destroy () 32player_table . stats_window =nil 33end 34end 35 36--- @param player LuaPlayer 37function stats_gui . update ( player , player_table ) 38local window =player_table . stats_window 39if not window or not window . valid then 40stats_gui . build ( player , player_table ) 41window =player_table . stats_window 42end 43local children =window . children 44 45local i =0 46for _ , sensor in pairs ( sensors ) do 47local caption =sensor ( player ) 48if caption then 49i =i + 1 50local label =children [ i ] 51if label then 52label . caption =caption 53else 54window . add ( { 55type ="label" , 56style ="statsgui_label" , 57caption =caption , 58} ) 59end 60end 61end 62-- remove extra children 63for j =i + 1 , # children do 64children [ j ]. destroy () 65end 66 67local in_remote =player . controller_type == defines . controllers . remote 68local in_train_gui =player . opened_gui_type == defines . gui_type . entity and player . opened . type == "locomotive" 69local location =window . location 70location . y =in_remote and ( 36 * player . display_scale ) or 0 71window . location =location 72window . style . width =( player . display_resolution . width / player . display_scale ) 73window . visible =not in_train_gui 74end 75 76return stats_gui