11var parent:Control = null
15var total_acquired:int = 0
16var total_created:int = 0
17var total_reused:int = 0
18var pass_acquired:int = 0
19var pass_created:int = 0
20var pass_reused:int = 0
21var last_hidden:int = 0
24func begin(next_parent:Control) -> void:
35 for key
in pools.keys():
36 var bucket = pools[key]
37 if not bucket
is Array:
39 var used =
int(used_counts.get(key, 0))
40 for index
in range(used, bucket.size()):
41 var node = bucket[index]
42 if node
is CanvasItem:
47func rect(rect:Rect2, color:Color, name_prefix:String =
"Rect") -> ColorRect:
48 var node = _acquire(
"rect:%s" % name_prefix, name_prefix, func():
49 var created = ColorRect.new()
50 created.mouse_filter = Control.MOUSE_FILTER_IGNORE
54 _apply_control_rect(node, rect)
58func sprite(sprite_manager, sprite_id:String, rect:Rect2, name_prefix:String =
"Sprite", options:Dictionary = {}) -> TextureRect:
59 if sprite_id ==
"" or sprite_manager == null:
61 var key =
"sprite:%s:%s" % [name_prefix, sprite_id]
62 var node = _acquire(key, name_prefix, func():
63 var created = sprite_manager.create_texture_node(sprite_id, options)
66 created.mouse_filter = Control.MOUSE_FILTER_IGNORE
71 node.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
72 node.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
73 _apply_control_rect(node, rect)
77func label(rect:Rect2, text:String, name_prefix:String =
"Label", options:Dictionary = {}) -> Label:
78 var node = _acquire(
"label:%s" % name_prefix, name_prefix, func():
79 var created = Label.new()
80 created.mouse_filter = Control.MOUSE_FILTER_IGNORE
84 node.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
if bool(options.get(
"wrap", true))
else TextServer.AUTOWRAP_OFF
86 node.max_lines_visible =
int(options.get(
"max_lines", 4))
87 node.horizontal_alignment =
int(options.get(
"horizontal_alignment", HORIZONTAL_ALIGNMENT_LEFT))
88 node.vertical_alignment =
int(options.get(
"vertical_alignment", VERTICAL_ALIGNMENT_TOP))
89 node.add_theme_font_size_override(
"font_size",
int(options.get(
"font_size", 12)))
90 node.add_theme_color_override(
"font_color", Color(str(options.get(
"font_color",
"#eef3f8"))))
91 if options.has(
"outline_color"):
92 node.add_theme_color_override(
"font_outline_color", Color(str(options.get(
"outline_color",
"#000000"))))
93 node.add_theme_constant_override(
"outline_size",
int(options.get(
"outline_size", 1)))
94 _apply_control_rect(node, rect)
99 for bucket
in pools.values():
100 if not bucket
is Array:
103 if is_instance_valid(node):
118func get_stats() -> Dictionary:
121 for bucket
in pools.values():
122 if not bucket
is Array:
124 total += bucket.size()
126 if node
is CanvasItem
and node.visible:
129 "pools": pools.size(),
132 "hidden_last_pass": last_hidden,
133 "pass_acquired": pass_acquired,
134 "pass_created": pass_created,
135 "pass_reused": pass_reused,
136 "total_acquired": total_acquired,
137 "total_created": total_created,
138 "total_reused": total_reused
142func _acquire(key:String, name_prefix:String, factory:Callable) -> Control:
145 var bucket = pools.get(key, [])
146 var index =
int(used_counts.get(key, 0))
148 if index < bucket.size():
153 node = factory.call()
157 parent.add_child(node)
161 used_counts[key] = index + 1
164 node.name = name_prefix
if index == 0
else "%s%d" % [name_prefix, index]
166 node.mouse_filter = Control.MOUSE_FILTER_IGNORE
167 if node.get_parent() != parent:
168 if node.get_parent() != null:
169 node.get_parent().remove_child(node)
170 parent.add_child(node)
171 parent.move_child(node, min(render_index, parent.get_child_count() - 1))
176func _apply_control_rect(node:Control, rect:Rect2) -> void:
177 node.position = rect.position
178 node.size = rect.size