Jump to content

[1.12.2] "Stacking" GUIs on top of each other?


Kinniken

Recommended Posts

Hi all,

 

I have two GUIs, one that extends GuiContainer to handle trade in my mod, and one with some user help that extends GuiScreen directly. There's a help button on the trade one that opens the other one, using Minecraft.getMinecraft().displayGuiScreen().

 

It works fine, except that opening the help closes the other one. I'd prefer it if the player was taken back to the other one instead. Is this possible "natively", or do I need to do it manually by reopening the first GUI with whatever context it had?

 

Thanks

Link to comment
Share on other sites

I can reuse the same GuiScreen object? Convenient, cool.

 

Is there any callback for when a GUI is closed? I can't seem to find any at least for when the GUI is closed by using the escape key. The only solution I can see would be to override keyTyped() and reopen the older gui if I detect that the key pressed was escape, but that feels very hacky...

Link to comment
Share on other sites

2 minutes ago, Kinniken said:

I can reuse the same GuiScreen object? Convenient, cool.

 

Is there any callback for when a GUI is closed? I can't seem to find any at least for when the GUI is closed by using the escape key. The only solution I can see would be to override keyTyped() and reopen the older gui if I detect that the key pressed was escape, but that feels very hacky...

GuiScreen#onGuiClosed() is probably what you want here.  It's actually called whenever the current GUI object (if already non-null) changes, which is generally by Minecraft#displayGuiScreen() - note that a change from non-null to null also counts here.

 

So when you create your sub-GUI, store a reference to Minecraft.currentScreen in your sub-GUI constructor, and in your sub-GUI's onGuiClosed() override, call Minecraft#displayGuiScreen(parentGuiReference) to redisplay the parent GUI.  Note that you're not actually reusing the GuiScreen object - the sub-GUI is a separate object which exists at the same time as the parent GUI.

 

If you need to pass data from the sub-GUI back to the parent GUI, store a reference to the sub-GUI in the parent when you create it (most likely in actionPerformed() when the player clicks a button).  The parent's initGui() method should check for a non-null sub-GUI field and use that to extract any data that was entered by the player.  The important point being that the parent GUI object still exists while the sub-GUI is being shown (because the sub-GUI holds a reference to it), so you're redisplaying an existing GUI object, not a new one.  And the sub-GUI object still exists after it's closed (at least until the parent closes), because the parent GUI holds a reference to it.

 

  • Thanks 1
Link to comment
Share on other sites

onGuiClosed() looked like what I wanted... except that it's called from within displayGuiScreen() before the new GUI is set, so if I call displayGuiScreen() from within it my resetting of currentScreen gets overiden by the parent call.

 

I don"t like the solution much but I went with putting my code in an overriden keyTyped(), that works fine.

 

Thanks for the help.

Link to comment
Share on other sites

Oh and on this:

"Note that you're not actually reusing the GuiScreen object - the sub-GUI is a separate object which exists at the same time as the parent GUI." 

 

Sorry, I wasn't clear, I meant that I'm reusing the GuiScreen object from my original parent screen display to display it again once the second screen is closed. Between the parent screen and the child one it's not possible of course, they are not even instances of the same class in my case.

Link to comment
Share on other sites

5 minutes ago, Kinniken said:

onGuiClosed() looked like what I wanted... except that it's called from within displayGuiScreen() before the new GUI is set, so if I call displayGuiScreen() from within it my resetting of currentScreen gets overiden by the parent call.

 

I don"t like the solution much but I went with putting my code in an overriden keyTyped(), that works fine.

 

Thanks for the help.

Actually, looking at the GUI code from PneumaticCraft, that's what we're doing too :)  You're right that it's not very elegant, but it does work.

 

4 minutes ago, Kinniken said:

 

Sorry, I wasn't clear, I meant that I'm reusing the GuiScreen object from my original parent screen display to display it again once the second screen is closed. Between the parent screen and the child one it's not possible of course, they are not even instances of the same class in my case.

Right, yes - that's perfectly OK to do.  You can keep a GUI object around for a while by holding a reference to it even though it's not the current GUI.

Edited by desht
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.