info.plugmania.novacraft.gui.Button.java Source code

Java tutorial

Introduction

Here is the source code for info.plugmania.novacraft.gui.Button.java

Source

/*
   Additional permission is granted, as an exception allowed under section
   7 of the GNU GPLv3 to allow the inclusion of modified versions of source
   code derived from the decompilation of Minecraft as if they were covered
   by the licence.
       
   You are completely free to remove this exception, however, it will
   mean you are unable to convey the modifications to Minecraft files
   (the modifications are under the GNU GPLv3) within the files they
   modify unless you use a similar exception yourself.
       
       
NovaCraft - Minecraft client side modding API
Copyright (C) 2012 korikisulda
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package info.plugmania.novacraft.gui;

import org.lwjgl.opengl.GL11;

import net.minecraft.client.Minecraft;
import net.minecraft.src.FontRenderer;
import net.minecraft.src.Gui;
import net.minecraft.src.GuiButton;
import net.minecraft.src.GuiTextField;

/**
 * A button Control. Not compatible with overlays
 */
public class Button extends Control {

    /**
     * Initialises the button
     * @param id Control ID, used to distinguish one control from another
     * @param xpos The X-axis(horizontal) position for the control
     * @param ypos The Y-axis(vertical) position for the control
     * @param width Width of this button
     * @param height Height of this button
     * @param text Text to display on the button
     */
    public Button(int id, int xpos, int ypos, int width, int height, String text) {
        super(id, xpos, ypos, width, height);
        setContent(text);
    }

    public Button() {

    }

    private boolean enabled = true;

    /**
     * Sets whether or not this button should be clickable
     * @param enabled Enabled
     */
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    /**
     * Gets whether or not this button is clickable
     * @return Enabled
     */
    public boolean getEnabled() {
        return enabled;
    }

    protected int getHoverState(boolean mouseOver) {
        byte var2 = 1;

        if (!this.enabled) {
            var2 = 0;
        } else if (mouseOver) {
            var2 = 2;
        }

        return var2;
    }

    @Override()
    public void draw(Screen screen) {
        FontRenderer fr = Minecraft.getMinecraft().fontRenderer;
        GL11.glBindTexture(GL11.GL_TEXTURE_2D,
                Minecraft.getMinecraft().novaCraftClient.getRenderManager().addTexture("/gui/gui.png"));
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        boolean isMouseOver = screen.getMouseX(offset) >= this.getX() && screen.getMouseY(offset) >= this.getY()
                && screen.getMouseX(offset) < this.getX() + this.getWidth()
                && screen.getMouseY(offset) < this.getY() + this.getHeight();

        screen.drawTexturedModalRect(getScreenX(screen), getScreenY(screen), 0,
                46 + getHoverState(isMouseOver) * 20, this.width / 2, this.height);
        screen.drawTexturedModalRect(getScreenX(screen) + this.width / 2, getScreenY(screen), 200 - this.width / 2,
                46 + getHoverState(isMouseOver) * 20, this.width / 2, this.height);
        int colour = 14737632;

        if (!this.enabled) {
            colour = -6250336;
        } else if (isMouseOver) {
            colour = 16777120;
        }

        screen.drawCentredString(getContent(), getScreenX(screen) + getCentreX(),
                getScreenY(screen) + (getCentreY() - 4), colour);
    }

    @Override()
    public void mouseClicked(Screen screen) {
        boolean isMouseOver = screen.getMouseX(offset) >= this.getX() && screen.getMouseY(offset) >= this.getY()
                && screen.getMouseX(offset) < this.getX() + this.getWidth()
                && screen.getMouseY(offset) < this.getY() + this.getHeight();
        if (isMouseOver) {
            Minecraft.getMinecraft().novaCraftClient.getAudioManager().playSoundCentred("random.click", 1.0F, 1.0F);
            screen.actionPerformed(this);
        }
    }

    public void setId(int id) {
        this.id = id;
    }

    /**
     * Gets the identifier for this control
     * @return ID
     */
    public int getId() {
        return this.id;
    }

}