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. enum Transit {
  6. BEAM,
  7. SMOOTH,
  8. }
  9. @export_group("Tween caracteristics", "tween_")
  10. @export var tween_is_parallel: bool = false
  11. @export_group("Move", "move_")
  12. @export var move_duration: float = 1.0
  13. @export var move_trans: Tween.TransitionType = Tween.TRANS_SINE
  14. @export var move_ease: Tween.EaseType = Tween.EASE_IN_OUT
  15. @export_group("Scope", "scope_")
  16. @export var scope_duration: float = 1.0
  17. @export var scope_trans: Tween.TransitionType = Tween.TRANS_SINE
  18. @export var scope_closer_ease: Tween.EaseType = Tween.EASE_IN
  19. @export var scope_wider_ease: Tween.EaseType = Tween.EASE_OUT
  20. var tween: Tween
  21. var scope: Vector2 : get = get_scope, set = set_scope
  22. var target_pos: Vector2 = Vector2.ZERO
  23. var target_scope: Vector2 = Vector2.ONE
  24. func focus_on(pos: Vector2, scalar_scale: float, trans: Transit) -> void:
  25. target_pos = pos
  26. target_scope = Vector2.ONE * scalar_scale
  27. if Transit.BEAM == trans:
  28. _beam_to_focus()
  29. elif Transit.SMOOTH == trans:
  30. _smooth_transit_to_focus()
  31. else:
  32. assert(false, "Trans %s is not implemented." % trans)
  33. func end() -> void:
  34. ended.emit()
  35. func get_scope() -> Vector2:
  36. return _invert_vector(get_zoom())
  37. func set_scope(new_scope: Vector2) -> void:
  38. set_zoom(_invert_vector(new_scope))
  39. func _beam_to_focus() -> void:
  40. _abort_ongoing_transit()
  41. set_position(target_pos)
  42. set_scope(target_scope)
  43. func _smooth_transit_to_focus() -> void:
  44. _abort_ongoing_transit()
  45. var is_wider: bool = target_scope.x > get_scope().x
  46. var scope_ease: Tween.EaseType = scope_wider_ease if is_wider else scope_closer_ease
  47. tween = create_tween().set_parallel(tween_is_parallel)
  48. tween.tween_property(self, "position", target_pos, move_duration).set_trans(move_trans).set_ease(move_ease)
  49. tween.tween_property(self, "scope", target_scope, scope_duration).set_trans(scope_trans).set_ease(scope_ease)
  50. tween.tween_callback(end)
  51. func _abort_ongoing_transit() -> void:
  52. if tween:
  53. tween.kill()
  54. func _invert_vector(vec: Vector2) -> Vector2:
  55. assert(vec.x > 0)
  56. assert(vec.y > 0)
  57. return Vector2(1.0 / vec.x, 1.0 / vec.y)