Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong mouse coordinates after window resize when using viewport stretch-mode canvas_items or viewport #52295

Closed
ZuBsPaCe opened this issue Aug 31, 2021 · 4 comments · Fixed by #57082

Comments

@ZuBsPaCe
Copy link
Contributor

Godot version

v4.0.dev (75697c0)

System information

Windows 10

Issue description

I want to position a Sprite2D at the global mouse position.

This works when setting stretch mode to disabled/keep-aspect, even when resizing the screen:
ViewportScalingDisabled

When I set stretch mode to canvas_items/keep-aspect, the mouse coordinates are sometimes wrong after I resize the window:
ViewportScalingCanvasItems

When I set stretch mode to viewport/keep-aspect, the mouse coordinates are always wrong after I resize the window:
ViewportScalingViewport

Hints:

Steps to reproduce

  1. Add a Sprite2D scene with following code:
func _process(delta):
    position = get_global_mouse_position()
  1. In the project settings select viewport/keep-aspect or canvas_items/keep-aspect.
  2. Run the game.
  3. Resize the window by dragging the top, right, bottom or left window border.

Minimal reproduction project

ViewportScalingGodot4.zip

@CosmicSpore
Copy link

This bug does exist in v3.3 (and also v3.2 at the very least) but in a slightly different way...
Instead of global_mouse_position() not returning proper coordinates, the same problem instead occurs with InputEvents, particularly with "event.global_position". In fact, ironically enough, using global_mouse_position() actually seems to FIX the bug in v3.3. (So I guess technically this could be considered a different bug? But it seems similar enough to me that I figured I would just post here.)

Steps to reproduce on 3.3:

  1. Add Sprite to scene with the following code:
func _input(event):
    if event is InputEventMouse:
	position = event.global_position
  1. In project settings select: Mode: 2D, Stretch: Keep Height (May occur with other settings as well)
  2. Run
  3. Resize the window

Minimal project:
TestTestTest.zip

I've had a longstanding issue with this and I could never figure out why it happened until I saw this ticket. It never occurred to me the problem may have been in the engine itself. So I started testing a couple things and thanks to @ZuBsPaCe, for posting this bug, I finally found a way to work around it properly in my project. :)

@Racinettee
Copy link

I'm not fully sure if its the same issue or not, but I think this might also affect UI elements in godot 4.
When using the ItemList in godot 4, and the viewport mode: after expanding the window or using a larger test_mode size, the UI elements cannot be clicked anymore.
I couldn't reproduce the issue on 3.3 using the above settings, but in godot 4 the viewport setting on its own causes the UI elements to seem to not get mouse input.

@Supergeek
Copy link

Supergeek commented Oct 4, 2021

In 3.3.3-stable using a StretchShrink setting other than 1 on a ViewportContainer makes GetGlobalMousePosition() incorrect. If you set it back to 1 it works. For values higher than 1, the reported position appears to be offset by an amount that seems to scale with the screen scale. (Using Display->Window->Stretch Mode->Viewport.)

My personal workaround. (C# version)

var global_position = (viewportContainer.GetLocalMousePosition() / viewportContainer.StretchShrink) + camera.Position;

@Calinou
Copy link
Member

Calinou commented Oct 20, 2021

Related to #50802.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants