z_mirror/RecipeBook

git clone https://git.raiguard.me/z_mirror/RecipeBook

Caleb HeuerFix clobbering of a generic `id` tag namea949b6c

trunk
3.0 KiB104 linesraw
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)
23  if not ids or #ids == 0 then
24    return
25  end
26  local outer = parent.add({ type = "flow", direction = "vertical" })
27  local header = outer.add({ type = "flow" })
28  header.style.vertical_align = "center"
29  header.add({
30    type = "checkbox",
31    style = "rb_list_box_caption",
32    caption = title,
33    state = false,
34    mouse_button_filter = {},
35    tags = flib_gui.format_handlers({ [defines.events.on_gui_click] = info_section.on_header_clicked }),
36  })
37  local count_label = header.add({ type = "label", style = "rb_info_label" })
38  if settings.remark then
39    header.add({ type = "empty-widget", style = "flib_horizontal_pusher" })
40    header.add({ type = "label", style = "count_label", caption = settings.remark })
41  end
42
43  local holder = outer
44    .add({
45      type = "frame",
46      name = "frame",
47      style = settings.style or "slot_button_deep_frame",
48      direction = "vertical",
49    })
50    .add({ type = "table", name = "table", style = "slot_table", column_count = settings.column_count or 1 })
51
52  local show_hidden = context.show_hidden
53  local show_unresearched = context.show_unresearched
54  local force_index = context.player.force.index
55  local always_show = settings.always_show
56
57  local result_count = 0
58  for id_index = 1, #ids do
59    local id = ids[id_index]
60    local prototype = util.get_prototype(id)
61    if not prototype then
62      goto continue
63    end
64
65    local is_hidden = prototype.hidden_in_factoriopedia
66    local is_unresearched = not researched.is(prototype, force_index)
67    if not always_show then
68      if is_hidden and not show_hidden then
69        goto continue
70      elseif is_unresearched and not show_unresearched then
71        goto continue
72      end
73    end
74    local button = callback(id, holder)
75    if not button then
76      goto continue
77    end
78
79    gui_util.add_tag(button, "rb_id", { type = id.type, name = id.name })
80
81    result_count = result_count + 1
82    ::continue::
83  end
84
85  if result_count == 0 then
86    outer.destroy()
87    return
88  end
89
90  count_label.caption = { "", "[", result_count, "]" }
91end
92
93--- @param e EventData.on_gui_click
94function info_section.on_header_clicked(e)
95  local frame = e.element.parent.parent.frame
96  if frame then
97    frame.style.height = e.element.state and 1 or 0
98    frame.ignored_by_interaction = e.element.state
99  end
100end
101
102flib_gui.add_handlers(info_section, nil, "info_section")
103
104return info_section