Jump to content

[1.12.2] [Solved] Problems with GuiButtonImage()


Spacejet

Recommended Posts

I wanted to have a different kind of button with different states and different images as the texture depending on the state. So, I decided to extend GuiButtonImage() and create class that just checks and sets the state and manages the texture. It is kind of unnecessary but I am going to use the same button with the same textures over and over again so decides to make the class. Also, I think I might have to override drawButton() anyways to update the textures on my button depending on the state.

 

Problems:

1) Texture does not load properly.

My Button class-

Spoiler

package com.spacejet.quantivity.gui.buttons;

import com.spacejet.quantivity.util.Reference;

import net.minecraft.client.gui.GuiButtonImage;
import net.minecraft.util.ResourceLocation;

public class ActiveDeactiveButton extends GuiButtonImage
{
    private static int state;
    private static final ResourceLocation TEXTURE = new ResourceLocation(Reference.MOD_ID + GetTextureLocationOnState(state));
    
    public ActiveDeactiveButton(int buttonID, int xPos, int yPos, int width, int height, int xTexStart, int yTexStart, int yDiffText, int state) 
    {
        super(buttonID, xPos, yPos, width, height, xTexStart, yTexStart, yDiffText, TEXTURE);
        ActiveDeactiveButton.state = state;
    }

    private static String GetTextureLocationOnState(int i) 
    {
        switch(i)
        {
            case 0:
                return ":textures/gui/buttons/active_deactive_redstone_disabled.png";
                
            case 1:
                return ":textures/gui/buttons/active_deactive_redstone_on.png";
                
            case 2:
                return ":textures/gui/buttons/active_deactive_redstone_off.png";
                
            default:
                return ":textures/gui/buttons/active_deactive_button_disabled.png";
        }
    } 
}
 

My Gui Class-

Spoiler

package com.spacejet.quantivity.gui;

import java.io.IOException;

import com.spacejet.quantivity.container.ContainerEnergyAccumulator;
import com.spacejet.quantivity.gui.buttons.ActiveDeactiveButton;
import com.spacejet.quantivity.tileentity.TileEntityEnergyAccumulator;
import com.spacejet.quantivity.util.Reference;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;

public class GuiEnergyAccumulator extends GuiContainer 
{    
    private int state;
    
    private static final ResourceLocation GUI_TEXTURE = new ResourceLocation(Reference.MOD_ID + ":textures/gui/energy_accumulator.png");
    private final TileEntityEnergyAccumulator tileEntity;
    
    public GuiEnergyAccumulator(InventoryPlayer player, TileEntityEnergyAccumulator tileEntity) 
    {
        super(new ContainerEnergyAccumulator(player, tileEntity));
        this.tileEntity = tileEntity;
    }
    
    @Override
    public void initGui() 
    {
        super.initGui();
        this.buttonList.add(new ActiveDeactiveButton(0, 200, 200, 16, 16, 200, 200, 0, state));
    }
    
    @Override
    protected void actionPerformed(GuiButton button) throws IOException 
    {
        if(button.id == 0)
         {
            System.out.println("My Button is Clicked!");
            
            if(state < 2)
                state++;
            else if (state == 2)
                state = 0;
            
            System.out.println(state);
         }
    }
    
    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) 
    {
        String tileName = this.tileEntity.getDisplayName().getUnformattedText();
        this.fontRenderer.drawString(tileName, (this.xSize / 2 - this.fontRenderer.getStringWidth(tileName) / 2), 8, 4210752);
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) 
    {
        GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
        this.mc.getTextureManager().bindTexture(GUI_TEXTURE);
        this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
    }

}
 

The image does not fit in the button - only a part of it or none is visible on the button.

 

2) I am not changing the texture based on state yet but is there any way to do that without overriding drawButton()??

 

Also, my background is not getting the dark fog effect even though I have 

GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);

in

@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) 
{
    GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
    this.mc.getTextureManager().bindTexture(GUI_TEXTURE);
    this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
}

Any ideas why?

 

Textures: (Button textures are 16 x 16)

active_deactive_redstone_off.png.64d11ddb05ef5d33e502297e9c884e3c.pngactive_deactive_redstone_on.png.0e848a98817285a70fd8bcfe1fbaea26.pngactive_deactive_redstone_disabled.png.95c23debcfcd7707a5e8c0d669b454f6.png

 

Gui:

Gui.png.d3a3f5b7c3e79290168d63f3c105d6b1.png

The circled thing is the button. It is not positioned, but as you can see, the texture is not fitting the button...

 

Any help is greatly appreciated!

Edited by Spacejet
Marked as Solved
Link to comment
Share on other sites

8 hours ago, Spacejet said:

 private static int state;

 

8 hours ago, Spacejet said:

private static final ResourceLocation TEXTURE = new ResourceLocation(Reference.MOD_ID + GetTextureLocationOnState(state));

As you can see yourself these are static fields, meaning they are the same for all your buttons. Why are you setting a static field to a thing passed in a constructor? And that doesn't even matter because by the time your resourcelocation resolver code gets executed state is guaranteed to be 0. How well do you know java?

 

8 hours ago, Spacejet said:

The image does not fit in the button - only a part of it or none is visible on the button.

drawTexturedModalRect expects a 256x256 texture, not a 16x16. You can use the overload which takes uv coordinates instead. Of course you would need to override GuiButton#drawButton for that to work.

 

8 hours ago, Spacejet said:

is there any way to do that without overriding drawButton()??

I don't think so. What's the problem though?

 

8 hours ago, Spacejet said:

Also, my background is not getting the dark fog effect even though I have 


GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);

How are these two things connected?

There is a GuiScreen#drawDefaultBackground method which makes the background darker. You are not calling it.

 

 

Link to comment
Share on other sites

10 hours ago, Spacejet said:

new ResourceLocation(Reference.MOD_ID + ":textures/gui/energy_accumulator.png");

 

10 hours ago, Spacejet said:

new ResourceLocation(Reference.MOD_ID + GetTextureLocationOnState(state));

Don’t use (modid+”:”+path)! Use the overload that accepts a resource domain and a path (modid, path)

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

3 hours ago, V0idWa1k3r said:
11 hours ago, Spacejet said:

 private static int state;

 

11 hours ago, Spacejet said:

private static final ResourceLocation TEXTURE = new ResourceLocation(Reference.MOD_ID + GetTextureLocationOnState(state));

As you can see yourself these are static fields, meaning they are the same for all your buttons. Why are you setting a static field to a thing passed in a constructor? And that doesn't even matter because by the time your resourcelocation resolver code gets executed state is guaranteed to be 0.

? oops... did not pay attention to that...

 

3 hours ago, V0idWa1k3r said:
11 hours ago, Spacejet said:

is there any way to do that without overriding drawButton()??

I don't think so. What's the problem though?

No problem, just asking

3 hours ago, V0idWa1k3r said:
11 hours ago, Spacejet said:

The image does not fit in the button - only a part of it or none is visible on the button.

drawTexturedModalRect expects a 256x256 texture, not a 16x16. You can use the overload which takes uv coordinates instead. Of course you would need to override GuiButton#drawButton for that to work.

I did also try using a 256 by 256 image, but that did not work either. But I'll just override drawButton()..

 

3 hours ago, V0idWa1k3r said:
11 hours ago, Spacejet said:


GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);

How are these two things connected?

There is a GuiScreen#drawDefaultBackground method which makes the background darker. You are not calling it.

Oh. I thought this was being called in the drawContainerBackhround so it was the thing... nice to know!

1 hour ago, Cadiboo said:
12 hours ago, Spacejet said:

new ResourceLocation(Reference.MOD_ID + ":textures/gui/energy_accumulator.png");

 

12 hours ago, Spacejet said:

new ResourceLocation(Reference.MOD_ID + GetTextureLocationOnState(state));

Don’t use (modid+”:”+path)! Use the overload that accepts a resource domain and a path (modid, path)

I considered doing that as it seemed like the better way to get the resource location. Will change that.

 

Thanks!

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.