godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.27k stars 21.04k forks source link

EditorPlugin mouse position wrong when holding right mouse button #70098

Open TokisanGames opened 1 year ago

TokisanGames commented 1 year ago

Godot version

v4.0.beta.custom_build [4828ac6b9]

System information

Windows 11/64, RTX 3070, Vulkan

Issue description

Updated Summary:

I'm making an EditorPlugin. When holding the right mouse button in the editor viewport and querying the mouse position, the information provided is wrong.

This is likely due to the right mouse button capturing the mouse and overriding the mouse position. Both _forward_3d_gui_input(camera, event): event.position and get_viewport().get_mouse_position() provide the center of the screen. Not the center of the viewport, nor the true position.

However, the middle mouse button also rotates the camera in the viewport, and it reports the true mouse position accurately. It also doesn't capture the mouse.

Right mouse button _forward_3d_gui_input(camera, event) and get_viewport().get_mouse_position() should mimic the middle mouse button behavior.


This reports incorrect, unchanging values for event.position when the right mouse button is held down (center of the screen). But not any other button, or none.

func _forward_3d_gui_input(camera: Camera3D, event: InputEvent) -> int:
    if event is InputEventMouseMotion:
        print("Mouse buttons: %d, pos: %.1v, global_pos: %.1v" % [ event.button_mask, event.position, event.global_position ])

eg

Mouse buttons: 0, pos: (519.0, 227.0), global_pos: (705.0, 337.0)
Mouse buttons: 0, pos: (519.0, 225.0), global_pos: (705.0, 335.0)
Mouse buttons: 0, pos: (519.0, 224.0), global_pos: (705.0, 334.0)
Mouse buttons: 0, pos: (519.0, 223.0), global_pos: (705.0, 333.0)
Mouse buttons: 0, pos: (519.0, 221.0), global_pos: (705.0, 331.0)
Mouse buttons: 0, pos: (519.0, 219.0), global_pos: (705.0, 329.0)
Mouse buttons: 0, pos: (518.0, 217.0), global_pos: (704.0, 327.0)
Mouse buttons: 0, pos: (517.0, 217.0), global_pos: (703.0, 327.0)
Mouse buttons: 0, pos: (517.0, 216.0), global_pos: (703.0, 326.0)
Mouse buttons: 0, pos: (516.0, 215.0), global_pos: (702.0, 325.0)
Mouse buttons: 2, pos: (774.0, 475.0), global_pos: (960.0, 585.0)
Mouse buttons: 2, pos: (774.0, 475.0), global_pos: (960.0, 585.0)
Mouse buttons: 2, pos: (774.0, 475.0), global_pos: (960.0, 585.0)
Mouse buttons: 2, pos: (774.0, 475.0), global_pos: (960.0, 585.0)
Mouse buttons: 2, pos: (774.0, 475.0), global_pos: (960.0, 585.0)
Mouse buttons: 2, pos: (774.0, 475.0), global_pos: (960.0, 585.0)
Mouse buttons: 2, pos: (774.0, 475.0), global_pos: (960.0, 585.0)
Mouse buttons: 2, pos: (774.0, 475.0), global_pos: (960.0, 585.0)

Steps to reproduce

Note the right mouse button captures the mouse and rotates the camera. The values become fixed to the screen center.

Middle mouse button also rotates the camera (and shift+mmb pans). The mouse is not captured. The position is reported accurately.

Minimal reproduction project

test_mouse_event.zip

TokisanGames commented 1 year ago

get_viewport().get_mouse_position() is also wrong with the same values.

Perhaps it is overriding the mouse position with the viewport center, since right-mouse click controls the camera angle? However this behavior with the mouse is not desired when making a plugin like a terrain editor. The numbers are wrong.

e.g. Here I can project the mouse position on to the ground accurately with no buttons or left clicking

image

When I right-click, the reported position jumps to the right (not the viewport center and far from the mouse). As I move the camera around, the sphere moves and tracks the terrain, but it keeps this offset. It doesn't matter where I start the click, whether in the top left, middle, bottom right of the viewport, it always jumps to the same position.

image

Sauermann commented 1 year ago

Open Node3DEditor, switch to the 3D viewport and move the mouse. _forward_3d_gui_input(event) Mouse position is fine with no buttons, left or middle buttons, but locks to a strange value if the right button is held down.

The strange value is the center of the ~Viewport~ Window. The right mouse button causes, that the mouse cursor gets captured which positions the mouse cursor at the center of the ~Viewport~ Window. For a description, see https://docs.godotengine.org/en/latest/classes/class_input.html#enumerations

Edit: I was mistaken - the center is relative to the Window and not the Viewport.

TokisanGames commented 1 year ago

I see. It is the center of the screen (as documented for mouse capture), not the viewport. I think get_viewport().get_mouse_position() should report either the true mouse position or the center of the viewport, not screen, and _forward_3d_gui_input(event) should report the true mouse position.

In _forward_3d_gui_input(event) if I return EditorPlugin.AFTER_GUI_INPUT_STOP upon right click, I will get the true mouse position in event.position and it will also disable the editor camera control. If I return EditorPlugin.AFTER_GUI_INPUT_PASS I get the center of the screen. How are the values I receive into the function dependent upon my return value, which controls where this event propagates next?!

Holding the middle mouse button also rotates the camera, but it reports the accurate mouse position. It also doesn't capture the mouse. Right-click should match that behavior.

Summary updated in the Op.