From b4ae097d14589932b1e3e36f884e6559dd185f4b Mon Sep 17 00:00:00 2001 From: OverloadedOrama <35376950+OverloadedOrama@users.noreply.github.com> Date: Sun, 10 May 2020 15:46:21 +0300 Subject: [PATCH] Changed zoom fit to frame algorithm, again Put the new code in a new fit_to_frame() method inside CameraMovement.gd. The old code did not work properly for images with width larger than their height. --- src/CameraMovement.gd | 26 ++++++++++++++++++++++++++ src/Canvas.gd | 16 +++------------- src/Main.gd | 8 +------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/CameraMovement.gd b/src/CameraMovement.gd index 9cb8d9717..5cbc0742c 100644 --- a/src/CameraMovement.gd +++ b/src/CameraMovement.gd @@ -159,3 +159,29 @@ func zoom_camera(dir : int) -> void: func _on_tween_step(_object: Object, _key: NodePath, _elapsed: float, _value: Object) -> void: Global.horizontal_ruler.update() Global.vertical_ruler.update() + + +func fit_to_frame() -> void: + viewport_container = get_parent().get_parent() + var h_ratio := viewport_container.rect_size.x / Global.canvas.size.x + var v_ratio := viewport_container.rect_size.y / Global.canvas.size.y + var ratio := min(h_ratio, v_ratio) + if ratio == 0: + ratio = 0.1 # Set it to a non-zero value just in case + # If the ratio is 0, it means that the viewport container is hidden + # in that case, use the other viewport to get the ratio + if name == "Camera2D": + h_ratio = Global.second_viewport.rect_size.x / Global.canvas.size.x + v_ratio = Global.second_viewport.rect_size.y / Global.canvas.size.y + ratio = min(h_ratio, v_ratio) + elif name == "Camera2D2": + h_ratio = Global.main_viewport.rect_size.x / Global.canvas.size.x + v_ratio = Global.main_viewport.rect_size.y / Global.canvas.size.y + ratio = min(h_ratio, v_ratio) + + zoom = Vector2(1 / ratio, 1 / ratio) + offset = Global.canvas.size / 2 + if name == "Camera2D": + Global.zoom_level_label.text = str(round(100 / Global.camera.zoom.x)) + " %" + Global.horizontal_ruler.update() + Global.vertical_ruler.update() diff --git a/src/Canvas.gd b/src/Canvas.gd index 12cafeb9b..0ab687198 100644 --- a/src/Canvas.gd +++ b/src/Canvas.gd @@ -539,19 +539,9 @@ func camera_zoom() -> void: Global.camera2.zoom_max = Vector2.ONE Global.camera_preview.zoom_max = Vector2.ONE - var smaller_viewport_axis = min(Global.main_viewport.rect_size.x, Global.main_viewport.rect_size.y) - Global.camera.zoom = Vector2(bigger_canvas_axis, bigger_canvas_axis) / smaller_viewport_axis - Global.camera2.zoom = Vector2(bigger_canvas_axis, bigger_canvas_axis) * 0.002 - Global.camera_preview.zoom = Vector2(bigger_canvas_axis, bigger_canvas_axis) * 0.007 - Global.zoom_level_label.text = str(round(100 / Global.camera.zoom.x)) + " %" - - # Set camera offset to the center of canvas - Global.camera.offset = size / 2 - Global.camera2.offset = size / 2 - Global.camera_preview.offset = size / 2 - - Global.horizontal_ruler.update() - Global.vertical_ruler.update() + Global.camera.fit_to_frame() + Global.camera2.fit_to_frame() + Global.camera_preview.fit_to_frame() Global.transparent_checker._ready() # To update the rect size diff --git a/src/Main.gd b/src/Main.gd index 3fe26cd6c..bd0c72bdc 100644 --- a/src/Main.gd +++ b/src/Main.gd @@ -791,13 +791,7 @@ func _on_RightZoomModeOptions_item_selected(ID : int) -> void: func _on_FitToFrameButton_pressed() -> void: - var bigger_canvas_axis = max(Global.canvas.size.x, Global.canvas.size.y) - var smaller_viewport_axis = min(Global.main_viewport.rect_size.x, Global.main_viewport.rect_size.y) - Global.camera.zoom = Vector2(bigger_canvas_axis, bigger_canvas_axis) / smaller_viewport_axis - Global.camera.offset = Global.canvas.size / 2 - Global.zoom_level_label.text = str(round(100 / Global.camera.zoom.x)) + " %" - Global.horizontal_ruler.update() - Global.vertical_ruler.update() + Global.camera.fit_to_frame() func _on_100ZoomButton_pressed() -> void: