Skip to content

Commit

Permalink
Switch auto UI scale to use screen DPI (#672)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Franke <[email protected]>
  • Loading branch information
Kiisu-Master and aaronfranke authored Apr 20, 2024
1 parent ed986c5 commit 7a97d94
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 27 deletions.
88 changes: 64 additions & 24 deletions src/GlobalSettings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,8 @@ func _enter_tree() -> void:
default_input_events[action] = InputMap.action_get_events(action)
load_settings()
load_user_data()
get_window().wrap_controls = true # Prevents the main window from getting too small.
get_window().dpi_changed.connect(update_ui_scale)
update_ui_scale()
get_window().size_changed.connect(update_ui_scale)
ThemeGenerator.generate_theme()


Expand Down Expand Up @@ -247,26 +246,67 @@ func get_validity_color(error_condition: bool, warning_condition := false) -> Co


func update_ui_scale() -> void:
await get_tree().process_frame
var window := get_window()
var min_size: Vector2 = window.get_contents_minimum_size()
var actual_ui_scale: float = ui_scale
if auto_ui_scale:
actual_ui_scale = _calculate_auto_scale(min_size, window.size)
min_size *= actual_ui_scale
window.min_size = min_size
window.content_scale_factor = actual_ui_scale


func _calculate_auto_scale(min_size: Vector2, size: Vector2i) -> float:
var div: Vector2 = (Vector2(size - Vector2i(10, 10)) / min_size)
var desired: float = div[div.min_axis_index()] * 0.5 * ui_scale
if desired > 4.0: return 4.0
if desired > 3.0: return 3.0
if desired > 2.5: return 2.5
if desired > 2.0: return 2.0
if desired > 1.75: return 1.75
if desired > 1.5: return 1.5
if desired > 1.25: return 1.25
if desired > 1.0: return 1.0
return 0.75
if not window.is_node_ready():
await window.ready

var usable_screen_size := Vector2(
DisplayServer.screen_get_usable_rect(DisplayServer.window_get_current_screen()).size
# Subtract window decoration size.
- (window.get_size_with_decorations() - window.size)
)

# How much can window content size be multiplied by before it extends over the usable screen size.
var diff := usable_screen_size / window.get_contents_minimum_size()
var max_scale := floorf(minf(diff.x, diff.y) * 4.0) / 4.0
var desired_scale: float = ui_scale * _calculate_auto_scale()

if not desired_scale > max_scale:
window.min_size = window.get_contents_minimum_size() * desired_scale
window.content_scale_factor = desired_scale
else:
window.min_size = usable_screen_size
window.content_scale_factor = max_scale


func _calculate_auto_scale() -> float:
if not 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
2 changes: 1 addition & 1 deletion src/ui_parts/settings_menu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func setup_setting_labels() -> void:

var auto_ui_scale := %Misc/AutoUIScale
auto_ui_scale.label.text = tr("Auto UI scale")
auto_ui_scale.tooltip_text = tr("Scale the user interface based on the window size.")
auto_ui_scale.tooltip_text = tr("Scales the user interface based on the screen size.")

%GeneralVBox/NumberPrecision.label.text = tr("Number precision digits")
%GeneralVBox/AnglePrecision.label.text = tr("Angle precision digits")
Expand Down
4 changes: 2 additions & 2 deletions src/ui_parts/settings_menu.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,11 @@ number_max = 2.5
layout_mode = 2
section_name = "other"
setting_name = "ui_scale"
values = Array[String](["0.75", "1.0", "1.25", "1.5", "1.75", "2.0"])
values = Array[String](["0.75", "1.0", "1.25", "1.5", "1.75", "2.0", "2.5", "3.0", "4.0"])
type = 3
restricted = false
number_min = 0.5
number_max = 2.5
number_max = 10.0

[node name="AutoUIScale" parent="VBoxContainer/HBoxContainer/MainPanel/ContentContainer/Other/OtherSettings/Misc" instance=ExtResource("4_2qeh2")]
layout_mode = 2
Expand Down
7 changes: 7 additions & 0 deletions translations/GodSVG.pot
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,20 @@ msgstr ""
msgid ""
"If turned on, scrolling will pan the view. To zoom, hold CTRL while "
"scrolling."
msgstr ""

msgid "UI scale"
msgstr ""

msgid "Changes the scale of the visual user interface."
msgstr ""

msgid "Auto UI scale"
msgstr ""

msgid "Scales the user interface based on the screen size."
msgstr ""

msgid "Input"
msgstr ""

Expand Down
7 changes: 7 additions & 0 deletions translations/bg.po
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ msgstr "Мащаб"
msgid "Changes the scale of the visual user interface."
msgstr ""

#, fuzzy
msgid "Auto UI scale"
msgstr "Мащаб"

msgid "Scales the user interface based on the screen size."
msgstr ""

msgid "Input"
msgstr "Входни сигнали"

Expand Down
7 changes: 7 additions & 0 deletions translations/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ msgstr "Skalieren"
msgid "Changes the scale of the visual user interface."
msgstr ""

#, fuzzy
msgid "Auto UI scale"
msgstr "Skalieren"

msgid "Scales the user interface based on the screen size."
msgstr ""

msgid "Input"
msgstr "Eingabe"

Expand Down
6 changes: 6 additions & 0 deletions translations/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ msgstr ""
msgid "Changes the scale of the visual user interface."
msgstr ""

msgid "Auto UI scale"
msgstr ""

msgid "Scales the user interface based on the screen size."
msgstr ""

msgid "Input"
msgstr ""

Expand Down
7 changes: 7 additions & 0 deletions translations/ru.po
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ msgstr "Масштаб интерфейса"
msgid "Changes the scale of the visual user interface."
msgstr "Изменить масштаб пользовательского интерфейса."

#, fuzzy
msgid "Auto UI scale"
msgstr "Масштаб"

msgid "Scales the user interface based on the screen size."
msgstr ""

msgid "Input"
msgstr "Устройства ввода"

Expand Down
7 changes: 7 additions & 0 deletions translations/uk.po
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ msgstr "Масштаб інтерфейсу"
msgid "Changes the scale of the visual user interface."
msgstr "Змінити масштаб інтерфейсу користувача."

#, fuzzy
msgid "Auto UI scale"
msgstr "Масштаб"

msgid "Scales the user interface based on the screen size."
msgstr ""

msgid "Input"
msgstr "Пристрої вводу"

Expand Down

0 comments on commit 7a97d94

Please sign in to comment.