Problème pour changer la texture des boutons du main menu



  • Bonjour! Cela fait quelques jours que j'apprends à développer des mods Minecraft. J'ai donc voulu modifier la texture des boutons du main menu mais j'ai rencontré un problème que je n'arrive pas à fixer :S

    text alternatif

    Voici le code de ma class GuiButtonCustom:

    package fr.mathis.mcraftrp;
    
    import net.minecraft.client.Minecraft;
    import net.minecraft.client.audio.PositionedSoundRecord;
    import net.minecraft.client.audio.SoundHandler;
    import net.minecraft.client.gui.FontRenderer;
    import net.minecraft.client.gui.Gui;
    import net.minecraft.client.renderer.GlStateManager;
    import net.minecraft.init.SoundEvents;
    import net.minecraft.util.ResourceLocation;
    import net.minecraftforge.fml.relauncher.Side;
    import net.minecraftforge.fml.relauncher.SideOnly;
    
    @SideOnly(Side.CLIENT)
    public class GuiButtonCustom extends Gui
    {
        protected static final ResourceLocation BUTTON_TEXTURES = new ResourceLocation(ModMcraftRP.MODID, "textures/gui/widgets.png");
        /** Button width in pixels */
        public int width;
        /** Button height in pixels */
        public int height;
        /** The x position of this control. */
        public int x;
        /** The y position of this control. */
        public int y;
        /** The string displayed on this control. */
        public String displayString;
        public int id;
        /** True if this control is enabled, false to disable. */
        public boolean enabled;
        /** Hides the button completely if false. */
        public boolean visible;
        protected boolean hovered;
        public int packedFGColour; //FML
    
        public GuiButtonCustom(int buttonId, int x, int y, String buttonText)
        {
            this(buttonId, x, y, 200, 20, buttonText);
        }
    
        public GuiButtonCustom(int buttonId, int x, int y, int widthIn, int heightIn, String buttonText)
        {
            this.width = 200;
            this.height = 20;
            this.enabled = true;
            this.visible = true;
            this.id = buttonId;
            this.x = x;
            this.y = y;
            this.width = widthIn;
            this.height = heightIn;
            this.displayString = buttonText;
        }
    
        /**
         * Returns 0 if the button is disabled, 1 if the mouse is NOT hovering over this button and 2 if it IS hovering over
         * this button.
         */
        protected int getHoverState(boolean mouseOver)
        {
            int i = 1;
    
            if (!this.enabled)
            {
                i = 0;
            }
            else if (mouseOver)
            {
                i = 2;
            }
    
            return i;
        }
    
        /**
         * Draws this button to the screen.
         */
        public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks)
        {
            if (this.visible)
            {
                FontRenderer fontrenderer = mc.fontRenderer;
                mc.getTextureManager().bindTexture(BUTTON_TEXTURES);
                GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
                this.hovered = mouseX >= this.x && mouseY >= this.y && mouseX < this.x + this.width && mouseY < this.y + this.height;
                int i = this.getHoverState(this.hovered);
                GlStateManager.enableBlend();
                GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
                GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
                this.drawTexturedModalRect(this.x, this.y, 0, 46 + i * 20, this.width / 2, this.height);
                this.drawTexturedModalRect(this.x + this.width / 2, this.y, 200 - this.width / 2, 46 + i * 20, this.width / 2, this.height);
                this.mouseDragged(mc, mouseX, mouseY);
                int j = 14737632;
    
                if (packedFGColour != 0)
                {
                    j = packedFGColour;
                }
                else
                if (!this.enabled)
                {
                    j = 10526880;
                }
                else if (this.hovered)
                {
                    j = 16777120;
                }
    
                this.drawCenteredString(fontrenderer, this.displayString, this.x + this.width / 2, this.y + (this.height - 8) / 2, j);
            }
        }
    
        /**
         * Fired when the mouse button is dragged. Equivalent of MouseListener.mouseDragged(MouseEvent e).
         */
        protected void mouseDragged(Minecraft mc, int mouseX, int mouseY)
        {
        }
    
        /**
         * Fired when the mouse button is released. Equivalent of MouseListener.mouseReleased(MouseEvent e).
         */
        public void mouseReleased(int mouseX, int mouseY)
        {
        }
    
        /**
         * Returns true if the mouse has been pressed on this control. Equivalent of MouseListener.mousePressed(MouseEvent
         * e).
         */
        public boolean mousePressed(Minecraft mc, int mouseX, int mouseY)
        {
            return this.enabled && this.visible && mouseX >= this.x && mouseY >= this.y && mouseX < this.x + this.width && mouseY < this.y + this.height;
        }
    
        /**
         * Whether the mouse cursor is currently over the button.
         */
        public boolean isMouseOver()
        {
            return this.hovered;
        }
    
        public void drawButtonForegroundLayer(int mouseX, int mouseY)
        {
        }
    
        public void playPressSound(SoundHandler soundHandlerIn)
        {
            soundHandlerIn.playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F));
        }
    
        public int getButtonWidth()
        {
            return this.width;
        }
    
        public void setWidth(int width)
        {
            this.width = width;
        }
    }
    
    

    Merci à la personne qui pourrait m'aider ❤ 😄


  • Administrateurs

    Salut,
    Ta classe GuiButtonCustom doit hériter de GuiButton pour que cela fonctionne.
    Et du-coup pas besoin de reprendre tout le code de la classe GuiButton, il suffit juste d'override les fonctions dont tu veux changer le comportement (donc drawButton).



  • Ca marche merci, mais maintenant les boutons sont invisible 😕 Pourtant j'ai bien mapper le widgets.png


Log in to reply