godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.14k stars 93 forks source link

Add window size/position/mode change signals/callbacks to `DisplayServer` #5751

Open okla opened 1 year ago

okla commented 1 year ago

Describe the project you are working on

Flight sim with a possible windowed mode

Describe the problem or limitation you are having in your project

When game is in windowed mode I need to track window position/size/mode to save and restore them on next launch. Currently, I check them for changes in _process() which is cumbersome and bad for performance

Describe the feature / enhancement and how it helps to overcome the problem or limitation

I propose to add a several signals or callbacks to DisplayServer which would be more straightforward than using _process()

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

I think it may look like signals

DisplayServer.window_size_changed.connect(func(new_size: Vector2i): print(new_size))
DisplayServer.window_position_changed.connect(func(new_position: Vector2i): print(new_position))
DisplayServer.window_mode_changed.connect(func(new_mode: DisplayServer.WindowMode): print(new_mode))

or like callbacks

DisplayServer.window_size_changed_callback(func(new_size: Vector2i): print(new_size))
DisplayServer.window_position_changed_callback(func(new_position: Vector2i): print(new_position))
DisplayServer.window_mode_changed_callback(func(new_mode: DisplayServer.WindowMode): print(new_mode))

If this enhancement will not be used often, can it be worked around with a few lines of script?

Look the described current solution

Is there a reason why this should be core and not an add-on in the asset library?

I don't think this can be done with an add-on

Mickeon commented 1 year ago

Regarding window_resized...

get_viewport().size_changed.connect()

okla commented 1 year ago

Regarding window_resized...

get_viewport().size_changed.connect()

As another workaround, yes. But window and root viewport are different things, maybe they can even have different sizes or change size independently of each other in some cases.

okla commented 1 year ago

Added a mode_changed signal/callback.

TheDuriel commented 1 year ago

But window and root viewport are different things

This is incorrect. The root viewport IS the window of the first window.

https://docs.godotengine.org/en/latest/classes/class_scenetree.html#class-scenetree-property-root

That workaround is the intended method, and for any subviews you create you then of course already have a reference to them to connect the signals from.

We could perhaps consider a better property name? SceneTree.main_window? Side by side with SceneTree.root, (which is redundant, but would clear up any confusion that might exist.)

Or maybe a DisplayServer.get_main_window() (though you can already get it through get_window_list()[0])

Sooshiko commented 1 year ago

Size is the only one that has a NOTIFICATION_WM equivalent. I would love to see one for position and fullscreen. Especially since I learned that mac let you change fullscreen by pressing a button.