Skip to content

Commit

Permalink
Allow window resizing and handle it
Browse files Browse the repository at this point in the history
  • Loading branch information
qrrk committed Feb 25, 2022
1 parent 66101ff commit fc893a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ WindowGeometry="*res://scripts/window_geometry.gd"

window/size/width=600
window/size/height=700
window/size/resizable=false
window/size/borderless=true
window/size/test_width=1
window/size/test_height=1
window/dpi/allow_hidpi=true
window/per_pixel_transparency/allowed=true
window/per_pixel_transparency/enabled=true
window/stretch/mode="2d"
window/stretch/aspect="expand"

[gdnative]

Expand Down
20 changes: 15 additions & 5 deletions scripts/window_geometry.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ extends Node
# Thanks to github.com/Lauson1ex for helping me figure this out.

var scale: float setget _set_scale
var min_base_size := Vector2(
ProjectSettings.get("display/window/size/width"),
ProjectSettings.get("display/window/size/height"))
var base_size := min_base_size

onready var _settings = $"/root/SettingsManager"
onready var _settings := $"/root/SettingsManager"
onready var _viewport := get_tree().get_root()


func _set_scale(new_scale: float) -> void:
Expand All @@ -22,13 +27,11 @@ func _apply_scale() -> void:
# This is a workaround for weird font aliasing that shows up when the scale
# is a simple rational fraction, like 125% (5/4) or 150% (3/2).

var base_size := Vector2(
ProjectSettings.get("display/window/size/width"),
ProjectSettings.get("display/window/size/height"))

OS.min_window_size = min_base_size * final_scale
OS.set_window_size(base_size * final_scale)



func calculate_scale_from_dpi() -> float:

var ratio = OS.get_screen_dpi() / 96.0
Expand Down Expand Up @@ -58,5 +61,12 @@ func _ready():

_apply_scale()
OS.center_window()
_viewport.connect("size_changed", self, "_on_window_resized")
_on_SceneTree_idle()


func _on_window_resized() -> void:

var new_size := OS.window_size / scale
_viewport.set_size_override(true, new_size)
base_size = new_size

0 comments on commit fc893a6

Please sign in to comment.