Focus.gd 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. func focus_on(pos: Vector2, scalar_scale: float) -> void:
  19. if tween:
  20. tween.kill()
  21. var target_scope = Vector2.ONE * scalar_scale
  22. var is_wider = target_scope.x > get_scope().x
  23. var scope_ease = scope_wider_ease if is_wider else scope_closer_ease
  24. tween = create_tween().set_parallel(tween_is_parallel)
  25. tween.tween_property(self, "position", pos, move_duration).set_trans(move_trans).set_ease(move_ease)
  26. tween.tween_property(self, "scope", target_scope, scope_duration).set_trans(scope_trans).set_ease(scope_ease)
  27. tween.tween_callback(end)
  28. func end():
  29. ended.emit()
  30. func get_scope() -> Vector2:
  31. return _invert_vector(get_zoom())
  32. func set_scope(new_scope: Vector2) -> void:
  33. assert(new_scope.x > 0)
  34. assert(new_scope.y > 0)
  35. set_zoom(_invert_vector(new_scope))
  36. func _on_show_new_focus(pos: Vector2, scalar_scale: float) -> void:
  37. focus_on(pos, scalar_scale)
  38. func _invert_vector(vec: Vector2) -> Vector2:
  39. return Vector2(1.0 / vec.x, 1.0 / vec.y)