prompt_container.gd 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. class_name PromptContainer
  2. extends Control
  3. ## Free itself when every prompt has been used
  4. ## If true, automatically frees itself when there are no more input prompts among children
  5. @export var remove_when_prompts_depleted := true
  6. var _nb_prompts: int
  7. func _ready() -> void:
  8. _nb_prompts = 0
  9. for child in get_children():
  10. if child is InputPrompt:
  11. child.pressed.connect(child.queue_free)
  12. if child is InputPrompt or child is PromptContainer:
  13. child.tree_exited.connect(_on_child_exited_tree)
  14. _nb_prompts += 1
  15. func switch(enable: bool) -> void:
  16. if enable:
  17. enable()
  18. else:
  19. disable()
  20. # Show and process
  21. func enable() -> void:
  22. set_visible(true)
  23. set_process_mode(Node.PROCESS_MODE_PAUSABLE)
  24. # Hide and stop processing
  25. func disable() -> void:
  26. set_visible(false)
  27. set_process_mode(Node.PROCESS_MODE_DISABLED)
  28. func remove_if_no_more_prompt() -> void:
  29. _nb_prompts -= 1
  30. print(_nb_prompts, " remaining prompts")
  31. if _nb_prompts <= 0 and remove_when_prompts_depleted:
  32. # Only label remaining
  33. print("Destruction !")
  34. queue_free()
  35. func _on_child_exited_tree() -> void:
  36. remove_if_no_more_prompt()