Jump to content

Create a GuiScreen just before the MainMenu, take an Input via GuiTextField, and return to the main Menu


Frontear

Recommended Posts

Effectively, here is the code I've currently been working with:

 

Main Class:

@SubscribeEvent
public void onGuiScreen(GuiOpenEvent event) {
  InputMenu menu;
  if (event.gui instanceof GuiMainMenu) {
    menu = new InputMenu(event.gui);
    event.gui = menu;
  }
}

 

InputMenu Class:

public class InputMenu extends GuiScreen {
  private GuiTextField inputField;
  private GuiScreen previousScreen;
  public InputMenu(GuiScreen previousScreen) {
    this.previousScreen = previousScreen; // save the main menu to return to later
  }
  
  @Override
  public void initGui() {
    inputField = new GuiTextField(-1, fontRendererObj ...);
  }
  
  @Override
  public void drawScreen(int mouseX, int mouseY, float partialTicks) {
    inputField.drawTextBox();
    inputField.setFocused(true);
  }
  
  @Override
  public void updateScreen() {
    inputField.updateCursorCounter();
  }
  
  @Override
  protected void keyTyped(char typedChar, int keyCode) throws IOException {
    if (keyCode == Keyboard.KEY_RETURN) {
      // go back to menu
      HandleInput();
      mc.displayGuiScreen(previousScreen); // doesn't work
    }
    
    inputField.textboxKeyTyped(typedChar, keyCode);
  }
}

This isn't returning to the main menu, but is instead staying on the InputMenu screen, and only resetting the textField to empty characters upon pressing enter. I can't get it to return, even though I have set the mc.displayGuiScreen correctly. What might be the issue?

 

EDIT: SOLVED

The issue was that GuiOpenEvent got called when I tried to go back to the MainMenu, which, according to my old conditionals, would just display the InputMenu again. Adding a boolean fixed this, by checking if the user has already seen this menu before

Edited by Frontear

I am a human and this action was performed manually. Please contact Frontear if you have any questions or concerns.

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.

Announcements



×
×
  • Create New...

Important Information

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