Jump to content

Recommended Posts

Posted

I know how to add click-able buttons, but how do you change the texture of buttons in a GUI?

 

heres a gui class that adds some buttons that have custom widths

 

 

@SideOnly(Side.CLIENT)
public class GuiBattleMenu extends GuiScreen
{
	    
    private int BUTTON_DONE_ID = 1;
    private int BUTTON_CANCEL_ID = 2;
    private int BUTTON_MORE_ID = 3;

    //private int updateCounter;
    private GuiButton doneBtn;
    private GuiButton moreBtn;
    private GuiButton cancelBtn;
  

    //whether initGui() was called.
    private boolean wasInitialized = false;

    public  GuiBattleMenu(){
        
    }

    /**
     * Adds the buttons (and other controls) to the screen in question.
     */
    @Override
    public void initGui()
    {
    	this.buttonList.clear();
        this.doneBtn = new GuiButton(BUTTON_DONE_ID, 20, this.height - 40, I18n.format("Apply", new Object[0]));
        this.doneBtn.width = 160;
        this.cancelBtn = new GuiButton(BUTTON_CANCEL_ID, this.width - 190, this.height - 40, I18n.format("Cancel", new Object[0]));
        this.cancelBtn.width = 160;
        this.moreBtn = new GuiButton(BUTTON_MORE_ID, this.width - 120, 5, I18n.format("More Options ...", new Object[0]));
        this.moreBtn.width = 100;
        
        this.buttonList.add(this.doneBtn);
        this.buttonList.add(this.cancelBtn);
        this.buttonList.add(this.moreBtn);
        
        //it seems like initGui() is called every time this.mc.displayGuiScreen() is used.
    	if(this.wasInitialized)
        	return;
        this.wasInitialized = true;
       
    }
    
    @Override
public boolean doesGuiPauseGame(){
	return false;
}

    /**
     * Called when the screen is unloaded. Used to disable keyboard repeat events
     */
    @Override
    public void onGuiClosed(){
    	
    }

    /**
     * Called from the main game loop to update the screen.
     */
    @Override
    public void updateScreen(){
         }

    @Override
    protected void actionPerformed(GuiButton button){
        if (button.enabled){
            if (button.id == BUTTON_DONE_ID){
            	
                this.mc.displayGuiScreen((GuiScreen)null);	//closes the GUI 
            }
            else if(button.id == BUTTON_CANCEL_ID){
            	this.mc.displayGuiScreen((GuiScreen)null);
            }
            else if(button.id == BUTTON_MORE_ID){
            	
            }
        }
    }
    
    @Override
    protected void mouseClicked(int x, int y, int event){
        if(event != 0){
            super.mouseClicked(x, y, event);
        }
    }
    
    @Override
    protected void mouseMovedOrUp(int mouseX, int mouseY, int event){
        if (event != 0){
            super.mouseMovedOrUp(mouseX, mouseY, event);
        }
    }
    
    @Override
protected void keyTyped(char key, int event){
    	super.keyTyped(key, event);
    	}

    /**
     * Draws the screen and all the components in it.
     */
    @Override
    public void drawScreen(int mouseX, int mouseY, float p_73863_3_){
       
        super.drawScreen(mouseX, mouseY, p_73863_3_);
        this.drawCenteredString(this.fontRendererObj, "Totals:", this.width / 2 -90, this.height - 60, 16777215);
      }
}

 

 

Posted

You need a class that extends GuiScreen, then you can override the draw method in there.

 

You mean GuiButton, right?

 

He is already extending GuiScreen, but the drawScreen() method there actually usually isn't used for the buttons since they have their own drawButton() method that is called automatically if the button is in the GuiScreen's button list.

 

Here is an example of the method from one of my mods.:

      /**

        * Draws this button to the screen.

        */

        @Override

        public void drawButton(Minecraft mc, int parX, int parY)

        {

            if (visible)

            {

                boolean isButtonPressed = (parX >= xPosition

                      && parY >= yPosition

                      && parX < xPosition + width

                      && parY < yPosition + height);

                GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

                mc.getTextureManager().bindTexture(bookPageTextures[1]);

                int textureX = 0;

                int textureY = 192;

 

                if (isButtonPressed)

                {

                    textureX += 23;

                }

 

                if (!isNextButton)

                {

                    textureY += 13;

                }

 

                drawTexturedModalRect(xPosition, yPosition,

                      textureX, textureY,

                      23, 13);

            }

        }

    }

 

The key is the drawTexturedModalRect() method which is taking part of a texture.  In this case I take a different part of the texture based on whether the button is pressed or not.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.