aboutsummaryrefslogtreecommitdiff
path: root/client/gui/components/smart_margin_container.gd
blob: 046dc6f32b81c6ceb61edb9567f63f04b743700f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Hurry Curry! - a game about cooking
# Copyright (C) 2025 Hurry Curry! contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
extends MarginContainer
class_name SmartMarginContainer
# A smart margin container which automatically adjusts the margin such that it doesn't cover notch on phones

func _ready() -> void:
	var os := OS.get_name()
	if os == "iOS" or os == "Android":
		update_margins()

func update_margins() -> void:
	var screen_safe_rect := Rect2(DisplayServer.get_display_safe_area())
	var viewport_transform := get_viewport().get_final_transform()
	var viewport_safe_rect := screen_safe_rect * viewport_transform.affine_inverse()
	var viewport_full_rect := get_viewport().get_visible_rect()
	var left := viewport_safe_rect.position.x - viewport_full_rect.position.x
	var top := viewport_safe_rect.position.y - viewport_full_rect.position.y
	var right := viewport_full_rect.end.x - viewport_safe_rect.end.x
	var bottom := viewport_full_rect.end.y - viewport_safe_rect.end.y
	# print("OLD: ", get_theme_constant("margin_left"))
	begin_bulk_theme_override()
	add_theme_constant_override('margin_left', roundi(left) + get_theme_constant("margin_left"))
	add_theme_constant_override('margin_top', roundi(top) + get_theme_constant("margin_top"))
	add_theme_constant_override('margin_right', roundi(right) + get_theme_constant("margin_right"))
	add_theme_constant_override('margin_bottom', roundi(bottom) + get_theme_constant("margin_bottom"))
	end_bulk_theme_override()
	# print("NEW: ", get_theme_constant("margin_left"))