Skip to content

Commit

Permalink
Platform UI scaling (mbrlabs#200)
Browse files Browse the repository at this point in the history
* Calculate UI auto scaling based on the OS platform

* Use default ui scale for linux or unknown OS's, Clamp with ui scalebox max and min for safety, Use both height and width to calculate appropriate min and max for ui

* Larger range for calculated ui min and max scales

* Remove uneccessary max function

* use original scaling method for linux

* update changelog

* Use default ui scale from config for smallest auto scale value on linux

* Use altered godot method for scaling ui automatically on linux
  • Loading branch information
Gurchain authored Sep 3, 2022
1 parent b7b6499 commit 5e8211d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed
- Changing the application language does not require a restart now
- Completed Spanish translation
- Improved UI auto scaling, especially for Windows and OSX

## [0.5.0] - 2022-06-12

Expand Down
24 changes: 23 additions & 1 deletion lorien/Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,31 @@ func _on_scale_changed() -> void:
var auto_scale: int = Settings.get_value(Settings.APPEARANCE_UI_SCALE_MODE, Config.DEFAULT_UI_SCALE_MODE)
var scale: float
match auto_scale:
Types.UIScale.AUTO: scale = OS.get_screen_size().x / ProjectSettings.get_setting("display/window/size/width")
Types.UIScale.AUTO: scale = _get_platform_ui_scale()
Types.UIScale.CUSTOM: scale = Settings.get_value(Settings.APPEARANCE_UI_SCALE, Config.DEFAULT_UI_SCALE)
scale = clamp(scale, _settings_dialog.get_min_ui_scale(), _settings_dialog.get_max_ui_scale())

_canvas.set_canvas_scale(scale)
get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_DISABLED, SceneTree.STRETCH_ASPECT_IGNORE, Vector2(0,0), scale)
OS.min_window_size = Config.MIN_WINDOW_SIZE * scale

# --------------------------------------------------------------------------------------------------
func _get_platform_ui_scale() -> float:
var platform: String = OS.get_name()
var scale: float
match platform:
"OSX": scale = OS.get_screen_scale()
"Windows": scale = OS.get_screen_dpi() / 96.0
_: scale = _get_general_ui_scale()
return scale

# --------------------------------------------------------------------------------------------------
func _get_general_ui_scale() -> float:
# Adapted from Godot EditorSettings::get_auto_display_scale()
# https://github.com/godotengine/godot/blob/3.x/editor/editor_settings.cpp
var smallest_dimension: int = min(OS.get_screen_size().x, OS.get_screen_size().y)
if OS.get_screen_dpi() >= 192 && smallest_dimension >= 1400:
return Config.DEFAULT_UI_SCALE * 2
elif smallest_dimension >= 1700:
return Config.DEFAULT_UI_SCALE * 1.5
return Config.DEFAULT_UI_SCALE
15 changes: 12 additions & 3 deletions lorien/UI/Dialogs/SettingsDialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,18 @@ func _set_languages(current_locale: String) -> void:

#--------------------------------------------------------------------------------------------------
func _set_UIScale_range():
var screen_scale: float = OS.get_screen_size().x / ProjectSettings.get_setting("display/window/size/width")
_ui_scale.min_value = screen_scale / 2
_ui_scale.max_value = screen_scale + 1.5
var screen_scale_max: float = (OS.get_screen_size().x * OS.get_screen_size().y) / (ProjectSettings.get_setting("display/window/size/width") * ProjectSettings.get_setting("display/window/size/height"))
var screen_scale_min: float = OS.get_screen_size().x / ProjectSettings.get_setting("display/window/size/width")
_ui_scale.min_value = max(screen_scale_min / 2, 0.5)
_ui_scale.max_value = screen_scale_max + 1.5

#--------------------------------------------------------------------------------------------------
func get_max_ui_scale() -> float:
return _ui_scale.max_value

#--------------------------------------------------------------------------------------------------
func get_min_ui_scale() -> float:
return _ui_scale.min_value

# -------------------------------------------------------------------------------------------------
func _on_DefaultBrushSize_value_changed(value: int) -> void:
Expand Down

0 comments on commit 5e8211d

Please sign in to comment.