123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- class_name Show
- extends Node2D
- # Manage slide cycling
- signal new_focus(pos: Vector2, scale: float)
- signal slow_move
- signal fast_move
- const VOID_SLIDE_ID := -1
- @export var start_slide := 0
- var current_id := VOID_SLIDE_ID
- var current_slide: Slide
- @onready var slides: Array = get_children()
- func _ready():
- _enforce()
- hide_every_slide()
- go_slowly_to_next_slide()
- func go_slowly_to_next_slide():
- _next_slide()
- slow_move.emit()
- func go_fast_to_next_slide():
- _next_slide()
- fast_move.emit()
- func _next_slide():
- _disconnect_current_slide()
- current_id = _next_id()
- current_slide = focus_slide(current_id)
- _connect_current_slide()
- func focus_slide(id: int) -> Slide:
- var slide: Slide = slides[id]
- slide.focus()
- new_focus.emit(slide.get_center(), slide.get_scale().x)
- return slide
- func hide_every_slide() -> void:
- for slide in slides:
- slide.gently_hide()
- func _disconnect_current_slide():
- if current_slide != null:
- current_slide.remove_focus()
- current_slide.finished.disconnect(_on_current_slide_finished)
- func _connect_current_slide():
- current_slide.finished.connect(_on_current_slide_finished)
- func _enforce() -> void:
- _enforce_parameters()
- _enforce_children()
- func _enforce_parameters() -> void:
- assert(start_slide >= 0,
- "Negative index is not supported for start_slide")
- assert(start_slide < slides.size(),
- "start_slide index is out of bound (max is %s)" % (slides.size() - 1))
- func _enforce_children() -> void:
- assert(!slides.is_empty(), "A slideshow must contain slides")
- for slide in slides:
- assert(slide is Slide, "A slideshow can only contain slides")
- func _next_id() -> int:
- if current_id == VOID_SLIDE_ID:
- return start_slide
- var next_id: int = current_id + 1
- if next_id >= slides.size():
- return 0
- return next_id
- func _reset_id() -> void:
- current_id = VOID_SLIDE_ID
- func _unhandled_key_input(event: InputEvent):
- if !event.is_pressed():
- return
- if event.is_action("ui_right"):
- get_viewport().set_input_as_handled()
- go_fast_to_next_slide()
- elif event.is_action("ui_cancel"):
- get_viewport().set_input_as_handled()
- _reset_id()
- go_slowly_to_next_slide()
- func _on_current_slide_finished():
- go_slowly_to_next_slide()
|