diff options
Diffstat (limited to 'src/main/java/gregtech/api/gui/widgets')
6 files changed, 410 insertions, 0 deletions
diff --git a/src/main/java/gregtech/api/gui/widgets/GT_GuiFakeItemButton.java b/src/main/java/gregtech/api/gui/widgets/GT_GuiFakeItemButton.java new file mode 100644 index 0000000000..db7029d60f --- /dev/null +++ b/src/main/java/gregtech/api/gui/widgets/GT_GuiFakeItemButton.java @@ -0,0 +1,64 @@ +package gregtech.api.gui.widgets; + +import gregtech.api.interfaces.IGuiScreen; +import net.minecraft.client.Minecraft; +import net.minecraft.item.ItemStack; +import org.lwjgl.opengl.GL11; + +import java.awt.*; + +public class GT_GuiFakeItemButton implements IGuiScreen.IGuiElement { + + private final GT_GuiIcon bgIcon; + private ItemStack item; + private IGuiScreen gui; + private int x0, y0, xPosition, yPosition; + private int width, height; + + public GT_GuiFakeItemButton(IGuiScreen gui, int x, int y, GT_GuiIcon bgIcon) { + this.gui = gui; + this.x0 = x; + this.y0 = y; + this.bgIcon = bgIcon; + item = null; + width = 18; + height = 18; + gui.addElement(this); + } + + public GT_GuiFakeItemButton setItem(ItemStack i) { + item = i; + return this; + } + + public ItemStack getItem(){ + return item; + } + + @Override + public void onInit() { + xPosition = x0 + gui.getGuiLeft(); + yPosition = y0 + gui.getGuiTop(); + } + + @Override + public void draw(int mouseX, int mouseY, float parTicks) { + GL11.glColor4f(1, 1, 1, 1); + GL11.glPushAttrib(GL11.GL_ENABLE_BIT); + GL11.glEnable(GL11.GL_BLEND); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + + if (bgIcon != null){ + GT_GuiIcon.render(bgIcon, xPosition-1, yPosition-1, 18, 18,0,true); + } + + if (item != null) + gui.getItemRenderer().renderItemAndEffectIntoGUI(gui.getFontRenderer(), Minecraft.getMinecraft().getTextureManager(), item, xPosition, yPosition); + + GL11.glPopAttrib(); + } + + public Rectangle getBounds() { + return new Rectangle(x0, y0, width, height); + } +} diff --git a/src/main/java/gregtech/api/gui/widgets/GT_GuiIcon.java b/src/main/java/gregtech/api/gui/widgets/GT_GuiIcon.java new file mode 100644 index 0000000000..1c31462e62 --- /dev/null +++ b/src/main/java/gregtech/api/gui/widgets/GT_GuiIcon.java @@ -0,0 +1,77 @@ +package gregtech.api.gui.widgets; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.Tessellator; +import net.minecraft.util.ResourceLocation; + +public enum GT_GuiIcon { + BUTTON_NORMAL (0, 0, 0), + BUTTON_DOWN (0, 32, 0), + BUTTON_HIGHLIGHT (0, 32*2, 0), + BUTTON_HIGHLIGHT_DOWN (0, 32*3, 0), + BUTTON_DISABLED (0, 32*4, 0), + + DISABLE (0, 0, 32), + REDSTONE_OFF (0, 32, 32), + REDSTONE_ON (0, 32*2, 32), + CHECKMARK (0, 32*3, 32), + CROSS (0, 32*4, 32), + WHITELIST (0, 32*5, 32), + BLACKLIST (0, 32*6, 32), + + EXPORT (0, 0, 32*2), + IMPORT (0, 32, 32*2), + ALLOW_INPUT (0, 32*2, 32*2), + BLOCK_INPUT (0, 32*3, 32*2), + + SLOT_DARKGRAY (1, 176,0,18,18), + SLOT_GRAY (1, 176,18,18,18); + + private static final int T_SIZE = 256; + private static final ResourceLocation[] TEXTURES = { + new ResourceLocation("gregtech", "textures/gui/GuiButtons.png"), + new ResourceLocation("gregtech", "textures/gui/GuiCover.png") + }; + + public final int x, y, width, height; + public final GT_GuiIcon overlay; + private final int texID; + + GT_GuiIcon(int texID, int x, int y, int width, int height, GT_GuiIcon overlay) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + this.overlay = overlay; + this.texID = texID; + } + + GT_GuiIcon(int texID, int x, int y) { + this(texID, x, y,32,32,null); + } + GT_GuiIcon(int texID, int x, int y, int width, int height) { + this(texID, x, y, width, height,null); + } + + public static void render(GT_GuiIcon icon, double x, double y, double width, double height, double zLevel, boolean doDraw) { + Tessellator tess = Tessellator.instance; + if (doDraw) { + Minecraft.getMinecraft().renderEngine.bindTexture(TEXTURES[icon.texID]); + tess.startDrawingQuads(); + } + double minU = (double) icon.x / T_SIZE; + double maxU = (double) (icon.x + icon.width) / T_SIZE; + double minV = (double) icon.y / T_SIZE; + double maxV = (double) (icon.y + icon.height) / T_SIZE; + tess.addVertexWithUV(x, y + height, zLevel, minU, maxV); + tess.addVertexWithUV(x + width, y + height, zLevel, maxU, maxV); + tess.addVertexWithUV(x + width, y + 0, zLevel, maxU, minV); + tess.addVertexWithUV(x, y + 0, zLevel, minU, minV); + + if (icon.overlay != null) + render(icon.overlay, x, y, width, height, zLevel, false); + + if (doDraw) + tess.draw(); + } +} diff --git a/src/main/java/gregtech/api/gui/widgets/GT_GuiIconButton.java b/src/main/java/gregtech/api/gui/widgets/GT_GuiIconButton.java new file mode 100644 index 0000000000..f68962f58f --- /dev/null +++ b/src/main/java/gregtech/api/gui/widgets/GT_GuiIconButton.java @@ -0,0 +1,111 @@ +package gregtech.api.gui.widgets; + +import gregtech.api.interfaces.IGuiScreen; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiButton; +import org.lwjgl.opengl.GL11; +import java.awt.Rectangle; + +public class GT_GuiIconButton extends GuiButton implements IGuiScreen.IGuiElement { + public static final int DEFAULT_WIDTH = 16; + public static final int DEFAULT_HEIGHT = 16; + + protected GT_GuiIcon icon; + private int x0, y0; + private IGuiScreen gui; + private String[] tooltipText; + + private GT_GuiTooltip tooltip; + + + public GT_GuiIconButton(IGuiScreen gui, int id, int x, int y, GT_GuiIcon icon) { + super(id, x, y, DEFAULT_WIDTH, DEFAULT_HEIGHT, ""); + this.gui = gui; + this.icon = icon; + this.x0 = x; + this.y0 = y; + gui.addElement(this); + } + + public void onInit() { + if (tooltip != null) + gui.addToolTip(tooltip); + xPosition = x0 + gui.getGuiLeft(); + yPosition = y0 + gui.getGuiTop(); + } + + @Override + public void draw(int mouseX, int mouseY, float parTicks) { + drawButton(Minecraft.getMinecraft(), mouseX, mouseY); + } + + public void drawButton(Minecraft mc, int mouseX, int mouseY) { + if (this.tooltip != null) + this.tooltip.enabled = true; + + if (this.visible) { + //moused over + this.field_146123_n = mouseX >= this.xPosition && mouseY >= this.yPosition && mouseX < this.xPosition + width && mouseY < this.yPosition + height; + + mouseDragged(mc, mouseX, mouseY); + + GL11.glPushAttrib(GL11.GL_ENABLE_BIT); + GL11.glEnable(GL11.GL_BLEND); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + + int x = xPosition; + int y = yPosition; + if(!this.field_146123_n) { + GL11.glColor4f(200F/255F, 210F/255F, 1, 1); + } + else + GL11.glColor4f(1, 1, 1, 1); + + GT_GuiIcon.render(getButtonTexture(this.field_146123_n), x, y, width, height, 0, true); + + GL11.glColor4f(1, 1, 1, 1); + if (icon != null) { + GT_GuiIcon.render(icon, x, y, width, height , 0, true); + } + + GL11.glPopAttrib(); + } + } + + @Override + public void mouseReleased(int mouseX, int mouseY) { + this.gui.clearSelectedButton(); + if(mousePressed(Minecraft.getMinecraft(), mouseX, mouseY)) + this.gui.buttonClicked(this); + } + + public GT_GuiIcon getButtonTexture(boolean mouseOver) { + if (!enabled) + return GT_GuiIcon.BUTTON_DISABLED; + if (this.equals(this.gui.getSelectedButton())) + return mouseOver ? GT_GuiIcon.BUTTON_HIGHLIGHT_DOWN : GT_GuiIcon.BUTTON_DOWN; + else + return mouseOver ? GT_GuiIcon.BUTTON_HIGHLIGHT : GT_GuiIcon.BUTTON_NORMAL; + } + + public GT_GuiIcon getIcon() { + return icon; + } + + public GT_GuiTooltip getTooltip() { + return tooltip; + } + + public GT_GuiIconButton setTooltipText(String... text) { + if (tooltip == null) + tooltip = new GT_GuiTooltip(getBounds(), text); + else + tooltip.setToolTipText(text); + this.tooltipText = text; + return this; + } + + public Rectangle getBounds() { + return new Rectangle(x0, y0, width, height); + } +} diff --git a/src/main/java/gregtech/api/gui/widgets/GT_GuiIntegerTextBox.java b/src/main/java/gregtech/api/gui/widgets/GT_GuiIntegerTextBox.java new file mode 100644 index 0000000000..3f6fe64e73 --- /dev/null +++ b/src/main/java/gregtech/api/gui/widgets/GT_GuiIntegerTextBox.java @@ -0,0 +1,50 @@ +package gregtech.api.gui.widgets; + +import gregtech.api.interfaces.IGuiScreen; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiTextField; + +import java.awt.*; + +public class GT_GuiIntegerTextBox extends GuiTextField implements IGuiScreen.IGuiElement { + private final int x0, y0; + private final IGuiScreen gui; + public final int id; + + public GT_GuiIntegerTextBox(IGuiScreen gui, int id, int x, int y, int width, int height) { + super(Minecraft.getMinecraft().fontRenderer, x, y, width, height); + super.setText(""); + this.id = id; + x0 = x; + y0 = y; + this.gui = gui; + gui.addElement(this); + } + + @Override + public void onInit() { + xPosition = x0 + gui.getGuiLeft(); + yPosition = y0 + gui.getGuiTop(); + } + + @Override + public void draw(int mouseX, int mouseY, float parTicks) { + super.drawTextBox(); + } + + public Rectangle getBounds() { + return new Rectangle(x0, y0, width, height); + } + + public boolean validChar(char c, int key) { + return Character.isDigit(c); + } + + @Override + public boolean textboxKeyTyped(char c, int key) { + if (validChar(c, key) || c == 1 || c == 3 || c == 22 || c == 24 || key == 14 || key == 199 || key == 203 || key == 205 || key == 207 || key == 211) { + return super.textboxKeyTyped(c, key); + } + return false; + } +} diff --git a/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltip.java b/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltip.java new file mode 100644 index 0000000000..d24437f018 --- /dev/null +++ b/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltip.java @@ -0,0 +1,37 @@ +package gregtech.api.gui.widgets; + +import java.awt.*; +import java.util.ArrayList; +import java.util.List; + +public class GT_GuiTooltip { + + protected Rectangle bounds; + private List<String> text; + public boolean enabled = true; + + public GT_GuiTooltip(Rectangle bounds, String... text) { + this.bounds = bounds; + setToolTipText(text); + } + + protected void updateText() { + } + + public void setToolTipText(String... text) { + if (text != null) { + this.text = new ArrayList<>(text.length); + for (int i = 0; i < text.length; i++) { + if (i == 0) + this.text.add("\u00a7f" + text[i]); + else + this.text.add("\u00a77" + text[i]); + } + } else + this.text = new ArrayList<>(); + } + + public List<String> getToolTipText() { + return text; + } +} diff --git a/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltipManager.java b/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltipManager.java new file mode 100644 index 0000000000..d0e6964abc --- /dev/null +++ b/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltipManager.java @@ -0,0 +1,71 @@ +package gregtech.api.gui.widgets; + +import net.minecraft.client.gui.FontRenderer; + +import java.util.ArrayList; +import java.util.List; + +public class GT_GuiTooltipManager { + public interface GT_IToolTipRenderer { + int getGuiLeft(); + int getGuiTop(); + int getXSize(); + FontRenderer getFontRenderer(); + void drawHoveringText(List<String> par1List, int par2, int par3, FontRenderer font); + } + + private static final long DELAY = 5; + private int mouseStopped; + private int lastMouseX = -1; + private int lastMouseY = -1; + private final List<GT_GuiTooltip> tips = new ArrayList<>(); + + public void addToolTip(GT_GuiTooltip tip) { + if (!tips.contains(tip)) tips.add(tip); + } + + public boolean removeToolTip(GT_GuiTooltip tip) { + return tips.remove(tip); + } + + public final void onTick(GT_IToolTipRenderer render, int mouseX, int mouseY) { + if ((Math.abs(mouseX-lastMouseX) < 2 ) && (Math.abs(mouseY-lastMouseY) < 2 )) + mouseStopped = Math.min(mouseStopped+1, 50); + else + mouseStopped = 0; + + lastMouseX = mouseX; + lastMouseY = mouseY; + + if (mouseStopped > DELAY) + mouseX -= render.getGuiLeft(); + mouseY -= render.getGuiTop(); + for (GT_GuiTooltip tip : tips) { + if(tip.enabled && tip.bounds.contains(mouseX, mouseY)){ + tip.updateText(); + drawTooltip(tip, mouseX, mouseY, render); + break; + } + } + } + + private void drawTooltip(GT_GuiTooltip tip, int mouseX, int mouseY, GT_IToolTipRenderer render) { + List<String> text = tip.getToolTipText(); + if (text == null) + return; + + if (mouseX > render.getGuiLeft() + render.getXSize()/2) { + int maxWidth = 0; + for (String s : text) { + int w = render.getFontRenderer().getStringWidth(s); + if (w > maxWidth) { + maxWidth = w; + } + } + mouseX -= (maxWidth + 18); + } + + render.drawHoveringText(text, mouseX, mouseY, render.getFontRenderer()); + } + +} |