From 3fbd1d35eb8807e73b28ce843edeaa37f6da1080 Mon Sep 17 00:00:00 2001 From: OverloadedOrama <35376950+OverloadedOrama@users.noreply.github.com> Date: Sun, 9 Feb 2020 02:16:14 +0200 Subject: [PATCH] The camera now zooms at the mouse's position. --- Changelog.md | 1 + Scripts/CameraMovement.gd | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Changelog.md b/Changelog.md index 759b3f628..3b7346240 100644 --- a/Changelog.md +++ b/Changelog.md @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Major changes to alpha blending behavior. The alpha values now get added/blended together instead of just replacing the pixel with the new value. - Replaced some OS alerts with a custom made error dialog. - Made the zooming smoother, is toggleable in Preferences whether to keep the new zooming or the old one. +- The camera now zooms at the mouse's position. - Made the "X" button on the custom brushes a little smaller. - The color picker will now have a small white triangle on the top left of the color preview if at least one of its RGB values are above 1 in Raw mode. (Added automatically because of the Godot 3.2 update) - You can now toggle the visibility of hidden items on and off in the file dialogs. (Added automatically because of the Godot 3.2 update) diff --git a/Scripts/CameraMovement.gd b/Scripts/CameraMovement.gd index 0567f1e89..f2c1b5945 100644 --- a/Scripts/CameraMovement.gd +++ b/Scripts/CameraMovement.gd @@ -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)) + " %"