Jump to content

[1.7.10] [Solved] Custom GuiButton class not firing events?


atack

Recommended Posts

I have a class which extends GuiButton in order to allow me to draw button labels in the drawButton method.  For some reason these buttons don't seem to be firing events that are picked up by actionPerformed in the custom GuiScreen class, though normal guibuttons do.  Why might this be happening?  All buttons are in the GuiScreen buttonList but that didn't seem to make a difference.

 

Relevant custom button class:

 

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;

public class MobButton extends GuiButton
{
private boolean on;
public final String mobName;

public MobButton(int id, int x, int y, int width, int height, String mobName, boolean on) {
	super(id, x, y,"Off");
	this.width = width;
	this.height = height;
	if(on)
		this.displayString = "On";
	this.on = on;
	this.mobName = mobName;
}

public void toggle()
{
	on = !on;
	if(on)
	{
		this.displayString = "On";
	}else
	{
		this.displayString = "Off";
	}
}

@Override
public void drawButton(Minecraft theMC, int a, int b)
{
	super.drawButton(theMC, a, b);
	int nameWidth = theMC.fontRenderer.getStringWidth(mobName);
	int labelX = this.xPosition - (nameWidth + SelectionGui.NAME_PADDING);
	int labelY = this.yPosition + SelectionGui.BUTTON_HEIGHT/2 - theMC.fontRenderer.FONT_HEIGHT/2;
	this.drawString(theMC.fontRenderer, mobName, labelX, labelY, 0xFFFFFF);
}
}

 

 

Link to comment
Share on other sites

That didn't seem to help.

 

Here's the GuiScreen code.  I apologize if it's messy.

 

package com.AndrewTackett.MobFinder.client;

import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.Level;

import com.AndrewTackett.MobFinder.EntityInfo;
import com.AndrewTackett.MobFinder.MobFinder;
import com.AndrewTackett.MobFinder.client.MobPicker.ModMobs;
import com.AndrewTackett.MobFinder.client.MobPicker.mobInstanceContainer;
import com.AndrewTackett.MobFinder.client.MobButton;

import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.util.ChatComponentText;

public class SelectionGui extends GuiScreen
{
public static final int GUI_ID = 35;
public static final int BUTTON_WIDTH = 35;
public static final int BUTTON_HEIGHT = 20;
public static final int NAME_PADDING = 3;
private static final int NAVIGATION_BUTTON_WIDTH = 78;
private static final int NEXT_ID = 1;
private static final int BACK_ID = 2;
private static final int NEXT_MOD_ID = 3;
private static final int PREVIOUS_MOD_ID = 4;
private static final int STARTING_ID = 5;
private static final int MIN_Y = 50;
private static int NAME_WIDTH = MobPicker.getMaxConfigMobNameLength() * 6;//80;
private static int MIN_X = NAME_WIDTH + 5;
private static int MAX_Y;
private ModMobs currentMod;
private int buttonsPerPage = 1;
private int numPages = 1;
private int curPage = 1;
private int startID = 1;
private int endID = 1;
private List<GuiButton> navButtons = new ArrayList<GuiButton>();

public SelectionGui()
{
	currentMod = MobPicker.getCurrentMod();
	//25 pixels between y for rows
	//35 pixel wide, 20 pixel high buttons
	//length * 7 in general, but 35 for on/off?
	//id,x,y,displaystring
	//id,x,y,width,height,displaystring

	int id = STARTING_ID;

	for(ModMobs mod : MobPicker.instance.trackedMobs)
	{
		for(mobInstanceContainer mob : mod.mobs)
		{
			mob.myButton = new MobButton(id,0,0,BUTTON_WIDTH,BUTTON_HEIGHT,EntityInfo.getMobName(mob.mobClass),false);
			this.buttonList.add(mob.myButton);
			id++;
		}
	}
}

@Override
public void initGui()
{
	super.initGui();
	calculatePageValues();
	calculateStartandEndIDs();

	//navButtons.add(new GuiButton(NEXT_ID, this.width - NAVIGATION_BUTTON_WIDTH,this.height - BUTTON_HEIGHT,NAVIGATION_BUTTON_WIDTH,BUTTON_HEIGHT,"Next Page"));
	//navButtons.add(new GuiButton(BACK_ID, 0, this.height - BUTTON_HEIGHT, NAVIGATION_BUTTON_WIDTH, BUTTON_HEIGHT,"Previous Page"));
	navButtons.add(new GuiButton(NEXT_ID, (int)(this.width * 4.0/5)-NAVIGATION_BUTTON_WIDTH,this.height - (BUTTON_HEIGHT + 5),NAVIGATION_BUTTON_WIDTH,BUTTON_HEIGHT,"Next Page"));
	navButtons.add(new GuiButton(BACK_ID, this.width/5, this.height - (BUTTON_HEIGHT + 5), NAVIGATION_BUTTON_WIDTH, BUTTON_HEIGHT,"Previous Page"));
	navButtons.add(new GuiButton(NEXT_MOD_ID,(int)(this.width * 4.0/5)-NAVIGATION_BUTTON_WIDTH,5, NAVIGATION_BUTTON_WIDTH, BUTTON_HEIGHT,"Next Mod"));
	navButtons.add(new GuiButton(PREVIOUS_MOD_ID,this.width/5,5, NAVIGATION_BUTTON_WIDTH, BUTTON_HEIGHT,"Previous Mod"));
	for(GuiButton button : navButtons)
	{
		this.buttonList.add(button);
	}
	sendChatMessage("init - this.height: " + this.height + ", this.width:" + this.width);
}

public void drawScreen(int p_73863_1_, int p_73863_2_, float p_73863_3_)
    {
	setButtonPositions();

	this.drawDefaultBackground();

	//Draw buttons and their labels
	for(int i=startID;i<endID;i++)
	{
		mobInstanceContainer mob = currentMod.mobs.get(i);
		mob.myButton.drawButton(this.mc, p_73863_1_, p_73863_2_);
	}
        
        for (int k = 0; k < this.buttonList.size(); ++k)
        {
            ((GuiButton)this.buttonList.get(k)).drawButton(this.mc, p_73863_1_, p_73863_2_);
        }

	//Draw current mod name
        this.drawCenteredString(Minecraft.getMinecraft().fontRenderer, currentMod.mod, this.width/2, 10, 0xFFFFFF);

        //Draw page indicator
        this.drawCenteredString(Minecraft.getMinecraft().fontRenderer, "(" + curPage + "/" + numPages + ")", this.width/2, this.height - 20, 0xFFFFFF);
    }

private void setButtonPositions()
{
	int x = MIN_X;
	int y = MIN_Y;
	calculateStartandEndIDs();
	for(int i=startID; i< endID;i++)
	{
		mobInstanceContainer mob = currentMod.mobs.get(i);
		mob.myButton.xPosition = x;
		mob.myButton.yPosition = y;
		y += BUTTON_HEIGHT + 5;
		if((y + BUTTON_HEIGHT) > MAX_Y)
		{
			y = MIN_Y;
			x += BUTTON_WIDTH + NAME_WIDTH;
		}
	}
}

private void calculateStartandEndIDs()
{
	startID = (curPage-1) * buttonsPerPage;
	endID = curPage * buttonsPerPage;
	if(endID > currentMod.mobs.size())
		endID = currentMod.mobs.size();
}

private void calculatePageValues()
{
	MAX_Y = this.height - (BUTTON_HEIGHT + 5);
	NAME_WIDTH = MobPicker.getMaxConfigMobNameLength() * 6;
	MIN_X = NAME_WIDTH + 5;
	sendChatMessage("name_width:" + NAME_WIDTH);
	//TODO: will all mods always have a mob?
	int buttonAreaHeight = this.height - MIN_Y;
	int numButtons = currentMod.mobs.size();
	int buttonsPerColumn = buttonAreaHeight / (BUTTON_HEIGHT + 5);
	int buttonsPerRow = this.width / (BUTTON_WIDTH + NAME_WIDTH + NAME_PADDING);
	buttonsPerPage = buttonsPerColumn * buttonsPerRow;
	numPages = (int) Math.ceil(numButtons * 1.0 / buttonsPerPage);
	sendChatMessage("mobs size:" + currentMod.mobs.size());
	sendChatMessage("page values -  numbuttons:" + numButtons 
			+ ", buttonsPerCol:" + buttonsPerColumn + ", buttonsPerRow:" + buttonsPerRow
			+ ", buttonsPerPage" + buttonsPerPage + ", maxPages:" + numPages);
}

public void updateScreen() 
{

}

public void onGuiClosed() 
{

	sendChatMessage("startID:" + startID + ", endID:" + endID + ", curPage:" + curPage + ", maxPages:" +
			numPages + ", buttonsPerPage" + buttonsPerPage);
	for(int i=startID;i<endID;i++)
	{
		mobInstanceContainer mob = currentMod.mobs.get(i);
		sendChatMessage("button:" + mob.myButton.mobName + " - enabled: " + mob.myButton.enabled + ", x:" + mob.myButton.xPosition +
				", y:" + mob.myButton.yPosition);
	}
	sendChatMessage("closed - this.height: " + this.height + ", this.width:" + this.width);
}

protected void actionPerformed(GuiButton guibutton) 
{
	String message = "button " + guibutton.id + ":" + guibutton.displayString + " pressed!";
	sendChatMessage(message);
	switch(guibutton.id)
	{
		case NEXT_ID:
			if(curPage < numPages)
				curPage++;
			else
				curPage = 1;

        	calculateStartandEndIDs();
			break;
		case BACK_ID:
			if(curPage > 1)
				curPage--;
			else
				curPage = numPages;

        	calculateStartandEndIDs();
			break;
		case NEXT_MOD_ID:
        	currentMod = MobPicker.incrementModConfig();
        	curPage = 1;
        	calculatePageValues();
        	calculateStartandEndIDs();
        	break;
		case PREVIOUS_MOD_ID:
        	currentMod = MobPicker.decrementModConfig();
        	curPage = 1;
        	calculatePageValues();
        	calculateStartandEndIDs();
        	break;
        	default:
            	MobPicker.toggleMobButton(guibutton.id);
            	break;
	}
}

public void sendChatMessage(String message)
{
	EntityClientPlayerMP ep = Minecraft.getMinecraft().thePlayer;
	ep.addChatComponentMessage(new ChatComponentText(message));
	MobFinder.logger.log(Level.ALL, message);
}
}

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.