The camera now zooms at the mouse's position.

This commit is contained in:
OverloadedOrama 2020-02-09 02:16:14 +02:00
parent f28a3a4405
commit 3fbd1d35eb
2 changed files with 13 additions and 7 deletions

View file

@ -4,6 +4,7 @@ var tween : Tween
var zoom_min := Vector2(0.005, 0.005)
var zoom_max := Vector2.ONE
var viewport_container : ViewportContainer
var mouse_pos := Vector2.ZERO
var drag := false
func _ready() -> void:
@ -12,7 +13,7 @@ func _ready() -> void:
add_child(tween)
func _input(event : InputEvent) -> void:
var mouse_pos := viewport_container.get_local_mouse_position()
mouse_pos = viewport_container.get_local_mouse_position()
var viewport_size := viewport_container.rect_size
if event.is_action_pressed("middle_mouse") || event.is_action_pressed("space"):
drag = true
@ -29,22 +30,26 @@ func _input(event : InputEvent) -> void:
# Zoom Camera
func zoom_camera(dir : int) -> void:
var viewport_size := viewport_container.rect_size
if Global.smooth_zoom:
var zoom_margin = zoom * dir / 5
if zoom + zoom_margin > zoom_min:
tween.interpolate_property(self, "zoom", zoom, zoom + zoom_margin, 0.05, Tween.TRANS_LINEAR, Tween.EASE_IN)
var new_zoom = zoom + zoom_margin
if new_zoom > zoom_min && new_zoom < zoom_max:
var new_offset = offset + (-0.5 * viewport_size + mouse_pos) * (zoom - new_zoom)
tween.interpolate_property(self, "zoom", zoom, new_zoom, 0.05, Tween.TRANS_LINEAR, Tween.EASE_IN)
tween.interpolate_property(self, "offset", offset, new_offset, 0.05, Tween.TRANS_LINEAR, Tween.EASE_IN)
tween.start()
if zoom > zoom_max:
tween.stop_all()
zoom = zoom_max
else:
var prev_zoom := zoom
var zoom_margin = zoom * dir / 10
if zoom + zoom_margin > zoom_min:
zoom += zoom_margin
if zoom > zoom_max:
zoom = zoom_max
offset = offset + (-0.5 * viewport_size + mouse_pos) * (prev_zoom - zoom)
if name == "Camera2D":
Global.zoom_level_label.text = str(round(100 / Global.camera.zoom.x)) + " %"