| 1 | package net.minecraft.client.gui;
|
| 2 |
|
| 3 | import net.minecraft.client.Minecraft;
|
| 4 | import net.minecraft.client.audio.PositionedSoundRecord;
|
| 5 | import net.minecraft.client.audio.SoundHandler;
|
| 6 | import net.minecraft.client.renderer.GlStateManager;
|
| 7 | import net.minecraft.util.ResourceLocation;
|
| 8 |
|
| 9 | public class GuiButton extends Gui
|
| 10 | {
|
| 11 | protected static final ResourceLocation buttonTextures = new ResourceLocation("textures/gui/widgets.png");
|
| 12 |
|
| 13 | /** Button width in pixels */
|
| 14 | protected int width;
|
| 15 |
|
| 16 | /** Button height in pixels */
|
| 17 | protected int height;
|
| 18 |
|
| 19 | /** The x position of this control. */
|
| 20 | public int xPosition;
|
| 21 |
|
| 22 | /** The y position of this control. */
|
| 23 | public int yPosition;
|
| 24 |
|
| 25 | /** The string displayed on this control. */
|
| 26 | public String displayString;
|
| 27 | public int id;
|
| 28 |
|
| 29 | /** True if this control is enabled, false to disable. */
|
| 30 | public boolean enabled;
|
| 31 |
|
| 32 | /** Hides the button completely if false. */
|
| 33 | public boolean visible;
|
| 34 | protected boolean hovered;
|
| 35 |
|
| 36 | public GuiButton(int buttonId, int x, int y, String buttonText)
|
| 37 | {
|
| 38 | this(buttonId, x, y, 200, 20, buttonText);
|
| 39 | }
|
| 40 |
|
| 41 | public GuiButton(int buttonId, int x, int y, int widthIn, int heightIn, String buttonText)
|
| 42 | {
|
| 43 | this.width = 200;
|
| 44 | this.height = 20;
|
| 45 | this.enabled = true;
|
| 46 | this.visible = true;
|
| 47 | this.id = buttonId;
|
| 48 | this.xPosition = x;
|
| 49 | this.yPosition = y;
|
| 50 | this.width = widthIn;
|
| 51 | this.height = heightIn;
|
| 52 | this.displayString = buttonText;
|
| 53 | }
|
| 54 |
|
| 55 | /**
|
| 56 | * Returns 0 if the button is disabled, 1 if the mouse is NOT hovering over this button and 2 if it IS hovering over
|
| 57 | * this button.
|
| 58 | */
|
| 59 | protected int getHoverState(boolean mouseOver)
|
| 60 | {
|
| 61 | int i = 1;
|
| 62 |
|
| 63 | if (!this.enabled)
|
| 64 | {
|
| 65 | i = 0;
|
| 66 | }
|
| 67 | else if (mouseOver)
|
| 68 | {
|
| 69 | i = 2;
|
| 70 | }
|
| 71 |
|
| 72 | return i;
|
| 73 | }
|
| 74 |
|
| 75 | /**
|
| 76 | * Draws this button to the screen.
|
| 77 | */
|
| 78 | public void drawButton(Minecraft mc, int mouseX, int mouseY)
|
| 79 | {
|
| 80 | if (this.visible)
|
| 81 | {
|
| 82 | FontRenderer fontrenderer = mc.fontRendererObj;
|
| 83 | mc.getTextureManager().bindTexture(buttonTextures);
|
| 84 | GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
|
| 85 | this.hovered = mouseX >= this.xPosition && mouseY >= this.yPosition && mouseX < this.xPosition + this.width && mouseY < this.yPosition + this.height;
|
| 86 | int i = this.getHoverState(this.hovered);
|
| 87 | GlStateManager.enableBlend();
|
| 88 | GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
|
| 89 | GlStateManager.blendFunc(770, 771);
|
| 90 | this.drawTexturedModalRect(this.xPosition, this.yPosition, 0, 46 + i * 20, this.width / 2, this.height);
|
| 91 | this.drawTexturedModalRect(this.xPosition + this.width / 2, this.yPosition, 200 - this.width / 2, 46 + i * 20, this.width / 2, this.height);
|
| 92 | this.mouseDragged(mc, mouseX, mouseY);
|
| 93 | int j = 14737632;
|
| 94 |
|
| 95 | if (!this.enabled)
|
| 96 | {
|
| 97 | j = 10526880;
|
| 98 | }
|
| 99 | else if (this.hovered)
|
| 100 | {
|
| 101 | j = 16777120;
|
| 102 | }
|
| 103 |
|
| 104 | this.drawCenteredString(fontrenderer, this.displayString, this.xPosition + this.width / 2, this.yPosition + (this.height - 8) / 2, j);
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | /**
|
| 109 | * Fired when the mouse button is dragged. Equivalent of MouseListener.mouseDragged(MouseEvent e).
|
| 110 | */
|
| 111 | protected void mouseDragged(Minecraft mc, int mouseX, int mouseY)
|
| 112 | {
|
| 113 | }
|
| 114 |
|
| 115 | /**
|
| 116 | * Fired when the mouse button is released. Equivalent of MouseListener.mouseReleased(MouseEvent e).
|
| 117 | */
|
| 118 | public void mouseReleased(int mouseX, int mouseY)
|
| 119 | {
|
| 120 | }
|
| 121 |
|
| 122 | /**
|
| 123 | * Returns true if the mouse has been pressed on this control. Equivalent of MouseListener.mousePressed(MouseEvent
|
| 124 | * e).
|
| 125 | */
|
| 126 | public boolean mousePressed(Minecraft mc, int mouseX, int mouseY)
|
| 127 | {
|
| 128 | return this.enabled && this.visible && mouseX >= this.xPosition && mouseY >= this.yPosition && mouseX < this.xPosition + this.width && mouseY < this.yPosition + this.height;
|
| 129 | }
|
| 130 |
|
| 131 | /**
|
| 132 | * Whether the mouse cursor is currently over the button.
|
| 133 | */
|
| 134 | public boolean isMouseOver()
|
| 135 | {
|
| 136 | return this.hovered;
|
| 137 | }
|
| 138 |
|
| 139 | public void drawButtonForegroundLayer(int mouseX, int mouseY)
|
| 140 | {
|
| 141 | }
|
| 142 |
|
| 143 | public void playPressSound(SoundHandler soundHandlerIn)
|
| 144 | {
|
| 145 | soundHandlerIn.playSound(PositionedSoundRecord.create(new ResourceLocation("gui.button.press"), 1.0F));
|
| 146 | }
|
| 147 |
|
| 148 | public int getButtonWidth()
|
| 149 | {
|
| 150 | return this.width;
|
| 151 | }
|
| 152 |
|
| 153 | public void setWidth(int width)
|
| 154 | {
|
| 155 | this.width = width;
|
| 156 | }
|
| 157 | }
|
| 158 |
|