From 2f80c27af6ce76b1d49e53f473f8bb6727989d24 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 1 Oct 2023 23:40:48 +0200 Subject: [PATCH] WE SCALING --- View.gd | 23 ++++++++++++++ bottle/bottle.tscn | 4 +-- game/game.tscn | 5 +-- interface/SafeUI.gd | 33 ++++++++++++++++++++ interface/game_container.tscn | 15 --------- interface/game_ui.gd | 2 +- interface/game_ui.tscn | 50 +++++++++++++++--------------- project.godot | 1 - root.tscn | 58 ++++++++++++++++++++++------------- 9 files changed, 125 insertions(+), 66 deletions(-) create mode 100644 View.gd create mode 100644 interface/SafeUI.gd delete mode 100644 interface/game_container.tscn diff --git a/View.gd b/View.gd new file mode 100644 index 0000000..263bea4 --- /dev/null +++ b/View.gd @@ -0,0 +1,23 @@ +extends Control +class_name View + + +@onready var window: Window = get_window() +@onready var viewport: Viewport = window.get_viewport() + + +func _ready(): + viewport.size_changed.connect(_on_viewport_size_changed) + + +func _on_viewport_size_changed(): + var window_size: Vector2i = DisplayServer.window_get_size() + print("[View] Window size is: ", window_size) + var scaling_factor + if window_size.x < window_size.y: + scaling_factor = window_size.x / 270 + else: + scaling_factor = window_size.y / 480 + print("[View] Scaling factor is: ", scaling_factor) + get_window().set_content_scale_factor(scaling_factor) + $SafeUI.set_safe_margins(scaling_factor) diff --git a/bottle/bottle.tscn b/bottle/bottle.tscn index 83d5ffe..0589b47 100644 --- a/bottle/bottle.tscn +++ b/bottle/bottle.tscn @@ -15,11 +15,11 @@ sources/0 = SubResource("TileSetAtlasSource_midqx") tile_set = SubResource("TileSet_tr7be") collision_visibility_mode = 1 format = 2 -layer_0/tile_data = PackedInt32Array(-1638406, 0, 0, -1638407, 0, 0, -1638408, 0, 0, -1572872, 0, 0, -1507336, 0, 0, -1441800, 0, 0, -1376264, 0, 0, -1310728, 0, 0, -1245192, 0, 0, -393224, 0, 0, -327688, 0, 0, -262152, 0, 0, -458760, 0, 0, -524296, 0, 0, -589832, 0, 0, -655368, 0, 0, -720904, 0, 0, -786440, 0, 0, -851976, 0, 0, -917512, 0, 0, -983048, 0, 0, -1048584, 0, 0, -1114120, 0, 0, -1179656, 0, 0, -196616, 0, 0, -131080, 0, 0, -65544, 0, 0, -8, 0, 0, -7, 0, 0, -4, 0, 0, -3, 0, 0, -2, 0, 0, -1, 0, 0, -5, 0, 0, -6, 0, 0, -65536, 0, 0, -65535, 0, 0, -65534, 0, 0, -65533, 0, 0, -65532, 0, 0, -65531, 0, 0, -65530, 0, 0, -65529, 0, 0, -131065, 0, 0, -196601, 0, 0, -262137, 0, 0, -327673, 0, 0, -393209, 0, 0, -458745, 0, 0, -524281, 0, 0, -589817, 0, 0, -655353, 0, 0, -720889, 0, 0, -786425, 0, 0, -851961, 0, 0, -917497, 0, 0, -983033, 0, 0, -1048569, 0, 0, -1114105, 0, 0, -1441785, 0, 0, -1507321, 0, 0, -1376249, 0, 0, -1310713, 0, 0, -1245177, 0, 0, -1179641, 0, 0, -1572857, 0, 0, -1638393, 0, 0, -1703929, 0, 0, -1703930, 0, 0, -1703931, 0, 0, -1638405, 0, 0, -1638404, 0, 0, -1703940, 0, 0, -1769476, 0, 0, -1835012, 0, 0, -1900548, 0, 0, -1703932, 0, 0, -1703933, 0, 0, -1769469, 0, 0, -1835005, 0, 0, -1900541, 0, 0, -1966077, 0, 0) +layer_0/tile_data = PackedInt32Array(-1376264, 0, 0, -1310728, 0, 0, -1245192, 0, 0, -393224, 0, 0, -327688, 0, 0, -262152, 0, 0, -458760, 0, 0, -524296, 0, 0, -589832, 0, 0, -655368, 0, 0, -720904, 0, 0, -786440, 0, 0, -851976, 0, 0, -917512, 0, 0, -983048, 0, 0, -1048584, 0, 0, -1114120, 0, 0, -1179656, 0, 0, -196616, 0, 0, -131080, 0, 0, -65544, 0, 0, -8, 0, 0, -7, 0, 0, -4, 0, 0, -3, 0, 0, -2, 0, 0, -1, 0, 0, -5, 0, 0, -6, 0, 0, -65536, 0, 0, -65535, 0, 0, -65534, 0, 0, -65533, 0, 0, -65532, 0, 0, -65531, 0, 0, -65530, 0, 0, -65529, 0, 0, -131065, 0, 0, -196601, 0, 0, -262137, 0, 0, -327673, 0, 0, -393209, 0, 0, -458745, 0, 0, -524281, 0, 0, -589817, 0, 0, -655353, 0, 0, -720889, 0, 0, -786425, 0, 0, -851961, 0, 0, -917497, 0, 0, -983033, 0, 0, -1048569, 0, 0, -1114105, 0, 0, -1441785, 0, 0, -1376249, 0, 0, -1310713, 0, 0, -1245177, 0, 0, -1179641, 0, 0, -1638404, 0, 0, -1703933, 0, 0, -1572868, 0, 0, -1507332, 0, 0, -1441796, 0, 0, -1376260, 0, 0, -1376261, 0, 0, -1376262, 0, 0, -1376263, 0, 0, -1638397, 0, 0, -1572861, 0, 0, -1507325, 0, 0, -1441789, 0, 0, -1441788, 0, 0, -1441787, 0, 0, -1441786, 0, 0) [node name="StaticBody2D" type="StaticBody2D" parent="."] collision_layer = 3 collision_mask = 0 [node name="Poly" type="CollisionPolygon2D" parent="StaticBody2D"] -polygon = PackedVector2Array(-320, -576, -320, 192, 320, 192, 320, -576, 48, -576, 48, -400, 112, -400, 112, -16, -112, -16, -112, -400, -48, -400, -48, -576) +polygon = PackedVector2Array(-320, -576, -320, 192, 320, 192, 320, -576, 48, -576, 48, -336, 112, -336, 112, -16, -112, -16, -112, -336, -48, -336, -48, -576) diff --git a/game/game.tscn b/game/game.tscn index 6b12a48..81f9092 100644 --- a/game/game.tscn +++ b/game/game.tscn @@ -12,6 +12,7 @@ texture_filter = 1 script = ExtResource("1_i3ly0") [node name="Bottle" parent="." instance=ExtResource("1_y7o2l")] +position = Vector2(0, 240) [node name="GravityFromGyro" parent="Bottle" instance=ExtResource("2_h2pfr")] @@ -20,7 +21,7 @@ z_index = 10 position = Vector2(2, -314) [node name="TimeSpawner" parent="." instance=ExtResource("3_qwsty")] -position = Vector2(0, -480) +position = Vector2(0, -144) scene = ExtResource("4_e5nwi") buffer_cap = 1 spawn_position_range_x = 32.0 @@ -31,7 +32,7 @@ overlapping_body_count_limit = 4 [node name="Timer" type="Timer" parent="TimeSpawner"] [node name="ButtonSpawner" parent="." instance=ExtResource("3_qwsty")] -position = Vector2(0, -480) +position = Vector2(0, -144) scene = ExtResource("4_e5nwi") buffer_cap = 50 spawn_position_range_x = 32.0 diff --git a/interface/SafeUI.gd b/interface/SafeUI.gd new file mode 100644 index 0000000..9ffca1e --- /dev/null +++ b/interface/SafeUI.gd @@ -0,0 +1,33 @@ +extends MarginContainer +class_name SafeUI + + +func set_safe_margins(scaling_factor: float): + var cutouts: Array[Rect2] = DisplayServer.get_display_cutouts() + + var left + var right + var top + var bottom + + if len(cutouts) == 0: + left = 8 + right = 8 + top = 8 + bottom = 8 + else: + var screen: Vector2i = DisplayServer.screen_get_size() + var rect: Rect2i = DisplayServer.get_display_safe_area() + left = rect.position.x + right = screen.x - rect.end.x + top = rect.position.y + bottom = screen.y - rect.end.y + + print("[SafeUI] Left margin is: ", left) + print("[SafeUI] Right margin is: ", right) + print("[SafeUI] Top margin is: ", top) + print("[SafeUI] Bottom margin is: ", bottom) + add_theme_constant_override("margin_left", left / scaling_factor) + add_theme_constant_override("margin_right", right / scaling_factor) + add_theme_constant_override("margin_top", top / scaling_factor) + add_theme_constant_override("margin_bottom", bottom / scaling_factor) diff --git a/interface/game_container.tscn b/interface/game_container.tscn deleted file mode 100644 index fb1b98c..0000000 --- a/interface/game_container.tscn +++ /dev/null @@ -1,15 +0,0 @@ -[gd_scene load_steps=2 format=3 uid="uid://c0uce7ssweke"] - -[ext_resource type="PackedScene" uid="uid://c3rxmcwa5nqng" path="res://game/game.tscn" id="1_dbqdv"] - -[node name="GameContainer" type="Control"] -layout_mode = 3 -anchors_preset = 7 -anchor_left = 0.5 -anchor_top = 1.0 -anchor_right = 0.5 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 0 - -[node name="Game" parent="." instance=ExtResource("1_dbqdv")] diff --git a/interface/game_ui.gd b/interface/game_ui.gd index 5717b98..1bafa22 100644 --- a/interface/game_ui.gd +++ b/interface/game_ui.gd @@ -1,4 +1,4 @@ -extends VBoxContainer +extends Control class_name GameUI ## Emitted when the Score button is pressed. diff --git a/interface/game_ui.tscn b/interface/game_ui.tscn index 13b44fb..939a206 100644 --- a/interface/game_ui.tscn +++ b/interface/game_ui.tscn @@ -3,7 +3,8 @@ [ext_resource type="Theme" uid="uid://ba5utvfhnxa5i" path="res://interface/interface_theme.tres" id="1_ppf8y"] [ext_resource type="Script" path="res://interface/game_ui.gd" id="2_33nqa"] -[node name="GameUI" type="VBoxContainer"] +[node name="GameUI" type="Control"] +layout_mode = 3 anchors_preset = 15 anchor_right = 1.0 anchor_bottom = 1.0 @@ -12,40 +13,41 @@ grow_vertical = 2 theme = ExtResource("1_ppf8y") script = ExtResource("2_33nqa") -[node name="UpperButtons" type="HBoxContainer" parent="."] -custom_minimum_size = Vector2(0, 54) -layout_mode = 2 -size_flags_vertical = 8 -alignment = 1 - -[node name="ScoreButton" type="Button" parent="UpperButtons"] +[node name="ScoreButton" type="Button" parent="."] custom_minimum_size = Vector2(72, 0) -layout_mode = 2 +layout_mode = 1 +offset_right = 72.0 +offset_bottom = 54.0 size_flags_horizontal = 0 text = "$0" alignment = 0 -[node name="UpperButtonsSpacerLeft" type="Control" parent="UpperButtons"] -layout_mode = 2 -size_flags_horizontal = 3 - -[node name="SpawnButton" type="Button" parent="UpperButtons"] +[node name="SpawnButton" type="Button" parent="."] custom_minimum_size = Vector2(72, 0) -layout_mode = 2 +layout_mode = 1 +anchors_preset = 5 +anchor_left = 0.5 +anchor_right = 0.5 +offset_left = -36.0 +offset_right = 36.0 +offset_bottom = 54.0 +grow_horizontal = 2 size_flags_horizontal = 4 text = "Drop" -[node name="UpperButtonsSpacerRight" type="Control" parent="UpperButtons"] -layout_mode = 2 -size_flags_horizontal = 3 - -[node name="ShopButton" type="Button" parent="UpperButtons"] +[node name="ShopButton" type="Button" parent="."] custom_minimum_size = Vector2(72, 0) -layout_mode = 2 +layout_mode = 1 +anchors_preset = 1 +anchor_left = 1.0 +anchor_right = 1.0 +offset_left = -72.0 +offset_bottom = 54.0 +grow_horizontal = 0 size_flags_horizontal = 8 text = "Shop" alignment = 2 -[connection signal="pressed" from="UpperButtons/ScoreButton" to="." method="_on_score_button_pressed"] -[connection signal="pressed" from="UpperButtons/SpawnButton" to="." method="_on_spawn_button_pressed"] -[connection signal="pressed" from="UpperButtons/ShopButton" to="." method="_on_shop_button_pressed"] +[connection signal="pressed" from="ScoreButton" to="." method="_on_score_button_pressed"] +[connection signal="pressed" from="SpawnButton" to="." method="_on_spawn_button_pressed"] +[connection signal="pressed" from="ShopButton" to="." method="_on_shop_button_pressed"] diff --git a/project.godot b/project.godot index 81c5e97..a3a1f88 100644 --- a/project.godot +++ b/project.godot @@ -22,7 +22,6 @@ config/icon="res://icon.svg" window/size/viewport_width=270 window/size/viewport_height=480 window/size/mode=2 -window/stretch/mode="viewport" window/stretch/aspect="keep_height" window/handheld/orientation=1 diff --git a/root.tscn b/root.tscn index 46ff088..8712d88 100644 --- a/root.tscn +++ b/root.tscn @@ -1,15 +1,17 @@ -[gd_scene load_steps=6 format=3 uid="uid://cbccs6kwwf265"] +[gd_scene load_steps=8 format=3 uid="uid://cbccs6kwwf265"] [ext_resource type="Script" path="res://root.gd" id="1_8jrhk"] [ext_resource type="Theme" uid="uid://ba5utvfhnxa5i" path="res://interface/interface_theme.tres" id="1_h26ax"] -[ext_resource type="PackedScene" uid="uid://c3rxmcwa5nqng" path="res://game/game.tscn" id="3_hrdyr"] +[ext_resource type="Script" path="res://View.gd" id="3_1ki2g"] +[ext_resource type="PackedScene" uid="uid://c3rxmcwa5nqng" path="res://game/game.tscn" id="3_8gkpc"] [ext_resource type="PackedScene" uid="uid://cklkdygv61bny" path="res://interface/shop_ui.tscn" id="3_lvefk"] +[ext_resource type="Script" path="res://interface/SafeUI.gd" id="4_grf0c"] [ext_resource type="PackedScene" uid="uid://bo5unrhqpoyim" path="res://interface/game_ui.tscn" id="5_2cc15"] [node name="Root" type="Node"] script = ExtResource("1_8jrhk") -[node name="UI" type="Control" parent="."] +[node name="View" type="Control" parent="."] texture_filter = 1 layout_mode = 3 anchors_preset = 15 @@ -19,33 +21,47 @@ grow_horizontal = 2 grow_vertical = 2 size_flags_vertical = 8 theme = ExtResource("1_h26ax") +script = ExtResource("3_1ki2g") -[node name="GameContainer" type="Control" parent="UI"] +[node name="GameWI" type="CenterContainer" parent="View"] process_mode = 1 layout_mode = 1 -anchors_preset = 7 -anchor_left = 0.5 -anchor_top = 1.0 -anchor_right = 0.5 +anchors_preset = 15 +anchor_right = 1.0 anchor_bottom = 1.0 grow_horizontal = 2 -grow_vertical = 0 +grow_vertical = 2 +size_flags_horizontal = 3 +size_flags_vertical = 3 +metadata/_edit_vertical_guides_ = [] -[node name="Game" parent="UI/GameContainer" instance=ExtResource("3_hrdyr")] +[node name="GameWorld" type="Control" parent="View/GameWI"] +layout_mode = 2 -[node name="GameUI" parent="UI" instance=ExtResource("5_2cc15")] -process_mode = 3 +[node name="Game" parent="View/GameWI/GameWorld" instance=ExtResource("3_8gkpc")] + +[node name="SafeUI" type="MarginContainer" parent="View"] layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("4_grf0c") -[node name="ShopUI" parent="UI" instance=ExtResource("3_lvefk")] +[node name="GameUI" parent="View/SafeUI" instance=ExtResource("5_2cc15")] +process_mode = 3 +layout_mode = 2 + +[node name="ShopUI" parent="View/SafeUI" instance=ExtResource("3_lvefk")] process_mode = 3 visible = false -layout_mode = 1 +layout_mode = 2 -[connection signal="score_button_pressed" from="UI/GameUI" to="." method="_on_game_ui_score_button_pressed"] -[connection signal="shop_button_pressed" from="UI/GameUI" to="." method="_on_game_ui_shop_button_pressed"] -[connection signal="spawn_button_pressed" from="UI/GameUI" to="." method="_on_game_ui_spawn_button_pressed"] -[connection signal="back_button_pressed" from="UI/ShopUI" to="." method="_on_shop_ui_back_button_pressed"] -[connection signal="delete_button_pressed" from="UI/ShopUI" to="." method="_on_shop_ui_delete_button_pressed"] -[connection signal="purchase_begin" from="UI/ShopUI" to="." method="_on_shop_ui_purchase_begin"] -[connection signal="score_button_pressed" from="UI/ShopUI" to="." method="_on_shop_ui_score_button_pressed"] +[connection signal="score_button_pressed" from="View/SafeUI/GameUI" to="." method="_on_game_ui_score_button_pressed"] +[connection signal="shop_button_pressed" from="View/SafeUI/GameUI" to="." method="_on_game_ui_shop_button_pressed"] +[connection signal="spawn_button_pressed" from="View/SafeUI/GameUI" to="." method="_on_game_ui_spawn_button_pressed"] +[connection signal="back_button_pressed" from="View/SafeUI/ShopUI" to="." method="_on_shop_ui_back_button_pressed"] +[connection signal="delete_button_pressed" from="View/SafeUI/ShopUI" to="." method="_on_shop_ui_delete_button_pressed"] +[connection signal="purchase_begin" from="View/SafeUI/ShopUI" to="." method="_on_shop_ui_purchase_begin"] +[connection signal="score_button_pressed" from="View/SafeUI/ShopUI" to="." method="_on_shop_ui_score_button_pressed"]