Focus.gd 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. class_name Focus
  2. extends Camera2D
  3. # Transition to next slide and focus current slide
  4. signal ended
  5. @export_group("Tween caracteristics", "tween_")
  6. @export var tween_is_parallel: bool = false
  7. @export_group("Move", "move_")
  8. @export var move_duration: float = 1.0
  9. @export var move_trans: Tween.TransitionType = Tween.TRANS_SINE
  10. @export var move_ease: Tween.EaseType = Tween.EASE_IN_OUT
  11. @export_group("Scope", "scope_")
  12. @export var scope_duration: float = 1.0
  13. @export var scope_trans: Tween.TransitionType = Tween.TRANS_SINE
  14. @export var scope_closer_ease: Tween.EaseType = Tween.EASE_IN
  15. @export var scope_wider_ease: Tween.EaseType = Tween.EASE_OUT
  16. var tween: Tween
  17. var scope: Vector2 : get = get_scope, set = set_scope
  18. var target_pos: Vector2 = Vector2.ZERO
  19. var target_scope: Vector2 = Vector2.ONE
  20. func focus_on(pos: Vector2, scalar_scale: float) -> void:
  21. target_pos = pos
  22. target_scope = Vector2.ONE * scalar_scale
  23. func beam_to_focus() -> void:
  24. _abort_ongoing_transit()
  25. set_position(target_pos)
  26. set_scope(target_scope)
  27. func transit_to_focus() -> void:
  28. _abort_ongoing_transit()
  29. var is_wider = target_scope.x > get_scope().x
  30. var scope_ease = scope_wider_ease if is_wider else scope_closer_ease
  31. tween = create_tween().set_parallel(tween_is_parallel)
  32. tween.tween_property(self, "position", target_pos, move_duration).set_trans(move_trans).set_ease(move_ease)
  33. tween.tween_property(self, "scope", target_scope, scope_duration).set_trans(scope_trans).set_ease(scope_ease)
  34. tween.tween_callback(end)
  35. func end() -> void:
  36. ended.emit()
  37. func get_scope() -> Vector2:
  38. return _invert_vector(get_zoom())
  39. func set_scope(new_scope: Vector2) -> void:
  40. assert(new_scope.x > 0)
  41. assert(new_scope.y > 0)
  42. set_zoom(_invert_vector(new_scope))
  43. func _abort_ongoing_transit() -> void:
  44. if tween:
  45. tween.kill()
  46. func _invert_vector(vec: Vector2) -> Vector2:
  47. return Vector2(1.0 / vec.x, 1.0 / vec.y)
  48. func _on_show_new_focus(pos: Vector2, scalar_scale: float) -> void:
  49. focus_on(pos, scalar_scale)
  50. func _on_show_fast_move() -> void:
  51. beam_to_focus()
  52. func _on_show_slow_move() -> void:
  53. transit_to_focus()