stackable_control.gd 678 B

123456789101112131415161718192021222324252627282930
  1. class_name StackableControl extends Control
  2. @export
  3. var _initial_scene: PackedScene
  4. var _current_ref: Node
  5. var _stacked_ref: Array[Node] = []
  6. func _ready() -> void:
  7. to_prefab(_initial_scene)
  8. func to(node: Node, push_prev: bool = false) -> void:
  9. if push_prev:
  10. _stacked_ref.push_back(_current_ref)
  11. if _current_ref != null:
  12. remove_child(_current_ref)
  13. _current_ref = null
  14. if node != null:
  15. add_child(node)
  16. _current_ref = node
  17. func to_prefab(prefab: PackedScene, push_prev: bool = false) -> void:
  18. if prefab != null:
  19. to(prefab.instantiate(), push_prev)
  20. func pop() -> void:
  21. to(_stacked_ref.pop_back(), false)
  22. func clear_stacked() -> void:
  23. _stacked_ref.clear()