Jump to content

Gui buttons and drawing in the foreground


latias1290

Recommended Posts

I have a GUI and I need button functionality, and I need to draw an icon over the button. I have two options:

1) draw a rectangle with the texture of a button, and handle the mouse clicks in the area myself. Draw my texture over the button.

2) Use GuiButton and draw the texture over the button.

 

The first option works, but I don't have click sounds and for some reason, mouseover detection doesn't work. But it does register clicks and drawing the texture over it also works.

 

The second option also works to register button clicks. It also does produce sound when I click it, and mouseover detection works. However, for some reason, the icon is drawn on a completely different offset when I draw the icon using drawTexturedModalRect() in the drawGuiContainerForegroundLayer() method. It works fine when I do it in the background method, but the icon will be drawn before the button, making it invisible.

 

Here's what I'm talking about:

 

 

Using first approach:

K4LO05h.jpg

 

Using GuiButton:

o3PL6ze.jpg

 

 

 

My code to draw the icon is this:

int i = (this.width - this.xSize) / 2;
	int j = (this.height - this.ySize) / 2;

	switch (this.te.getMode()) {
		case 0: this.drawTexturedModalRect(i + 30, j + 50, 176, 71,  20, 20);
			break;
		case 1: this.drawTexturedModalRect(i + 30, j + 50, 176, 91,  20, 20);
			break;
		case 2: this.drawTexturedModalRect(i + 30, j + 50, 176, 111, 20, 20);
			break;
	}

When I switch between approaches for testing, I just cut/paste the above code between the background and the foreground methods. Background for manual button, and foreground for GuiButton.

 

My code to create the GuiButton is this:

public void initGui() {
	super.initGui();
	//this.buttonList.add(this.modeButton = new GuiButton(0, this.guiLeft + 30, this.guiTop + 50, 20, 20, "Hi!"));
}

@Override
public void actionPerformed(GuiButton button) throws IOException {
	if (button == this.modeButton) {
		switch (te.getMode()) {
			case 0: this.te.setMode(1);
					break;
			case 1: this.te.setMode(2);
					break;
			case 2: this.te.setMode(0);
					break;
		}
	}
}

 

Should it be useful, here is the entire class I'm working on:

 

package nl.dirkkok.chemicalcraft.gui;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.ResourceLocation;
import nl.dirkkok.chemicalcraft.tileentity.ChemistryStandEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;

public class GuiChemistryStandEntity extends GuiContainer {
private static final Logger log = LogManager.getLogger();
private final ChemistryStandEntity te;
private final IInventory playerInv;
private GuiButton modeButton;

public GuiChemistryStandEntity(IInventory playerInv, ChemistryStandEntity te) {
	super(new ContainerChemistryStandEntity(playerInv, te));
	this.te = te;
	this.playerInv = playerInv;

	this.xSize = 176;
	this.ySize = 166;
}

/* GUI structure:
 * Progress bar is centered. The rest of the gui is build around it.
 * progress: (76, 29) to (100, 46)
 * first input: (54, 28) to (72, 46)
 * second input: (31, 28) to (48, 45)
 * first output: (104, 28) to (121, 45)
 * second output: (127, 28) to (144, 45)
 * residue: (104, 51) to (121, 68)
 * fuel meter: (80, 53) to (93, 66)
 * fuel: (54, 51) to (71, 68)
 * mode: (30, 50) to (49, 69)
 *
 * Textures:
 * fuel: (176, 0) to (189, 13)
 * progress: (176, 14) to (199, 30)
 * button (inactive): (176, 31) to (195, 50)
 * button (active): (176, 51) to (195, 70)
 * heat: (176, 71) to (195, 90)
 * react: (176, 91) to (195, 110)
 * filter: (176, 111) to (195, 120)
 */
@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
	GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
	mc.getTextureManager().bindTexture(new ResourceLocation("chemicalcraft:textures/gui/chemical_stand.png"));
	int i = (this.width - this.xSize) / 2;
	int j = (this.height - this.ySize) / 2;

	this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize); // GUI background
	if (mouseX >= 78 && mouseX <= 97 && mouseY >= 4 && mouseY <= 23) { // Mode button
		// Inside box
		this.drawTexturedModalRect(this.guiLeft + 30, this.guiTop + 50, 176, 51, 20, 20);
	} else {
		// Outside box
		this.drawTexturedModalRect(this.guiLeft + 30, this.guiTop + 50, 176, 31, 20, 20);
	}

	switch (this.te.getMode()) {
		case 0: this.drawTexturedModalRect(i + 30, j + 50, 176, 71,  20, 20);
			break;
		case 1: this.drawTexturedModalRect(i + 30, j + 50, 176, 91,  20, 20);
			break;
		case 2: this.drawTexturedModalRect(i + 30, j + 50, 176, 111, 20, 20);
			break;
	}

}

@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
	mc.getTextureManager().bindTexture(new ResourceLocation("chemicalcraft:textures/gui/chemical_stand.png"));
	int i = (this.width - this.xSize) / 2;
	int j = (this.height - this.ySize) / 2;

	switch (this.te.getMode()) {
		case 0: this.drawTexturedModalRect(i + 30, j + 50, 176, 71,  20, 20);
			break;
		case 1: this.drawTexturedModalRect(i + 30, j + 50, 176, 91,  20, 20);
			break;
		case 2: this.drawTexturedModalRect(i + 30, j + 50, 176, 111, 20, 20);
			break;
	}

	String s = this.te.getDisplayName().getUnformattedText();
	this.fontRendererObj.drawString(s, this.xSize / 2 - this.fontRendererObj.getStringWidth(s) / 2, 6, 0x404040);
	this.fontRendererObj.drawString(this.playerInv.getDisplayName().getUnformattedText(), 8, this.ySize - 94,
			0x404040);
}

@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
	super.drawScreen(mouseX, mouseY, partialTicks);
}

@Override
public void mouseClicked(int mouseX, int mouseY, int button) throws IOException {
	if (mouseX >= 156 && mouseX <= 175 && mouseY >= 88 && mouseY <= 107 && button == 0) { // Mode button
		switch (te.getMode()) {
			case 0: this.te.setMode(1);
					break;
			case 1: this.te.setMode(2);
					break;
			case 2: this.te.setMode(0);
					break;
		}
	}

	super.mouseClicked(mouseX, mouseY, button);
}

/*
@Override
public void initGui() {
	super.initGui();
	this.buttonList.add(this.modeButton = new GuiButton(0, this.guiLeft + 30, this.guiTop + 50, 20, 20, "Hi!"));
}

@Override
public void actionPerformed(GuiButton button) throws IOException {
	if (button == this.modeButton) {
		switch (te.getMode()) {
			case 0: this.te.setMode(1);
					break;
			case 1: this.te.setMode(2);
					break;
			case 2: this.te.setMode(0);
					break;
		}
	}
}*/
}

If I switch between approaches, I uncomment the last two methods and comment the mouseClicked() method, and move the code mentioned above between the foreground and background draw methods.

 

I have done some testing, and the i and j variables do not change whether they're calculated in the foreground or background methods.

 

 

 

I could continue using the first approach, but I think it's better to use the GuiButton.

Link to comment
Share on other sites

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.