z_mirror/RecipeBook
git clone https://git.raiguard.me/z_mirror/RecipeBook
f677f42
trunk
1local flib_format =require ( "__flib__.format" ) 2local flib_math =require ( "__flib__.math" ) 3local core_util =require ( "__core__.lualib.util" ) 4local util =require ( "scripts.util" ) 5 6--- @class GuiUtil 7local gui_util ={} 8 9--- @param crafting_time double? 10--- @return LocalisedString? 11function gui_util . format_crafting_time ( crafting_time ) 12if not crafting_time then 13return nil 14end 15return { 16"" , 17"[img=quantity-time]" , 18gui_util . format_time ( crafting_time ), 19" " , 20{ "description.crafting-time" } , 21} 22end 23 24--- @param count uint? 25--- @param time double? 26--- @return LocalisedString 27function gui_util . format_technology_count_and_time ( count , time ) 28if not count or not time then 29return 30end 31 32--- @type LocalisedString 33return { 34"" , 35"[img=quantity-time]" , 36{ "time-symbol-seconds" , flib_math . floored ( time , 0.01 ) } , 37" × " , 38flib_format . number ( count , true ), 39} 40end 41 42local temperature_edge =( 2 - 2 ^ - 23 ) * 2 ^ 127 43 44--- @param id DatabaseID 45--- @return LocalisedString 46function gui_util . format_caption ( id ) 47--- @type LocalisedString 48local caption ={ "" } 49if id . probability and id . probability < 1 then 50caption [ # caption + 1 ] ={ 51"" , 52"[font=default-semibold]" , 53{ "format-percent" , flib_math . round ( id . probability * 100 , 0.01 ) } , 54"[/font] " , 55} 56end 57if id . amount then 58if id . extra_count_fraction then 59caption [ # caption + 1 ] ={ 60"" , 61"[font=default-semibold]" , 62core_util . format_number ( flib_math . round ( id . amount , 0.01 ), true ), 63"-" , 64core_util . format_number ( flib_math . round ( id . amount + 1 , 0.01 ), true ), 65" ×[/font] " , 66} 67else 68caption [ # caption + 1 ] ={ 69"" , 70"[font=default-semibold]" , 71core_util . format_number ( flib_math . round ( id . amount , 0.01 ), true ), 72" ×[/font] " , 73} 74end 75elseif id . amount_min and id . amount_max then 76caption [ # caption + 1 ] ={ 77"" , 78"[font=default-semibold]" , 79core_util . format_number ( flib_math . round ( id . amount_min , 0.01 ), true ), 80"-" , 81core_util . format_number ( flib_math . round ( id . amount_max , 0.01 ), true ), 82" ×[/font] " , 83} 84end 85if id . time then 86caption [ # caption + 1 ] ={ 87"" , 88"[font=default-semibold](" , 89gui_util . format_time ( id . time ), 90")[/font] " , 91} 92end 93 94caption [ # caption + 1 ] =util . get_prototype ( id ). localised_name --- @diagnostic disable-line:assign-type-mismatch 95 96if id . temperature then 97caption [ # caption + 1 ] ={ "" , " (" , flib_math . round ( id . temperature , 0.01 ), { "si-unit-degree-celsius" } , ")" } 98elseif id . minimum_temperature and id . maximum_temperature then 99local temperature_min =id . minimum_temperature --[[@as number]] 100local temperature_max =id . maximum_temperature --[[@as number]] 101local temperature_string 102if temperature_min == - temperature_edge then 103temperature_string ="≤ " .. flib_math . round ( temperature_max , 0.01 ) 104elseif temperature_max == temperature_edge then 105temperature_string ="≥ " .. flib_math . round ( temperature_min , 0.01 ) 106else 107temperature_string ="" 108.. flib_math . round ( temperature_min , 0.01 ) 109.. " - " 110.. flib_math . round ( temperature_max , 0.01 ) 111end 112caption [ # caption + 1 ] ={ "" , " (" , temperature_string , { "si-unit-degree-celsius" } , ")" } 113end 114 115if id . extra_count_fraction then 116caption [ # caption + 1 ] =" [img=info]" 117end 118 119return caption 120end 121 122--- @param id DatabaseID 123function gui_util . format_tooltip ( id ) 124--- @type LocalisedString 125local output ={ "" } 126if id . extra_count_fraction then 127output [ # output + 1 ] ={ 128"" , 129{ 130"description.extra-count-factor" , 131core_util . format_number ( flib_math . round ( id . extra_count_fraction * 100 , 0.01 )), 132} , 133"\n" , 134} 135end 136output [ # output + 1 ] ={ "" , { "gui.rb-control-hint" } , "\n" , { "gui.rb-pipette-control-hint" } } 137return output 138end 139 140--- @param input number? 141--- @return LocalisedString? 142function gui_util . format_short_number ( input ) 143if not input then 144return 145end 146local divisor =0.1 147if input >= 100 then 148divisor =1 149elseif input < 1 then 150divisor =0.01 151end 152 153return core_util . format_number ( flib_math . floored ( input , divisor ), true ) 154end 155 156--- @param input number Time in seconds. 157--- @return LocalisedString 158function gui_util . format_time ( input ) 159local output =input 160local format ="time-symbol-seconds" 161if input >= 60 * 60 then 162output =input / 60 / 60 163format ="time-symbol-hours" 164elseif input >= 60 then 165output =input / 60 166format ="time-symbol-minutes" 167end 168 169return { format , gui_util . format_short_number ( output ) } 170end 171 172--- @param id DatabaseID 173--- @return LocalisedString? bottom 174--- @return LocalisedString? top 175function gui_util . format_slot_button_remarks ( id ) 176local amount =id . amount 177local amount_min =id . amount_min 178local amount_max =id . amount_max 179if amount then 180return gui_util . format_short_number ( amount ) 181elseif amount_min and amount_max and amount_min ~= amount_max then 182return gui_util . format_short_number ( amount_min ), gui_util . format_short_number ( amount_max ) 183elseif amount_min then 184return gui_util . format_short_number ( amount_min ) 185elseif amount_max then 186return gui_util . format_short_number ( amount_max ) 187end 188if id . time then 189return gui_util . format_time ( id . time ) 190end 191local temperature =id . temperature 192local temperature_min =id . minimum_temperature 193local temperature_max =id . maximum_temperature 194--- @type LocalisedString? 195local bottom 196--- @type LocalisedString? 197local top 198if temperature then 199bottom ={ "" , gui_util . format_short_number ( temperature ), { "si-unit-degree-celsius" } } 200elseif temperature_min and temperature_max then 201if temperature_min == - temperature_edge then 202bottom ={ "" , "≤" , gui_util . format_short_number ( temperature_max ), { "si-unit-degree-celsius" } } 203elseif temperature_max == temperature_edge then 204bottom ={ "" , "≥" , gui_util . format_short_number ( temperature_min ), { "si-unit-degree-celsius" } } 205else 206bottom ={ "" , gui_util . format_short_number ( temperature_min ), { "si-unit-degree-celsius" } } 207top ={ "" , gui_util . format_short_number ( temperature_max ), { "si-unit-degree-celsius" } } 208end 209end 210 211return bottom , top 212end 213 214--- @alias FabState 215--- | "default" 216--- | "selected" 217--- | "disabled" 218 219--- @param button LuaGuiElement 220--- @param state FabState 221function gui_util . update_frame_action_button ( button , state ) 222if state == "default" then 223button . enabled =true 224button . toggled =false 225elseif state == "selected" then 226button . enabled =true 227button . toggled =true 228elseif state == "disabled" then 229button . enabled =false 230button . toggled =false 231end 232end 233 234--- @type table<string, LocalisedString> 235gui_util . type_locale ={ 236LuaEntityPrototype ={ "factoriopedia.entity" } , 237LuaEquipmentPrototype ={ "gui-map-editor.character-equipment" } , 238LuaFluidPrototype ={ "factoriopedia.fluid" } , 239LuaItemPrototype ={ "factoriopedia.item" } , 240LuaRecipePrototype ={ "description.recipe" } , 241LuaTechnologyPrototype ={ "gui-map-generator.technology-difficulty-group-tile" } , 242LuaTilePrototype ={ "factoriopedia.tile" } , 243} 244 245--- @type table<string, boolean> 246gui_util . vehicles ={ 247[ "car" ] =true , 248[ "artillery-wagon" ] =true , 249[ "cargo-wagon" ] =true , 250[ "fluid-wagon" ] =true , 251[ "locomotive" ] =true , 252[ "spider-vehicle" ] =true , 253} 254 255--- @return LocalisedString 256function gui_util . format_power ( input ) 257local formatted =flib_format . number ( input , true ) 258if input < 1000 then 259return { "" , formatted , " " , { "si-unit-symbol-watt" } } 260end 261return { "" , formatted , { "si-unit-symbol-watt" } } 262end 263 264--- @param id DatabaseID 265--- @param fallback string 266function gui_util . get_tooltip_category_sprite ( id , fallback ) 267local by_name ="tooltip-category-" .. id . name 268if helpers . is_valid_sprite_path ( by_name ) then 269return by_name 270end 271return "tooltip-category-" .. fallback 272end 273 274--- @param elem LuaGuiElement 275--- @param key string 276--- @param value AnyBasic 277function gui_util . add_tag ( elem , key , value ) 278local tags =elem . tags 279tags [ key ] =value 280elem . tags =tags 281end 282 283--- @param tick uint 284--- @return string 285function gui_util . format_spoil_time ( tick ) 286local total_seconds =math . floor ( tick / 60 ) 287local seconds =total_seconds % 60 288local minutes =math . floor ( total_seconds / 60 % 60 ) 289local hours =math . floor ( total_seconds / 60 / 60 ) 290local output ="" 291if hours > 0 then 292output =output .. hours .. "h" 293end 294if minutes > 0 then 295output =output .. minutes .. "m" 296end 297if seconds > 0 then 298output =output .. seconds .. "s" 299end 300return output 301end 302 303return gui_util