11const SpriteManager = preload(
"res://scripts/systems/spriteManager/sprite_manager.gd")
13var resource_symbols := {}
14var selected_resource_id :=
""
15var sprite_manager = SpriteManager.new()
17@onready var resource_hud = $MainLayout/TopBar/ResourceHUD
19func _make_row_style(is_selected:bool = false) -> StyleBoxFlat:
20 var style = StyleBoxFlat.new()
21 style.bg_color = Color(
"#303b47")
if is_selected
else Color(
"#27313b")
22 style.border_color = Color(
"#f2c86b")
if is_selected
else Color(
"#3a4652")
23 style.set_border_width_all(1)
24 style.set_corner_radius_all(6)
25 style.content_margin_left = 4
26 style.content_margin_right = 4
27 style.content_margin_top = 5
28 style.content_margin_bottom = 5
31func _build_icon_control(resource_definition:Dictionary) -> Control:
32 if sprite_manager != null:
33 var texture = sprite_manager.get_scaled_texture_for_definition(resource_definition, 30, {
"path_key":
"icon_path"})
35 var icon = TextureRect.new()
36 icon.texture = texture
37 icon.custom_minimum_size = Vector2(30, 30)
38 icon.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
39 icon.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
40 icon.mouse_filter = Control.MOUSE_FILTER_IGNORE
43 var icon = Label.new()
44 icon.text = resource_definition.get(
"icon",
"?")
45 icon.add_theme_font_size_override(
"font_size", 24)
46 icon.custom_minimum_size = Vector2(30, 30)
47 icon.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
48 icon.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
49 icon.mouse_filter = Control.MOUSE_FILTER_IGNORE
52func _make_bar_style(color:Color) -> StyleBoxFlat:
53 var style = StyleBoxFlat.new()
54 style.bg_color = color
55 style.set_corner_radius_all(3)
58func _get_resource_color(value:int, max_value:int) -> Color:
59 var percent = float(value) / float(max(max_value, 1))
61 return Color(
"#55d17a")
63 return Color(
"#f2c86b")
64 return Color(
"#f05f64")
66func _build_value_bar(value:int, max_value:int, show_percent:bool) -> Control:
67 var percent = clampf(float(value) / float(max(max_value, 1)), 0.0, 1.0)
68 var frame = Control.new()
69 frame.custom_minimum_size = Vector2(42, 15)
70 frame.size_flags_horizontal = Control.SIZE_EXPAND_FILL
71 frame.mouse_filter = Control.MOUSE_FILTER_IGNORE
73 var background = Panel.new()
74 background.set_anchors_preset(Control.PRESET_FULL_RECT)
75 background.offset_top = 2
76 background.offset_bottom = -2
77 background.add_theme_stylebox_override(
"panel", _make_bar_style(Color(
"#141b22")))
78 background.mouse_filter = Control.MOUSE_FILTER_IGNORE
79 frame.add_child(background)
81 var fill = Panel.new()
82 fill.anchor_left = 0.0
84 fill.anchor_bottom = 1.0
85 fill.anchor_right = percent
87 fill.offset_bottom = -2
88 fill.add_theme_stylebox_override(
"panel", _make_bar_style(_get_resource_color(value, max_value)))
89 fill.mouse_filter = Control.MOUSE_FILTER_IGNORE
93 var label = Label.new()
94 label.name =
"ResourcePercent"
95 label.set_anchors_preset(Control.PRESET_FULL_RECT)
96 label.text = str(roundi(percent * 100.0)) +
"%"
97 label.add_theme_font_size_override(
"font_size", 10)
98 label.add_theme_color_override(
"font_color", Color(
"#f8fbff"))
99 label.add_theme_color_override(
"font_shadow_color", Color(
"#081018"))
100 label.add_theme_constant_override(
"shadow_offset_x", 1)
101 label.add_theme_constant_override(
"shadow_offset_y", 1)
102 label.add_theme_constant_override(
"outline_size", 2)
103 label.add_theme_color_override(
"font_outline_color", Color(
"#081018"))
104 label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
105 label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
106 label.mouse_filter = Control.MOUSE_FILTER_IGNORE
107 frame.add_child(label)
111func _build_detail_label(id:String, resource_definition:Dictionary, is_selected:bool) -> Label:
112 var label = Label.new()
113 label.name =
"ResourceName"
114 label.text = resource_definition.get(
"display_name", id.capitalize())
if is_selected
else ""
115 label.add_theme_font_size_override(
"font_size", 10)
116 label.add_theme_color_override(
"font_color", Color(
"#f4f7fb"))
117 label.add_theme_color_override(
"font_shadow_color", Color(
"#081018"))
118 label.add_theme_constant_override(
"shadow_offset_x", 1)
119 label.add_theme_constant_override(
"shadow_offset_y", 1)
120 label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
121 label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
122 label.custom_minimum_size = Vector2(34, 12)
123 label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
124 label.clip_text = true
125 label.mouse_filter = Control.MOUSE_FILTER_IGNORE
133func apply_compact_text_style(root:Node) -> void:
137 var label := root
as Label
138 if label.name ==
"ResourcePercent":
139 label.add_theme_font_size_override(
"font_size", 10)
140 label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
141 label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
142 elif label.name ==
"ResourceName":
143 label.add_theme_font_size_override(
"font_size", 10)
144 label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
145 label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
146 label.clip_text = true
147 for child
in root.get_children():
148 apply_compact_text_style(child)
150func _build_spacer(height:int) -> Control:
151 var spacer = Control.new()
152 spacer.custom_minimum_size = Vector2(1, height)
153 spacer.mouse_filter = Control.MOUSE_FILTER_IGNORE
156func _toggle_resource_detail(id:String, resources, resource_hud, resource_order, resource_definitions) -> void:
157 selected_resource_id = id
158 refresh_hud(resources, resource_hud, resource_order, resource_definitions)
161func clear_selection(resources, resource_hud, resource_order, resource_definitions) -> void:
162 if selected_resource_id ==
"":
164 selected_resource_id =
""
165 refresh_hud(resources, resource_hud, resource_order, resource_definitions)
171func refresh_hud(resources, resource_hud, resource_order, resource_definitions):
172 for c
in resource_hud.get_children():
174 for id
in resource_order:
175 var definition = resource_definitions[id]
176 var value = int(resources.get(id, 0))
177 var max_value = int(definition.get(
"max_value", 100))
178 var is_selected = selected_resource_id == id
180 var panel = Button.new()
182 panel.custom_minimum_size = Vector2(0, 48)
183 panel.size_flags_horizontal = Control.SIZE_EXPAND_FILL
184 panel.size_flags_stretch_ratio = 1.0
185 panel.add_theme_stylebox_override(
"normal", _make_row_style(is_selected))
186 panel.add_theme_stylebox_override(
"hover", _make_row_style(true))
187 panel.add_theme_stylebox_override(
"pressed", _make_row_style(true))
188 panel.pressed.connect(func(): _toggle_resource_detail(id, resources, resource_hud, resource_order, resource_definitions))
190 var inset = MarginContainer.new()
191 inset.set_anchors_preset(Control.PRESET_FULL_RECT)
192 inset.offset_left = 10
194 inset.offset_right = -10
195 inset.offset_bottom = -5
196 inset.mouse_filter = Control.MOUSE_FILTER_IGNORE
198 var row = HBoxContainer.new()
199 row.set_anchors_preset(Control.PRESET_FULL_RECT)
200 row.alignment = BoxContainer.ALIGNMENT_CENTER
201 row.add_theme_constant_override(
"separation", 6)
202 row.mouse_filter = Control.MOUSE_FILTER_IGNORE
204 var icon_slot = CenterContainer.new()
205 icon_slot.custom_minimum_size = Vector2(30, 36)
206 icon_slot.mouse_filter = Control.MOUSE_FILTER_IGNORE
207 icon_slot.add_child(_build_icon_control(definition))
209 var value_stack = VBoxContainer.new()
210 value_stack.custom_minimum_size = Vector2(42, 36)
211 value_stack.size_flags_horizontal = Control.SIZE_EXPAND_FILL
212 value_stack.alignment = BoxContainer.ALIGNMENT_BEGIN
213 value_stack.add_theme_constant_override(
"separation", 0)
214 value_stack.mouse_filter = Control.MOUSE_FILTER_IGNORE
215 value_stack.add_child(_build_spacer(7))
216 value_stack.add_child(_build_value_bar(value, max_value, is_selected))
217 value_stack.add_child(_build_detail_label(id, definition, is_selected))
219 row.add_child(icon_slot)
220 row.add_child(value_stack)
222 panel.add_child(inset)
223 resource_hud.add_child(panel)
229func build_from_resources(resource_definitions:Dictionary, resources:Dictionary):
230 for child
in get_children():
233 for id
in resource_definitions.keys():
234 var panel = PanelContainer.new()
235 panel.add_theme_stylebox_override(
"panel", _make_row_style(false))
236 var row = HBoxContainer.new()
237 row.alignment = BoxContainer.ALIGNMENT_CENTER
238 row.add_theme_constant_override(
"separation", 7)
239 var icon = _build_icon_control(resource_definitions[id])
240 var label = _build_value_bar(int(resources.get(id, 0)), int(resource_definitions[id].get(
"max_value", 100)), false)