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

2d or viewport stretch mode does not scale mouse coordinates #34805

Closed
Bleuzen opened this issue Jan 4, 2020 · 8 comments · Fixed by #82800
Closed

2d or viewport stretch mode does not scale mouse coordinates #34805

Bleuzen opened this issue Jan 4, 2020 · 8 comments · Fixed by #82800

Comments

@Bleuzen
Copy link

Bleuzen commented Jan 4, 2020

Godot version:
Godot_v3.2-beta5_x11

OS/device including version:
Manjaro Linux (rolling (as of 2020-01-04))

Issue description:
I placed my games view in a viewport which itself is placed within a ViewportContainer.
I attached a script to it to scale the viewport to the current display/window size.
The scaling of the graphics work, but the mouse coordinates are not scaled with them.
Mouse detection on Controls is still acting as if the Viewport would not be scaled up.

You can see it in action in the beginning of this video I made:
https://www.youtube.com/watch?v=c58L8MyqYEU

Additional / what I found out so far:
If i disable and re-enable the "handle input locally" option of the viewport during runtime, it works as long as I don't change the window size. (another bug?)

Steps to reproduce:
Make a 2D scene with some controls in it (for example buttons).
Scale up this scene with with a Viewport within a ViewportContainer.
(You can do it easier and during the game is running for example by using a script like the one that I used in the demo / reproduction project linked below.)

Minimal reproduction project:
https://github.com/Bleuzen/godot-viewport-scaling-bug-demo

@Calinou
Copy link
Member

Calinou commented Jan 4, 2020

You can manually compensate the mouse coordinates to take scaling into account:

# `get_size_override()` will return a valid size only if the stretch mode is `2d`.
# Otherwise, the viewport size is used directly.
var viewport_base_size = (
		get_viewport().get_size_override() if get_viewport().get_size_override() > Vector2(0, 0)
		else get_viewport().size
)

This should make the coordinates behave seamlessly regardless of the current stretch mode.

@Bleuzen
Copy link
Author

Bleuzen commented Jan 4, 2020

@Calinou Sorry, I'm still not a pro with Godot. Where do I have to place this code in my demo project?

I tried it in the Viewport.gd script and in a script attached only to the real Viewport in the scene. Both didn't work.
If i paste it as you wrote it, the game crashes with a null instance error.
So I replaced the get_viewport() calls with my viewport.
But this changes nothing, mouse position is still not scaled up.

I guess I will have to tell Godot somehow to use the result of this variable for the mouse scaling, but I'm not sure how exactly.

@hardikbhalodi
Copy link

@Calinou same issue as @Bleuzen mentioned in 3.2 stable and 3.2.2 beta

@Duehok
Copy link

Duehok commented Aug 24, 2020

I just got bitten by that one!
win 10, godot 3.2.2 stable

Workaround: instead of using GetViewport().GetMousePosition()
use
GetViewport().GetFinalTransform().XformInv(GlobalMousePosition())
(In C#)

@RedHeadphone
Copy link
Contributor

You can manually compensate the mouse coordinates to take scaling into account:

# `get_size_override()` will return a valid size only if the stretch mode is `2d`.
# Otherwise, the viewport size is used directly.
var viewport_base_size = (
		get_viewport().get_size_override() if get_viewport().get_size_override() > Vector2(0, 0)
		else get_viewport().size
)

This should make the coordinates behave seamlessly regardless of the current stretch mode.

@Calinou In 3d gui , code uses "on mouse enter" how can I fix it?

@Calinou
Copy link
Member

Calinou commented Aug 31, 2020

@RedHeadphone This repository is meant to be used for bug reports only. For support questions, please use one of the other community channels instead.

@theludovyc
Copy link
Contributor

theludovyc commented Jan 30, 2021

Hello,

We have the same error, you can see the projet here https://github.com/salty64/Godot_Obj_viewer/tree/master/src. I made the little project with this error in a days.

Parameters are "2d" and "Keep".

We correct it with :

#get the real size of the viewport
var root_size = $"/root".size

#calculate black strip
var correction = (OS.get_window_size() - root_size)/2.0

#we use fixed resolution of 1600 * 900
var ratio = Vector2(1600.0, 900.0)/root_size

var real_mouse_position = (get_viewport().get_mouse_position()-correction)*ratio

Scene Tree :
root <= it's a viewport
-ViewportContainer
--Viewport <= current viewport
---Gimbal <= script is here
----InnerGimbal
-----Camera <= current camera

Where get_mouse_position() is declared in scene/main/viewport.cpp : here

@Calinou
Copy link
Member

Calinou commented Aug 21, 2022

For drag-and-drop code, use something like this (tested with the 2d stretch mode):

func _input(event):
	# ...
	var viewport_base_size = (
		get_viewport().get_size_override() if get_viewport().get_size_override() > Vector2(0, 0)
		else get_viewport().size
	)
	var scale_factor = OS.window_size / viewport_base_size

	target.global_position = event.global_position / scale_factor

Regarding the original issue, I wonder if a event.scaled_global_position property should be added (ditto for event.scaled_relative, for resolution-independent mouse sensitivity in FPS games). Are there frequent situations where you need the original unscaled mouse coordinates available?

@Calinou Calinou changed the title Viewport scaling does not scale mouse coordinates 2d or viewport stretch mode does not scale mouse coordinates Aug 21, 2022
@YuriSizov YuriSizov removed this from the 4.0 milestone Feb 27, 2023
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.

8 participants