RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
stats_panel.gd
Go to the documentation of this file.
1extends RefCounted
2
3
6
7var layout_manager = preload("res://scripts/systems/layoutManager/layout_manager.gd").new()
8
9
10
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)
27
28 var title = Label.new()
29 title.name = "Title"
30 title.text = title_text
31 title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
32 title.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
33 title.size_flags_horizontal = Control.SIZE_EXPAND_FILL
34 title.add_theme_font_size_override("font_size", int(options.get("title_size", 20)))
35 content.add_child(title)
36
37 var scroll = ScrollContainer.new()
38 scroll.name = "Scroll"
39 scroll.size_flags_horizontal = Control.SIZE_EXPAND_FILL
40 scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL
41 scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
42 content.add_child(scroll)
43
44 var body = Label.new()
45 body.name = "Body"
46 body.size_flags_horizontal = Control.SIZE_EXPAND_FILL
47 body.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
48 body.text = format_rows(rows)
49 scroll.add_child(body)
50
51 var close = Button.new()
52 close.name = "Back"
53 close.text = str(options.get("back_label", "Back"))
54 close.custom_minimum_size = Vector2(0, int(options.get("button_height", 42)))
55 var callback:Callable = options.get("back_callback", Callable())
56 close.pressed.connect(func():
57 popup.hide()
58 if callback.is_valid():
59 callback.call()
60 )
61 content.add_child(close)
62 if parent != null:
63 parent.add_child(popup)
64 _fit_content(popup, popup.size, int(options.get("margin", 14)))
65 return popup
66
67
68
70func popup_centered(popup:PopupPanel, viewport_size:Vector2, options:Dictionary = {}) -> void:
71 if popup == null or not is_instance_valid(popup):
72 return
73 layout_manager.popup_centered_standard(popup, viewport_size, options)
74 var popup_size := popup.size
75 _fit_content(popup, popup_size, int(options.get("content_margin", options.get("margin", 14))))
76
77
78
79func update_popup(popup:PopupPanel, rows:Array) -> void:
80 if popup == null or not is_instance_valid(popup):
81 return
82 var body = popup.get_node_or_null("Content/Scroll/Body")
83 if body == null:
84 body = popup.get_node_or_null("Content/Body")
85 if body is Label:
86 body.text = format_rows(rows)
87
88
89
90func format_rows(rows:Array) -> String:
91 var lines := []
92 var last_section := ""
93 for row in rows:
94 if row is String:
95 lines.append(str(row))
96 continue
97 if not row is Dictionary:
98 continue
99 var section := str(row.get("section", ""))
100 if section != "" and section != last_section:
101 if not lines.is_empty():
102 lines.append("")
103 lines.append(section)
104 last_section = section
105 var label := str(row.get("label", ""))
106 var value := str(row.get("value", ""))
107 if label == "":
108 lines.append(value)
109 elif value == "":
110 lines.append(label)
111 else:
112 lines.append("%s: %s" % [label, value])
113 return "\n".join(lines)
114
115
116func _fit_content(popup:PopupPanel, _popup_size:Vector2i, margin:int) -> void:
117 if popup == null or not is_instance_valid(popup):
118 return
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)