RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
resource_bar.gd
Go to the documentation of this file.
1extends RefCounted
2
3
12
13
14func render(parent:Control, rect:Rect2, state:Dictionary, options:Dictionary = {}) -> Dictionary:
15 if parent == null:
16 return {}
17 var prefix = str(options.get("name", "ResourceBar"))
18 var max_value = max(1.0, float(state.get("max", 1.0)))
19 var value = clamp(float(state.get("value", 0.0)), 0.0, max_value)
20 var ratio = value / max_value
21 var border_width = float(options.get("border_width", 2.0))
22 var fill_color = _value_color(value, max_value)
23 if options.has("fill"):
24 fill_color = Color(str(options.get("fill", "#63d978")))
25 var bg = ColorRect.new()
26 bg.name = "%sBackground" % prefix
27 bg.position = rect.position
28 bg.size = rect.size
29 bg.color = Color(str(options.get("background", "#16090c")))
30 bg.mouse_filter = Control.MOUSE_FILTER_IGNORE
31 parent.add_child(bg)
32 var fill_rect = Rect2(rect.position + Vector2(border_width, border_width), Vector2(max(0.0, rect.size.x - border_width * 2.0) * ratio, max(0.0, rect.size.y - border_width * 2.0)))
33 var fill = ColorRect.new()
34 fill.name = "%sFill" % prefix
35 fill.position = fill_rect.position
36 fill.size = fill_rect.size
37 fill.color = fill_color
38 fill.mouse_filter = Control.MOUSE_FILTER_IGNORE
39 parent.add_child(fill)
40 _add_border(parent, rect, prefix, Color(str(options.get("border", "#e8f0f7"))), border_width)
41 var label = str(options.get("label", "%d" % int(value)))
42 if label != "":
43 _add_label(parent, rect, prefix, label, options)
44 return {
45 "background": bg,
46 "fill": fill
47 }
48
49
50func _value_color(value:float, max_value:float) -> Color:
51 var percent = value / max(max_value, 1.0)
52 if percent >= 0.66:
53 return Color("#55d17a")
54 if percent >= 0.33:
55 return Color("#f2c86b")
56 return Color("#f05f64")
57
58
59func _add_border(parent:Control, rect:Rect2, prefix:String, color:Color, width:float) -> void:
60 var pieces = [
61 Rect2(rect.position, Vector2(rect.size.x, width)),
62 Rect2(Vector2(rect.position.x, rect.end.y - width), Vector2(rect.size.x, width)),
63 Rect2(rect.position, Vector2(width, rect.size.y)),
64 Rect2(Vector2(rect.end.x - width, rect.position.y), Vector2(width, rect.size.y))
65 ]
66 for index in range(pieces.size()):
67 var node = ColorRect.new()
68 node.name = "%sBorder%d" % [prefix, index]
69 node.position = pieces[index].position
70 node.size = pieces[index].size
71 node.color = color
72 node.mouse_filter = Control.MOUSE_FILTER_IGNORE
73 parent.add_child(node)
74
75
76func _add_label(parent:Control, rect:Rect2, prefix:String, text:String, options:Dictionary) -> void:
77 var font_size = int(options.get("font_size", 12))
78 for shadow_offset in [Vector2(1, 1), Vector2(-1, 1), Vector2(1, -1), Vector2(-1, -1)]:
79 var shadow = _make_label("%sTextShadow" % prefix, text, rect, Color(str(options.get("shadow", "#050607"))), font_size)
80 shadow.position += shadow_offset
81 parent.add_child(shadow)
82 var label = _make_label("%sText" % prefix, text, rect, Color(str(options.get("text", "#f8fafc"))), font_size)
83 parent.add_child(label)
84
85
86func _make_label(name:String, text:String, rect:Rect2, color:Color, font_size:int) -> Label:
87 var label = Label.new()
88 label.name = name
89 label.text = text
90 label.position = rect.position
91 label.size = rect.size
92 label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
93 label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
94 label.clip_contents = true
95 label.add_theme_font_size_override("font_size", font_size)
96 label.add_theme_color_override("font_color", color)
97 label.mouse_filter = Control.MOUSE_FILTER_IGNORE
98 return label