Show.gd 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. class_name Show
  2. extends Node2D
  3. # Manage slide cycling
  4. signal end_reached
  5. const VOID_SLIDE_ID := -1
  6. @export_group("Slide management")
  7. @export var start_slide := 0
  8. @export var hide_slides := false
  9. @export_group("Input setup", "action_")
  10. @export var action_finish_slide := "ui_accept"
  11. @export var action_skip_slide := "ui_right"
  12. @export var action_previous_slide := "ui_left"
  13. @export var action_home_slide := "ui_cancel"
  14. @export_group("Export")
  15. @export var one_slide_per_frame := true
  16. @export var quit_after_last_slide := true
  17. var current_id := VOID_SLIDE_ID
  18. var current_slide: Slide
  19. @onready var focus: Focus = $"../Focus"
  20. @onready var slides: Array = get_children()
  21. @onready var movie_enabled: bool = OS.has_feature("movie")
  22. @onready var auto_next_slide: bool = movie_enabled and one_slide_per_frame
  23. func _ready() -> void:
  24. _enforce()
  25. print("The show contains %s slides" % slides.size())
  26. if hide_slides:
  27. _hide_every_slide()
  28. _global_slide_setup()
  29. if auto_next_slide:
  30. end_reached.connect(get_tree().quit)
  31. return
  32. go_slowly_to_next_slide()
  33. func _process(_delta: float) -> void:
  34. if auto_next_slide:
  35. go_fast_to_next_slide()
  36. func go_slowly_to_next_slide() -> void:
  37. next_slide(Focus.Transit.SMOOTH)
  38. func go_fast_to_next_slide() -> void:
  39. next_slide(Focus.Transit.BEAM)
  40. func go_fast_to_previous_slide() -> void:
  41. next_slide_at(_previous_id(), Focus.Transit.BEAM)
  42. func next_slide(trans: Focus.Transit) -> void:
  43. next_slide_at(_next_id(), trans)
  44. func next_slide_at(next_id: int, trans: Focus.Transit) -> void:
  45. _disable_current_slide()
  46. current_id = next_id
  47. current_slide = _focus_slide(current_id, trans)
  48. if is_last_slide():
  49. end_reached.emit()
  50. _enable_current_slide()
  51. func is_last_slide() -> bool:
  52. return current_id == slides.size() - 1
  53. func _focus_slide(id: int, trans: Focus.Transit) -> Slide:
  54. var slide: Slide = slides[id]
  55. focus.focus_on(slide.get_center(), slide.get_scale().x, trans)
  56. return slide
  57. func _hide_every_slide() -> void:
  58. for slide in slides:
  59. slide.gently_hide()
  60. func _global_slide_setup() -> void:
  61. for slide in slides:
  62. slide.set_action_finish(action_finish_slide)
  63. func _disable_current_slide() -> void:
  64. if current_slide != null:
  65. current_slide.disable()
  66. current_slide.finished.disconnect(_on_current_slide_finished)
  67. func _enable_current_slide() -> void:
  68. current_slide.enable()
  69. current_slide.finished.connect(_on_current_slide_finished)
  70. func _enforce() -> void:
  71. _enforce_parameters()
  72. _enforce_children()
  73. func _enforce_parameters() -> void:
  74. assert(focus, "A Focus node must be next to the Show node")
  75. assert(start_slide >= 0,
  76. "Negative index is not supported for start_slide")
  77. assert(start_slide < slides.size(),
  78. "start_slide index is out of bound (max is %s)" % (slides.size() - 1))
  79. func _enforce_children() -> void:
  80. assert(!slides.is_empty(), "A slideshow must contain slides")
  81. for object in slides:
  82. assert(object is Slide, "A slideshow can only contain slides but encontered %s" % object)
  83. func _next_id() -> int:
  84. if current_id == VOID_SLIDE_ID:
  85. return start_slide
  86. elif is_last_slide():
  87. return 0
  88. return current_id + 1
  89. func _previous_id() -> int:
  90. var next_id: int = current_id - 1
  91. if next_id < 0:
  92. return slides.size() - 1
  93. return next_id
  94. func _reset_id() -> void:
  95. current_id = VOID_SLIDE_ID
  96. func _unhandled_key_input(event: InputEvent) -> void:
  97. if !event.is_pressed():
  98. return
  99. if event.is_action(action_skip_slide):
  100. get_viewport().set_input_as_handled()
  101. go_fast_to_next_slide()
  102. elif event.is_action(action_previous_slide):
  103. get_viewport().set_input_as_handled()
  104. go_fast_to_previous_slide()
  105. elif event.is_action(action_home_slide):
  106. get_viewport().set_input_as_handled()
  107. _reset_id()
  108. go_slowly_to_next_slide()
  109. func _unhandled_input(event: InputEvent) -> void:
  110. if event is InputEventMouseButton and event.is_pressed():
  111. _focus_nearest_slide_from(get_global_mouse_position())
  112. func _focus_nearest_slide_from(target_pos: Vector2) -> void:
  113. var next_slide = _search_nearest_slide_from(target_pos)
  114. var next_id = slides.find(next_slide)
  115. next_slide_at(next_id, Focus.Transit.SMOOTH)
  116. func _search_nearest_slide_from(target_pos: Vector2) -> Slide:
  117. var nearest_slide = current_slide
  118. var shortest_distance = target_pos.distance_squared_to(nearest_slide.get_center())
  119. for current_slide in slides:
  120. var current_distance = target_pos.distance_squared_to(current_slide.get_center())
  121. if current_distance < shortest_distance:
  122. nearest_slide = current_slide
  123. shortest_distance = current_distance
  124. return nearest_slide
  125. func _on_current_slide_finished() -> void:
  126. go_slowly_to_next_slide()