Jump to content

Recommended Posts

Posted

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()
	{
		
	}

}

 

Posted

@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.

Posted
6 minutes ago, Mekelaina said:

if(searchBinder.textboxKeyTyped(typedChar, keyCode))

 

12 hours ago, diesieben07 said:

So only call super (i.e. perform the normal inventory functions for that key) when the textbox did not handle the key (it returned false).

 

Posted

@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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • This is the last line before the crash: [ebwizardry]: Synchronising spell emitters for PixelTraveler But I have no idea what this means
    • What in particular? I barely used that mod this time around, and it's never been a problem in the past.
    • Im trying to build my mod using shade since i use the luaj library however i keep getting this error Reason: Task ':reobfJar' uses this output of task ':shadowJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. So i try adding reobfJar.dependsOn shadowJar  Could not get unknown property 'reobfJar' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. my gradle file plugins { id 'eclipse' id 'idea' id 'maven-publish' id 'net.minecraftforge.gradle' version '[6.0,6.2)' id 'com.github.johnrengelman.shadow' version '7.1.2' id 'org.spongepowered.mixin' version '0.7.+' } apply plugin: 'net.minecraftforge.gradle' apply plugin: 'org.spongepowered.mixin' apply plugin: 'com.github.johnrengelman.shadow' version = mod_version group = mod_group_id base { archivesName = mod_id } // Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. java.toolchain.languageVersion = JavaLanguageVersion.of(17) //jarJar.enable() println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" minecraft { mappings channel: mapping_channel, version: mapping_version copyIdeResources = true runs { configureEach { workingDirectory project.file('run') property 'forge.logging.markers', 'REGISTRIES' property 'forge.logging.console.level', 'debug' arg "-mixin.config=derp.mixin.json" mods { "${mod_id}" { source sourceSets.main } } } client { // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. property 'forge.enabledGameTestNamespaces', mod_id } server { property 'forge.enabledGameTestNamespaces', mod_id args '--nogui' } gameTestServer { property 'forge.enabledGameTestNamespaces', mod_id } data { workingDirectory project.file('run-data') args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') } } } sourceSets.main.resources { srcDir 'src/generated/resources' } repositories { flatDir { dirs './libs' } maven { url = "https://jitpack.io" } } configurations { shade implementation.extendsFrom shade } dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation 'org.luaj:luaj-jse-3.0.2' implementation fg.deobf("com.github.Virtuoel:Pehkui:${pehkui_version}") annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' minecraftLibrary 'luaj:luaj-jse:3.0.2' shade 'luaj:luaj-jse:3.0.2' } // Example for how to get properties into the manifest for reading at runtime. tasks.named('jar', Jar).configure { manifest { attributes([ 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors, 'Specification-Version' : '1', // We are version 1 of ourselves 'Implementation-Title' : project.name, 'Implementation-Version' : project.jar.archiveVersion, 'Implementation-Vendor' : mod_authors, 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), "TweakClass" : "org.spongepowered.asm.launch.MixinTweaker", "TweakOrder" : 0, "MixinConfigs" : "derp.mixin.json" ]) } rename 'mixin.refmap.json', 'derp.mixin-refmap.json' } shadowJar { archiveClassifier = '' configurations = [project.configurations.shade] finalizedBy 'reobfShadowJar' } assemble.dependsOn shadowJar reobf { re shadowJar {} } publishing { publications { mavenJava(MavenPublication) { artifact jar } } repositories { maven { url "file://${project.projectDir}/mcmodsrepo" } } }  
    • All versions of Minecraft Forge suddenly black screen even without mods (tried reinstalling original Minecraft, Java, updating drivers doesn't work)
  • Topics

×
×
  • Create New...

Important Information

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