godot: OS.get_window_safe_area returning area off screen

Godot version

3.3.2 Stable

System information

iOS 14, Android 11

Issue description

I am currently working on a mobile game, and one of my testers owns an iPhone 11 and he let me know that my UI is being cut off by the notch on his phone.

PXL_20210620_190050670

I am trying to use the function OS.get_window_safe_area() to adjust my user interface so that my UI is not cut off by the notch. The documentation says that this function returns a rect2, and the rect2 documentation says that it is a X, Y, Width and Height so I use the following code to adjust the rect of a control node that has my UI in it.

var safe_area = get_window_safe_area(); var pos_x = safe_area.position.x; var pos_y = safe_area.position.y; var safe_width = safe_area.end.x; var safe_height = safe_area.end.y; var safe_position = Vector2(pos_x, pos_y); var safe_size = Vector2(safe_width, safe_height); $canvaslayer/control.set_position(safe_position, false); $canvaslayer/control.set_size(safe_size, false);

When I adjust my control node with the position and width / height of the safe area, this is what my UI looks like IMG_0187

The function is returning a size that is larger than the resolution that my game is configured to run at.

I suspect that I have some sort of configuration problem in the display / window settings, but I have googled to try to resolve this issue, I have posted on forums and discords, and nobody that I have spoken to seems to know how to use this function properly or what is happening with my game.

Steps to reproduce

My game is set up with a resolution of 360 * 640, and I have the test width and test height set to the same dimensions.

The stretch mode is set to viewport, and the stretch aspect is set to expand.

When I run this function on my iPhone SE it returns (0, 0, 750, 1334).

The position of 0,0 I would expect on my personal iPhone (the iPhone SE does not have a notch), the 750, 1334 is the resolution of the phone (which would imply that the full screen width and height is safe), but when I apply this size to the control, it shifts my UI around. Items that are anchored on the right and side, and bottom are moved off screen.

This issue doesn’t happen when I run the game on PC / Mac / Linux because it renders in a 360 * 640 window.

Once again, I suspect that this is User Error and not an actual bug. I have asked in forums and discords and I have googled to no avail on how to get this function to work the way I would like it to.

Minimal reproduction project

Sample Project.zip

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

That’s because the rect returned by get_window_safe_area is in device pixel, not in viewport coordinate space. It can not be used directly for node in gdscript. I usually convert it by a scale of get_viewport_rect().size.x/OS.window_size.x That’s not convenient.

@Tekuzo Ok, I’ve done some testing myself. Seems like “viewport rect” -> “screen rect” part of the transformation isn’t incorporated into root viewport’s transform (note that I’m refering to 3.x branch, didn’t take a look at master) and this transformation is of course affected by the window stretch settings from the project settings (reference: loading settings and updating root rect). The problem is that Viewport::get_attach_to_screen_rect() isn’t exposed/binded so I think it’s not possible to obtain that transformation in GDScript (it’s possible to set it using Viewport.set_attach_to_screen_rect() or VisualServer.viewport_attach_to_screen() though). At least I didn’t find a way to do so. 🤔 As a workaround you can manually incorporate it into calculations. This should work (at least for your specific case, works for me on Android):

var window_to_root = Transform2D.IDENTITY.scaled(get_tree().root.size / OS.window_size)
var safe_area_root = window_to_root.xform(OS.get_window_safe_area())

var control = $CanvasLayer/Control

assert(control.get_viewport() == get_tree().root, "Assumption: control is not in a nested Viewport")
var parent_to_root = control.get_viewport_transform() * control.get_global_transform() * control.get_transform().affine_inverse()
var root_to_parent = parent_to_root.affine_inverse()

var safe_area_relative_to_parent = root_to_parent.xform(safe_area_root)
control.rect_position = safe_area_relative_to_parent.position
control.rect_size = safe_area_relative_to_parent.size