TL;DR: what methods/properties are available for me to create a Vector2 that is the ratio between the window size and the viewport size (e.g. new Vector2(windowWidth, windowHeight) / new Vector2(viewportWidth, viewportHeight)
). I know Window.Size
exists but I'm struggling with the Viewport. Where can I find that? Or where can I find the ratio I'm looking for directly?
The Full Description
I have a game with a viewport dimension of 640x360 and display/window/stretch/mode
as canvas_items. So when I launch game from editor it launches in a window that is 640x360 real pixels on the monitor. When I maximize the window, I'm rendering those same game pixels but stretched 3x to 1920x1080 real monitor pixels.
I have some code that converts a mouse click location to a global world position (C# but should be recognizable to gdscript devs)
Vector2 worldPosition = _worldTileMap.GetCanvasTransform().AffineInverse() * inputEventMouseButton.GlobalPosition;
This works fine when the game pixel dimensions match the real pixel dimensions of the window. But when the window is maximized, the transform is not scaling with the increased zoom into the world. If I want to click on my scaled up 640x360 render of the game, I have to click somewhere in the first 640x360 real pixels of the real window on the computer.
I need to change the above code such that I am always translating the mouse click to the part of the world that is being rendered beneath the cursor. I think what I need is to multiply by the ratio between window dimensions and viewport dimensions, as follows:
Vector2 worldPosition = _worldTileMap.GetCanvasTransform().AffineInverse() * inputEventMouseButton.GlobalPosition * new Vector2(windowWidth, windowHeight) / new Vector2(viewportWidth, viewportHeight);
I found how to get the window dimensions: _worldTileMap.GetWindow().Size
.
I'm just not sure how to get the viewport dimensions. Viewport seems to no longer have a Size property in godot 4. Anyone know where I can find that? Or where I can find the ratio I'm looking for already calculated for me?