Show.gd 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. class_name Show
  2. extends Node2D
  3. # Manage slide cycling
  4. signal new_focus(pos: Vector2, scale: float)
  5. const VOID_SLIDE_ID := -1
  6. @export var start_slide := 0
  7. var current_id := VOID_SLIDE_ID
  8. var current_slide: Slide
  9. @onready var slides: Array = get_children()
  10. func _ready():
  11. _enforce()
  12. next_slide()
  13. func next_slide():
  14. _disconnect_current_slide()
  15. current_id = _next_id()
  16. current_slide = focus_slide(current_id)
  17. _connect_current_slide()
  18. func focus_slide(id: int) -> Slide:
  19. var slide: Slide = slides[id]
  20. new_focus.emit(slide.get_center(), slide.get_scale().x)
  21. return slide
  22. func _disconnect_current_slide():
  23. if current_slide != null:
  24. current_slide.finished.disconnect(_on_current_slide_finished)
  25. func _connect_current_slide():
  26. current_slide.finished.connect(_on_current_slide_finished)
  27. func _enforce() -> void:
  28. _enforce_parameters()
  29. _enforce_children()
  30. func _enforce_parameters() -> void:
  31. assert(start_slide >= 0,
  32. "Negative index is not supported for start_slide")
  33. assert(start_slide < slides.size(),
  34. "start_slide index is out of bound (max is %s)" % (slides.size() - 1))
  35. func _enforce_children() -> void:
  36. assert(!slides.is_empty(), "A slideshow must contain slides")
  37. for slide in slides:
  38. assert(slide is Slide, "A slideshow can only contain slides")
  39. func _next_id() -> int:
  40. if current_id == VOID_SLIDE_ID:
  41. return start_slide
  42. var next_id: int = current_id + 1
  43. if next_id >= slides.size():
  44. return 0
  45. return next_id
  46. func _on_current_slide_finished():
  47. next_slide()