1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-02-21 21:13:14 +00:00

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.
This commit is contained in:
Emmanouil Papadeas 2023-04-19 21:35:29 +03:00
parent 1a9691052e
commit 9607981567
5 changed files with 32 additions and 24 deletions

View file

@ -5,7 +5,6 @@ signal rotation_changed
enum Cameras { MAIN, SECOND, SMALL } enum Cameras { MAIN, SECOND, SMALL }
const KEY_MOVE_ACTION_NAMES := ["camera_left", "camera_right", "camera_up", "camera_down"]
const CAMERA_SPEED_RATE := 15.0 const CAMERA_SPEED_RATE := 15.0
export(Cameras) var index := 0 export(Cameras) var index := 0
@ -22,6 +21,7 @@ var should_tween := true
func _ready() -> void: func _ready() -> void:
set_process_input(false)
if index == Cameras.MAIN: if index == Cameras.MAIN:
rotation_slider = Global.top_menu_container.get_node("%RotationSlider") rotation_slider = Global.top_menu_container.get_node("%RotationSlider")
rotation_slider.connect("value_changed", self, "_rotation_value_changed") rotation_slider.connect("value_changed", self, "_rotation_value_changed")
@ -78,22 +78,6 @@ func _input(event: InputEvent) -> void:
drag = false drag = false
return return
mouse_pos = viewport_container.get_local_mouse_position() 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"): if event.is_action_pressed("pan"):
drag = true drag = true
elif event.is_action_released("pan"): 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 elif event.is_action_pressed("zoom_out", false, true): # Wheel Down Event
zoom_camera(1) 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: if event.factor < 1:
zoom_camera(1) zoom_camera(1)
else: else:
zoom_camera(-1) zoom_camera(-1)
elif event is InputEventPanGesture and OS.get_name() != "Android": elif event is InputEventPanGesture and OS.get_name() != "Android":
# Pan Gesture on a Latop touchpad # Pan Gesture on a laptop touchpad
offset = offset + event.delta.rotated(rotation) * zoom * 7 # for moving the canvas offset = offset + event.delta.rotated(rotation) * zoom * 7
elif event is InputEventMouseMotion && drag: elif event is InputEventMouseMotion:
offset = offset - event.relative.rotated(rotation) * zoom if drag:
update_transparent_checker_offset() offset = offset - event.relative.rotated(rotation) * zoom
_update_rulers() 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() save_values_to_project()

View file

@ -73,3 +73,11 @@ func _on_EndFrame_value_changed(value: float) -> void:
start_frame.value = value start_frame.value = value
canvas_preview.frame = value - 1 canvas_preview.frame = value - 1
canvas_preview.update() 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)

View file

@ -238,6 +238,8 @@ script = ExtResource( 8 )
prefix = "End frame:" prefix = "End frame:"
[connection signal="value_changed" from="VBox/HBox/VBoxContainer/PreviewZoomSlider" to="." method="_on_PreviewZoomSlider_value_changed"] [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="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="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"] [connection signal="value_changed" from="VBox/Animation/VBoxContainer/Options/GridContainer/HFrames" to="." method="_on_HFrames_value_changed"]

View file

@ -317,6 +317,7 @@ size_flags_horizontal = 3
size_flags_vertical = 3 size_flags_vertical = 3
stretch = true stretch = true
script = ExtResource( 23 ) script = ExtResource( 23 )
camera_path = NodePath("Viewport/Camera2D")
[node name="Viewport" type="Viewport" parent="DockableContainer/Main Canvas/ViewportandVerticalRuler/ViewportContainer"] [node name="Viewport" type="Viewport" parent="DockableContainer/Main Canvas/ViewportandVerticalRuler/ViewportContainer"]
size = Vector2( 901, 576 ) size = Vector2( 901, 576 )
@ -354,6 +355,7 @@ margin_bottom = 350.0
size_flags_vertical = 3 size_flags_vertical = 3
stretch = true stretch = true
script = ExtResource( 23 ) script = ExtResource( 23 )
camera_path = NodePath("Viewport/Camera2D2")
[node name="Viewport" type="Viewport" parent="DockableContainer/Second Canvas"] [node name="Viewport" type="Viewport" parent="DockableContainer/Second Canvas"]
size = Vector2( 2, 342 ) size = Vector2( 2, 342 )

View file

@ -1,5 +1,9 @@
extends ViewportContainer extends ViewportContainer
export(NodePath) var camera_path
onready var camera := get_node(camera_path) as Camera2D
func _ready() -> void: func _ready() -> void:
material = CanvasItemMaterial.new() material = CanvasItemMaterial.new()
@ -7,12 +11,14 @@ func _ready() -> void:
func _on_ViewportContainer_mouse_entered() -> void: func _on_ViewportContainer_mouse_entered() -> void:
camera.set_process_input(true)
Global.has_focus = true Global.has_focus = true
Global.control.left_cursor.visible = Global.show_left_tool_icon Global.control.left_cursor.visible = Global.show_left_tool_icon
Global.control.right_cursor.visible = Global.show_right_tool_icon Global.control.right_cursor.visible = Global.show_right_tool_icon
func _on_ViewportContainer_mouse_exited() -> void: func _on_ViewportContainer_mouse_exited() -> void:
camera.set_process_input(false)
Global.has_focus = false Global.has_focus = false
Global.control.left_cursor.visible = false Global.control.left_cursor.visible = false
Global.control.right_cursor.visible = false Global.control.right_cursor.visible = false