z_mirror/RecipeBook
git clone https://git.raiguard.me/z_mirror/RecipeBook
a949b6c
trunk
1local flib_gui =require ( "__flib__.gui" ) 2 3local gui_util =require ( "scripts.gui.util" ) 4local researched =require ( "scripts.database.researched" ) 5local util =require ( "scripts.util" ) 6 7--- @class InfoSectionSettings 8--- @field always_show boolean? 9--- @field column_count integer? 10--- @field remark LocalisedString? 11--- @field style string? 12 13--- @class InfoSection 14local info_section ={} 15 16--- @param parent LuaGuiElement 17--- @param context MainGuiContext 18--- @param title LocalisedString 19--- @param ids DatabaseID[]? 20--- @param settings InfoSectionSettings 21--- @param callback function(id: DatabaseID, holder: LuaGuiElement): LuaGuiElement? 22function info_section . build ( parent , context , title , ids , settings , callback ) 23if not ids or # ids == 0 then 24return 25end 26local outer =parent . add ( { type ="flow" , direction ="vertical" } ) 27local header =outer . add ( { type ="flow" } ) 28header . style . vertical_align ="center" 29header . add ( { 30type ="checkbox" , 31style ="rb_list_box_caption" , 32caption =title , 33state =false , 34mouse_button_filter ={} , 35tags =flib_gui . format_handlers ( { [ defines . events . on_gui_click ] =info_section . on_header_clicked } ), 36} ) 37local count_label =header . add ( { type ="label" , style ="rb_info_label" } ) 38if settings . remark then 39header . add ( { type ="empty-widget" , style ="flib_horizontal_pusher" } ) 40header . add ( { type ="label" , style ="count_label" , caption =settings . remark } ) 41end 42 43local holder =outer 44. add ( { 45type ="frame" , 46name ="frame" , 47style =settings . style or "slot_button_deep_frame" , 48direction ="vertical" , 49} ) 50. add ( { type ="table" , name ="table" , style ="slot_table" , column_count =settings . column_count or 1 } ) 51 52local show_hidden =context . show_hidden 53local show_unresearched =context . show_unresearched 54local force_index =context . player . force . index 55local always_show =settings . always_show 56 57local result_count =0 58for id_index =1 , # ids do 59local id =ids [ id_index ] 60local prototype =util . get_prototype ( id ) 61if not prototype then 62goto continue 63end 64 65local is_hidden =prototype . hidden_in_factoriopedia 66local is_unresearched =not researched . is ( prototype , force_index ) 67if not always_show then 68if is_hidden and not show_hidden then 69goto continue 70elseif is_unresearched and not show_unresearched then 71goto continue 72end 73end 74local button =callback ( id , holder ) 75if not button then 76goto continue 77end 78 79gui_util . add_tag ( button , "rb_id" , { type =id . type , name =id . name } ) 80 81result_count =result_count + 1 82:: continue :: 83end 84 85if result_count == 0 then 86outer . destroy () 87return 88end 89 90count_label . caption ={ "" , "[" , result_count , "]" } 91end 92 93--- @param e EventData.on_gui_click 94function info_section . on_header_clicked ( e ) 95local frame =e . element . parent . parent . frame 96if frame then 97frame . style . height =e . element . state and 1 or 0 98frame . ignored_by_interaction =e . element . state 99end 100end 101 102flib_gui . add_handlers ( info_section , nil , "info_section" ) 103 104return info_section