Jump to content

[1.12.2] GuiTextField wont "regain" focus


Mekelaina

Recommended Posts

So, I'm working on GUI building for my mod and I have an issue with what will become my search bar. If I don't hard set it focused, it just won't work. Even when I set it to be able to lose and regain focus, it doesn't. Also when I do set it focused, hitting "e" closes the menu. How can I fix these two problems?

 

Here is my GUI code (sorry it's a tad messy)

 

The ModGuiElement is just a failed attempt at a wrapper class for all of the different ints related to a single texture (when it goes on the screen, where its located in the image file, etc). It was supposed to simplify things but it kinda just made it more clunky

package com.mekelaina.duelcraft.gui;

import java.io.IOException;

import org.lwjgl.input.Keyboard;

import com.mekelaina.duelcraft.container.ContainerBinder;
import com.mekelaina.duelcraft.util.Reference;
import com.mekelaina.duelcraft.util.handlers.ExpandableItemStackHandler;

import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.items.ItemStackHandler;

public class GuiBinder extends GuiContainer 
{

	private static ResourceLocation BINDER_GUI_1 = new ResourceLocation(Reference.MOD_ID, "textures/gui/binder_gui1.png");
	private static ResourceLocation BINDER_GUI_2 = new ResourceLocation(Reference.MOD_ID, "textures/gui/binder_gui2.png");
	private static ResourceLocation BINDER_GUI_3 = new ResourceLocation(Reference.MOD_ID, "textures/gui/binder_gui3.png");
	
	private static ModGuiElement MAIN_ON = new ModGuiElement(BINDER_GUI_1, 0, 0, 0, 0, 252, 198);
	private static ModGuiElement MAIN_OFF = new ModGuiElement(BINDER_GUI_2, 0, 0, 0, 0, 252, 198);
	private static ModGuiElement MAIN_FILTER = new ModGuiElement(BINDER_GUI_3, 0, 0, 0, 0, 252, 198);
	private static ModGuiElement BACK_40 = new ModGuiElement(BINDER_GUI_1, 148, 220, 18, 18);
	private static ModGuiElement BACK_60 = new ModGuiElement(BINDER_GUI_1, 166, 220, 18, 18);
	private static ModGuiElement BACK_EXTRA = new ModGuiElement(BINDER_GUI_1, 148, 238, 18, 18);
	private static ModGuiElement BACK_SIDE = new ModGuiElement(BINDER_GUI_1, 166, 238, 18, 18);
	private static ModGuiElement LOCKED_NORMAL = new ModGuiElement(BINDER_GUI_1, 216, 208, 16, 16);
	private static ModGuiElement LOCKED_HOVER = new ModGuiElement(BINDER_GUI_1, 200, 208, 16, 16);
	//private static ModGuiElement LOCKED_CLICK = new ModGuiElement(BINDER_GUI_1, 184, 208, 16, 16);
	//private static ModGuiElement UNLOCKED_CLICK = new ModGuiElement(BINDER_GUI_1, 184, 224, 16, 16);
	private static ModGuiElement UNLOCKED_HOVER = new ModGuiElement(BINDER_GUI_1, 200, 224, 16, 16);
	private static ModGuiElement UNLOCKED_NORMAL = new ModGuiElement(BINDER_GUI_1, 216, 224, 16, 16);
	//private static ModGuiElement FILTER_CLICK = new ModGuiElement(BINDER_GUI_1, 184, 240, 16, 16);
	private static ModGuiElement FILTER_HOVER = new ModGuiElement(BINDER_GUI_1, 200, 240, 16, 16);
	private static ModGuiElement FILTER_NORMAL = new ModGuiElement(BINDER_GUI_1, 216, 240, 16, 16);
	private static ModGuiElement SCROLL_NORMAL = new ModGuiElement(BINDER_GUI_1, 232, 241, 12, 15);
	private static ModGuiElement SCROLL_CLICK = new ModGuiElement(BINDER_GUI_1, 244, 241, 12, 15);
	private static GuiTextField searchBinder;
	
	private static final int FILTER_ID = 0;
	private static final int  LOCK_ID = 1;
	private static final int BINDER_SEARCH = 2;
	private int refX;
	private int refY;
	
	private ItemStackHandler binderInv;
	private ItemStackHandler deckboxInv;
	private ItemStack binder;
	private InventoryPlayer player;
	//private ContainerBinder container;
	
	private boolean deckboxInserted;
	private boolean locked;
	private boolean inFilter;
	private int deckboxScrollstate;
	
	
	public GuiBinder(InventoryPlayer player, ItemStackHandler binderInv, ItemStack binder) 
	{
		super(new ContainerBinder(player, binderInv, binderInv.getSlots()));
		this.player = player;
		this.binderInv = binderInv;
		this.binder = binder;
		this.deckboxInserted = false;
		this.locked = false;
		this.inFilter = false;
		
	}

	@Override
	public void initGui()
	{
		refX = (width - MAIN_ON.getWidth()) / 2;
		refY = (height - MAIN_ON.getHeight()) / 4;
		this.buttonList.add(new CustomButton(FILTER_ID, refX + 118, refY + 18, 
				FILTER_NORMAL.getWidth(), FILTER_NORMAL.getHeight(), FILTER_NORMAL.getImage(),
				FILTER_NORMAL.getTextureX(), FILTER_NORMAL.getTextureY(), FILTER_NORMAL.getTextureX() - 16,
				FILTER_NORMAL.getTextureY()));
		
		searchBinder = new GuiTextField(BINDER_SEARCH, mc.fontRenderer, refX + 156, refY + 5, 70, mc.fontRenderer.FONT_HEIGHT);
		//searchBinder.setEnableBackgroundDrawing(false);
		searchBinder.setCanLoseFocus(true);
		searchBinder.setEnabled(true);
		
		Keyboard.enableRepeatEvents(true);
	}
	
	@Override
	protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) 
	{
		
		
		
		
		
		
	}
	
	@Override
	public void updateScreen() 
	{
		searchBinder.updateCursorCounter();
		
		super.updateScreen();
	}
	
	@Override
	public void drawScreen(int mouseX, int mouseY, float partialTicks)
	{
		GlStateManager.color(1, 1, 1, 1);
		
		if(inFilter)
		{
			mc.getTextureManager().bindTexture(MAIN_FILTER.getImage());
		}
		else
		{
			if(deckboxInserted)
			{
				mc.getTextureManager().bindTexture(MAIN_ON.getImage());
			}
			else
			{
				mc.getTextureManager().bindTexture(MAIN_OFF.getImage());
			}
		}
		
		xSize = MAIN_OFF.getWidth();
		ySize = MAIN_OFF.getHeight();
		int x = refX;
		int y = refY;
		drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
		
	
		
		
		searchBinder.drawTextBox();
		
		super.drawScreen(mouseX, mouseY, partialTicks);
	}
	
	@Override
	protected void keyTyped(char typedChar, int keyCode) throws IOException 
	{
		searchBinder.textboxKeyTyped(typedChar, keyCode);
		
		super.keyTyped(typedChar, keyCode);
	}
	
	@Override
	protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
	{
		/*final int refX = (width - MAIN_ON.getWidth()) / 2;
		final int refY = (height - MAIN_ON.getHeight()) / 4;
		mc.getTextureManager().bindTexture(FILTER_NORMAL.getImage());
		xSize = FILTER_NORMAL.getWidth();
		ySize = FILTER_NORMAL.getHeight();
		int x = refX + FILTER_NORMAL.getX1();
		int y = refY + FILTER_NORMAL.getY1();*/
		
		//drawTexturedModalRect(FILTER_NORMAL.getX1(), FILTER_NORMAL.getY1(), x, y, FILTER_NORMAL.getWidth(), FILTER_NORMAL.getHeight());
	}
	
	private void updateComponents()
	{
		
	}

}

 

Link to comment
Share on other sites

@diesieben07 Alright, I adjusted my keyTyped method to this, but the problem of focusing still persists.

 

	@Override
	protected void keyTyped(char typedChar, int keyCode) throws IOException 
	{
		if(searchBinder.textboxKeyTyped(typedChar, keyCode))
		{
			super.keyTyped(typedChar, keyCode);
		}
	}

 

When I open the GUI, the text field just won't focus unless I manually set it focused in code. The vanilla code for whether it should focus (if you click on it or not) isn't working, or (more likely) is being prevented from working somehow. With the updated code, if I manually set the focus, the escape key no longer closes the GUI. the inventory key still does.

Link to comment
Share on other sites

@V0idWa1k3r ok... I realize that mistake, and it seems to have fixed the problem of not being able to type with whatever key is set as the inventory key, but the focusing part is still broken. If I manually (in code) set it focused it works fine, but it doesn't seem to be registering the mouse clicking even with canLoseFocus being set to true.

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.