-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Found and implemented viewport resize script
- Loading branch information
1 parent
c2924a8
commit b3906e0
Showing
7 changed files
with
63 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Script from chanon and sysharm | ||
# https://github.com/godotengine/godot/issues/6506 | ||
|
||
extends Node | ||
|
||
# don't forget to use stretch mode 'viewport' and aspect 'ignore' | ||
onready var viewport = get_viewport() | ||
|
||
func _ready(): | ||
get_tree().connect("screen_resized", self, "_screen_resized") | ||
|
||
func _screen_resized(): | ||
var window_size = OS.get_window_size() | ||
|
||
# see how big the window is compared to the viewport size | ||
# floor it so we only get round numbers (0, 1, 2, 3 ...) | ||
var scale_x = floor(window_size.x / viewport.size.x) | ||
var scale_y = floor(window_size.y / viewport.size.y) | ||
|
||
# use the smaller scale with 1x minimum scale | ||
var scale = max(1, min(scale_x, scale_y)) | ||
|
||
# find the coordinate we will use to center the viewport inside the window | ||
var diff = window_size - (viewport.size * scale) | ||
var diffhalf = (diff * 0.5).floor() | ||
|
||
# attach the viewport to the rect we calculated | ||
viewport.set_attach_to_screen_rect(Rect2(diffhalf, viewport.size * scale)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Script from chanon and sysharm | ||
# https://github.com/godotengine/godot/issues/6506 | ||
|
||
extends Node | ||
|
||
# don't forget to use stretch mode 'viewport' and aspect 'ignore' | ||
onready var viewport = get_viewport() | ||
|
||
func _ready(): | ||
get_tree().connect("screen_resized", self, "_screen_resized") | ||
|
||
func _screen_resized(): | ||
var window_size = OS.get_window_size() | ||
|
||
var scale_x = window_size.x / viewport.size.x | ||
var scale_y = window_size.y / viewport.size.y | ||
|
||
var scale = min(scale_x, scale_y) | ||
|
||
# find the coordinate we will use to center the viewport inside the window | ||
var diff = window_size - (viewport.size * scale) | ||
var diffhalf = (diff * 0.5) | ||
|
||
# attach the viewport to the rect we calculated | ||
viewport.set_attach_to_screen_rect(Rect2(diffhalf, viewport.size * scale)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters