Repository: godotengine/godot (language: cpp)

Issue:
`ViewPanner` panning-mouse-warp doesn't work in SubViewports or embedded Windows
### Tested versions

- reproducible in v4.0.stable.official [92bee43ad] and v4.4.dev.custom_build [b9437c393]

### System information

Debian Linux X11

### Issue description

Using a `GraphEdit` in a `SubViewport` or in an embedded `Window` has currently the problem, that panning doesn't work, when moving the mouse out of the `GraphEdit` area. In that case, the mouse cursor jumps in unexpected ways:
- In a `SubViewport`, this leads the mouse to be shifted by the distance from the top-left corner of the root window to the top-left corner of the SubViewportContainer.
- In an embedded `Window`, this leads the mouse to be constantly jumping around.

I would expect, that the mouse reappears on the other side of the GraphEdit area without lateral movement.

https://github.com/user-attachments/assets/7a4fd227-fe27-44d3-ba62-ecf53f240c45

I have tracked this to:

https://github.com/godotengine/godot/blob/b9437c393871c377b5ece131afaf831e78dfb6f8/scene/gui/view_panner.cpp#L113

`Input::get_singleton()->warp_mouse_motion` assumes, that its argument are in root-viewport coordinates, which they are not.

### Steps to reproduce

1. Load and run MRP
2. Try panning with the middle mouse button as shown in the video above

### Minimal reproduction project (MRP)

[viewpanner-in-subviewport.zip](https://github.com/user-attachments/files/18141183/viewpanner-in-subviewport.zip)



Discussion / hints:

Fix patch:
diff --git a/editor/animation_bezier_editor.cpp b/editor/animation_bezier_editor.cpp
index 36ca41763873..e352b57ba322 100644
--- a/editor/animation_bezier_editor.cpp
+++ b/editor/animation_bezier_editor.cpp
@@ -222,6 +222,7 @@ void AnimationBezierTrackEdit::_notification(int p_what) {
 
 		case NOTIFICATION_ENTER_TREE: {
 			panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/animation_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
+			panner->set_viewport(get_viewport());
 			[[fallthrough]];
 		}
 		case NOTIFICATION_THEME_CHANGED: {
diff --git a/editor/animation_track_editor.cpp b/editor/animation_track_editor.cpp
index ae2b63deaddf..5f7433ac2a38 100644
--- a/editor/animation_track_editor.cpp
+++ b/editor/animation_track_editor.cpp
@@ -5141,6 +5141,7 @@ void AnimationTrackEditor::_notification(int p_what) {
 
 		case NOTIFICATION_ENTER_TREE: {
 			panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/animation_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
+			panner->set_viewport(get_viewport());
 			[[fallthrough]];
 		}
 		case NOTIFICATION_THEME_CHANGED: {
diff --git a/editor/plugins/animation_blend_tree_editor_plugin.cpp b/editor/plugins/animation_blend_tree_editor_plugin.cpp
index 175aab05fbd7..b4581afdc46c 100644
--- a/editor/plugins/animation_blend_tree_editor_plugin.cpp
+++ b/editor/plugins/animation_blend_tree_editor_plugin.cpp
@@ -942,6 +942,7 @@ void AnimationNodeBlendTreeEditor::_notification(int p_what) {
 	switch (p_what) {
 		case NOTIFICATION_ENTER_TREE: {
 			_update_editor_settings();
+			graph->get_panner()->set_viewport(get_viewport());
 		} break;
 
 		case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp
index c242357a9eff..edf27146927b 100644
--- a/editor/plugins/canvas_item_editor_plugin.cpp
+++ b/editor/plugins/canvas_item_editor_plugin.cpp
@@ -4178,6 +4178,7 @@ void CanvasItemEditor::_notification(int p_what) {
 			AnimationPlayerEditor::get_singleton()->connect("animation_selected", callable_mp(this, &CanvasItemEditor::_keying_changed).unbind(1));
 			_keying_changed();
 			_update_editor_settings();
+			panner->set_viewport(get_viewport());
 
 			connect("item_lock_status_changed", callable_mp(this, &CanvasItemEditor::_update_lock_and_group_button));
 			connect("item_group_status_changed", callable_mp(this, &CanvasItemEditor::_update_lock_and_group_button));
diff --git a/editor/plugins/polygon_2d_editor_plugin.cpp b/editor/plugins/polygon_2d_editor_plugin.cpp
index 6fc4bef71ebe..60cbfd59952f 100644
--- a/editor/plugins/polygon_2d_editor_plugin.cpp
+++ b/editor/plugins/polygon_2d_editor_plugin.cpp
@@ -108,6 +108,7 @@ void Polygon2DEditor::_notification(int p_what) {
 		}
 		case NOTIFICATION_ENTER_TREE: {
 			uv_panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
+			uv_panner->set_viewport(get_viewport());
 		} break;
 
 		case NOTIFICATION_READY: {
diff --git a/editor/plugins/sprite_2d_editor_plugin.cpp b/editor/plugins/sprite_2d_editor_plugin.cpp
index c365ad61338f..f518eb6ecfac 100644
--- a/editor/plugins/sprite_2d_editor_plugin.cpp
+++ b/editor/plugins/sprite_2d_editor_plugin.cpp
@@ -558,7 +558,9 @@ void Sprite2DEditor::_notification(int p_what) {
 		case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
 			panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
 		} break;
-		case NOTIFICATION_ENTER_TREE:
+		case NOTIFICATION_ENTER_TREE: {
+			panner->set_viewport(get_viewport());
+		} break;
 		case NOTIFICATION_THEME_CHANGED: {
 			options->set_button_icon(get_editor_theme_icon(SNAME("Sprite2D")));
 
diff --git a/editor/plugins/texture_region_editor_plugin.cpp b/editor/plugins/texture_region_editor_plugin.cpp
index bd653e4eedaa..21f7c812f2f6 100644
--- a/editor/plugins/texture_region_editor_plugin.cpp
+++ b/editor/plugins/texture_region_editor_plugin.cpp
@@ -824,10 +824,6 @@ void TextureRegionEditor::_notification(int p_what) {
 			[[fallthrough]];
 		}
 
-		case NOTIFICATION_READY: {
-			panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
-		} break;
-
 		case NOTIFICATION_ENTER_TREE: {
 			get_tree()->connect("node_removed", callable_mp(this, &TextureRegionEditor::_node_removed));
 
@@ -835,6 +831,9 @@ void TextureRegionEditor::_notification(int p_what) {
 			if (snap_mode == SNAP_AUTOSLICE && is_visible() && autoslice_is_dirty) {
 				_update_autoslice();
 			}
+
+			panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
+			panner->set_viewport(get_viewport());
 		} break;
 
 		case NOTIFICATION_EXIT_TREE: {
diff --git a/editor/plugins/tiles/tile_atlas_view.cpp b/editor/plugins/tiles/tile_atlas_view.cpp
index bafacf8e36d8..144493ab40bb 100644
--- a/editor/plugins/tiles/tile_atlas_view.cpp
+++ b/editor/plugins/tiles/tile_atlas_view.cpp
@@ -615,6 +615,7 @@ void TileAtlasView::_notification(int p_what) {
 		}
 		case NOTIFICATION_ENTER_TREE: {
 			panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
+			panner->set_viewport(get_viewport());
 		} break;
 
 		case NOTIFICATION_THEME_CHANGED: {
diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp
index 1162943b4a8e..bf1d441c6f96 100644
--- a/editor/plugins/visual_shader_editor_plugin.cpp
+++ b/editor/plugins/visual_shader_editor_plugin.cpp
@@ -5188,6 +5188,7 @@ void VisualShaderEditor::_notification(int p_what) {
 			}
 
 			graph->get_panner()->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
+			graph->get_panner()->set_viewport(get_viewport());
 			graph->set_warped_panning(bool(EDITOR_GET("editors/panning/warped_mouse_panning")));
 		} break;
 
diff --git a/scene/debugger/scene_debugger.cpp b/scene/debugger/scene_debugger.cpp
index f369aec569ec..1e9e961cf611 100644
--- a/scene/debugger/scene_debugger.cpp
+++ b/scene/debugger/scene_debugger.cpp
@@ -1250,6 +1250,7 @@ void RuntimeNodeSelect::_setup(const Dictionary &p_settings) {
 	int pan_speed = p_settings.get("editors/panning/2d_editor_pan_speed", 20);
 	Array keys = p_settings.get("canvas_item_editor/pan_view", Array()).operator Array();
 	panner->setup(panning_scheme, DebuggerMarshalls::deserialize_key_shortcut(keys), simple_panning);
+	panner->set_viewport(root);
 	panner->set_scroll_speed(pan_speed);
 	warped_panning = p_settings.get("editors/panning/warped_mouse_panning", false);
 
diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp
index bcf0f5bdc89f..e909b4bc48ef 100644
--- a/scene/gui/graph_edit.cpp
+++ b/scene/gui/graph_edit.cpp
@@ -785,6 +785,10 @@ void GraphEdit::_notification(int p_what) {
 			minimap->queue_redraw();
 			callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
 		} break;
+
+		case NOTIFICATION_ENTER_TREE: {
+			panner->set_viewport(get_viewport());
+		} break;
 	}
 }
 
diff --git a/scene/gui/view_panner.cpp b/scene/gui/view_panner.cpp
index 438a228c4ff4..a436e662fd0b 100644
--- a/scene/gui/view_panner.cpp
+++ b/scene/gui/view_panner.cpp
@@ -33,6 +33,7 @@
 #include "core/input/input.h"
 #include "core/input/shortcut.h"
 #include "core/os/keyboard.h"
+#include "scene/main/viewport.h"
 
 bool ViewPanner::gui_input(const Ref<InputEvent> &p_event, Rect2 p_canvas_rect) {
 	Ref<InputEventMouseButton> mb = p_event;
@@ -109,8 +110,8 @@ bool ViewPanner::gui_input(const Ref<InputEvent> &p_event, Rect2 p_canvas_rect)
 	Ref<InputEventMouseMotion> mm = p_event;
 	if (mm.is_valid()) {
 		if (is_dragging) {
-			if (p_canvas_rect != Rect2()) {
-				pan_callback.call(Input::get_singleton()->warp_mouse_motion(mm, p_canvas_rect), p_event);
+			if (viewport && p_canvas_rect != Rect2()) {
+				pan_callback.call(viewport->wrap_mouse_in_rect(mm->get_relative(), p_canvas_rect), p_event);
 			} else {
 				pan_callback.call(mm->get_relative(), p_event);
 			}
@@ -212,6 +213,10 @@ void ViewPanner::setup(ControlScheme p_scheme, Ref<Shortcut> p_shortcut, bool p_
 	set_simple_panning_enabled(p_simple_panning);
 }
 
+void ViewPanner::set_viewport(Viewport *p_viewport) {
+	viewport = p_viewport;
+}
+
 bool ViewPanner::is_panning() const {
 	return is_dragging || pan_key_pressed;
 }
diff --git a/scene/gui/view_panner.h b/scene/gui/view_panner.h
index 5aec2d4f6b59..7cdd43e12405 100644
--- a/scene/gui/view_panner.h
+++ b/scene/gui/view_panner.h
@@ -35,6 +35,7 @@
 
 class InputEvent;
 class Shortcut;
+class Viewport;
 
 class ViewPanner : public RefCounted {
 	GDCLASS(ViewPanner, RefCounted);
@@ -63,6 +64,8 @@ class ViewPanner : public RefCounted {
 	bool enable_rmb = false;
 	bool simple_panning_enabled = false;
 
+	Viewport *viewport = nullptr;
+
 	Ref<Shortcut> pan_view_shortcut;
 
 	Callable pan_callback;
@@ -81,6 +84,7 @@ class ViewPanner : public RefCounted {
 	void set_pan_axis(PanAxis p_pan_axis);
 
 	void setup(ControlScheme p_scheme, Ref<Shortcut> p_shortcut, bool p_simple_panning);
+	void set_viewport(Viewport *p_viewport);
 
 	bool is_panning() const;
 	void set_force_drag(bool p_force);
diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp
index 4b89167ce99a..b90352add5ab 100644
--- a/scene/main/viewport.cpp
+++ b/scene/main/viewport.cpp
@@ -1377,6 +1377,40 @@ void Viewport::warp_mouse(const Vector2 &p_position) {
 	Input::get_singleton()->warp_mouse(gpos);
 }
 
+Point2 Viewport::wrap_mouse_in_rect(const Vector2 &p_relative, const Rect2 &p_rect) {
+	// Move the mouse cursor from its current position to a location bounded by `p_rect`
+	// in accordance with a heuristic that takes the traveled distance `p_relative` of the mouse
+	// into account.
+
+	// All parameters are in viewport coordinates.
+	// p_relative denotes the distance to the previous mouse position.
+	// p_rect denotes the area, in which the mouse should be confined in.
+
+	// The relative distance reported for the next event after a warp is in the boundaries of the
+	// size of the rect on that axis, but it may be greater, in which case there's no problem as
+	// fmod() will warp it, but if the pointer has moved in the opposite direction between the
+	// pointer relocation and the subsequent event, the reported relative distance will be less
+	// than the size of the rect and thus fmod() will be disabled for handling the situation.
+	// And due to this mouse warping mechanism being stateless, we need to apply some heuristics
+	// to detect the warp: if the relative distance is greater than the half of the size of the
+	// relevant rect (checked per each axis), it will be considered as the consequence of a former
+	// pointer warp.
+
+	const Point2 rel_sign(p_relative.x >= 0.0f ? 1 : -1, p_relative.y >= 0.0 ? 1 : -1);
+	const Size2 warp_margin = p_rect.size * 0.5f;
+	const Point2 rel_warped(
+			Math::fmod(p_relative.x + rel_sign.x * warp_margin.x, p_rect.size.x) - rel_sign.x * warp_margin.x,
+			Math::fmod(p_relative.y + rel_sign.y * warp_margin.y, p_rect.size.y) - rel_sign.y * warp_margin.y);
+
+	const Point2 pos_local = get_mouse_position() - p_rect.position;
+	const Point2 pos_warped(Math::fposmod(pos_local.x, p_rect.size.x), Math::fposmod(pos_local.y, p_rect.size.y));
+	if (pos_warped != pos_local) {
+		warp_mouse(pos_warped + p_rect.position);
+	}
+
+	return rel_warped;
+}
+
 void Viewport::_gui_sort_roots() {
 	if (!gui.roots_order_dirty) {
 		return;
diff --git a/scene/main/viewport.h b/scene/main/viewport.h
index 4dee21646b8c..38514f238751 100644
--- a/scene/main/viewport.h
+++ b/scene/main/viewport.h
@@ -586,6 +586,7 @@ class Viewport : public Node {
 
 	Vector2 get_mouse_position() const;
 	void warp_mouse(const Vector2 &p_position);
+	Point2 wrap_mouse_in_rect(const Vector2 &p_relative, const Rect2 &p_rect);
 	virtual void update_mouse_cursor_state();
 
 	void set_physics_object_picking(bool p_enable);


Test patch: