Show.gd 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. class_name Show
  2. extends Node2D
  3. # Manage slide cycling
  4. signal new_focus(pos: Vector2, scale: float)
  5. var current_id := -1
  6. var current_slide: Slide
  7. @onready var slides: Array = get_children()
  8. func _ready():
  9. _enforce_children()
  10. next_slide()
  11. func next_slide():
  12. current_id = _next_id()
  13. _disconnect_current_slide()
  14. current_slide = focus_slide(current_id)
  15. _connect_current_slide()
  16. func focus_slide(id: int) -> Slide:
  17. var slide: Slide = slides[id]
  18. new_focus.emit(slide.get_position(), slide.get_scale().x)
  19. return slide
  20. func _disconnect_current_slide():
  21. if current_slide != null:
  22. current_slide.finished.disconnect(_on_current_slide_finished)
  23. func _connect_current_slide():
  24. current_slide.finished.connect(_on_current_slide_finished)
  25. func _enforce_children() -> void:
  26. assert(!slides.is_empty(), "A slideshow must contain slides")
  27. for slide in slides:
  28. assert(slide is Slide, "A slidsehow can only contain slides")
  29. func _next_id() -> int:
  30. var next_id: int = current_id + 1
  31. if next_id >= slides.size():
  32. return 0
  33. return next_id
  34. func _on_current_slide_finished():
  35. next_slide()