From 3c4b9ad5b4fb03127ddf94564dd1a8c8b9ee89b8 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Mon, 2 Jan 2023 14:40:48 +0200 Subject: [PATCH] Use signals for zoom_changed and rotation_changed in CameraMovement --- src/Classes/Project.gd | 5 ++--- src/UI/Canvas/CameraMovement.gd | 26 ++++++++++++++------------ src/UI/Canvas/PixelGrid.gd | 4 ++++ src/UI/Canvas/Selection.gd | 4 +++- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/Classes/Project.gd b/src/Classes/Project.gd index e39a5f290..b5d400617 100644 --- a/src/Classes/Project.gd +++ b/src/Classes/Project.gd @@ -140,7 +140,6 @@ func selection_map_changed() -> void: func _selection_offset_changed(value: Vector2) -> void: selection_offset = value Global.canvas.selection.marching_ants_outline.offset = selection_offset - Global.canvas.selection.update_on_zoom(Global.camera.zoom.x) func change_project() -> void: @@ -246,8 +245,8 @@ func change_project() -> void: camera.rotation = cameras_rotation[i] camera.zoom = cameras_zoom[i] camera.offset = cameras_offset[i] - camera.rotation_changed() - camera.zoom_changed() + camera.emit_signal("rotation_changed") + camera.emit_signal("zoom_changed") i += 1 diff --git a/src/UI/Canvas/CameraMovement.gd b/src/UI/Canvas/CameraMovement.gd index 738d5cc54..dd0cbd5d3 100644 --- a/src/UI/Canvas/CameraMovement.gd +++ b/src/UI/Canvas/CameraMovement.gd @@ -1,5 +1,8 @@ extends Camera2D +signal zoom_changed +signal rotation_changed + enum Cameras { MAIN, SECOND, SMALL } const KEY_MOVE_ACTION_NAMES := ["camera_left", "camera_right", "camera_up", "camera_down"] @@ -15,6 +18,8 @@ var index := 0 func _ready() -> void: + connect("zoom_changed", self, "_zoom_changed") + connect("rotation_changed", self, "_rotation_changed") rotating = true viewport_container = get_parent().get_parent() transparent_checker = get_parent().get_node("TransparentChecker") @@ -147,7 +152,7 @@ func _has_selection_tool() -> bool: func _rotate_camera_around_point(degrees: float, point: Vector2) -> void: offset = (offset - point).rotated(deg2rad(degrees)) + point rotation_degrees = wrapf(rotation_degrees + degrees, -180, 180) - rotation_changed() + emit_signal("rotation_changed") func _set_camera_rotation_degrees(degrees: float) -> void: @@ -155,10 +160,10 @@ func _set_camera_rotation_degrees(degrees: float) -> void: var canvas_center: Vector2 = Global.current_project.size / 2 offset = (offset - canvas_center).rotated(deg2rad(difference)) + canvas_center rotation_degrees = wrapf(degrees, -180, 180) - rotation_changed() + emit_signal("rotation_changed") -func rotation_changed() -> void: +func _rotation_changed() -> void: if index == Cameras.MAIN: # Negative to make going up in value clockwise, and match the spinbox which does the same Global.rotation_level_button.text = str(wrapi(round(-rotation_degrees), -180, 180)) + " °" @@ -192,7 +197,7 @@ func zoom_camera(dir: int) -> void: zoom = zoom_max offset = offset + (-0.5 * viewport_size + mouse_pos).rotated(rotation) * (prev_zoom - zoom) - zoom_changed() + emit_signal("zoom_changed") func zoom_camera_percent(value: float) -> void: @@ -204,20 +209,17 @@ func zoom_camera_percent(value: float) -> void: tween.tween_property(self, "zoom", new_zoom, 0.05) else: zoom = new_zoom - zoom_changed() + emit_signal("zoom_changed") -func zoom_changed() -> void: +func _zoom_changed() -> void: update_transparent_checker_offset() if index == Cameras.MAIN: Global.zoom_level_button.text = str(round(100 / zoom.x)) + " %" - Global.canvas.pixel_grid.update() _update_rulers() for guide in Global.current_project.guides: guide.width = zoom.x * 2 - Global.canvas.selection.update_on_zoom(zoom.x) - elif index == Cameras.SMALL: Global.preview_zoom_slider.value = -zoom.x @@ -228,13 +230,13 @@ func _update_rulers() -> void: func _on_tween_step(_idx: int) -> void: - zoom_changed() + emit_signal("zoom_changed") func zoom_100() -> void: zoom = Vector2.ONE offset = Global.current_project.size / 2 - zoom_changed() + emit_signal("zoom_changed") func fit_to_frame(size: Vector2) -> void: @@ -271,7 +273,7 @@ func fit_to_frame(size: Vector2) -> void: ratio = clamp(ratio, 0.1, ratio) zoom = Vector2(1 / ratio, 1 / ratio) - zoom_changed() + emit_signal("zoom_changed") func save_values_to_project() -> void: diff --git a/src/UI/Canvas/PixelGrid.gd b/src/UI/Canvas/PixelGrid.gd index b5ba863ba..2147a01da 100644 --- a/src/UI/Canvas/PixelGrid.gd +++ b/src/UI/Canvas/PixelGrid.gd @@ -1,6 +1,10 @@ extends Node2D +func _ready() -> void: + Global.camera.connect("zoom_changed", self, "update") + + func _draw() -> void: if not Global.draw_pixel_grid: return diff --git a/src/UI/Canvas/Selection.gd b/src/UI/Canvas/Selection.gd index 11daeec61..0a816e65c 100644 --- a/src/UI/Canvas/Selection.gd +++ b/src/UI/Canvas/Selection.gd @@ -60,6 +60,7 @@ class Gizmo: func _ready() -> void: + Global.camera.connect("zoom_changed", self, "_update_on_zoom") gizmos.append(Gizmo.new(Gizmo.Type.SCALE, Vector2(-1, -1))) # Top left gizmos.append(Gizmo.new(Gizmo.Type.SCALE, Vector2(0, -1))) # Center top gizmos.append(Gizmo.new(Gizmo.Type.SCALE, Vector2(1, -1))) # Top right @@ -282,7 +283,8 @@ func _update_gizmos() -> void: update() -func update_on_zoom(zoom: float) -> void: +func _update_on_zoom() -> void: + var zoom := Global.camera.zoom.x var size := max( Global.current_project.selection_map.get_size().x, Global.current_project.selection_map.get_size().y