From b79ce0ae15d279970ff771bf7c15dd8cafc38c46 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Fri, 11 Oct 2024 14:44:21 +0300 Subject: [PATCH] Add a new "CanvasCameras" node group for the canvas cameras --- project.godot | 4 +++ src/Autoload/Global.gd | 6 ---- src/Tools/UtilityTools/Pan.gd | 8 ++--- src/Tools/UtilityTools/Zoom.gd | 27 +++++++------- src/UI/Canvas/Canvas.gd | 2 +- src/UI/Canvas/CanvasCamera.gd | 35 +++++++------------ .../CanvasPreviewContainer.tscn | 5 ++- src/UI/UI.tscn | 4 +-- 8 files changed, 40 insertions(+), 51 deletions(-) diff --git a/project.godot b/project.godot index 35ca2ee8d..3737bddf8 100644 --- a/project.godot +++ b/project.godot @@ -61,6 +61,10 @@ window/per_pixel_transparency/allowed.web=false enabled=PackedStringArray("res://addons/aimg_io/plugin.cfg", "res://addons/dockable_container/plugin.cfg", "res://addons/keychain/plugin.cfg") +[global_group] + +CanvasCameras="" + [importer_defaults] texture={ diff --git a/src/Autoload/Global.gd b/src/Autoload/Global.gd index 4f0ce33d0..5b8259fae 100644 --- a/src/Autoload/Global.gd +++ b/src/Autoload/Global.gd @@ -622,12 +622,6 @@ var cel_button_scene: PackedScene = load("res://src/UI/Timeline/CelButton.tscn") ) ## Camera of the main canvas. @onready var camera: CanvasCamera = main_viewport.find_child("Camera2D") -## Camera of the second canvas preview. -@onready var camera2: CanvasCamera = second_viewport.find_child("Camera2D2") -## Camera of the canvas preview. -@onready var camera_preview: CanvasCamera = control.find_child("CameraPreview") -## Array of cameras used in Pixelorama. -@onready var cameras := [camera, camera2, camera_preview] ## Horizontal ruler of the main canvas. It has the [param HorizontalRuler.gd] script attached. @onready var horizontal_ruler: BaseButton = control.find_child("HorizontalRuler") ## Vertical ruler of the main canvas. It has the [param VerticalRuler.gd] script attached. diff --git a/src/Tools/UtilityTools/Pan.gd b/src/Tools/UtilityTools/Pan.gd index b6b5c7d89..5c9d0d652 100644 --- a/src/Tools/UtilityTools/Pan.gd +++ b/src/Tools/UtilityTools/Pan.gd @@ -3,8 +3,8 @@ extends BaseTool func draw_start(pos: Vector2i) -> void: super.draw_start(pos) - Global.camera.drag = true - Global.camera2.drag = true + for camera: CanvasCamera in get_tree().get_nodes_in_group("CanvasCameras"): + camera.drag = true func draw_move(pos: Vector2i) -> void: @@ -13,5 +13,5 @@ func draw_move(pos: Vector2i) -> void: func draw_end(pos: Vector2i) -> void: super.draw_end(pos) - Global.camera.drag = false - Global.camera2.drag = false + for camera: CanvasCamera in get_tree().get_nodes_in_group("CanvasCameras"): + camera.drag = false diff --git a/src/Tools/UtilityTools/Zoom.gd b/src/Tools/UtilityTools/Zoom.gd index 2fa158af3..0c2efbe0d 100644 --- a/src/Tools/UtilityTools/Zoom.gd +++ b/src/Tools/UtilityTools/Zoom.gd @@ -28,11 +28,13 @@ func _on_ModeOptions_item_selected(id: ZoomMode) -> void: func _on_FitToFrame_pressed() -> void: - Global.camera.fit_to_frame(Global.current_project.size) + for camera: CanvasCamera in get_tree().get_nodes_in_group("CanvasCameras"): + camera.fit_to_frame(Global.current_project.size) func _on_100_pressed() -> void: - Global.camera.zoom_100() + for camera: CanvasCamera in get_tree().get_nodes_in_group("CanvasCameras"): + camera.zoom_100() func get_config() -> Dictionary: @@ -50,20 +52,21 @@ func update_config() -> void: func draw_start(pos: Vector2i) -> void: super.draw_start(pos) var mouse_pos := get_global_mouse_position() - var viewport_rect := Rect2(Global.main_viewport.global_position, Global.main_viewport.size) - var viewport_rect_2 := Rect2( - Global.second_viewport.global_position, Global.second_viewport.size - ) - - if viewport_rect.has_point(mouse_pos): - Global.camera.zoom_camera(_zoom_mode * 2 - 1) - elif viewport_rect_2.has_point(mouse_pos): - Global.camera2.zoom_camera(_zoom_mode * 2 - 1) + for camera: CanvasCamera in get_tree().get_nodes_in_group("CanvasCameras"): + var viewport_container := camera.get_viewport().get_parent() as SubViewportContainer + var viewport_rect := Rect2(viewport_container.global_position, viewport_container.size) + if viewport_rect.has_point(mouse_pos): + camera.zoom_camera(_zoom_mode * 2 - 1) func draw_move(pos: Vector2i) -> void: super.draw_move(pos) - Global.camera.zoom_camera(-_relative.x / 3) + var mouse_pos := get_global_mouse_position() + for camera: CanvasCamera in get_tree().get_nodes_in_group("CanvasCameras"): + var viewport_container := camera.get_viewport().get_parent() as SubViewportContainer + var viewport_rect := Rect2(viewport_container.global_position, viewport_container.size) + if viewport_rect.has_point(mouse_pos): + camera.zoom_camera(-_relative.x / 3) func draw_end(pos: Vector2i) -> void: diff --git a/src/UI/Canvas/Canvas.gd b/src/UI/Canvas/Canvas.gd index 2f35195e0..35867c633 100644 --- a/src/UI/Canvas/Canvas.gd +++ b/src/UI/Canvas/Canvas.gd @@ -98,7 +98,7 @@ func _input(event: InputEvent) -> void: func camera_zoom() -> void: - for camera in Global.cameras: + for camera: CanvasCamera in get_tree().get_nodes_in_group("CanvasCameras"): camera.fit_to_frame(Global.current_project.size) Global.transparent_checker.update_rect() diff --git a/src/UI/Canvas/CanvasCamera.gd b/src/UI/Canvas/CanvasCamera.gd index c75e4a22b..19465324f 100644 --- a/src/UI/Canvas/CanvasCamera.gd +++ b/src/UI/Canvas/CanvasCamera.gd @@ -142,7 +142,13 @@ func zoom_100() -> void: func fit_to_frame(size: Vector2) -> void: - # temporarily disable integer zoom + viewport_container = get_viewport().get_parent() + var h_ratio := viewport_container.size.x / size.x + var v_ratio := viewport_container.size.y / size.y + var ratio := minf(h_ratio, v_ratio) + if ratio == 0 or !viewport_container.visible: + return + # Temporarily disable integer zoom. var reset_integer_zoom := Global.integer_zoom if reset_integer_zoom: Global.integer_zoom = !Global.integer_zoom @@ -150,33 +156,16 @@ func fit_to_frame(size: Vector2) -> void: # Adjust to the rotated size: if camera_angle != 0.0: - # Calculating the rotated corners of the frame to find its rotated size + # Calculating the rotated corners of the frame to find its rotated size. var a := Vector2.ZERO # Top left - var b := Vector2(size.x, 0).rotated(camera_angle) # Top right - var c := Vector2(0, size.y).rotated(camera_angle) # Bottom left - var d := Vector2(size.x, size.y).rotated(camera_angle) # Bottom right + var b := Vector2(size.x, 0).rotated(camera_angle) # Top right. + var c := Vector2(0, size.y).rotated(camera_angle) # Bottom left. + var d := Vector2(size.x, size.y).rotated(camera_angle) # Bottom right. - # Find how far apart each opposite point is on each axis, and take the longer one + # Find how far apart each opposite point is on each axis, and take the longer one. size.x = maxf(absf(a.x - d.x), absf(b.x - c.x)) size.y = maxf(absf(a.y - d.y), absf(b.y - c.y)) - viewport_container = get_viewport().get_parent() - var h_ratio := viewport_container.size.x / size.x - var v_ratio := viewport_container.size.y / size.y - var ratio := minf(h_ratio, v_ratio) - if ratio == 0 or !viewport_container.visible: - 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 index == Cameras.MAIN: - h_ratio = Global.second_viewport.size.x / size.x - v_ratio = Global.second_viewport.size.y / size.y - ratio = minf(h_ratio, v_ratio) - elif index == Cameras.SECOND: - h_ratio = Global.main_viewport.size.x / size.x - v_ratio = Global.main_viewport.size.y / size.y - ratio = minf(h_ratio, v_ratio) - ratio = clampf(ratio, 0.1, ratio) zoom = Vector2(ratio, ratio) if reset_integer_zoom: diff --git a/src/UI/CanvasPreviewContainer/CanvasPreviewContainer.tscn b/src/UI/CanvasPreviewContainer/CanvasPreviewContainer.tscn index 715a8e54e..097f0c53b 100644 --- a/src/UI/CanvasPreviewContainer/CanvasPreviewContainer.tscn +++ b/src/UI/CanvasPreviewContainer/CanvasPreviewContainer.tscn @@ -77,7 +77,7 @@ anchors_preset = 0 [node name="CanvasPreview" parent="VBox/HBox/PreviewViewportContainer/SubViewport" instance=ExtResource("5")] unique_name_in_owner = true -[node name="CameraPreview" type="Node2D" parent="VBox/HBox/PreviewViewportContainer/SubViewport"] +[node name="CameraPreview" type="Node2D" parent="VBox/HBox/PreviewViewportContainer/SubViewport" groups=["CanvasCameras"]] unique_name_in_owner = true script = ExtResource("5_ge2km") index = 2 @@ -122,10 +122,9 @@ layout_mode = 2 size_flags_horizontal = 3 mouse_default_cursor_shape = 2 clip_text = true -item_count = 2 selected = 0 +item_count = 2 popup/item_0/text = "All frames" -popup/item_0/id = 0 popup/item_1/text = "Current frame as spritesheet" popup/item_1/id = 1 diff --git a/src/UI/UI.tscn b/src/UI/UI.tscn index 1bb78eb62..c6b892717 100644 --- a/src/UI/UI.tscn +++ b/src/UI/UI.tscn @@ -325,7 +325,7 @@ anchors_preset = 0 [node name="Canvas" parent="DockableContainer/Main Canvas/ViewportandVerticalRuler/SubViewportContainer/SubViewport" instance=ExtResource("19")] -[node name="Camera2D" type="Node2D" parent="DockableContainer/Main Canvas/ViewportandVerticalRuler/SubViewportContainer/SubViewport"] +[node name="Camera2D" type="Node2D" parent="DockableContainer/Main Canvas/ViewportandVerticalRuler/SubViewportContainer/SubViewport" groups=["CanvasCameras"]] script = ExtResource("7") [node name="CanvasLayer" type="CanvasLayer" parent="DockableContainer/Main Canvas/ViewportandVerticalRuler/SubViewportContainer/SubViewport"] @@ -361,7 +361,7 @@ anchors_preset = 0 [node name="CanvasPreview" parent="DockableContainer/Second Canvas/SubViewport" instance=ExtResource("2")] -[node name="Camera2D2" type="Node2D" parent="DockableContainer/Second Canvas/SubViewport"] +[node name="Camera2D2" type="Node2D" parent="DockableContainer/Second Canvas/SubViewport" groups=["CanvasCameras"]] script = ExtResource("7") index = 1