From fbda1422d573c142ec18b51ea77f196f5627498b Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Wed, 2 Aug 2023 19:48:45 +0930 Subject: [PATCH] Change display scaling logic Window size in editor is now 640x360 The default 2x scale is now obtained by a hook in Common singleton, using shrink. Window will snap to integer scales (720p = 2x, 1080p = 3x, 1440p = 4x, 3200x1800 = 5x, 2160p = 6x ...) one second after last window resize --- main_menu.tscn | 1 - project.godot | 7 +++++-- scripts/loaders/common.gd | 29 +++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/main_menu.tscn b/main_menu.tscn index 4fc7d76..43a4e95 100644 --- a/main_menu.tscn +++ b/main_menu.tscn @@ -12,7 +12,6 @@ anchor_right = 1.0 anchor_bottom = 1.0 margin_right = -640.0 margin_bottom = -360.0 -rect_scale = Vector2( 2, 2 ) theme = ExtResource( 2 ) [node name="party_menu" parent="." instance=ExtResource( 5 )] diff --git a/project.godot b/project.godot index 204ea1c..e4f88e3 100644 --- a/project.godot +++ b/project.godot @@ -37,9 +37,11 @@ gdscript/warnings/integer_division=false [display] -window/size/width=1280 -window/size/height=720 +window/size/width=640 +window/size/height=360 window/dpi/allow_hidpi=true +window/stretch/shrink=2.0 +window/size/snap_to_integer=true [gui] @@ -57,5 +59,6 @@ texture={ [rendering] quality/driver/driver_name="GLES2" +2d/snapping/use_gpu_pixel_snap=true environment/default_clear_color=Color( 0, 0, 0, 1 ) environment/default_environment="res://default_env.tres" diff --git a/scripts/loaders/common.gd b/scripts/loaders/common.gd index d862607..a397cc6 100644 --- a/scripts/loaders/common.gd +++ b/scripts/loaders/common.gd @@ -1,5 +1,8 @@ extends Node +const base_resolution := Vector2(640, 360) +var shrink_timer := Timer.new() + static func load_json(filename: String) -> Dictionary: var file := File.new() var error := file.open(filename, File.READ) @@ -36,3 +39,29 @@ static func load_tsv(filename: String, delimiter: String = '\t') -> Dictionary: return {} var SNES_PSX_addresses := load_tsv('res://data/SNES_PSX_addresses.tsv') + +func shrink_to_integer(): + var size := OS.get_window_size() + var scale_vec := size / base_resolution + var scale_f := max(min(scale_vec.x, scale_vec.y), 1) + var scale := int(scale_f) + OS.window_size = base_resolution*scale + +func update_window_scale(): + if OS.window_size < base_resolution*2: + OS.window_size = base_resolution*2 + var size := OS.get_window_size() + var scale_vec := size / base_resolution + var scale_f := max(min(scale_vec.x, scale_vec.y), 1) + var scale := int(scale_f) + if ProjectSettings.get_setting('display/window/size/snap_to_integer'): + # OS.window_size = base_resolution*scale + shrink_timer.paused = false + shrink_timer.start(1) + get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_DISABLED, SceneTree.STRETCH_ASPECT_KEEP, base_resolution*scale, scale) + +func _ready(): + shrink_timer.connect('timeout', self, 'shrink_to_integer') + add_child(shrink_timer) + get_tree().connect('screen_resized', self, 'update_window_scale') + OS.set_min_window_size(base_resolution*2)