-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpixel_perfect.gd
99 lines (67 loc) · 2.87 KB
/
pixel_perfect.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
extends Node
"""
https://gist.github.com/CowThing/2f091036457da281c546937bc8fad695
An autoload singleton for pixel perfect rendering of the game.
Set this script as an autoload.
`base_size` - Is the target size of the viewport. The viewport will scale up to fit the largest
possible integer multiple of this size within the window.
`expand` - If true the viewport will expand to the edges of the window after scaling up.
Otherwise the empty space outside of the viewport will be filled with black bars.
`pixel_perfect` - If false the viewport will still scale up, but not render pixel perfectly.
`pixel_aspect` - Used to stretch pixels to a non-square size.
Notes
----
The commented out code for black bar texture will let you set the texture of the black bars.
This is a work around until black bars can be easily edited in a project.
"""
onready var _root : Viewport = get_tree().root
# const black_bar_texture = preload("res://black_bars.png")
var base_size : = Vector2(640, 360) setget set_base_size
var expand : = false setget set_expand
var pixel_perfect : = true setget set_pixel_perfect
var pixel_aspect : = Vector2(1, 1) setget set_pixel_aspect
func _ready() -> void:
get_tree().connect("screen_resized", self, "_on_screen_resized")
# var t_id = black_bar_texture.get_rid()
# VisualServer.black_bars_set_images(t_id, t_id, t_id, t_id)
_on_screen_resized()
func set_base_size(value : Vector2) -> void:
base_size = value.floor()
_on_screen_resized()
func set_expand(value : bool) -> void:
expand = value
_on_screen_resized()
func set_pixel_perfect(value : bool) -> void:
pixel_perfect = value
_on_screen_resized()
func set_pixel_aspect(value : Vector2) -> void:
pixel_aspect = value
_on_screen_resized()
func _on_screen_resized() -> void:
var new_window_size : = OS.window_size
var total_base_size : Vector2 = base_size * pixel_aspect
var scale_width : = max(1, new_window_size.x / total_base_size.x)
var scale_height : = max(1, new_window_size.y / total_base_size.y)
var scale : = min(scale_width, scale_height) as int
var size : Vector2 = new_window_size / scale if expand else total_base_size
size = size.floor()
var offset : Vector2 = (new_window_size - size * scale) * 0.5
offset = offset.floor()
if pixel_perfect:
_root.size = size / pixel_aspect
_root.set_attach_to_screen_rect(Rect2(offset, size * scale))
_root.set_size_override(false)
_root.set_size_override_stretch(false)
else:
_root.size = (size * scale) / pixel_aspect
_root.set_attach_to_screen_rect(Rect2(offset, size * scale))
_root.set_size_override(true, size / pixel_aspect)
_root.set_size_override_stretch(true)
offset.x = max(0, offset.x)
offset.y = max(0, offset.y)
VisualServer.black_bars_set_margins(
offset.x as int,
offset.y as int,
new_window_size.x as int - (offset.x + size.x * scale) as int,
new_window_size.y as int - (offset.y + size.y * scale) as int
)