Remember last location for GUIs

This is the place to request new mods or give ideas about what could be done.
Post Reply
jdedmo
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sat Oct 08, 2022 1:30 pm
Contact:

Remember last location for GUIs

Post by jdedmo »

I am just looking to see if anyone has seen mods that will simply remember where GUI Windows were last and keep them there when they are reopened.

I found the defines.events.on_gui_opened and defines.events.on_gui_closed. However I do not know enough about Lua to quickly read and set the positional data on open/close event hooks.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1651
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Remember last location for GUIs

Post by Pi-C »

jdedmo wrote:
Sat Oct 08, 2022 2:02 pm
I am just looking to see if anyone has seen mods that will simply remember where GUI Windows were last and keep them there when they are reopened.
One way to achieve that would be to construct the main frame just once:

Code: Select all

local function create_gui(player)  
  local   gui = player.gui.screen
  if not gui["your-gui-name"] then
    gui = gui.add({
      type = "frame",
      name = "your-gui-name",
      direction = "vertical",
      visible = true, 
      autocenter = true
    })
    AD.created_msg(gui)
  end
end
This is the container for your GUI, you can place other frames, flows, tables, buttons, textfields etc. inside this container. On creation, it will be placed prominently in the center of the screen. When you disable the GUI, you don't destroy it, but just hide it:

Code: Select all

local function show_or_hide(gui)
if gui and gui.valid then
  gui.visible = not gui.visible
  gui.autocenter = false

  -- Optional:
  if not gui.visible then
    -- Remove all child elements of the main frame
    gui.clear()
  
  else
    -- Rebuild child elements
    populate(gui)
  end
end
This will always keep your GUI's main frame alive and just toggle between visible and invisible state. While the GUI was centered when it was created, autocentering will be turned off (and remain so) the first time the player hides the GUI. Thus, it will always remember the last position it has been moved to by the player.

Whether or not you want to add the optional part depends on the nature of your GUI. If the child elements never change, there's no need to destroy/rebuild the GUI each time it's toggled. However, if you expect the contents to change a lot (e.g. labels or buttons linked to specific entities), gui.clear() and a complete rebuild of the inner GUI may be safer than keeping track of and updating the individual elements.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

jdedmo
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sat Oct 08, 2022 1:30 pm
Contact:

Re: Remember last location for GUIs

Post by jdedmo »

Ok thanks for the info.

I think this mod will be a bit out of my scope for now need to look into Lua bit more.
Pi-C wrote:
Sat Oct 08, 2022 6:24 pm
One way to achieve that would be to construct the main frame just once:
Are the base GUIs (player inventory, boxes, smelters, etc) able to be moved?
Pi-C wrote:
Sat Oct 08, 2022 6:24 pm
expect the contents to change a lot
If they are not able to be moved then yes I would have to make a custom inventory which I do not have the skills to do (if even possible) at the moment lol

Thanks again for the help :D

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Remember last location for GUIs

Post by Deadlock989 »

jdedmo wrote:
Sat Oct 08, 2022 2:02 pm
I am just looking to see if anyone has seen mods that will simply remember where GUI Windows were last and keep them there when they are reopened.

I found the defines.events.on_gui_opened and defines.events.on_gui_closed. However I do not know enough about Lua to quickly read and set the positional data on open/close event hooks.
I use functions like this one in the on_gui_closed and on_gui_location_changed events:

Code: Select all

-- on_gui_location_changed
function CONTROL.on_gui_location_changed(event)
	if event.element.name == CONTROL.transmat_gui then
		CONTROL.set_player_global(event.player_index, "transmat_gui_location", event.element.location)
	end
end

-- when creating or opening a gui
...
	if CONTROL.get_player_global(player.index,"transmat_gui_location") then
		frame.location = CONTROL.get_player_global(player.index,"transmat_gui_location")
	else
		frame.force_auto_center()
	end
...
It is important to keep cached data like this in the global table to avoid desyncs.

I have only tried doing this for custom GUIs made from scratch. However I think you could probably do something like cache GUIs per entity type and so forth, although I am not sure off the top of my head how much data about the GUI element gets passed in when it is a game engine GUI e.g. opening an assembling machine.
Image

Pi-C
Smart Inserter
Smart Inserter
Posts: 1651
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Remember last location for GUIs

Post by Pi-C »

jdedmo wrote:
Sat Oct 08, 2022 8:06 pm
Are the base GUIs (player inventory, boxes, smelters, etc) able to be moved?
I don't think so. You could close them, however: player.opened is your friend here.
Pi-C wrote:
Sat Oct 08, 2022 6:24 pm
expect the contents to change a lot
If they are not able to be moved then yes I would have to make a custom inventory which I do not have the skills to do (if even possible) at the moment lol
I think the "choose-elem-button" (see here) is the element you are looking for: "A button that lets the player pick from a certain kind of prototype, with optional filtering. Relevant event: on_gui_elem_changed".
Thanks again for the help :D
You're welcome! If you want to mess around with GUIs, you should study this guide. The main part walks you through building a mod that constructs a simple GUI and makes it usable. This way, you learn not only how to create GUI elements, but also how a mod works, how you can react to events.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Ideas and Requests For Mods”