Browse Source

feat: stackable_control.gd

LanzaSchneider 1 year ago
parent
commit
5a1dceba40
1 changed files with 30 additions and 0 deletions
  1. 30 0
      components/ui/stackable_control.gd

+ 30 - 0
components/ui/stackable_control.gd

@@ -0,0 +1,30 @@
+class_name StackableControl extends Control
+
+@export
+var _initial_scene: PackedScene
+
+var _current_ref: Node
+var _stacked_ref: Array[Node] = []
+
+func _ready() -> void:
+	to_prefab(_initial_scene)
+
+func to(node: Node, push_prev: bool = false) -> void:
+	if push_prev:
+		_stacked_ref.push_back(_current_ref)
+	if _current_ref != null:
+		remove_child(_current_ref)
+		_current_ref = null
+	if node != null:
+		add_child(node)
+		_current_ref = node
+
+func to_prefab(prefab: PackedScene, push_prev: bool = false) -> void:
+	if prefab != null:
+		to(prefab.instantiate(), push_prev)
+
+func pop() -> void:
+	to(_stacked_ref.pop_back(), false)
+
+func clear_stacked() -> void:
+	_stacked_ref.clear()