Page 1 of 1

New LuaGuiElement: window

Posted: Tue Jun 22, 2021 12:24 pm
by andrei
TL;DR
Allow mods to create windows that behave similarly to built-in game windows.
What ?
A window can be added only to `player.gui.screen` GUI root. A window behaves similarly to built-in game windows like inventory or entity details. In particular:
  1. It has a close button in the top-right corner.
  2. It is automatically closed when another window is opened, e.g. when the player presses “E”.
  3. It is closed if the player presses “Escape”.
This can be implemented as a new LuaGuiElement type (“window”) or by adding certain flags to the “frame” element.
Why ?
Many mods need to create windows, this is a popular request: viewtopic.php?t=44378, viewtopic.php?t=29876. It was partially fixed in version 0.17.59 when `LuaGui::screen` was added. The suggested solution was to add a “frame” GUI element to the “screen” root in order to create a window. This does solve part of the problem: the created window is draggable and in many respects behaves as one would expect. However it doesn't have the properties mentioned above:
  1. Close button needs to be added manually and it's quite tiresome (if you want it you basically cannot use “caption” to set window title and need to re-implement the title bar by hand).
  2. Closing the window when another window is opened also needs to be done manually.
  3. Overriding “Escape” is outright impossible if I understand correctly.
Would be nice if we could have an out-of-the-box solution for windows that just works.
Update
PFQNiet pointed out that 2. and 3. can be both implemented by setting `player.opened = frame`, i.e. it is possible to fully replicate behavior of built-in windows, it just needs some work.

Re: New LuaGuiElement: window

Posted: Tue Jun 22, 2021 1:32 pm
by PFQNiet
Point 1 is resolved by using a decent framework, or making your own - it's a fairly trivial matter to define a function, say "create_window", and let that handle creating the close button etc.

Points 2 and 3 are resolved by setting "player.opened = window". The player can only have one GUI "opened" at a time, so opening another will first close the open one (firing gui_closed event as expected) and E and Escape both close it too.

I don't think this suggestion is needed, personally.

Re: New LuaGuiElement: window

Posted: Tue Jun 22, 2021 1:59 pm
by andrei
Hmm, I didn't know the `player.opened = ...` trick. Thanks! Updated the post.

I still think that it makes sense to have a built-in window class because it's more future-proof: if Factorio decides to add new windows management features (tiling, snapping, collapsing, etc.) they would be supported by all mods immediately. But I agree that it is a weaker argument.

Re: New LuaGuiElement: window

Posted: Tue Jun 22, 2021 2:42 pm
by DaveMcW
Here is a reference implementation for 1. 98713