
atack
Members-
Posts
6 -
Joined
-
Last visited
Everything posted by atack
-
I have a gui that contains some custom buttons (mobbutton extends guibutton) and regular guibuttons. Whenever I resize the screen the normal guibuttons seem to render in multiple places like an old windows computer locking up. Why might this be? I've tried resetting the button positions everytime I call draw screen or in updateScreen but that doesn't seem to help. Here's my gui code: package com.AndrewTackett.MobFinder.client.gui; import java.util.ArrayList; import java.util.List; import com.AndrewTackett.MobFinder.EntityInfo; import com.AndrewTackett.MobFinder.client.ClientProxy; import com.AndrewTackett.MobFinder.client.MobPicker; import com.AndrewTackett.MobFinder.client.MobPicker.ModMobs; import com.AndrewTackett.MobFinder.client.MobPicker.mobInstanceContainer; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import net.minecraft.entity.Entity; @SideOnly(Side.CLIENT) 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.getMaxMobNameLength() * 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>(); private MobButton curButton; private String curMobName = ""; @Override public void initGui() { super.initGui(); currentMod = MobPicker.getCurrentMod(); if(MobPicker.getCurrentMob()!=null) curMobName = EntityInfo.getMobName(MobPicker.getCurrentMob().mobClass); else curMobName = ""; int id = STARTING_ID; mobInstanceContainer curMob = MobPicker.getCurrentMob(); for(ModMobs mod : MobPicker.instance.ModContainers) { for(mobInstanceContainer mob : mod.mobs) { boolean isCurrentMob = (mob == curMob); mob.myButton = new MobButton(id,0,0,BUTTON_WIDTH,BUTTON_HEIGHT,EntityInfo.getMobName(mob.mobClass),isCurrentMob); this.buttonList.add(mob.myButton); id++; } } calculatePageValues(); calculateStartandEndIDs(); 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); } ClientProxy.sendChatMessage("init - this.height: " + this.height + ", this.width:" + this.width); } @Override public void drawScreen(int p_73863_1_, int p_73863_2_, float p_73863_3_) { setButtonPositions(); setNavigationButtonPositions(); this.drawDefaultBackground(); //Draw mob toggle 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_); } //Draw navigation buttons for(int i=0;i<navButtons.size();i++) { navButtons.get(i).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 current mob name this.drawCenteredString(Minecraft.getMinecraft().fontRenderer, "Current mob: " + curMobName, this.width/2, 30, 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.getMaxMobNameLength() * 6; MIN_X = NAME_WIDTH + NAME_PADDING; ClientProxy.sendChatMessage("name_width:" + NAME_WIDTH); int buttonAreaHeight = MAX_Y - 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); } public void updateScreen() { //setNavigationButtonPositions(); } private void setNavigationButtonPositions() { for(int i=0;i<navButtons.size();i++) { GuiButton button = navButtons.get(i); switch(i) { case 0: button.xPosition = (int)(this.width * 4.0/5)-NAVIGATION_BUTTON_WIDTH; button.yPosition = this.height - (BUTTON_HEIGHT + 5); break; case 1: button.xPosition = this.width/5; button.yPosition = this.height - (BUTTON_HEIGHT + 5); break; case 2: button.xPosition = (int)(this.width * 4.0/5)-NAVIGATION_BUTTON_WIDTH; button.yPosition = 5; break; case 3: button.xPosition = this.width/5; button.yPosition = 5; } } } public void onGuiClosed() { locateLoadedMobInstances(); } private void locateLoadedMobInstances() { List<Entity> entityList = Minecraft.getMinecraft().theWorld.getLoadedEntityList(); for(Entity myEntity : entityList) { MobPicker.addMobInstance(myEntity); } } @Override protected void actionPerformed(GuiButton guibutton) { String message = "button " + guibutton.id + ":" + guibutton.displayString + " pressed!"; ClientProxy.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.incrementMod(); curPage = 1; calculatePageValues(); calculateStartandEndIDs(); break; case PREVIOUS_MOD_ID: currentMod = MobPicker.decrementMod(); curPage = 1; calculatePageValues(); calculateStartandEndIDs(); break; default: MobPicker.setCurMobInstance(guibutton.id); ((MobButton)guibutton).toggle(); if(MobPicker.getCurrentMob()!=null) { curMobName = EntityInfo.getMobName(MobPicker.getCurrentMob().mobClass); if(curButton != (MobButton)guibutton) { if(curButton != null) curButton.toggle(); curButton = (MobButton) guibutton; } } else { curMobName = ""; curButton = null; } break; } } } The rest is here if that's useful: https://github.com/andrewtackett/MobFinder/tree/master/src/main/java/com/AndrewTackett/MobFinder
-
[1.7.10] [Solved] Custom GuiButton class not firing events?
atack replied to atack's topic in Modder Support
Well, I solved my own problem. Just in case anyone else runs into this issue - the problem cleared up after I moved the creation of my buttons and adding them to the buttonList into initGui. -
[1.7.10] [Solved] Custom GuiButton class not firing events?
atack replied to atack's topic in Modder Support
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); } } -
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); } }
-
Thanks. I'll look into that. Edit: Looks like that'll work great. Thanks again.
-
I'm making a mod where I need a list of all mobs in the game and I'm trying to figure out how to get a list of them but I'm not sure how. It would be trivial if EntityRegistry exposed some of it's private members but it doesn't. I could do some complicated stuff with Reflection or repeatedly querying the EntityRegistry but that seems horribly inefficient. It seems that functionality like this for items and blocks is there, how can I similarly get all entities in the game?