Show.gd 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. class_name Show
  2. extends Node2D
  3. # Manage slide cycling
  4. signal new_focus(pos: Vector2, scale: float, trans: Focus.Transit)
  5. const VOID_SLIDE_ID := -1
  6. @export var start_slide := 0
  7. @export var hide_slides := false
  8. var current_id := VOID_SLIDE_ID
  9. var current_slide: Slide
  10. @onready var slides: Array = get_children()
  11. func _ready() -> void:
  12. _enforce()
  13. if hide_slides:
  14. hide_every_slide()
  15. go_slowly_to_next_slide()
  16. func go_slowly_to_next_slide() -> void:
  17. next_slide(Focus.Transit.SMOOTH)
  18. func go_fast_to_next_slide() -> void:
  19. next_slide(Focus.Transit.BEAM)
  20. func next_slide(trans: Focus.Transit) -> void:
  21. next_slide_at(_next_id(), trans)
  22. func next_slide_at(next_id: int, trans: Focus.Transit) -> void:
  23. _disconnect_current_slide()
  24. current_id = next_id
  25. current_slide = focus_slide(current_id, trans)
  26. _connect_current_slide()
  27. func focus_slide(id: int, trans: Focus.Transit) -> Slide:
  28. var slide: Slide = slides[id]
  29. slide.focus()
  30. new_focus.emit(slide.get_center(), slide.get_scale().x, trans)
  31. return slide
  32. func hide_every_slide() -> void:
  33. for slide in slides:
  34. slide.gently_hide()
  35. func _disconnect_current_slide() -> void:
  36. if current_slide != null:
  37. current_slide.remove_focus()
  38. current_slide.finished.disconnect(_on_current_slide_finished)
  39. func _connect_current_slide() -> void:
  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) -> void:
  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 _unhandled_input(event: InputEvent) -> void:
  73. if event is InputEventMouseButton and event.is_pressed():
  74. var gpos: Vector2 = get_global_mouse_position()
  75. var nearest_slide = current_slide
  76. var shortest_distance = gpos.distance_squared_to(nearest_slide.get_center())
  77. for current_slide in slides:
  78. var current_distance = gpos.distance_squared_to(current_slide.get_center())
  79. if current_distance < shortest_distance:
  80. nearest_slide = current_slide
  81. shortest_distance = current_distance
  82. var next_id = slides.find(nearest_slide)
  83. next_slide_at(next_id, Focus.Transit.SMOOTH)
  84. func _on_current_slide_finished() -> void:
  85. go_slowly_to_next_slide()