7var layout_manager = preload(
"res://scripts/systems/layoutManager/layout_manager.gd").new()
12func create_popup(parent:Node, title_text:String, rows:Array, options:Dictionary = {}) -> PopupPanel:
13 var popup = PopupPanel.new()
14 popup.name = str(options.get(
"name",
"StatsPopup"))
15 popup.size = Vector2i(int(options.get(
"width", 340)), int(options.get(
"height", 360)))
16 var content = VBoxContainer.new()
17 content.name =
"Content"
18 content.set_anchors_preset(Control.PRESET_FULL_RECT)
19 content.offset_left = int(options.get(
"margin", 14))
20 content.offset_top = int(options.get(
"margin", 14))
21 content.offset_right = -int(options.get(
"margin", 14))
22 content.offset_bottom = -int(options.get(
"margin", 14))
23 content.size_flags_horizontal = Control.SIZE_EXPAND_FILL
24 content.size_flags_vertical = Control.SIZE_EXPAND_FILL
25 content.add_theme_constant_override(
"separation", int(options.get(
"separation", 8)))
26 popup.add_child(content)
28 var title = Label.new()
30 title.text = title_text
31 title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
32 title.add_theme_font_size_override(
"font_size", int(options.get(
"title_size", 20)))
33 content.add_child(title)
35 var scroll = ScrollContainer.new()
36 scroll.name =
"Scroll"
37 scroll.size_flags_horizontal = Control.SIZE_EXPAND_FILL
38 scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL
39 scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
40 content.add_child(scroll)
42 var body = Label.new()
44 body.size_flags_horizontal = Control.SIZE_EXPAND_FILL
45 body.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
46 body.text = format_rows(rows)
47 scroll.add_child(body)
49 var close = Button.new()
51 close.text = str(options.get(
"back_label",
"Back"))
52 close.custom_minimum_size = Vector2(0, int(options.get(
"button_height", 42)))
53 var callback:Callable = options.get(
"back_callback",
Callable())
54 close.pressed.connect(func():
56 if callback.is_valid():
59 content.add_child(close)
61 parent.add_child(popup)
62 _fit_content(popup, popup.size, int(options.get(
"margin", 14)))
68func popup_centered(popup:PopupPanel, viewport_size:Vector2, options:Dictionary = {}) -> void:
69 if popup == null
or not is_instance_valid(popup):
71 var popup_size = layout_manager.fit_popup_to_viewport(popup, viewport_size, options)
72 popup.size = popup_size
73 _fit_content(popup, popup_size, int(options.get(
"content_margin", options.get(
"margin", 14))))
74 if popup.is_inside_tree():
75 popup.popup_centered(popup_size)
79func update_popup(popup:PopupPanel, rows:Array) -> void:
80 if popup == null
or not is_instance_valid(popup):
82 var body = popup.get_node_or_null(
"Content/Scroll/Body")
84 body = popup.get_node_or_null(
"Content/Body")
86 body.text = format_rows(rows)
90func format_rows(rows:Array) -> String:
92 var last_section :=
""
95 lines.append(str(row))
97 if not row
is Dictionary:
99 var section := str(row.get(
"section",
""))
100 if section !=
"" and section != last_section:
101 if not lines.is_empty():
103 lines.append(section)
104 last_section = section
105 var label := str(row.get(
"label",
""))
106 var value := str(row.get(
"value",
""))
112 lines.append(
"%s: %s" % [label, value])
113 return "\n".join(lines)
116func _fit_content(popup:PopupPanel, _popup_size:Vector2i, margin:int) -> void:
117 if popup == null
or not is_instance_valid(popup):
119 var content = popup.get_node_or_null(
"Content")
120 if content
is Control:
121 content.set_anchors_preset(Control.PRESET_FULL_RECT)
122 content.offset_left = margin
123 content.offset_top = margin
124 content.offset_right = -margin
125 content.offset_bottom = -margin
126 content.size_flags_horizontal = Control.SIZE_EXPAND_FILL
127 content.size_flags_vertical = Control.SIZE_EXPAND_FILL
128 var scroll = popup.get_node_or_null(
"Content/Scroll")
129 if scroll
is Control:
130 scroll.size_flags_horizontal = Control.SIZE_EXPAND_FILL
131 scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL
132 var back = popup.get_node_or_null(
"Content/Back")
133 if back
is Control
and back.custom_minimum_size.y <= 0:
134 back.custom_minimum_size = Vector2(0, 42)