From f67b481178b0a671670f0113080d62d88732062f Mon Sep 17 00:00:00 2001 From: Anish Mishra Date: Sat, 1 Feb 2025 14:06:38 +0530 Subject: [PATCH] Fix UI scaling --- src/autoload/HandlerGUI.gd | 93 +++++++++---------------------- src/portrait_ui/editor_scene.tscn | 8 ++- src/ui_parts/code_editor.tscn | 3 +- src/ui_parts/display.tscn | 2 +- src/ui_parts/inspector.tscn | 3 +- 5 files changed, 35 insertions(+), 74 deletions(-) diff --git a/src/autoload/HandlerGUI.gd b/src/autoload/HandlerGUI.gd index 9cef765..a8c6516 100644 --- a/src/autoload/HandlerGUI.gd +++ b/src/autoload/HandlerGUI.gd @@ -259,36 +259,12 @@ func update_ui_scale() -> void: var window := get_window() if not window.is_node_ready(): await window.ready - var old_scale_factor := window.content_scale_factor - - # Get window size without the decorations. - var usable_screen_size := Vector2i(DisplayServer.screen_get_usable_rect( - DisplayServer.window_get_current_screen()).size -\ - window.get_size_with_decorations() + window.size) - - # Presumably the default size would always be enough for the contents. - var window_default_size := Vector2i( - ProjectSettings.get_setting("display/window/size/viewport_width"), - ProjectSettings.get_setting("display/window/size/viewport_height")) - - # How much can the default size be increased before it takes all usable screen space. - var max_expansion := Vector2(usable_screen_size) / Vector2(window_default_size) - var max_scale := snappedf(minf(max_expansion.x, max_expansion.y) - 0.125, 0.25) - if OS.get_name() == "Android": - # This is a temporary fix for smaller UI scale on Android. - # TODO Update this logic after moving to Godot 4.4 - max_scale *= 1.1 - var final_scale := minf(Configs.savedata.ui_scale * _calculate_auto_scale(), max_scale) - var resize_factor := final_scale / old_scale_factor - - if not OS.get_name() in ["Android", "Web"]: - # TODO Check later if this workaround is still necessary for Windows. - if OS.get_name() != "Windows" or window.mode == Window.MODE_WINDOWED: - # The window's minimum size can mess with the size change, so we set it to zero. - window.min_size = Vector2i.ZERO - window.size *= resize_factor - window.min_size = window_default_size * final_scale - window.content_scale_factor = final_scale + if Configs.savedata.auto_ui_scale: + window.content_scale_factor = _calculate_auto_scale() + else: + var final_scale := minf(Configs.savedata.ui_scale, 4.0) + window.content_scale_factor = final_scale + func open_update_checker() -> void: var confirmation_dialog = ConfirmDialog.instantiate() @@ -325,44 +301,25 @@ func open_export() -> void: Translator.translate("Export"), FileUtils.open_export_dialog.bind(svg_export_data)) func _calculate_auto_scale() -> float: - if not Configs.savedata.auto_ui_scale: - return 1.0 - - # Credit: Godots (MIT, by MakovWait and contributors) - - var screen := DisplayServer.window_get_current_screen() - if DisplayServer.screen_get_size(screen) == Vector2i(): - return 1.0 - - # Use the smallest dimension to use a correct display scale on portrait displays. - var smallest_dimension := mini(DisplayServer.screen_get_size(screen).x, - DisplayServer.screen_get_size(screen).y) - - var dpi := DisplayServer.screen_get_dpi(screen) - if dpi != 72: - if dpi < 72: - return 0.75 - elif dpi <= 96: - return 1.0 - elif dpi <= 120: - return 1.25 - elif dpi <= 160: - return 1.5 - elif dpi <= 200: - return 2.0 - elif dpi <= 240: - return 2.5 - elif dpi <= 320: - return 3.0 - elif dpi <= 480: - return 4.0 - else: # dpi > 480 - return 5.0 - elif smallest_dimension >= 1700: - # Likely a hiDPI display, but we aren't certain due to the returned DPI. - # Use an intermediate scale to handle this situation. - return 1.5 - return 1.0 + var dpi := DisplayServer.screen_get_dpi(DisplayServer.window_get_current_screen()) + + if dpi <= 120: + return 0.75 # ldpi + elif dpi <= 160: + return 1.0 # mdpi (baseline) + elif dpi <= 240: + return 1.5 # hdpi + elif dpi <= 320: + return 2.0 # xhdpi + elif dpi <= 480: + return 3.0 # xxhdpi + elif dpi <= 640: + return 4.0 # xxxhdpi + else: + return 5.0 # Beyond xxxhdpi + + return 1.0 # Default fallback scale + # Helpers diff --git a/src/portrait_ui/editor_scene.tscn b/src/portrait_ui/editor_scene.tscn index 514a919..2f09cab 100644 --- a/src/portrait_ui/editor_scene.tscn +++ b/src/portrait_ui/editor_scene.tscn @@ -18,7 +18,7 @@ theme_override_constants/separation = 0 script = ExtResource("1_o7lif") [node name="PanelContainer" type="PanelContainer" parent="."] -custom_minimum_size = Vector2(360, 0) +custom_minimum_size = Vector2(300, 0) layout_mode = 2 size_flags_horizontal = 3 size_flags_vertical = 3 @@ -26,8 +26,7 @@ size_flags_vertical = 3 [node name="HSplitContainer" type="VSplitContainer" parent="PanelContainer"] layout_mode = 2 size_flags_horizontal = 3 -theme_override_constants/separation = 40 -theme_override_constants/minimum_grab_thickness = 30 +theme_override_constants/separation = 30 theme_override_styles/split_bar_background = SubResource("StyleBoxFlat_mt61i") [node name="TabContainer" type="TabContainer" parent="PanelContainer/HSplitContainer"] @@ -50,3 +49,6 @@ metadata/_tab_index = 1 unique_name_in_owner = true layout_mode = 2 size_flags_vertical = 3 + +[node name="MarginContainer" type="MarginContainer" parent="."] +layout_mode = 2 diff --git a/src/ui_parts/code_editor.tscn b/src/ui_parts/code_editor.tscn index ce4563a..4a46ba6 100644 --- a/src/ui_parts/code_editor.tscn +++ b/src/ui_parts/code_editor.tscn @@ -34,6 +34,7 @@ corner_radius_bottom_right = 5 corner_radius_bottom_left = 5 [node name="CodeEditor" type="VBoxContainer"] +custom_minimum_size = Vector2(0, 90) theme_override_constants/separation = 0 script = ExtResource("1_nffk0") @@ -99,7 +100,7 @@ size_flags_vertical = 3 theme_override_constants/separation = -2 [node name="SVGCodeEdit" type="TextEdit" parent="ScriptEditor"] -custom_minimum_size = Vector2(0, 96) +custom_minimum_size = Vector2(0, 80) layout_mode = 2 size_flags_horizontal = 3 size_flags_vertical = 3 diff --git a/src/ui_parts/display.tscn b/src/ui_parts/display.tscn index 9b76c51..839bb09 100644 --- a/src/ui_parts/display.tscn +++ b/src/ui_parts/display.tscn @@ -39,7 +39,7 @@ layout_mode = 2 size_flags_vertical = 3 [node name="ViewportContainer" type="SubViewportContainer" parent="ViewportPanel"] -custom_minimum_size = Vector2(450, 0) +custom_minimum_size = Vector2(300, 0) layout_mode = 2 size_flags_vertical = 3 stretch = true diff --git a/src/ui_parts/inspector.tscn b/src/ui_parts/inspector.tscn index 2963f45..cdf16e1 100644 --- a/src/ui_parts/inspector.tscn +++ b/src/ui_parts/inspector.tscn @@ -7,6 +7,7 @@ [ext_resource type="Script" uid="uid://27atmrvxgbjt" path="res://src/ui_parts/move_to_overlay.gd" id="5_otlmf"] [node name="Inspector" type="Container"] +custom_minimum_size = Vector2(0, 90) anchors_preset = 15 anchor_right = 1.0 anchor_bottom = 1.0 @@ -34,7 +35,7 @@ theme_override_constants/h_separation = 4 icon = ExtResource("3_vo6hf") [node name="ElementContainer" type="Control" parent="."] -custom_minimum_size = Vector2(0, 240) +custom_minimum_size = Vector2(0, 80) layout_mode = 2 size_flags_vertical = 3 script = ExtResource("3_qeptj")