Show.gd 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. class_name Show
  2. extends Node2D
  3. # Manage slide cycling
  4. signal new_focus(pos: Vector2, scale: float)
  5. signal slow_move
  6. signal fast_move
  7. const VOID_SLIDE_ID := -1
  8. @export var start_slide := 0
  9. var current_id := VOID_SLIDE_ID
  10. var current_slide: Slide
  11. @onready var slides: Array = get_children()
  12. func _ready():
  13. _enforce()
  14. hide_every_slide()
  15. go_slowly_to_next_slide()
  16. func go_slowly_to_next_slide():
  17. _next_slide()
  18. slow_move.emit()
  19. func go_fast_to_next_slide():
  20. _next_slide()
  21. fast_move.emit()
  22. func _next_slide():
  23. _disconnect_current_slide()
  24. current_id = _next_id()
  25. current_slide = focus_slide(current_id)
  26. _connect_current_slide()
  27. func focus_slide(id: int) -> Slide:
  28. var slide: Slide = slides[id]
  29. slide.focus()
  30. new_focus.emit(slide.get_center(), slide.get_scale().x)
  31. return slide
  32. func hide_every_slide() -> void:
  33. for slide in slides:
  34. slide.gently_hide()
  35. func _disconnect_current_slide():
  36. if current_slide != null:
  37. current_slide.remove_focus()
  38. current_slide.finished.disconnect(_on_current_slide_finished)
  39. func _connect_current_slide():
  40. current_slide.finished.connect(_on_current_slide_finished)
  41. func _enforce() -> void:
  42. _enforce_parameters()
  43. _enforce_children()
  44. func _enforce_parameters() -> void:
  45. assert(start_slide >= 0,
  46. "Negative index is not supported for start_slide")
  47. assert(start_slide < slides.size(),
  48. "start_slide index is out of bound (max is %s)" % (slides.size() - 1))
  49. func _enforce_children() -> void:
  50. assert(!slides.is_empty(), "A slideshow must contain slides")
  51. for slide in slides:
  52. assert(slide is Slide, "A slideshow can only contain slides")
  53. func _next_id() -> int:
  54. if current_id == VOID_SLIDE_ID:
  55. return start_slide
  56. var next_id: int = current_id + 1
  57. if next_id >= slides.size():
  58. return 0
  59. return next_id
  60. func _reset_id() -> void:
  61. current_id = VOID_SLIDE_ID
  62. func _unhandled_key_input(event: InputEvent):
  63. if !event.is_pressed():
  64. return
  65. if event.is_action("ui_right"):
  66. get_viewport().set_input_as_handled()
  67. go_fast_to_next_slide()
  68. elif event.is_action("ui_cancel"):
  69. get_viewport().set_input_as_handled()
  70. _reset_id()
  71. go_slowly_to_next_slide()
  72. func _on_current_slide_finished():
  73. go_slowly_to_next_slide()