Is there a window resize event / signal ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sparks
:warning: Old Version Published before Godot 3 was released.

Hi,

is there a window resize event or signal from the engine?
For example: You want to draw a rectangle as a console background:

draw_rectangle(0, 0, window_width, some_height, ...)

However, you don’t need this to be updated every frame, but when a window resize happens. Something like:

func _on_resize():  # this does not exist
>> update() 

Or an alternative approach: Can you somehow force a GUI node to stretch with the window automatically?

Thanks!

:bust_in_silhouette: Reply From: YeOldeDM

Control nodes have a resized() signal which emits when the Control’s rect changes size.

Can you somehow force a GUI node to stretch with the window automatically? Yes, using Anchors and/or Size Flags.

Can you help me out with an example?

Let’s take the panel node. I want it to be as wide as the window. When the window resizes, so does the panel. The height does not matter.

How do I setup anchors and size flags?

sparks | 2017-04-12 19:37

In the project settings → display change stretch_mode to viewport and stretch_aspect to keep_width.
it should do exactly what you want.

bobokox | 2017-04-13 02:21

Yea, but in my case I don’t need the aspect ratio nor the resolution to stay the same. So it is intended that you can actually see more of the game when you resize the window.

I still don’t know how to use these anchors and size flags. Please help!

sparks | 2017-04-13 11:36

:bust_in_silhouette: Reply From: avencherus

As already mentioned by @bobokox, you can do this easily do this with stretch mode in the project settings. It should only take a minute of fiddling with the check boxes and resizing your windows to see what they do.

The details on the mode choices are here: http://docs.godotengine.org/en/stable/tutorials/engine/multiple_resolutions.html#stretch-settings

The only non-obvious things are 2D/Viewport options. 2D is what you would expect, and viewport takes the resulting image of the viewport and scales that. So a 3D scene could become pixelated or squashed.

The other options are between distorting the image to fill the stretch, or preserve the ratio using black bars. The first let’s it decide which black bar to us, the other two are a choice between forcing a vertical or horizontal black bar.

If you still would like to some kind of code version, you can get the root viewport, and use it’s resize signal to alert you when the window is being resized.

extends Node2D

func _ready():
	get_tree().get_root().connect("size_changed", self, "myfunc")
	
func myfunc():
	print("Resizing: ", get_viewport_rect().size)

The code is perfect! Thanks!

sparks | 2017-04-13 16:39