Jump to content

[1.7.10] How to change the texture of buttons in a GUI


Thornack

Recommended Posts

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);
      }
}

 

 

Link to comment
Share on other sites

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/

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • For hours I have been trying to just instal Mr. Crayfish's Refurbished Furniture Mod, but each step to fix the error codes, the more problems arise. The farthest I got through the steps was getting to the Forge Installer, but once I had selected the file, an error saying "The directory is missing a launcher profile. Please run the minecraft launcher first". At this point I don;'t know what more I can do. Please help.
    • I create my mod pack,yesterday my mod pack is fine but i add one mod and error. I'm delete this mmod but minecraft is still stop on CONFIG_LOAD then I tried to delete config and restart it but again. If you can pleace help me. https://imgur.com/ngZBzuv
    • game crashes before even opening (log:https://mclo.gs/M8xvX7c)
    • I have created a custom entity that extends "TamableAnimal", but I am wanting to have it spawn in the ocean. I have it spawning right now, but it spawns way too frequently even with weight set to 1. I am guessing it is because it is rolling in the spawn pool of land animals since TameableAnimal extends Animal and is different than WaterAnimal, and since no land animals spawn in the ocean it just fills every inch up with my custom entity. I have followed basic tutorials for spawning entities with Forge, but I feel like I am missing something about how to change what spawn pool this custom entity ends up in. Is it possible to change that or do I need to refactor it to be based off of WaterAnimal to get those spawn? My biome modifier JSON file: { "type": "forge:add_spawns", "biomes": "#minecraft:is_ocean", "spawners": { "type": "darwinsmysticalmounts:water_animal", "weight": 20, "minCount": 1, "maxCount": 1 } } My client event: event.register(ModEntityTypes.WATER_ANIMAL.get(), SpawnPlacements.Type.NO_RESTRICTIONS, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, WaterWyvernEntity::checkCustomWaterAnimalSpawnRules, SpawnPlacementRegisterEvent.Operation.REPLACE); And the actual custom spawn rule that makes it spawn in the water: public static boolean checkCustomWaterAnimalSpawnRules(EntityType<WaterAnimalEntity> pAnimal, LevelAccessor pLevel, MobSpawnType pSpawnType, BlockPos pPos, RandomSource pRandom) { return pPos.getY() > pLevel.getSeaLevel() - 16 && pLevel.getFluidState(pPos.below()).is(FluidTags.WATER); }  
    • Starting today, I am unable to load my modded minecraft world. Forge crash log initially said it was a specific mod called Doggy Talents, which I disabled. Then it blamed JEI, and when that was disabled it blamed another mod so I assume it's something more than a specific mod. Minecraft launcher log claims "Exit Code 1". Nothing had changed since last night when it was working fine Forge Log: https://pastebin.com/S1GiBGVJ Client Log: https://pastebin.com/aLwuGUNL  
  • Topics

×
×
  • Create New...

Important Information

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