1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- extends Camera2D
- # Handle zoom and move for keeping focus points in view
- export var zoom_border = 100
- var zoom_rate:float
- var dist_max_x:float
- var dist_max_y:float
- var view:Viewport
- func _ready():
- view = get_tree().root
- view.connect("size_changed", self, "_on_size_changed")
- update_zoom_reference()
- func _process(_d):
- move()
- func move():
- if !get_tree().has_group("liners"):
- return
- var running
- if get_tree().has_group("running"):
- running = get_tree().get_nodes_in_group("running")
- if !running or running.size() == 0:
- # Show every liners
- var liners = get_tree().get_nodes_in_group("liners")
- center_on(liners)
- else:
- # Show remaining liners only
- center_on(running)
- func center_on(focus_points: Array) -> void:
- # Focus middle point
- var last_point: Node2D = focus_points.pop_back()
- var position_min: Vector2
- if last_point:
- position_min = last_point.position
- else:
- position_min = Vector2(0.0, 0.0)
- var position_max := position_min
- for p in focus_points:
- position_min.x = min(p.position.x, position_min.x)
- position_min.y = min(p.position.y, position_min.y)
- position_max.x = max(p.position.x, position_max.x)
- position_max.y = max(p.position.y, position_max.y)
- position = (position_min + position_max) / 2
- # Extend camera zoom if focus points are far from each other
- var dist_x: float = position_max.x - position_min.x
- var dist_y: float = position_max.y - position_min.y
- var new_zoom: float = 1.0
- if dist_x > dist_max_x:
- new_zoom += (dist_x - dist_max_x) * zoom_rate
- if dist_y > dist_max_y:
- new_zoom = max(new_zoom, 1.0 + (dist_y - dist_max_y) * zoom_rate)
- zoom = Vector2(new_zoom, new_zoom)
- func update_zoom_reference():
- dist_max_x = view.size.x - 2 * zoom_border
- dist_max_y = view.size.y - 2 * zoom_border
- zoom_rate = 1.0 / min(dist_max_x, dist_max_y)
- func _on_size_changed():
- update_zoom_reference()
|