1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-18 17:19:50 +00:00

Add a new "CanvasCameras" node group for the canvas cameras

This commit is contained in:
Emmanouil Papadeas 2024-10-11 14:44:21 +03:00
parent fc695a038e
commit b79ce0ae15
8 changed files with 40 additions and 51 deletions

View file

@ -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={

View file

@ -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.

View file

@ -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

View file

@ -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
)
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):
Global.camera.zoom_camera(_zoom_mode * 2 - 1)
elif viewport_rect_2.has_point(mouse_pos):
Global.camera2.zoom_camera(_zoom_mode * 2 - 1)
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:

View file

@ -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()

View file

@ -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:

View file

@ -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

View file

@ -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