Major Backport of Button code

I recently did a major overhaul of Railcraft's button code for some
upcoming features. Figured I backport it to Buildcraft as well.

Texture information was abstracted into an easily swapped object.
Multi-Buttons can now have different textures for each state.
Added tool tip support for all buttons (with mouse over delay!).
Added textures and states for a "Lock" Multi-Button.
Greatly reduced code duplication.
This commit is contained in:
CovertJaguar 2013-05-15 04:17:26 -07:00
parent 8721cb9719
commit 0d392a1b6e
16 changed files with 503 additions and 165 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 9.4 KiB

View file

@ -10,7 +10,12 @@ import net.minecraft.util.Icon;
import org.lwjgl.opengl.GL11;
import buildcraft.core.DefaultProps;
import buildcraft.core.gui.buttons.GuiBetterButton;
import buildcraft.core.gui.tooltips.ToolTip;
import buildcraft.core.gui.tooltips.ToolTipLine;
import buildcraft.core.utils.SessionVars;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.entity.player.InventoryPlayer;
public abstract class GuiBuildCraft extends GuiContainer {
@ -55,8 +60,9 @@ public abstract class GuiBuildCraft extends GuiContainer {
ledger.currentShiftX = xShift;
ledger.currentShiftY = yShift;
if (ledger.intersectsWith(mX, mY, xShift, yShift))
if (ledger.intersectsWith(mX, mY, xShift, yShift)) {
return ledger;
}
yShift += ledger.getHeight();
}
@ -100,16 +106,16 @@ public abstract class GuiBuildCraft extends GuiContainer {
// ledger itself.
if (ledger != null && !ledger.handleMouseClicked(x, y, mouseButton)) {
for (Ledger other : ledgers)
for (Ledger other : ledgers) {
if (other != ledger && other.isOpen()) {
other.toggleOpen();
}
}
ledger.toggleOpen();
}
}
}
}
/**
@ -118,17 +124,13 @@ public abstract class GuiBuildCraft extends GuiContainer {
protected abstract class Ledger {
private boolean open;
protected int overlayColor = 0xffffff;
public int currentShiftX = 0;
public int currentShiftY = 0;
protected int limitWidth = 128;
protected int maxWidth = 124;
protected int minWidth = 24;
protected int currentWidth = minWidth;
protected int maxHeight = 24;
protected int minHeight = 24;
protected int currentHeight = minHeight;
@ -163,8 +165,9 @@ public abstract class GuiBuildCraft extends GuiContainer {
public boolean intersectsWith(int mouseX, int mouseY, int shiftX, int shiftY) {
if (mouseX >= shiftX && mouseX <= shiftX + currentWidth && mouseY >= shiftY && mouseY <= shiftY + getHeight())
if (mouseX >= shiftX && mouseX <= shiftX + currentWidth && mouseY >= shiftY && mouseY <= shiftY + getHeight()) {
return true;
}
return false;
}
@ -205,7 +208,7 @@ public abstract class GuiBuildCraft extends GuiContainer {
GL11.glColor4f(colorR, colorG, colorB, 1.0F);
mc.renderEngine.bindTexture(DefaultProps.TEXTURE_PATH_GUI + "/ledger.png");
mc.renderEngine.bindTexture(DefaultProps.TEXTURE_PATH_GUI + "/ledger.png");
drawTexturedModalRect(x, y, 0, 256 - currentHeight, 4, currentHeight);
drawTexturedModalRect(x + 4, y, 256 - currentWidth + 4, 0, currentWidth - 4, 4);
// Add in top left corner again
@ -222,7 +225,6 @@ public abstract class GuiBuildCraft extends GuiContainer {
drawTexturedModelRectFromIcon(x, y, icon, 16, 16);
}
}
protected TileEntity tile;
public GuiBuildCraft(BuildCraftContainer container, IInventory inventory) {
@ -238,6 +240,48 @@ public abstract class GuiBuildCraft extends GuiContainer {
protected void initLedgers(IInventory inventory) {
}
/**
* Draws the screen and all the components in it.
*/
@Override
public void drawScreen(int mouseX, int mouseY, float par3) {
super.drawScreen(mouseX, mouseY, par3);
int left = this.guiLeft;
int top = this.guiTop;
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glPushMatrix();
GL11.glTranslatef((float) left, (float) top, 0.0F);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
RenderHelper.disableStandardItemLighting();
InventoryPlayer playerInv = this.mc.thePlayer.inventory;
if (playerInv.getItemStack() == null) {
for (Object button : buttonList) {
if (!(button instanceof GuiBetterButton)) {
continue;
}
GuiBetterButton betterButton = (GuiBetterButton) button;
ToolTip tips = betterButton.getToolsTips();
if (tips == null) {
continue;
}
boolean mouseOver = betterButton.isMouseOverButton(mouseX, mouseY);
tips.onTick(mouseOver);
if (mouseOver && tips.isReady()) {
tips.refresh();
drawToolTips(tips, mouseX, mouseY);
}
}
}
GL11.glPopMatrix();
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_DEPTH_TEST);
}
@Override
protected void drawGuiContainerForegroundLayer(int par1, int par2) {
ledgerManager.drawLedgers(par1, par2);
@ -260,4 +304,61 @@ public abstract class GuiBuildCraft extends GuiContainer {
ledgerManager.handleMouseClicked(par1, par2, mouseButton);
}
private void drawToolTips(ToolTip toolTips, int mouseX, int mouseY) {
if (toolTips.size() > 0) {
int left = this.guiLeft;
int top = this.guiTop;
int lenght = 0;
int x;
int y;
for (ToolTipLine tip : toolTips) {
y = this.fontRenderer.getStringWidth(tip.text);
if (y > lenght) {
lenght = y;
}
}
x = mouseX - left + 12;
y = mouseY - top - 12;
int var14 = 8;
if (toolTips.size() > 1) {
var14 += 2 + (toolTips.size() - 1) * 10;
}
this.zLevel = 300.0F;
itemRenderer.zLevel = 300.0F;
int var15 = -267386864;
this.drawGradientRect(x - 3, y - 4, x + lenght + 3, y - 3, var15, var15);
this.drawGradientRect(x - 3, y + var14 + 3, x + lenght + 3, y + var14 + 4, var15, var15);
this.drawGradientRect(x - 3, y - 3, x + lenght + 3, y + var14 + 3, var15, var15);
this.drawGradientRect(x - 4, y - 3, x - 3, y + var14 + 3, var15, var15);
this.drawGradientRect(x + lenght + 3, y - 3, x + lenght + 4, y + var14 + 3, var15, var15);
int var16 = 1347420415;
int var17 = (var16 & 16711422) >> 1 | var16 & -16777216;
this.drawGradientRect(x - 3, y - 3 + 1, x - 3 + 1, y + var14 + 3 - 1, var16, var17);
this.drawGradientRect(x + lenght + 2, y - 3 + 1, x + lenght + 3, y + var14 + 3 - 1, var16, var17);
this.drawGradientRect(x - 3, y - 3, x + lenght + 3, y - 3 + 1, var16, var16);
this.drawGradientRect(x - 3, y + var14 + 2, x + lenght + 3, y + var14 + 3, var17, var17);
for (ToolTipLine tip : toolTips) {
String line = tip.text;
if (tip.color == -1) {
line = "\u00a77" + line;
} else {
line = "\u00a7" + Integer.toHexString(tip.color) + line;
}
this.fontRenderer.drawStringWithShadow(line, x, y, -1);
y += 10 + tip.getSpacing();
}
this.zLevel = 0.0F;
itemRenderer.zLevel = 0.0F;
}
}
}

View file

@ -0,0 +1,37 @@
package buildcraft.core.gui.buttons;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public class ButtonTextureSet implements IButtonTextureSet {
private final int x, y, height, width;
public ButtonTextureSet(int x, int y, int height, int width) {
this.x = x;
this.y = y;
this.height = height;
this.width = width;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
@Override
public int getHeight() {
return height;
}
@Override
public int getWidth() {
return width;
}
}

View file

@ -1,8 +1,12 @@
package buildcraft.core.gui.buttons;
import buildcraft.core.gui.tooltips.ToolTip;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import org.lwjgl.opengl.GL11;
/**
*
@ -12,14 +16,20 @@ import net.minecraft.client.gui.GuiButton;
public class GuiBetterButton extends GuiButton {
public static final String BUTTON_TEXTURES = "/gfx/buildcraft/gui/buttons.png";
protected final IButtonTextureSet texture;
private ToolTip tooltips;
public GuiBetterButton(int id, int x, int y, String label) {
this(id, x, y, 200, 20, label);
this(id, x, y, 200, StandardButtonTextureSets.LARGE_BUTTON, label);
}
public GuiBetterButton(int id, int x, int y, int width, int height, String label) {
super(id, x, y, width, height, label);
public GuiBetterButton(int id, int x, int y, int width, String label) {
this(id, x, y, width, StandardButtonTextureSets.LARGE_BUTTON, label);
}
public GuiBetterButton(int id, int x, int y, int width, IButtonTextureSet texture, String label) {
super(id, x, y, width, texture.getHeight(), label);
this.texture = texture;
}
public int getWidth() {
@ -27,6 +37,52 @@ public class GuiBetterButton extends GuiButton {
}
public int getHeight() {
return height;
return texture.getHeight();
}
public int getTextColor(boolean mouseOver) {
if (!enabled) {
return 0xffa0a0a0;
} else if (mouseOver) {
return 0xffffa0;
} else {
return 0xe0e0e0;
}
}
public boolean isMouseOverButton(int mouseX, int mouseY) {
return mouseX >= xPosition && mouseY >= yPosition && mouseX < xPosition + width && mouseY < yPosition + getHeight();
}
protected void bindButtonTextures(Minecraft minecraft) {
minecraft.renderEngine.bindTexture(BUTTON_TEXTURES);
}
@Override
public void drawButton(Minecraft minecraft, int mouseX, int mouseY) {
if (!drawButton) {
return;
}
FontRenderer fontrenderer = minecraft.fontRenderer;
bindButtonTextures(minecraft);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
int xOffset = texture.getX();
int yOffset = texture.getY();
int h = texture.getHeight();
int w = texture.getWidth();
boolean mouseOver = isMouseOverButton(mouseX, mouseY);
int hoverState = getHoverState(mouseOver);
drawTexturedModalRect(xPosition, yPosition, xOffset, yOffset + hoverState * h, width / 2, h);
drawTexturedModalRect(xPosition + width / 2, yPosition, xOffset + w - width / 2, yOffset + hoverState * h, width / 2, h);
mouseDragged(minecraft, mouseX, mouseY);
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (h - 8) / 2, getTextColor(mouseOver));
}
public ToolTip getToolsTips() {
return tooltips;
}
public void setToolTips(ToolTip tips) {
this.tooltips = tips;
}
}

View file

@ -2,9 +2,6 @@ package buildcraft.core.gui.buttons;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import org.lwjgl.opengl.GL11;
/**
*
@ -18,28 +15,6 @@ public class GuiButtonSmall extends GuiBetterButton {
}
public GuiButtonSmall(int i, int x, int y, int w, String s) {
super(i, x, y, w, 15, s);
}
@Override
public void drawButton(Minecraft minecraft, int i, int j) {
if (!drawButton) {
return;
}
FontRenderer fontrenderer = minecraft.fontRenderer;
minecraft.renderEngine.bindTexture(BUTTON_TEXTURES);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
boolean flag = i >= xPosition && j >= yPosition && i < xPosition + width && j < yPosition + height;
int k = getHoverState(flag);
drawTexturedModalRect(xPosition, yPosition, 0, 168 + k * 15, width / 2, height);
drawTexturedModalRect(xPosition + width / 2, yPosition, 200 - width / 2, 168 + k * 15, width / 2, height);
mouseDragged(minecraft, i, j);
if (!enabled) {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0xffa0a0a0);
} else if (flag) {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0xffffa0);
} else {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0xe0e0e0);
}
super(i, x, y, w, StandardButtonTextureSets.SMALL_BUTTON, s);
}
}

View file

@ -1,6 +1,5 @@
package buildcraft.core.gui.buttons;
import static buildcraft.core.gui.buttons.GuiBetterButton.BUTTON_TEXTURES;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
@ -15,40 +14,52 @@ import org.lwjgl.opengl.GL11;
public class GuiMultiButton extends GuiBetterButton {
private final MultiButtonController control;
protected int texOffset = 88;
public GuiMultiButton(int id, int x, int y, int width, MultiButtonController control) {
super(id, x, y, width, 20, "");
this.control = control.copy();
super(id, x, y, width, StandardButtonTextureSets.LARGE_BUTTON, "");
this.control = control;
}
@Override
public void drawButton(Minecraft minecraft, int i, int j) {
public int getHeight() {
return texture.getHeight();
}
@Override
public void drawButton(Minecraft minecraft, int x, int y) {
if (!drawButton) {
return;
}
FontRenderer fontrenderer = minecraft.fontRenderer;
minecraft.renderEngine.bindTexture(BUTTON_TEXTURES);
bindButtonTextures(minecraft);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
boolean flag = i >= xPosition && j >= yPosition && i < xPosition + width && j < yPosition + height;
IMultiButtonState state = control.getButtonState();
IButtonTextureSet tex = state.getTextureSet();
int xOffset = tex.getX();
int yOffset = tex.getY();
int h = tex.getHeight();
int w = tex.getWidth();
boolean flag = x >= xPosition && y >= yPosition && x < xPosition + width && y < yPosition + h;
int hoverState = getHoverState(flag);
drawTexturedModalRect(xPosition, yPosition, 0, texOffset + hoverState * height, width / 2, height);
drawTexturedModalRect(xPosition + width / 2, yPosition, 200 - width / 2, texOffset + hoverState * height, width / 2, height);
mouseDragged(minecraft, i, j);
displayString = control.getButtonState().getLabel();
if (!enabled) {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0xffa0a0a0);
} else if (flag) {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0xffffa0);
} else {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0xe0e0e0);
drawTexturedModalRect(xPosition, yPosition, xOffset, yOffset + hoverState * h, width / 2, h);
drawTexturedModalRect(xPosition + width / 2, yPosition, xOffset + w - width / 2, yOffset + hoverState * h, width / 2, h);
mouseDragged(minecraft, x, y);
displayString = state.getLabel();
if (!displayString.equals("")) {
if (!enabled) {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (h - 8) / 2, 0xffa0a0a0);
} else if (flag) {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (h - 8) / 2, 0xffffa0);
} else {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (h - 8) / 2, 0xe0e0e0);
}
}
}
@Override
public boolean mousePressed(Minecraft par1Minecraft, int par2, int par3) {
boolean pressed = super.mousePressed(par1Minecraft, par2, par3);
if (pressed) {
if (pressed && enabled) {
control.incrementState();
}
return pressed;

View file

@ -1,18 +0,0 @@
package buildcraft.core.gui.buttons;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/**
*
* @author CovertJaguar <railcraft.wikispaces.com>
*/
@SideOnly(Side.CLIENT)
public class GuiMultiButtonSmall extends GuiMultiButton {
public GuiMultiButtonSmall(int id, int x, int y, int width, MultiButtonController control) {
super(id, x, y, width, control);
height = 15;
texOffset = 168;
}
}

View file

@ -1,9 +1,5 @@
package buildcraft.core.gui.buttons;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import org.lwjgl.opengl.GL11;
/**
*
* @author CovertJaguar <railcraft.wikispaces.com>
@ -13,16 +9,16 @@ public class GuiToggleButton extends GuiBetterButton {
public boolean active;
public GuiToggleButton(int id, int x, int y, String label, boolean active) {
this(id, x, y, 200, 20, label, active);
this(id, x, y, 200, StandardButtonTextureSets.LARGE_BUTTON, label, active);
}
public GuiToggleButton(int id, int x, int y, int width, String s, boolean active) {
super(id, x, y, width, 20, s);
super(id, x, y, width, StandardButtonTextureSets.LARGE_BUTTON, s);
this.active = active;
}
public GuiToggleButton(int id, int x, int y, int width, int height, String s, boolean active) {
super(id, x, y, width, height, s);
public GuiToggleButton(int id, int x, int y, int width, IButtonTextureSet texture, String s, boolean active) {
super(id, x, y, width, texture, s);
this.active = active;
}
@ -31,11 +27,11 @@ public class GuiToggleButton extends GuiBetterButton {
}
@Override
protected int getHoverState(boolean flag) {
protected int getHoverState(boolean mouseOver) {
int state = 1;
if (!enabled) {
state = 0;
} else if (flag) {
} else if (mouseOver) {
state = 2;
} else if (!active) {
state = 3;
@ -44,26 +40,15 @@ public class GuiToggleButton extends GuiBetterButton {
}
@Override
public void drawButton(Minecraft minecraft, int i, int j) {
if (!drawButton) {
return;
}
FontRenderer fontrenderer = minecraft.fontRenderer;
minecraft.renderEngine.bindTexture(BUTTON_TEXTURES);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
boolean flag = i >= xPosition && j >= yPosition && i < xPosition + width && j < yPosition + height;
int k = getHoverState(flag);
drawTexturedModalRect(xPosition, yPosition, 0, 88 + k * 20, width / 2, height);
drawTexturedModalRect(xPosition + width / 2, yPosition, 200 - width / 2, 88 + k * 20, width / 2, height);
mouseDragged(minecraft, i, j);
public int getTextColor(boolean mouseOver) {
if (!enabled) {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0xffa0a0a0);
} else if (flag) {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0xffffa0);
return 0xffa0a0a0;
} else if (mouseOver) {
return 0xffffa0;
} else if (!active) {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0x777777);
return 0x777777;
} else {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0xe0e0e0);
return 0xe0e0e0;
}
}
}

View file

@ -2,9 +2,6 @@ package buildcraft.core.gui.buttons;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import org.lwjgl.opengl.GL11;
/**
*
@ -18,31 +15,7 @@ public class GuiToggleButtonSmall extends GuiToggleButton {
}
public GuiToggleButtonSmall(int i, int x, int y, int w, String s, boolean active) {
super(i, x, y, w, 15, s, active);
super(i, x, y, w, StandardButtonTextureSets.SMALL_BUTTON, s, active);
this.active = active;
}
@Override
public void drawButton(Minecraft minecraft, int i, int j) {
if (!drawButton) {
return;
}
FontRenderer fontrenderer = minecraft.fontRenderer;
minecraft.renderEngine.bindTexture(BUTTON_TEXTURES);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
boolean flag = i >= xPosition && j >= yPosition && i < xPosition + width && j < yPosition + height;
int k = getHoverState(flag);
drawTexturedModalRect(xPosition, yPosition, 0, 168 + k * 15, width / 2, height);
drawTexturedModalRect(xPosition + width / 2, yPosition, 200 - width / 2, 168 + k * 15, width / 2, height);
mouseDragged(minecraft, i, j);
if (!enabled) {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0xffa0a0a0);
} else if (flag) {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0xffffa0);
} else if (!active) {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0x777777);
} else {
drawCenteredString(fontrenderer, displayString, xPosition + width / 2, yPosition + (height - 8) / 2, 0xe0e0e0);
}
}
}

View file

@ -0,0 +1,25 @@
/*
* Copyright (c) CovertJaguar, 2011 http://railcraft.info
*
* This code is the property of CovertJaguar
* and may only be used with explicit written
* permission unless otherwise specified on the
* license page at railcraft.wikispaces.com.
*/
package buildcraft.core.gui.buttons;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public interface IButtonTextureSet {
public int getX();
public int getY();
public int getHeight();
public int getWidth();
}

View file

@ -1,12 +1,14 @@
package buildcraft.core.gui.buttons;
/**
* This could possibly be expanded to include the graphic that should be
* rendered.
*
* @author CovertJaguar <railcraft.wikispaces.com>
*/
public interface IMultiButtonState {
public String getLabel();
public String name();
public IButtonTextureSet getTextureSet();
}

View file

@ -0,0 +1,27 @@
package buildcraft.core.gui.buttons;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public enum LockButtonState implements IMultiButtonState {
UNLOCKED(new ButtonTextureSet(224, 0, 16, 16)),
LOCKED(new ButtonTextureSet(240, 0, 16, 16));
public static final LockButtonState[] VALUES = values();
private final IButtonTextureSet texture;
private LockButtonState(IButtonTextureSet texture) {
this.texture = texture;
}
@Override
public String getLabel() {
return "";
}
@Override
public IButtonTextureSet getTextureSet() {
return texture;
}
}

View file

@ -1,49 +1,81 @@
package buildcraft.core.gui.buttons;
import net.minecraft.nbt.NBTTagByte;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagString;
/**
* T should be an Enum of button states
*
* @author CovertJaguar <railcraft.wikispaces.com>
*/
public class MultiButtonController<T extends IMultiButtonState> {
private int currentState;
private final T[] validStates;
private int currentState;
private final T[] validStates;
private MultiButtonController(int startState, T... validStates) {
this.currentState = startState;
this.validStates = validStates;
}
private MultiButtonController(int startState, T... validStates) {
this.currentState = startState;
this.validStates = validStates;
}
public static <T extends IMultiButtonState> MultiButtonController getController(int startState, T... validStates) {
return new MultiButtonController<T>(startState, validStates);
}
public static <T extends IMultiButtonState> MultiButtonController getController(int startState, T... validStates) {
return new MultiButtonController<T>(startState, validStates);
}
public MultiButtonController copy() {
return new MultiButtonController(currentState, validStates.clone());
}
public MultiButtonController copy() {
return new MultiButtonController(currentState, validStates.clone());
}
public T[] getValidStates() {
return validStates;
}
public T[] getValidStates() {
return validStates;
}
public int incrementState() {
int newState = currentState + 1;
if (newState >= validStates.length) {
newState = 0;
}
currentState = newState;
return currentState;
}
public int incrementState() {
int newState = currentState + 1;
if (newState >= validStates.length) {
newState = 0;
}
currentState = newState;
return currentState;
}
public void setCurrentState(int state) {
currentState = state;
}
public void setCurrentState(int state) {
currentState = state;
}
public int getCurrentState() {
return currentState;
}
public void setCurrentState(T state) {
for (int i = 0; i < validStates.length; i++) {
if (validStates[i] == state) {
currentState = i;
return;
}
}
}
public T getButtonState() {
return validStates[currentState];
}
public int getCurrentState() {
return currentState;
}
public T getButtonState() {
return validStates[currentState];
}
public void writeToNBT(NBTTagCompound nbt, String tag) {
nbt.setString(tag, getButtonState().name());
}
public void readFromNBT(NBTTagCompound nbt, String tag) {
if (nbt.getTag(tag) instanceof NBTTagString) {
String name = nbt.getString(tag);
for (int i = 0; i < validStates.length; i++) {
if (validStates[i].name().equals(name)) {
currentState = i;
break;
}
}
} else if (nbt.getTag(tag) instanceof NBTTagByte) {
currentState = nbt.getByte(tag);
}
}
}

View file

@ -0,0 +1,40 @@
package buildcraft.core.gui.buttons;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public enum StandardButtonTextureSets implements IButtonTextureSet{
LARGE_BUTTON(0, 0, 20, 200),
SMALL_BUTTON(0, 80, 15, 200);
private final int x, y, height, width;
private StandardButtonTextureSets(int x, int y, int height, int width) {
this.x = x;
this.y = y;
this.height = height;
this.width = width;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
@Override
public int getHeight() {
return height;
}
@Override
public int getWidth() {
return width;
}
}

View file

@ -0,0 +1,55 @@
package buildcraft.core.gui.tooltips;
import com.google.common.collect.ForwardingList;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public class ToolTip extends ForwardingList<ToolTipLine> {
private final List<ToolTipLine> delegate = new ArrayList<ToolTipLine>();
private final long delay;
private long mouseOverStart;
public ToolTip() {
this.delay = 0;
}
public ToolTip(int delay) {
this.delay = delay;
}
@Override
protected final List<ToolTipLine> delegate() {
return delegate;
}
public void onTick(boolean mouseOver) {
if (delay == 0) {
return;
}
if (mouseOver) {
if (mouseOverStart == 0) {
mouseOverStart = System.currentTimeMillis();
}
} else {
mouseOverStart = 0;
}
}
public boolean isReady() {
if (delay == 0) {
return true;
}
if (mouseOverStart == 0) {
return false;
}
return System.currentTimeMillis() - mouseOverStart >= delay;
}
public void refresh() {
}
}

View file

@ -0,0 +1,37 @@
package buildcraft.core.gui.tooltips;
/**
*
* @author CovertJaguar <http://www.railcraft.info>
*/
public class ToolTipLine
{
public String text;
public final int color;
public int spacing;
public ToolTipLine(String text, int color)
{
this.text = text;
this.color = color;
}
public ToolTipLine(String text)
{
this(text, -1);
}
public ToolTipLine()
{
this("", -1);
}
public void setSpacing(int spacing){
this.spacing = spacing;
}
public int getSpacing(){
return spacing;
}
}