Transit.gd 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. extends Control
  2. signal scene_changed
  3. const DEFAULT_DURATION := 0.2
  4. const DEFAULT_DELAY := 0.0
  5. onready var _animator := $AnimationPlayer
  6. onready var _curtain := $CanvasLayer/ColorRect
  7. func set_color(color: Color):
  8. color.a = _curtain.color.a
  9. _curtain.color = color
  10. func change_scene(path: String, duration: float = DEFAULT_DURATION, delay: float = DEFAULT_DELAY):
  11. if duration <= 0.0:
  12. push_error("TRANSIT ERROR: change_scene duration must be > 0. Defaulting to %s" % DEFAULT_DURATION)
  13. duration = DEFAULT_DURATION
  14. if delay < 0.0:
  15. push_error("TRANSIT ERROR: change_scene delay must be >= 0. Defaulting to %s" % DEFAULT_DELAY)
  16. delay = DEFAULT_DELAY
  17. # disable mouse interaction while fading out
  18. _curtain.mouse_filter = MOUSE_FILTER_STOP
  19. if delay > 0:
  20. yield(get_tree().create_timer(delay), "timeout")
  21. _animator.playback_speed = 1.0 / duration
  22. _animator.play("fade")
  23. yield(_animator, "animation_finished")
  24. var err := get_tree().change_scene(path)
  25. if err:
  26. push_error("TRANSIT ERROR: Failed to change scene to %s: %s" % [path, err])
  27. # re-enable mouse interaction before fading back in
  28. _curtain.mouse_filter = MOUSE_FILTER_IGNORE
  29. _animator.play_backwards("fade")
  30. yield(_animator, "animation_finished")
  31. emit_signal("scene_changed")