From 9607981567061ef7dbb7cf86c98eee5dacd169cf Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Wed, 19 Apr 2023 21:35:29 +0300 Subject: [PATCH] Do not call CameraMovement._input() if the cursor is not on the canvas Slight optimization, as only one camera can now receive input at a time. --- src/UI/Canvas/CameraMovement.gd | 38 +++++++------------ .../CanvasPreviewContainer.gd | 8 ++++ .../CanvasPreviewContainer.tscn | 2 + src/UI/UI.tscn | 2 + src/UI/ViewportContainer.gd | 6 +++ 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/UI/Canvas/CameraMovement.gd b/src/UI/Canvas/CameraMovement.gd index 525f535c9..b9640755f 100644 --- a/src/UI/Canvas/CameraMovement.gd +++ b/src/UI/Canvas/CameraMovement.gd @@ -5,7 +5,6 @@ signal rotation_changed enum Cameras { MAIN, SECOND, SMALL } -const KEY_MOVE_ACTION_NAMES := ["camera_left", "camera_right", "camera_up", "camera_down"] const CAMERA_SPEED_RATE := 15.0 export(Cameras) var index := 0 @@ -22,6 +21,7 @@ var should_tween := true func _ready() -> void: + set_process_input(false) if index == Cameras.MAIN: rotation_slider = Global.top_menu_container.get_node("%RotationSlider") rotation_slider.connect("value_changed", self, "_rotation_value_changed") @@ -78,22 +78,6 @@ func _input(event: InputEvent) -> void: drag = false return mouse_pos = viewport_container.get_local_mouse_position() - var viewport_size := viewport_container.rect_size - if !Rect2(Vector2.ZERO, viewport_size).has_point(mouse_pos): - drag = false - return - - var get_velocity := false - for action in KEY_MOVE_ACTION_NAMES: - if event.is_action(action): - get_velocity = true - - if get_velocity: - var velocity := Input.get_vector("camera_left", "camera_right", "camera_up", "camera_down") - if velocity != Vector2.ZERO and !_has_selection_tool(): - offset += velocity.rotated(rotation) * zoom * CAMERA_SPEED_RATE - _update_rulers() - if event.is_action_pressed("pan"): drag = true elif event.is_action_released("pan"): @@ -103,18 +87,24 @@ func _input(event: InputEvent) -> void: elif event.is_action_pressed("zoom_out", false, true): # Wheel Down Event zoom_camera(1) - elif event is InputEventMagnifyGesture: # Zoom Gesture on a Laptop touchpad + elif event is InputEventMagnifyGesture: # Zoom Gesture on a laptop touchpad if event.factor < 1: zoom_camera(1) else: zoom_camera(-1) elif event is InputEventPanGesture and OS.get_name() != "Android": - # Pan Gesture on a Latop touchpad - offset = offset + event.delta.rotated(rotation) * zoom * 7 # for moving the canvas - elif event is InputEventMouseMotion && drag: - offset = offset - event.relative.rotated(rotation) * zoom - update_transparent_checker_offset() - _update_rulers() + # Pan Gesture on a laptop touchpad + offset = offset + event.delta.rotated(rotation) * zoom * 7 + elif event is InputEventMouseMotion: + if drag: + offset = offset - event.relative.rotated(rotation) * zoom + update_transparent_checker_offset() + _update_rulers() + else: + var velocity := Input.get_vector("camera_left", "camera_right", "camera_up", "camera_down") + if velocity != Vector2.ZERO and !_has_selection_tool(): + offset += velocity.rotated(rotation) * zoom * CAMERA_SPEED_RATE + _update_rulers() save_values_to_project() diff --git a/src/UI/CanvasPreviewContainer/CanvasPreviewContainer.gd b/src/UI/CanvasPreviewContainer/CanvasPreviewContainer.gd index e36df3e52..9f5b5e8ab 100644 --- a/src/UI/CanvasPreviewContainer/CanvasPreviewContainer.gd +++ b/src/UI/CanvasPreviewContainer/CanvasPreviewContainer.gd @@ -73,3 +73,11 @@ func _on_EndFrame_value_changed(value: float) -> void: start_frame.value = value canvas_preview.frame = value - 1 canvas_preview.update() + + +func _on_PreviewViewportContainer_mouse_entered() -> void: + camera.set_process_input(true) + + +func _on_PreviewViewportContainer_mouse_exited() -> void: + camera.set_process_input(false) diff --git a/src/UI/CanvasPreviewContainer/CanvasPreviewContainer.tscn b/src/UI/CanvasPreviewContainer/CanvasPreviewContainer.tscn index 03c097bb1..276abd72e 100644 --- a/src/UI/CanvasPreviewContainer/CanvasPreviewContainer.tscn +++ b/src/UI/CanvasPreviewContainer/CanvasPreviewContainer.tscn @@ -238,6 +238,8 @@ script = ExtResource( 8 ) prefix = "End frame:" [connection signal="value_changed" from="VBox/HBox/VBoxContainer/PreviewZoomSlider" to="." method="_on_PreviewZoomSlider_value_changed"] +[connection signal="mouse_entered" from="VBox/HBox/PreviewViewportContainer" to="." method="_on_PreviewViewportContainer_mouse_entered"] +[connection signal="mouse_exited" from="VBox/HBox/PreviewViewportContainer" to="." method="_on_PreviewViewportContainer_mouse_exited"] [connection signal="toggled" from="VBox/Animation/PlayButton" to="." method="_on_PlayButton_toggled"] [connection signal="item_selected" from="VBox/Animation/VBoxContainer/Mode/OptionButton" to="." method="_on_OptionButton_item_selected"] [connection signal="value_changed" from="VBox/Animation/VBoxContainer/Options/GridContainer/HFrames" to="." method="_on_HFrames_value_changed"] diff --git a/src/UI/UI.tscn b/src/UI/UI.tscn index 0b2fbf447..f1c1233c3 100644 --- a/src/UI/UI.tscn +++ b/src/UI/UI.tscn @@ -317,6 +317,7 @@ size_flags_horizontal = 3 size_flags_vertical = 3 stretch = true script = ExtResource( 23 ) +camera_path = NodePath("Viewport/Camera2D") [node name="Viewport" type="Viewport" parent="DockableContainer/Main Canvas/ViewportandVerticalRuler/ViewportContainer"] size = Vector2( 901, 576 ) @@ -354,6 +355,7 @@ margin_bottom = 350.0 size_flags_vertical = 3 stretch = true script = ExtResource( 23 ) +camera_path = NodePath("Viewport/Camera2D2") [node name="Viewport" type="Viewport" parent="DockableContainer/Second Canvas"] size = Vector2( 2, 342 ) diff --git a/src/UI/ViewportContainer.gd b/src/UI/ViewportContainer.gd index 565fe4e4b..48ca5b843 100644 --- a/src/UI/ViewportContainer.gd +++ b/src/UI/ViewportContainer.gd @@ -1,5 +1,9 @@ extends ViewportContainer +export(NodePath) var camera_path + +onready var camera := get_node(camera_path) as Camera2D + func _ready() -> void: material = CanvasItemMaterial.new() @@ -7,12 +11,14 @@ func _ready() -> void: func _on_ViewportContainer_mouse_entered() -> void: + camera.set_process_input(true) Global.has_focus = true Global.control.left_cursor.visible = Global.show_left_tool_icon Global.control.right_cursor.visible = Global.show_right_tool_icon func _on_ViewportContainer_mouse_exited() -> void: + camera.set_process_input(false) Global.has_focus = false Global.control.left_cursor.visible = false Global.control.right_cursor.visible = false