Added repackaged cofh lib to help with the IDSU gui

This commit is contained in:
modmuss50 2015-06-17 08:35:39 +01:00
parent 54904b223f
commit 90be01753b
40 changed files with 5242 additions and 86 deletions

View file

@ -9,25 +9,23 @@ import net.minecraft.util.StatCollector;
import org.lwjgl.input.Mouse; import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import techreborn.client.container.ContainerIDSU; import techreborn.client.container.ContainerIDSU;
import techreborn.cofhLib.gui.GuiBase;
import techreborn.cofhLib.gui.element.ElementListBox;
import techreborn.cofhLib.gui.element.listbox.ListBoxElementText;
import techreborn.tiles.iesu.TileIDSU; import techreborn.tiles.iesu.TileIDSU;
import java.awt.*; import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
public class GuiIDSU extends GuiContainer { public class GuiIDSU extends GuiBase {
private static final ResourceLocation texture = new ResourceLocation(
"techreborn", "textures/gui/aesu.png");
TileIDSU idsu; TileIDSU idsu;
ContainerIDSU containerIDSU; ContainerIDSU containerIDSU;
public ArrayList<String> names = new ArrayList<String>(); ElementListBox listBox;
int scrollpos = 0;
int listSize = 5;
public GuiIDSU(EntityPlayer player, public GuiIDSU(EntityPlayer player,
TileIDSU tileIDSU) TileIDSU tileIDSU)
@ -37,10 +35,11 @@ public class GuiIDSU extends GuiContainer {
this.ySize = 200; this.ySize = 200;
idsu = tileIDSU; idsu = tileIDSU;
this.containerIDSU = (ContainerIDSU) this.inventorySlots; this.containerIDSU = (ContainerIDSU) this.inventorySlots;
texture = new ResourceLocation(
for (int i = 0; i < 15; i++) { "techreborn", "textures/gui/aesu.png");
names.add(i + " name"); drawTitle = true;
} drawInventory = true;
name = StatCollector.translateToLocal("tile.techreborn.aesu.name");
} }
@Override @Override
@ -53,82 +52,11 @@ public class GuiIDSU extends GuiContainer {
this.buttonList.add(new GuiButton(1, k + 96, l + 8 + 22, 18, 20, "+")); this.buttonList.add(new GuiButton(1, k + 96, l + 8 + 22, 18, 20, "+"));
this.buttonList.add(new GuiButton(2, k + 96, l + 8 + (22*2), 18, 20, "-")); this.buttonList.add(new GuiButton(2, k + 96, l + 8 + (22*2), 18, 20, "-"));
this.buttonList.add(new GuiButton(3, k + 96, l + 8 + (22*3), 18, 20, "--")); this.buttonList.add(new GuiButton(3, k + 96, l + 8 + (22*3), 18, 20, "--"));
listBox = new ElementListBox(this, k + 20, l + 20, 60, 60);
for (int i = 0; i < 15; i++) {
listBox.add(new ListBoxElementText("Name " + i));
}
addElement(listBox);
} }
@Override
protected void drawGuiContainerBackgroundLayer(float p_146976_1_,
int p_146976_2_, int p_146976_3_)
{
this.mc.getTextureManager().bindTexture(texture);
int k = (this.width - this.xSize) / 2;
int l = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
}
protected void drawGuiContainerForegroundLayer(int p_146979_1_,
int p_146979_2_)
{
this.fontRendererObj.drawString(StatCollector.translateToLocal("tile.techreborn.aesu.name"), 40, 10, Color.WHITE.getRGB());
}
@Override
public void drawScreen(int p_73863_1_, int p_73863_2_, float p_73863_3_) {
super.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_);
int k = (this.width - this.xSize) / 2;
int l = (this.height - this.ySize) / 2;
int listX = k + 20;
int listY = l + 20;
{
drawColourOnScreen(Color.lightGray.getRGB(), 255, listX - 2, listY -2 , 54, listSize * 10 + 12, 0);
for (int i = scrollpos; i < listSize; i++) {
if(names.size() >= i){
drawColourOnScreen(Color.gray.getRGB(), 255, listX, listY + (12 * i), 50, 10, 0);
fontRendererObj.drawString(names.get(i), listX + 1, listY + (12 * i), Color.WHITE.getRGB());
}
}
}
// int mouseScroll = Mouse.getDWheel();
// System.out.println(mouseScroll);
// if(mouseScroll > 0){
// if(scrollpos + mouseScroll < names.size()){
// scrollpos += Mouse.getDWheel();
// }
// } else {
// if(!(scrollpos + mouseScroll < 0)){
// scrollpos -= Mouse.getDWheel();
// }
// }
}
@Override
protected void actionPerformed(GuiButton button) {
super.actionPerformed(button);
}
public static void drawColourOnScreen(int colour, int alpha, double posX, double posY, double width, double height, double zLevel) {
int r = (colour >> 16 & 0xff);
int g = (colour >> 8 & 0xff);
int b = (colour & 0xff);
drawColourOnScreen(r, g, b, alpha, posX, posY, width, height, zLevel);
}
public static void drawColourOnScreen(int r, int g, int b, int alpha, double posX, double posY, double width, double height, double zLevel) {
if (width <= 0 || height <= 0) {
return;
}
GL11.glDisable(GL11.GL_TEXTURE_2D);
Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.setColorRGBA(r, g, b, alpha);
tessellator.addVertex(posX, posY + height, zLevel);
tessellator.addVertex(posX + width, posY + height, zLevel);
tessellator.addVertex(posX + width, posY, zLevel);
tessellator.addVertex(posX, posY, zLevel);
tessellator.draw();
GL11.glEnable(GL11.GL_TEXTURE_2D);
}
} }

View file

@ -0,0 +1,197 @@
package techreborn.cofhLib.audio;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.audio.ISound;
import net.minecraft.util.ResourceLocation;
/**
* Generic ISound class with lots of constructor functionality. Required because - of course - Mojang has no generic that lets you specify *any* arguments for
* this.
*
* @author skyboy
*/
@SideOnly(Side.CLIENT)
public class SoundBase implements ISound
{
protected AttenuationType attenuation;
protected final ResourceLocation sound;
protected float volume;
protected float pitch;
protected float x;
protected float y;
protected float z;
protected boolean repeat;
protected int repeatDelay;
public SoundBase(String sound)
{
this(sound, 0);
}
public SoundBase(String sound, float volume)
{
this(sound, volume, 0);
}
public SoundBase(String sound, float volume, float pitch)
{
this(sound, volume, pitch, false, 0);
}
public SoundBase(String sound, float volume, float pitch, boolean repeat, int repeatDelay)
{
this(sound, volume, pitch, repeat, repeatDelay, 0, 0, 0, AttenuationType.NONE);
}
public SoundBase(String sound, float volume, float pitch, double x, double y, double z)
{
this(sound, volume, pitch, false, 0, x, y, z);
}
public SoundBase(String sound, float volume, float pitch, boolean repeat, int repeatDelay, double x, double y, double z)
{
this(sound, volume, pitch, repeat, repeatDelay, x, y, z, AttenuationType.LINEAR);
}
public SoundBase(String sound, float volume, float pitch, boolean repeat, int repeatDelay, double x, double y, double z, AttenuationType attenuation)
{
this(new ResourceLocation(sound), volume, pitch, repeat, repeatDelay, x, y, z, attenuation);
}
public SoundBase(ResourceLocation sound)
{
this(sound, 0);
}
public SoundBase(ResourceLocation sound, float volume)
{
this(sound, volume, 0);
}
public SoundBase(ResourceLocation sound, float volume, float pitch)
{
this(sound, volume, pitch, false, 0);
}
public SoundBase(ResourceLocation sound, float volume, float pitch, boolean repeat, int repeatDelay)
{
this(sound, volume, pitch, repeat, repeatDelay, 0, 0, 0, AttenuationType.NONE);
}
public SoundBase(ResourceLocation sound, float volume, float pitch, double x, double y, double z)
{
this(sound, volume, pitch, false, 0, x, y, z);
}
public SoundBase(ResourceLocation sound, float volume, float pitch, boolean repeat, int repeatDelay, double x, double y, double z)
{
this(sound, volume, pitch, repeat, repeatDelay, x, y, z, AttenuationType.LINEAR);
}
public SoundBase(ResourceLocation sound, float volume, float pitch, boolean repeat, int repeatDelay, double x, double y, double z, AttenuationType attenuation)
{
this.attenuation = attenuation;
this.sound = sound;
this.volume = volume;
this.pitch = pitch;
this.x = (float) x;
this.y = (float) y;
this.z = (float) z;
this.repeat = repeat;
this.repeatDelay = repeatDelay;
}
public SoundBase(SoundBase other)
{
this.attenuation = other.attenuation;
this.sound = other.sound;
this.volume = other.volume;
this.pitch = other.pitch;
this.x = other.x;
this.y = other.y;
this.z = other.z;
this.repeat = other.repeat;
this.repeatDelay = other.repeatDelay;
}
@Override
public AttenuationType getAttenuationType()
{
return attenuation;
}
@Override
public ResourceLocation getPositionedSoundLocation()
{
return sound;
}
@Override
public float getVolume()
{
return volume;
}
@Override
public float getPitch()
{
return pitch;
}
@Override
public float getXPosF()
{
return x;
}
@Override
public float getYPosF()
{
return y;
}
@Override
public float getZPosF()
{
return z;
}
@Override
public boolean canRepeat()
{
return repeat;
}
@Override
public int getRepeatDelay()
{
return repeatDelay;
}
}

View file

@ -0,0 +1,878 @@
package techreborn.cofhLib.gui;
import techreborn.cofhLib.gui.element.TabBase;
import techreborn.cofhLib.render.RenderHelper;
import techreborn.cofhLib.util.helpers.StringHelper;
import cpw.mods.fml.client.FMLClientHandler;
import net.minecraft.client.audio.SoundHandler;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import net.minecraftforge.fluids.FluidStack;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* Base class for a modular GUIs. Works with Elements {@link techreborn.cofhLib.gui.element.ElementBase} and Tabs {@link techreborn.cofhLib.gui.element.TabBase} which are both modular elements.
*
* @author King Lemming
*/
public abstract class GuiBase extends GuiContainer
{
public static final SoundHandler guiSoundManager = FMLClientHandler.instance().getClient().getSoundHandler();
protected boolean drawTitle = true;
protected boolean drawInventory = true;
protected int mouseX = 0;
protected int mouseY = 0;
protected int lastIndex = -1;
protected String name;
protected ResourceLocation texture;
public ArrayList<TabBase> tabs = new ArrayList<TabBase>();
protected ArrayList<techreborn.cofhLib.gui.element.ElementBase> elements = new ArrayList<techreborn.cofhLib.gui.element.ElementBase>();
protected List<String> tooltip = new LinkedList<String>();
protected boolean tooltips = true;
public static void playSound(String name, float volume, float pitch)
{
guiSoundManager.playSound(new techreborn.cofhLib.audio.SoundBase(name, volume, pitch));
}
public GuiBase(Container container)
{
super(container);
}
public GuiBase(Container container, ResourceLocation texture)
{
super(container);
this.texture = texture;
}
@Override
public void initGui()
{
super.initGui();
tabs.clear();
elements.clear();
}
@Override
public void drawScreen(int x, int y, float partialTick)
{
updateElementInformation();
super.drawScreen(x, y, partialTick);
if (tooltips && mc.thePlayer.inventory.getItemStack() == null)
{
addTooltips(tooltip);
drawTooltip(tooltip);
}
mouseX = x - guiLeft;
mouseY = y - guiTop;
updateElements();
}
@Override
protected void drawGuiContainerForegroundLayer(int x, int y)
{
if (drawTitle)
{
fontRendererObj.drawString(StringHelper.localize(name), getCenteredOffset(StringHelper.localize(name)), 6, 0x404040);
}
if (drawInventory)
{
fontRendererObj.drawString(StatCollector.translateToLocal("container.inventory"), 8, ySize - 96 + 3, 0x404040);
}
drawElements(0, true);
drawTabs(0, true);
}
@Override
protected void drawGuiContainerBackgroundLayer(float partialTick, int x, int y)
{
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
bindTexture(texture);
drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
mouseX = x - guiLeft;
mouseY = y - guiTop;
GL11.glPushMatrix();
GL11.glTranslatef(guiLeft, guiTop, 0.0F);
drawElements(partialTick, false);
drawTabs(partialTick, false);
GL11.glPopMatrix();
}
@Override
protected void keyTyped(char characterTyped, int keyPressed)
{
for (int i = elements.size(); i-- > 0; )
{
techreborn.cofhLib.gui.element.ElementBase c = elements.get(i);
if (!c.isVisible() || !c.isEnabled())
{
continue;
}
if (c.onKeyTyped(characterTyped, keyPressed))
{
return;
}
}
super.keyTyped(characterTyped, keyPressed);
}
@Override
public void handleMouseInput()
{
int x = Mouse.getEventX() * width / mc.displayWidth;
int y = height - Mouse.getEventY() * height / mc.displayHeight - 1;
mouseX = x - guiLeft;
mouseY = y - guiTop;
int wheelMovement = Mouse.getEventDWheel();
if (wheelMovement != 0)
{
for (int i = elements.size(); i-- > 0; )
{
techreborn.cofhLib.gui.element.ElementBase c = elements.get(i);
if (!c.isVisible() || !c.isEnabled() || !c.intersectsWith(mouseX, mouseY))
{
continue;
}
if (c.onMouseWheel(mouseX, mouseY, wheelMovement))
{
return;
}
}
TabBase tab = getTabAtPosition(mouseX, mouseY);
if (tab != null && tab.onMouseWheel(mouseX, mouseY, wheelMovement))
{
return;
}
}
super.handleMouseInput();
}
@Override
protected void mouseClicked(int mX, int mY, int mouseButton)
{
mX -= guiLeft;
mY -= guiTop;
for (int i = elements.size(); i-- > 0; )
{
techreborn.cofhLib.gui.element.ElementBase c = elements.get(i);
if (!c.isVisible() || !c.isEnabled() || !c.intersectsWith(mX, mY))
{
continue;
}
if (c.onMousePressed(mX, mY, mouseButton))
{
return;
}
}
TabBase tab = getTabAtPosition(mX, mY);
if (tab != null)
{
int tMx = mX;
if (!tab.onMousePressed(tMx, mY, mouseButton))
{
for (int i = tabs.size(); i-- > 0; )
{
TabBase other = tabs.get(i);
if (other != tab && other.open && other.side == tab.side)
{
other.toggleOpen();
}
}
tab.toggleOpen();
return;
}
}
mX += guiLeft;
mY += guiTop;
if (tab != null)
{
switch (tab.side)
{
case TabBase.LEFT:
guiLeft -= tab.currentWidth;
break;
case TabBase.RIGHT:
xSize += tab.currentWidth;
break;
}
}
super.mouseClicked(mX, mY, mouseButton);
if (tab != null)
{
switch (tab.side)
{
case TabBase.LEFT:
guiLeft += tab.currentWidth;
break;
case TabBase.RIGHT:
xSize -= tab.currentWidth;
break;
}
}
}
@Override
protected void mouseMovedOrUp(int mX, int mY, int mouseButton)
{
mX -= guiLeft;
mY -= guiTop;
if (mouseButton >= 0 && mouseButton <= 2)
{ // 0:left, 1:right, 2: middle
for (int i = elements.size(); i-- > 0; )
{
techreborn.cofhLib.gui.element.ElementBase c = elements.get(i);
if (!c.isVisible() || !c.isEnabled())
{ // no bounds checking on mouseUp events
continue;
}
c.onMouseReleased(mX, mY);
}
}
mX += guiLeft;
mY += guiTop;
super.mouseMovedOrUp(mX, mY, mouseButton);
}
@Override
protected void mouseClickMove(int mX, int mY, int lastClick, long timeSinceClick)
{
Slot slot = getSlotAtPosition(mX, mY);
ItemStack itemstack = this.mc.thePlayer.inventory.getItemStack();
if (this.field_147007_t && slot != null && itemstack != null && slot instanceof techreborn.cofhLib.gui.slot.SlotFalseCopy)
{
if (lastIndex != slot.slotNumber)
{
lastIndex = slot.slotNumber;
this.handleMouseClick(slot, slot.slotNumber, 0, 0);
}
}
else
{
lastIndex = -1;
super.mouseClickMove(mX, mY, lastClick, timeSinceClick);
}
}
public Slot getSlotAtPosition(int xCoord, int yCoord)
{
for (int k = 0; k < this.inventorySlots.inventorySlots.size(); ++k)
{
Slot slot = (Slot) this.inventorySlots.inventorySlots.get(k);
if (this.isMouseOverSlot(slot, xCoord, yCoord))
{
return slot;
}
}
return null;
}
public boolean isMouseOverSlot(Slot theSlot, int xCoord, int yCoord)
{
return this.func_146978_c(theSlot.xDisplayPosition, theSlot.yDisplayPosition, 16, 16, xCoord, yCoord);
}
/**
* Draws the elements for this GUI.
*/
protected void drawElements(float partialTick, boolean foreground)
{
if (foreground)
{
for (int i = 0; i < elements.size(); i++)
{
techreborn.cofhLib.gui.element.ElementBase element = elements.get(i);
if (element.isVisible())
{
element.drawForeground(mouseX, mouseY);
}
}
}
else
{
for (int i = 0; i < elements.size(); i++)
{
techreborn.cofhLib.gui.element.ElementBase element = elements.get(i);
if (element.isVisible())
{
element.drawBackground(mouseX, mouseY, partialTick);
}
}
}
}
/**
* Draws the tabs for this GUI. Handles Tab open/close animation.
*/
protected void drawTabs(float partialTick, boolean foreground)
{
if (foreground)
{
return;
}
int yPosRight = 4;
int yPosLeft = 4;
for (int i = 0; i < tabs.size(); i++)
{
TabBase tab = tabs.get(i);
tab.update();
if (!tab.isVisible())
{
continue;
}
if (tab.side == TabBase.LEFT)
{
tab.draw(0, yPosLeft);
yPosLeft += tab.currentHeight;
}
else
{
tab.draw(xSize, yPosRight);
yPosRight += tab.currentHeight;
}
}
}
/**
* Called by NEI if installed
*/
// @Override
public List<String> handleTooltip(int mousex, int mousey, List<String> tooltip)
{
if (mc.thePlayer.inventory.getItemStack() == null)
{
addTooltips(tooltip);
}
return tooltip;
}
public void addTooltips(List<String> tooltip)
{
TabBase tab = getTabAtPosition(mouseX, mouseY);
if (tab != null)
{
tab.addTooltip(tooltip);
}
techreborn.cofhLib.gui.element.ElementBase element = getElementAtPosition(mouseX, mouseY);
if (element != null)
{
element.addTooltip(tooltip);
}
}
/* ELEMENTS */
public techreborn.cofhLib.gui.element.ElementBase addElement(techreborn.cofhLib.gui.element.ElementBase element)
{
elements.add(element);
return element;
}
public TabBase addTab(TabBase tab)
{
int yOffset = 4;
for (int i = 0; i < tabs.size(); i++)
{
if (tabs.get(i).side == tab.side && tabs.get(i).isVisible())
{
yOffset += tabs.get(i).currentHeight;
}
}
tab.setPosition(tab.side == TabBase.LEFT ? 0 : xSize, yOffset);
tabs.add(tab);
if (TabTracker.getOpenedLeftTab() != null && tab.getClass().equals(TabTracker.getOpenedLeftTab()))
{
tab.setFullyOpen();
}
else if (TabTracker.getOpenedRightTab() != null && tab.getClass().equals(TabTracker.getOpenedRightTab()))
{
tab.setFullyOpen();
}
return tab;
}
protected techreborn.cofhLib.gui.element.ElementBase getElementAtPosition(int mX, int mY)
{
for (int i = elements.size(); i-- > 0; )
{
techreborn.cofhLib.gui.element.ElementBase element = elements.get(i);
if (element.intersectsWith(mX, mY))
{
return element;
}
}
return null;
}
protected TabBase getTabAtPosition(int mX, int mY)
{
int xShift = 0;
int yShift = 4;
for (int i = 0; i < tabs.size(); i++)
{
TabBase tab = tabs.get(i);
if (!tab.isVisible() || tab.side == TabBase.RIGHT)
{
continue;
}
tab.setCurrentShift(xShift, yShift);
if (tab.intersectsWith(mX, mY, xShift, yShift))
{
return tab;
}
yShift += tab.currentHeight;
}
xShift = xSize;
yShift = 4;
for (int i = 0; i < tabs.size(); i++)
{
TabBase tab = tabs.get(i);
if (!tab.isVisible() || tab.side == TabBase.LEFT)
{
continue;
}
tab.setCurrentShift(xShift, yShift);
if (tab.intersectsWith(mX, mY, xShift, yShift))
{
return tab;
}
yShift += tab.currentHeight;
}
return null;
}
protected final void updateElements()
{
for (int i = elements.size(); i-- > 0; )
{
techreborn.cofhLib.gui.element.ElementBase c = elements.get(i);
if (c.isVisible() && c.isEnabled())
{
c.update(mouseX, mouseY);
}
}
}
protected void updateElementInformation()
{
}
public void handleElementButtonClick(String buttonName, int mouseButton)
{
}
/* HELPERS */
public void bindTexture(ResourceLocation texture)
{
mc.renderEngine.bindTexture(texture);
}
/**
* Abstract method to retrieve icons by name from a registry. You must override this if you use any of the String methods below.
*/
public IIcon getIcon(String name)
{
return null;
}
/**
* Essentially a placeholder method for tabs to use should they need to draw a button.
*/
public void drawButton(IIcon icon, int x, int y, int spriteSheet, int mode)
{
drawIcon(icon, x, y, spriteSheet);
}
public void drawButton(String iconName, int x, int y, int spriteSheet, int mode)
{
drawButton(getIcon(iconName), x, y, spriteSheet, mode);
}
/**
* Simple method used to draw a fluid of arbitrary size.
*/
public void drawFluid(int x, int y, FluidStack fluid, int width, int height)
{
if (fluid == null || fluid.getFluid() == null)
{
return;
}
RenderHelper.setBlockTextureSheet();
RenderHelper.setColor3ub(fluid.getFluid().getColor(fluid));
drawTiledTexture(x, y, fluid.getFluid().getIcon(fluid), width, height);
}
public void drawTiledTexture(int x, int y, IIcon icon, int width, int height)
{
int i = 0;
int j = 0;
int drawHeight = 0;
int drawWidth = 0;
for (i = 0; i < width; i += 16)
{
for (j = 0; j < height; j += 16)
{
drawWidth = Math.min(width - i, 16);
drawHeight = Math.min(height - j, 16);
drawScaledTexturedModelRectFromIcon(x + i, y + j, icon, drawWidth, drawHeight);
}
}
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0F);
}
public void drawIcon(IIcon icon, int x, int y, int spriteSheet)
{
if (spriteSheet == 0)
{
RenderHelper.setBlockTextureSheet();
}
else
{
RenderHelper.setItemTextureSheet();
}
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0F);
drawTexturedModelRectFromIcon(x, y, icon, 16, 16);
}
public void drawColorIcon(IIcon icon, int x, int y, int spriteSheet)
{
if (spriteSheet == 0)
{
RenderHelper.setBlockTextureSheet();
}
else
{
RenderHelper.setItemTextureSheet();
}
drawTexturedModelRectFromIcon(x, y, icon, 16, 16);
}
public void drawIcon(String iconName, int x, int y, int spriteSheet)
{
drawIcon(getIcon(iconName), x, y, spriteSheet);
}
public void drawSizedModalRect(int x1, int y1, int x2, int y2, int color)
{
int temp;
if (x1 < x2)
{
temp = x1;
x1 = x2;
x2 = temp;
}
if (y1 < y2)
{
temp = y1;
y1 = y2;
y2 = temp;
}
float a = (color >> 24 & 255) / 255.0F;
float r = (color >> 16 & 255) / 255.0F;
float g = (color >> 8 & 255) / 255.0F;
float b = (color & 255) / 255.0F;
Tessellator tessellator = Tessellator.instance;
GL11.glEnable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_TEXTURE_2D);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glColor4f(r, g, b, a);
tessellator.startDrawingQuads();
tessellator.addVertex(x1, y2, this.zLevel);
tessellator.addVertex(x2, y2, this.zLevel);
tessellator.addVertex(x2, y1, this.zLevel);
tessellator.addVertex(x1, y1, this.zLevel);
tessellator.draw();
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glDisable(GL11.GL_BLEND);
}
public void drawSizedRect(int x1, int y1, int x2, int y2, int color)
{
int temp;
if (x1 < x2)
{
temp = x1;
x1 = x2;
x2 = temp;
}
if (y1 < y2)
{
temp = y1;
y1 = y2;
y2 = temp;
}
float a = (color >> 24 & 255) / 255.0F;
float r = (color >> 16 & 255) / 255.0F;
float g = (color >> 8 & 255) / 255.0F;
float b = (color & 255) / 255.0F;
Tessellator tessellator = Tessellator.instance;
GL11.glDisable(GL11.GL_TEXTURE_2D);
GL11.glColor4f(r, g, b, a);
tessellator.startDrawingQuads();
tessellator.addVertex(x1, y2, this.zLevel);
tessellator.addVertex(x2, y2, this.zLevel);
tessellator.addVertex(x2, y1, this.zLevel);
tessellator.addVertex(x1, y1, this.zLevel);
tessellator.draw();
GL11.glEnable(GL11.GL_TEXTURE_2D);
}
public void drawSizedTexturedModalRect(int x, int y, int u, int v, int width, int height, float texW, float texH)
{
float texU = 1 / texW;
float texV = 1 / texH;
Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.addVertexWithUV(x + 0, y + height, this.zLevel, (u + 0) * texU, (v + height) * texV);
tessellator.addVertexWithUV(x + width, y + height, this.zLevel, (u + width) * texU, (v + height) * texV);
tessellator.addVertexWithUV(x + width, y + 0, this.zLevel, (u + width) * texU, (v + 0) * texV);
tessellator.addVertexWithUV(x + 0, y + 0, this.zLevel, (u + 0) * texU, (v + 0) * texV);
tessellator.draw();
}
public void drawScaledTexturedModelRectFromIcon(int x, int y, IIcon icon, int width, int height)
{
if (icon == null)
{
return;
}
double minU = icon.getMinU();
double maxU = icon.getMaxU();
double minV = icon.getMinV();
double maxV = icon.getMaxV();
Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.addVertexWithUV(x + 0, y + height, this.zLevel, minU, minV + (maxV - minV) * height / 16F);
tessellator.addVertexWithUV(x + width, y + height, this.zLevel, minU + (maxU - minU) * width / 16F, minV + (maxV - minV) * height / 16F);
tessellator.addVertexWithUV(x + width, y + 0, this.zLevel, minU + (maxU - minU) * width / 16F, minV);
tessellator.addVertexWithUV(x + 0, y + 0, this.zLevel, minU, minV);
tessellator.draw();
}
public void drawTooltip(List<String> list)
{
drawTooltipHoveringText(list, mouseX + guiLeft, mouseY + guiTop, fontRendererObj);
tooltip.clear();
}
@SuppressWarnings("rawtypes")
protected void drawTooltipHoveringText(List list, int x, int y, FontRenderer font)
{
if (list == null || list.isEmpty())
{
return;
}
GL11.glDisable(GL12.GL_RESCALE_NORMAL);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glDisable(GL11.GL_DEPTH_TEST);
int k = 0;
Iterator iterator = list.iterator();
while (iterator.hasNext())
{
String s = (String) iterator.next();
int l = font.getStringWidth(s);
if (l > k)
{
k = l;
}
}
int i1 = x + 12;
int j1 = y - 12;
int k1 = 8;
if (list.size() > 1)
{
k1 += 2 + (list.size() - 1) * 10;
}
if (i1 + k > this.width)
{
i1 -= 28 + k;
}
if (j1 + k1 + 6 > this.height)
{
j1 = this.height - k1 - 6;
}
this.zLevel = 300.0F;
itemRender.zLevel = 300.0F;
int l1 = -267386864;
this.drawGradientRect(i1 - 3, j1 - 4, i1 + k + 3, j1 - 3, l1, l1);
this.drawGradientRect(i1 - 3, j1 + k1 + 3, i1 + k + 3, j1 + k1 + 4, l1, l1);
this.drawGradientRect(i1 - 3, j1 - 3, i1 + k + 3, j1 + k1 + 3, l1, l1);
this.drawGradientRect(i1 - 4, j1 - 3, i1 - 3, j1 + k1 + 3, l1, l1);
this.drawGradientRect(i1 + k + 3, j1 - 3, i1 + k + 4, j1 + k1 + 3, l1, l1);
int i2 = 1347420415;
int j2 = (i2 & 16711422) >> 1 | i2 & -16777216;
this.drawGradientRect(i1 - 3, j1 - 3 + 1, i1 - 3 + 1, j1 + k1 + 3 - 1, i2, j2);
this.drawGradientRect(i1 + k + 2, j1 - 3 + 1, i1 + k + 3, j1 + k1 + 3 - 1, i2, j2);
this.drawGradientRect(i1 - 3, j1 - 3, i1 + k + 3, j1 - 3 + 1, i2, i2);
this.drawGradientRect(i1 - 3, j1 + k1 + 2, i1 + k + 3, j1 + k1 + 3, j2, j2);
for (int k2 = 0; k2 < list.size(); ++k2)
{
String s1 = (String) list.get(k2);
font.drawStringWithShadow(s1, i1, j1, -1);
if (k2 == 0)
{
j1 += 2;
}
j1 += 10;
}
this.zLevel = 0.0F;
itemRender.zLevel = 0.0F;
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL12.GL_RESCALE_NORMAL);
}
/**
* Passthrough method for tab use.
*/
public void mouseClicked(int mouseButton)
{
super.mouseClicked(guiLeft + mouseX, guiTop + mouseY, mouseButton);
}
public FontRenderer getFontRenderer()
{
return fontRendererObj;
}
protected int getCenteredOffset(String string)
{
return this.getCenteredOffset(string, xSize);
}
protected int getCenteredOffset(String string, int xWidth)
{
return (xWidth - fontRendererObj.getStringWidth(string)) / 2;
}
public int getGuiLeft()
{
return guiLeft;
}
public int getGuiTop()
{
return guiTop;
}
public int getMouseX()
{
return mouseX;
}
public int getMouseY()
{
return mouseY;
}
public void overlayRecipe()
{
}
}

View file

@ -0,0 +1,11 @@
package techreborn.cofhLib.gui;
import net.minecraft.client.gui.GuiScreen;
public class GuiBook extends GuiScreen
{
protected int mouseX = 0;
protected int mouseY = 0;
}

View file

@ -0,0 +1,127 @@
package techreborn.cofhLib.gui;
public class GuiColor extends Number
{
private static final long serialVersionUID = 7024827242888861187L;
private final int _color;
public GuiColor(int argb)
{
_color = argb;
}
public GuiColor(int rgba, Void dummy)
{
this(rgba >>> 24, rgba >> 16, rgba >> 8, rgba);
}
public GuiColor(byte alpha, int argb)
{
this(argb >> 16, argb >> 8, argb, alpha);
}
public GuiColor(int rgba, byte alpha)
{
this(rgba >>> 24, rgba >> 16, rgba >> 8, alpha);
}
public GuiColor(int r, int g, int b)
{
this(r, g, b, 255);
}
public GuiColor(int r, int g, int b, int a)
{
_color = (b & 0xFF) | (g & 0xFF) << 8 | (r & 0xFF) << 16 | (a & 0xFF) << 24;
}
public int getColor()
{
return _color;
}
public int getIntR()
{
return (_color >> 16) & 0xFF;
}
public int getIntG()
{
return (_color >> 8) & 0xFF;
}
public int getIntB()
{
return (_color >> 0) & 0xFF;
}
public int getIntA()
{
return (_color >> 24) & 0xFF;
}
public float getFloatR()
{
return getIntR() / 255f;
}
public float getFloatG()
{
return getIntG() / 255f;
}
public float getFloatB()
{
return getIntB() / 255f;
}
public float getFloatA()
{
return getIntA() / 255f;
}
@Override
public int intValue()
{
return getColor();
}
@Override
public long longValue()
{
return getColor();
}
@Override
public float floatValue()
{
return getColor();
}
@Override
public double doubleValue()
{
return getColor();
}
}

View file

@ -0,0 +1,16 @@
package techreborn.cofhLib.gui;
import com.pahimar.ee3.reference.Textures;
public class GuiProps
{
/* GUI */
public static final String PATH_GFX = Textures.RESOURCE_PREFIX + "textures/";
public static final String PATH_ARMOR = PATH_GFX + "armor/";
public static final String PATH_GUI = PATH_GFX + "gui/";
public static final String PATH_RENDER = PATH_GFX + "blocks/";
public static final String PATH_ELEMENTS = PATH_GUI + "elements/";
public static final String PATH_ICON = PATH_GUI + "icons/";
}

View file

@ -0,0 +1,40 @@
package techreborn.cofhLib.gui;
import techreborn.cofhLib.gui.element.TabBase;
/**
* Keeps track of which tabs should be open by default when a player opens a GUI.
*
* @author King Lemming
*/
public class TabTracker
{
private static Class<? extends TabBase> openedLeftTab;
private static Class<? extends TabBase> openedRightTab;
public static Class<? extends TabBase> getOpenedLeftTab()
{
return openedLeftTab;
}
public static Class<? extends TabBase> getOpenedRightTab()
{
return openedRightTab;
}
public static void setOpenedLeftTab(Class<? extends TabBase> tabClass)
{
openedLeftTab = tabClass;
}
public static void setOpenedRightTab(Class<? extends TabBase> tabClass)
{
openedRightTab = tabClass;
}
}

View file

@ -0,0 +1,257 @@
package techreborn.cofhLib.gui.element;
import techreborn.cofhLib.gui.GuiBase;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
import java.util.List;
/**
* Base class for a modular GUI element. Has self-contained rendering methods and a link back to the {@link techreborn.cofhLib.gui.GuiBase} it is a part of.
*
* @author King Lemming
*/
public abstract class ElementBase
{
protected GuiBase gui;
protected ResourceLocation texture;
protected int posX;
protected int posY;
protected int sizeX;
protected int sizeY;
protected int texW = 256;
protected int texH = 256;
protected String name;
private boolean visible = true;
private boolean enabled = true;
public ElementBase(GuiBase gui, int posX, int posY)
{
this.gui = gui;
this.posX = posX;
this.posY = posY;
}
public ElementBase(GuiBase gui, int posX, int posY, int width, int height)
{
this.gui = gui;
this.posX = posX;
this.posY = posY;
this.sizeX = width;
this.sizeY = height;
}
public ElementBase setName(String name)
{
this.name = name;
return this;
}
public ElementBase setPosition(int posX, int posY)
{
this.posX = posX;
this.posY = posY;
return this;
}
public ElementBase setSize(int sizeX, int sizeY)
{
this.sizeX = sizeX;
this.sizeY = sizeY;
return this;
}
public ElementBase setTexture(String texture, int texW, int texH)
{
this.texture = new ResourceLocation(texture);
this.texW = texW;
this.texH = texH;
return this;
}
public ElementBase setTexture(ResourceLocation texture, int texW, int texH)
{
this.texture = texture;
this.texW = texW;
this.texH = texH;
return this;
}
public final ElementBase setVisible(boolean visible)
{
this.visible = visible;
return this;
}
public boolean isVisible()
{
return visible;
}
public final ElementBase setEnabled(boolean enabled)
{
this.enabled = enabled;
return this;
}
public boolean isEnabled()
{
return enabled;
}
public void update(int mouseX, int mouseY)
{
update();
}
public void update()
{
}
public abstract void drawBackground(int mouseX, int mouseY, float gameTicks);
public abstract void drawForeground(int mouseX, int mouseY);
public void addTooltip(List<String> list)
{
}
public void drawModalRect(int x, int y, int width, int height, int color)
{
gui.drawSizedModalRect(x, y, width, height, color);
}
public void drawStencil(int xStart, int yStart, int xEnd, int yEnd, int flag)
{
GL11.glDisable(GL11.GL_TEXTURE_2D);
GL11.glStencilFunc(GL11.GL_ALWAYS, flag, flag);
GL11.glStencilOp(GL11.GL_KEEP, GL11.GL_KEEP, GL11.GL_REPLACE);
GL11.glStencilMask(1);
GL11.glColorMask(false, false, false, false);
GL11.glDepthMask(false);
Tessellator.instance.startDrawingQuads();
Tessellator.instance.addVertex(xStart, yEnd, 0);
Tessellator.instance.addVertex(xEnd, yEnd, 0);
Tessellator.instance.addVertex(xEnd, yStart, 0);
Tessellator.instance.addVertex(xStart, yStart, 0);
Tessellator.instance.draw();
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glStencilFunc(GL11.GL_EQUAL, flag, flag);
GL11.glStencilMask(0);
GL11.glColorMask(true, true, true, true);
GL11.glDepthMask(true);
}
public void drawTexturedModalRect(int x, int y, int u, int v, int width, int height)
{
gui.drawSizedTexturedModalRect(x, y, u, v, width, height, texW, texH);
}
public void drawCenteredString(FontRenderer fontRenderer, String text, int x, int y, int color)
{
fontRenderer.drawStringWithShadow(text, x - fontRenderer.getStringWidth(text) / 2, y, color);
}
public boolean onMousePressed(int mouseX, int mouseY, int mouseButton)
{
return false;
}
public void onMouseReleased(int mouseX, int mouseY)
{
}
public boolean onMouseWheel(int mouseX, int mouseY, int movement)
{
return false;
}
public boolean onKeyTyped(char characterTyped, int keyPressed)
{
return false;
}
public boolean intersectsWith(int mouseX, int mouseY)
{
return mouseX >= this.posX && mouseX <= this.posX + this.sizeX && mouseY >= this.posY && mouseY <= this.posY + this.sizeY;
}
public final String getName()
{
return name;
}
public final GuiBase getContainerScreen()
{
return gui;
}
public final FontRenderer getFontRenderer()
{
return gui.getFontRenderer();
}
/**
* This method is relative to the GUI's y coordinate
*/
public final int getPosY()
{
return posY;
}
/**
* This method is relative to the GUI's x coordinate
*/
public final int getPosX()
{
return posX;
}
public final int getHeight()
{
return sizeY;
}
public final int getWidth()
{
return sizeX;
}
}

View file

@ -0,0 +1,191 @@
package techreborn.cofhLib.gui.element;
import com.pahimar.ee3.util.ResourceLocationHelper;
import techreborn.cofhLib.render.RenderHelper;
import techreborn.cofhLib.util.helpers.StringHelper;
import net.minecraft.util.ResourceLocation;
import java.util.List;
public class ElementButton extends ElementBase
{
int sheetX;
int sheetY;
int hoverX;
int hoverY;
int disabledX = 0;
int disabledY = 0;
boolean tooltipLocalized = false;
String tooltip;
public ElementButton(techreborn.cofhLib.gui.GuiBase gui, int posX, int posY, String name, int sheetX, int sheetY, int hoverX, int hoverY, int sizeX, int sizeY, String texture) {
this(gui, posX, posY, name, sheetX, sheetY, hoverX, hoverY, sizeX, sizeY, 256, 256, texture);
}
public ElementButton(techreborn.cofhLib.gui.GuiBase gui, int posX, int posY, String name, int sheetX, int sheetY, int hoverX, int hoverY, int sizeX, int sizeY, ResourceLocation texture) {
this(gui, posX, posY, name, sheetX, sheetY, hoverX, hoverY, sizeX, sizeY, 256, 256, texture);
}
public ElementButton(techreborn.cofhLib.gui.GuiBase gui, int posX, int posY, String name, int sheetX, int sheetY, int hoverX, int hoverY, int sizeX, int sizeY, int texW, int texH, String texture) {
this(gui, posX, posY, name, sheetX, sheetY, hoverX, hoverY, sizeX, sizeY, texW, texH, ResourceLocationHelper.getResourceLocation(texture));
}
public ElementButton(techreborn.cofhLib.gui.GuiBase gui, int posX, int posY, String name, int sheetX, int sheetY, int hoverX, int hoverY, int sizeX, int sizeY, int texW, int texH, ResourceLocation texture) {
super(gui, posX, posY);
setName(name);
setSize(sizeX, sizeY);
setTexture(texture, texW, texH);
this.sheetX = sheetX;
this.sheetY = sheetY;
this.hoverX = hoverX;
this.hoverY = hoverY;
}
public ElementButton(techreborn.cofhLib.gui.GuiBase gui, int posX, int posY, String name, int sheetX, int sheetY, int hoverX, int hoverY, int disabledX, int disabledY, int sizeX, int sizeY, int texW, int texH, String texture) {
this(gui, posX, posY, name, sheetX, sheetY, hoverX, hoverY, disabledX, disabledY, sizeX, sizeY, texW, texH, ResourceLocationHelper.getResourceLocation(texture));
}
public ElementButton(techreborn.cofhLib.gui.GuiBase gui, int posX, int posY, String name, int sheetX, int sheetY, int hoverX, int hoverY, int disabledX, int disabledY, int sizeX, int sizeY, int texW, int texH, ResourceLocation texture) {
super(gui, posX, posY);
setName(name);
setSize(sizeX, sizeY);
setTexture(texture, texW, texH);
this.sheetX = sheetX;
this.sheetY = sheetY;
this.hoverX = hoverX;
this.hoverY = hoverY;
this.disabledX = disabledX;
this.disabledY = disabledY;
}
public ElementButton clearToolTip()
{
this.tooltip = null;
return this;
}
public ElementButton setToolTip(String tooltip)
{
this.tooltip = tooltip;
return this;
}
public ElementButton setToolTipLocalized(boolean localized)
{
this.tooltipLocalized = localized;
return this;
}
@Override
public void drawBackground(int mouseX, int mouseY, float gameTicks)
{
RenderHelper.bindTexture(texture);
if (isEnabled())
{
if (intersectsWith(mouseX, mouseY))
{
drawTexturedModalRect(posX, posY, hoverX, hoverY, sizeX, sizeY);
}
else
{
drawTexturedModalRect(posX, posY, sheetX, sheetY, sizeX, sizeY);
}
}
else
{
drawTexturedModalRect(posX, posY, disabledX, disabledY, sizeX, sizeY);
}
}
@Override
public void drawForeground(int mouseX, int mouseY)
{
}
@Override
public void addTooltip(List<String> list)
{
if (tooltip != null)
{
if (tooltipLocalized)
{
list.add(tooltip);
}
else
{
list.add(StringHelper.localize(tooltip));
}
}
}
@Override
public boolean onMousePressed(int x, int y, int mouseButton)
{
if (isEnabled())
{
gui.handleElementButtonClick(getName(), mouseButton);
return true;
}
return false;
}
public void setSheetX(int pos)
{
sheetX = pos;
}
public void setSheetY(int pos)
{
sheetY = pos;
}
public void setHoverX(int pos)
{
hoverX = pos;
}
public void setHoverY(int pos)
{
hoverY = pos;
}
public ElementButton setDisabledX(int pos)
{
disabledX = pos;
return this;
}
public ElementButton setDisabledY(int pos)
{
disabledY = pos;
return this;
}
public void setActive()
{
setEnabled(true);
}
public void setDisabled()
{
setEnabled(false);
}
}

View file

@ -0,0 +1,101 @@
package techreborn.cofhLib.gui.element;
import techreborn.cofhLib.gui.GuiProps;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
public abstract class ElementButtonManaged extends ElementBase
{
public static final ResourceLocation HOVER = new ResourceLocation(GuiProps.PATH_ELEMENTS + "Button_Hover.png");
public static final ResourceLocation ENABLED = new ResourceLocation(GuiProps.PATH_ELEMENTS + "Button_Enabled.png");
public static final ResourceLocation DISABLED = new ResourceLocation(GuiProps.PATH_ELEMENTS + "Button_Disabled.png");
private String _text;
public ElementButtonManaged(techreborn.cofhLib.gui.GuiBase containerScreen, int posX, int posY, int sizeX, int sizeY, String text)
{
super(containerScreen, posX, posY, sizeX, sizeY);
_text = text;
}
public void setText(String text)
{
_text = text;
}
@Override
public void drawBackground(int mouseX, int mouseY, float gameTicks)
{
if (!isEnabled())
{
gui.bindTexture(DISABLED);
}
else if (intersectsWith(mouseX, mouseY))
{
gui.bindTexture(HOVER);
}
else
{
gui.bindTexture(ENABLED);
}
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
drawTexturedModalRect(posX, posY, 0, 0, sizeX / 2, sizeY / 2);
drawTexturedModalRect(posX, posY + sizeY / 2, 0, 256 - sizeY / 2, sizeX / 2, sizeY / 2);
drawTexturedModalRect(posX + sizeX / 2, posY, 256 - sizeX / 2, 0, sizeX / 2, sizeY / 2);
drawTexturedModalRect(posX + sizeX / 2, posY + sizeY / 2, 256 - sizeX / 2, 256 - sizeY / 2, sizeX / 2, sizeY / 2);
}
@Override
public void drawForeground(int mouseX, int mouseY)
{
String text = getFontRenderer().trimStringToWidth(_text, sizeX - 4);
drawCenteredString(getFontRenderer(), text, posX + sizeX / 2, posY + (sizeY - 8) / 2, getTextColor(mouseX, mouseY));
}
protected int getTextColor(int mouseX, int mouseY)
{
if (!isEnabled())
{
return -6250336;
}
else if (intersectsWith(mouseX, mouseY))
{
return 16777120;
}
else
{
return 14737632;
}
}
@Override
public boolean onMousePressed(int mouseX, int mouseY, int mouseButton)
{
techreborn.cofhLib.gui.GuiBase.playSound("random.click", 1.0F, 1.0F);
if (mouseButton == 0)
{
onClick();
}
else if (mouseButton == 1)
{
onRightClick();
}
else if (mouseButton == 2)
{
onMiddleClick();
}
return true;
}
public abstract void onClick();
public void onRightClick()
{
}
public void onMiddleClick()
{
}
}

View file

@ -0,0 +1,84 @@
package techreborn.cofhLib.gui.element;
import java.util.HashMap;
import java.util.Map;
public abstract class ElementButtonOption extends ElementButtonManaged
{
private final Map<Integer, String> _values = new HashMap<Integer, String>();
private int _currentValue = 0;
private int _maxValue;
public ElementButtonOption(techreborn.cofhLib.gui.GuiBase containerScreen, int x, int y, int width, int height)
{
super(containerScreen, x, y, width, height, "");
}
public void setValue(int value, String label)
{
_values.put(value, label);
if (value > _maxValue)
{
_maxValue = value;
}
}
@Override
public void onClick()
{
int nextValue = _currentValue;
do
{
nextValue++;
if (nextValue > _maxValue)
{
nextValue = 0;
}
} while (_values.get(nextValue) == null);
setSelectedIndex(nextValue);
}
@Override
public void onRightClick()
{
int nextValue = _currentValue;
do
{
nextValue--;
if (nextValue < 0)
{
nextValue = _maxValue;
}
} while (_values.get(nextValue) == null);
setSelectedIndex(nextValue);
}
public int getSelectedIndex()
{
return _currentValue;
}
public void setSelectedIndex(int index)
{
_currentValue = index;
setText(_values.get(_currentValue));
onValueChanged(_currentValue, _values.get(_currentValue));
}
public String getValue()
{
return _values.get(_currentValue);
}
public abstract void onValueChanged(int value, String label);
}

View file

@ -0,0 +1,71 @@
package techreborn.cofhLib.gui.element;
import techreborn.cofhLib.render.RenderHelper;
public class ElementDualScaled extends ElementBase
{
public int quantity;
public int mode;
public boolean background = true;
public ElementDualScaled(techreborn.cofhLib.gui.GuiBase gui, int posX, int posY)
{
super(gui, posX, posY);
}
public ElementDualScaled setBackground(boolean background)
{
this.background = background;
return this;
}
public ElementDualScaled setMode(int mode)
{
this.mode = mode;
return this;
}
public ElementDualScaled setQuantity(int quantity)
{
this.quantity = quantity;
return this;
}
@Override
public void drawBackground(int mouseX, int mouseY, float gameTicks)
{
RenderHelper.bindTexture(texture);
if (background)
{
drawTexturedModalRect(posX, posY, 0, 0, sizeX, sizeY);
}
switch (mode)
{
case 0:
// vertical bottom -> top
drawTexturedModalRect(posX, posY + sizeY - quantity, sizeX, sizeY - quantity, sizeX, quantity);
return;
case 1:
// horizontal left -> right
drawTexturedModalRect(posX, posY, sizeX, 0, quantity, sizeY);
return;
case 2:
// horizontal right -> left
drawTexturedModalRect(posX + sizeX - quantity, posY, sizeX + sizeX - quantity, 0, quantity, sizeY);
}
}
@Override
public void drawForeground(int mouseX, int mouseY)
{
}
}

View file

@ -0,0 +1,367 @@
package techreborn.cofhLib.gui.element;
import techreborn.cofhLib.util.helpers.StringHelper;
import org.lwjgl.opengl.GL11;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
public class ElementListBox extends ElementBase
{
public int borderColor = new techreborn.cofhLib.gui.GuiColor(120, 120, 120, 255).getColor();
public int backgroundColor = new techreborn.cofhLib.gui.GuiColor(0, 0, 0, 255).getColor();
public int selectedLineColor = new techreborn.cofhLib.gui.GuiColor(0, 0, 0, 255).getColor();
public int textColor = new techreborn.cofhLib.gui.GuiColor(150, 150, 150, 255).getColor();
public int selectedTextColor = new techreborn.cofhLib.gui.GuiColor(255, 255, 255, 255).getColor();
private final int _marginTop = 2;
private final int _marginLeft = 2;
private final int _marginRight = 2;
private final int _marginBottom = 2;
private final List<techreborn.cofhLib.gui.element.listbox.IListBoxElement> _elements = new LinkedList<techreborn.cofhLib.gui.element.listbox.IListBoxElement>();
private int _firstIndexDisplayed;
private int _selectedIndex;
private int scrollHoriz;
public ElementListBox(techreborn.cofhLib.gui.GuiBase containerScreen, int x, int y, int width, int height)
{
super(containerScreen, x, y, width, height);
}
public void add(techreborn.cofhLib.gui.element.listbox.IListBoxElement element)
{
_elements.add(element);
}
public void add(Collection<? extends techreborn.cofhLib.gui.element.listbox.IListBoxElement> elements)
{
_elements.addAll(elements);
}
public void remove(techreborn.cofhLib.gui.element.listbox.IListBoxElement element)
{
_elements.remove(element);
}
public void removeAt(int index)
{
_elements.remove(index);
}
public int getInternalWidth()
{
int width = 0;
for (int i = 0; i < _elements.size(); i++)
{
width = Math.max(_elements.get(i).getWidth(), width);
}
return width;
}
public int getInternalHeight()
{
int height = 0;
for (int i = 0; i < _elements.size(); i++)
{
height += _elements.get(i).getHeight();
}
return height;
}
public int getContentWidth()
{
return sizeX - _marginLeft - _marginRight;
}
public int getContentHeight()
{
return sizeY - _marginTop - _marginBottom;
}
public int getContentTop()
{
return posY + _marginTop;
}
public int getContentLeft()
{
return posX + _marginLeft;
}
public final int getContentBottom()
{
return getContentTop() + getContentHeight();
}
public final int getContentRight()
{
return getContentLeft() + getContentWidth();
}
@Override
public void drawBackground(int mouseX, int mouseY, float gameTicks)
{
drawModalRect(posX - 1, posY - 1, posX + sizeX + 1, posY + sizeY + 1, borderColor);
drawModalRect(posX, posY, posX + sizeX, posY + sizeY, backgroundColor);
}
@Override
public void drawForeground(int mouseX, int mouseY)
{
int heightDrawn = 0;
int nextElement = _firstIndexDisplayed;
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glPushMatrix();
GL11.glEnable(GL11.GL_STENCIL_TEST);
GL11.glClear(GL11.GL_STENCIL_BUFFER_BIT);
drawStencil(getContentLeft(), getContentTop(), getContentRight(), getContentBottom(), 1);
GL11.glTranslated(-scrollHoriz, 0, 0);
int e = _elements.size();
while (nextElement < e && heightDrawn <= getContentHeight())
{
if (nextElement == _selectedIndex)
{
_elements.get(nextElement).draw(this, getContentLeft(), getContentTop() + heightDrawn, selectedLineColor, selectedTextColor);
}
else
{
_elements.get(nextElement).draw(this, getContentLeft(), getContentTop() + heightDrawn, backgroundColor, textColor);
}
heightDrawn += _elements.get(nextElement).getHeight();
nextElement++;
}
GL11.glDisable(GL11.GL_STENCIL_TEST);
GL11.glPopMatrix();
}
@Override
public boolean onMousePressed(int mouseX, int mouseY, int mouseButton)
{
int heightChecked = 0;
for (int i = _firstIndexDisplayed; i < _elements.size(); i++)
{
if (heightChecked > getContentHeight())
{
break;
}
int elementHeight = _elements.get(i).getHeight();
if (getContentTop() + heightChecked <= mouseY && getContentTop() + heightChecked + elementHeight >= mouseY)
{
setSelectedIndex(i);
onElementClicked(_elements.get(i));
break;
}
heightChecked += elementHeight;
}
return true;
}
@Override
public boolean onMouseWheel(int mouseX, int mouseY, int movement)
{
if (StringHelper.isControlKeyDown())
{
if (movement > 0)
{
scrollLeft();
}
else if (movement < 0)
{
scrollRight();
}
}
else
{
if (movement > 0)
{
scrollUp();
}
else if (movement < 0)
{
scrollDown();
}
}
return true;
}
public void scrollDown()
{
int heightDisplayed = 0;
int elementsDisplayed = 0;
for (int i = _firstIndexDisplayed; i < _elements.size(); i++)
{
if (heightDisplayed + _elements.get(i).getHeight() > sizeY)
{
break;
}
heightDisplayed += _elements.get(i).getHeight();
elementsDisplayed++;
}
if (_firstIndexDisplayed + elementsDisplayed < _elements.size())
{
_firstIndexDisplayed++;
}
onScrollV(_firstIndexDisplayed);
}
public void scrollUp()
{
if (_firstIndexDisplayed > 0)
{
_firstIndexDisplayed--;
}
onScrollV(_firstIndexDisplayed);
}
public void scrollLeft()
{
scrollHoriz = Math.max(scrollHoriz - 15, 0);
onScrollH(scrollHoriz);
}
public void scrollRight()
{
scrollHoriz = Math.min(scrollHoriz + 15, getLastScrollPositionH());
onScrollH(scrollHoriz);
}
public int getLastScrollPosition()
{
int position = _elements.size() - 1;
int heightUsed = _elements.get(position).getHeight();
while (position > 0 && heightUsed < sizeY)
{
position--;
heightUsed += _elements.get(position).getHeight();
}
return position + 1;
}
public int getLastScrollPositionH()
{
return Math.max(getInternalWidth() - getContentWidth(), 0);
}
public int getSelectedIndex()
{
return _selectedIndex;
}
public int getIndexOf(Object value)
{
for (int i = 0; i < _elements.size(); i++)
{
if (_elements.get(i).getValue().equals(value))
{
return i;
}
}
return -1;
}
public techreborn.cofhLib.gui.element.listbox.IListBoxElement getSelectedElement()
{
return _elements.get(_selectedIndex);
}
public void setSelectedIndex(int index)
{
if (index >= 0 && index < _elements.size() && index != _selectedIndex)
{
_selectedIndex = index;
onSelectionChanged(_selectedIndex, getSelectedElement());
}
}
public techreborn.cofhLib.gui.element.listbox.IListBoxElement getElement(int index)
{
return _elements.get(index);
}
public int getElementCount()
{
return _elements.size();
}
public void scrollToV(int index)
{
if (index >= 0 && index < _elements.size())
{
_firstIndexDisplayed = index;
}
}
public void scrollToH(int index)
{
if (index >= 0 && index <= getLastScrollPositionH())
{
scrollHoriz = index;
}
}
protected void onElementClicked(techreborn.cofhLib.gui.element.listbox.IListBoxElement element)
{
}
protected void onScrollV(int newStartIndex)
{
}
protected void onScrollH(int newStartIndex)
{
}
protected void onSelectionChanged(int newIndex, techreborn.cofhLib.gui.element.listbox.IListBoxElement newElement)
{
}
}

View file

@ -0,0 +1,45 @@
package techreborn.cofhLib.gui.element;
import techreborn.cofhLib.render.RenderHelper;
/**
* Basic element which can render an arbitrary texture.
*
* @author King Lemming
*/
public class ElementSimple extends ElementBase
{
int texU = 0;
int texV = 0;
public ElementSimple(techreborn.cofhLib.gui.GuiBase gui, int posX, int posY)
{
super(gui, posX, posY);
}
public ElementSimple setTextureOffsets(int u, int v)
{
texU = u;
texV = v;
return this;
}
@Override
public void drawBackground(int mouseX, int mouseY, float gameTicks)
{
RenderHelper.bindTexture(texture);
drawTexturedModalRect(posX, posY, texU, texV, sizeX, sizeY);
}
@Override
public void drawForeground(int mouseX, int mouseY)
{
return;
}
}

View file

@ -0,0 +1,224 @@
package techreborn.cofhLib.gui.element;
import com.pahimar.ee3.reference.Textures;
import techreborn.cofhLib.gui.GuiBase;
import techreborn.cofhLib.gui.GuiColor;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
public abstract class ElementSlider extends ElementBase
{
protected int _value;
protected int _valueMin;
protected int _valueMax;
protected int _sliderWidth;
protected int _sliderHeight;
protected boolean _isDragging;
public int borderColor = new GuiColor(120, 120, 120, 255).getColor();
public int backgroundColor = new GuiColor(0, 0, 0, 255).getColor();
protected ResourceLocation enabledTexture = Textures.Gui.Elements.BUTTON_ENABLED;
protected ResourceLocation hoverTexture = Textures.Gui.Elements.BUTTON_HOVER;
protected ResourceLocation disabledTexture = Textures.Gui.Elements.BUTTON_DISABLED;
protected ElementSlider(GuiBase containerScreen, int x, int y, int width, int height, int maxValue)
{
this(containerScreen, x, y, width, height, maxValue, 0);
}
protected ElementSlider(GuiBase containerScreen, int x, int y, int width, int height, int maxValue, int minValue)
{
super(containerScreen, x, y, width, height);
_valueMax = maxValue;
_valueMin = minValue;
}
protected ElementSlider(GuiBase containerScreen, String elementName, int x, int y, int width, int height, int maxValue, int minValue)
{
super(containerScreen, x, y, width, height);
this.name = elementName;
_valueMax = maxValue;
_valueMin = minValue;
}
public ElementSlider setColor(int backgroundColor, int borderColor)
{
this.borderColor = borderColor;
this.backgroundColor = backgroundColor;
return this;
}
public ElementSlider setSliderSize(int width, int height)
{
_sliderWidth = width;
_sliderHeight = height;
return this;
}
public int getValue()
{
return this._value;
}
public int getValueMin()
{
return this._valueMin;
}
public int getValueMax()
{
return this._valueMax;
}
public ElementSlider setValue(int value)
{
value = Math.max(_valueMin, Math.min(_valueMax, value));
if (value != _value)
{
_value = value;
onValueChanged(_value);
}
return this;
}
public ElementSlider setMinValue(int minValue)
{
_valueMin = minValue;
return this;
}
public ElementSlider setMaxValue(int maxValue)
{
_valueMax = maxValue;
return this;
}
@Override
public void drawBackground(int mouseX, int mouseY, float gameTicks)
{
drawModalRect(posX - 1, posY - 1, posX + sizeX + 1, posY + sizeY + 1, borderColor);
drawModalRect(posX, posY, posX + sizeX, posY + sizeY, backgroundColor);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
}
protected void drawSlider(int mouseX, int mouseY, int sliderX, int sliderY)
{
int sliderMidX = _sliderWidth / 2;
int sliderMidY = _sliderHeight / 2;
int sliderEndX = _sliderWidth - sliderMidX;
int sliderEndY = _sliderHeight - sliderMidY;
if (!isEnabled())
{
gui.bindTexture(disabledTexture);
}
else if (isHovering(mouseX, mouseY))
{
gui.bindTexture(hoverTexture);
}
else
{
gui.bindTexture(enabledTexture);
}
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
drawTexturedModalRect(sliderX, sliderY, 0, 0, sliderMidX, sliderMidY);
drawTexturedModalRect(sliderX, sliderY + sliderMidY, 0, 256 - sliderEndY, sliderMidX, sliderEndY);
drawTexturedModalRect(sliderX + sliderMidX, sliderY, 256 - sliderEndX, 0, sliderEndX, sliderMidY);
drawTexturedModalRect(sliderX + sliderMidX, sliderY + sliderMidY, 256 - sliderEndX, 256 - sliderEndY, sliderEndX, sliderEndY);
}
@Override
public void drawForeground(int mouseX, int mouseY)
{
int sliderX = posX + getSliderX();
int sliderY = posY + getSliderY();
drawSlider(mouseX, mouseY, sliderX, sliderY);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
}
protected boolean isHovering(int x, int y)
{
return intersectsWith(x, y);
}
public int getSliderX()
{
return 0;
}
public int getSliderY()
{
return 0;
}
@Override
public boolean onMousePressed(int mouseX, int mouseY, int mouseButton)
{
_isDragging = mouseButton == 0;
update(mouseX, mouseY);
return true;
}
@Override
public void onMouseReleased(int mouseX, int mouseY)
{
if (_isDragging)
{
onStopDragging();
}
_isDragging = false;
}
@Override
public void update(int mouseX, int mouseY)
{
if (_isDragging)
{
dragSlider(mouseX - posX, mouseY - posY);
}
}
protected abstract void dragSlider(int x, int y);
@Override
public boolean onMouseWheel(int mouseX, int mouseY, int movement)
{
if (movement > 0)
{
setValue(_value - 1);
}
else if (movement < 0)
{
setValue(_value + 1);
}
return true;
}
public void onValueChanged(int value)
{
return;
}
public void onStopDragging()
{
return;
}
public void setEnabledTexture(ResourceLocation enabledTexture)
{
this.enabledTexture = enabledTexture;
}
public void setDisabledTexture(ResourceLocation disabledTexture)
{
this.disabledTexture = disabledTexture;
}
public void setHoverTexture(ResourceLocation hoverTexture)
{
this.hoverTexture = hoverTexture;
}
}

View file

@ -0,0 +1,781 @@
package techreborn.cofhLib.gui.element;
import techreborn.cofhLib.gui.GuiBase;
import techreborn.cofhLib.gui.GuiColor;
import techreborn.cofhLib.util.helpers.MathHelper;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.util.ChatAllowedCharacters;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;
import static org.lwjgl.opengl.GL11.*;
public class ElementTextField extends ElementBase
{
public int borderColor = new GuiColor(55, 55, 55).getColor();
public int backgroundColor = new GuiColor(139, 139, 139).getColor();
public int disabledColor = new GuiColor(198, 198, 198).getColor();
public int selectedLineColor = new GuiColor(160, 160, 224).getColor();
public int textColor = new GuiColor(224, 224, 224).getColor();
public int selectedTextColor = new GuiColor(224, 224, 224).getColor();
public int defaultCaretColor = new GuiColor(255, 255, 255).getColor();
protected char[] text;
protected int textLength;
protected int selectionStart, selectionEnd;
protected int renderStart, caret;
private int paddingLeft, paddingRight, paddingTop, paddingBottom;
private boolean isFocused;
private boolean canFocusChange = true;
private boolean selecting;
private byte caretCounter;
protected boolean caretInsert;
protected boolean smartCaret = true;
protected boolean smartCaretCase = true;
protected boolean enableStencil = true;
public ElementTextField(GuiBase gui, int posX, int posY, int width, int height)
{
this(gui, posX, posY, width, height, (short) 32);
}
public ElementTextField(GuiBase gui, int posX, int posY, int width, int height, short limit)
{
super(gui, posX, posY, width, height);
setPadding(1, 1, 1, 1);
setMaxLength(limit);
}
public ElementTextField(GuiBase gui, int posX, int posY, String name, int width, int height)
{
this(gui, posX, posY, name, width, height, (short) 32);
}
public ElementTextField(GuiBase gui, int posX, int posY, String name, int width, int height, short limit)
{
super(gui, posX, posY, width, height);
setName(name);
setPadding(1, 1, 1, 1);
setMaxLength(limit);
}
public ElementTextField setTextColor(Number textColor, Number selectedTextColor)
{
if (textColor != null)
{
this.textColor = textColor.intValue();
}
if (selectedTextColor != null)
{
this.selectedTextColor = selectedTextColor.intValue();
}
return this;
}
public ElementTextField setPaddingLeft(int paddingLeft)
{
this.paddingLeft = paddingLeft;
return this;
}
public ElementTextField setPaddingRight(int paddingRight)
{
this.paddingRight = paddingRight;
return this;
}
public ElementTextField setPaddingTop(int paddingTop)
{
this.paddingTop = paddingTop;
return this;
}
public ElementTextField setPaddingBottom(int paddingBottom)
{
this.paddingBottom = paddingBottom;
return this;
}
public ElementTextField setPadding(int paddingTop, int paddingRight, int paddingBottom, int paddingLeft)
{
this.paddingTop = paddingTop;
this.paddingRight = paddingRight;
this.paddingBottom = paddingBottom;
this.paddingLeft = paddingLeft;
return this;
}
public ElementTextField setSelectionColor(Number selectedLineColor, Number defaultCaretColor)
{
if (selectedLineColor != null)
{
this.selectedLineColor = selectedLineColor.intValue();
}
if (defaultCaretColor != null)
{
this.defaultCaretColor = defaultCaretColor.intValue();
}
return this;
}
public ElementTextField setBackgroundColor(Number backgroundColor, Number disabledColor, Number borderColor)
{
if (backgroundColor != null)
{
this.backgroundColor = backgroundColor.intValue();
}
if (disabledColor != null)
{
this.disabledColor = disabledColor.intValue();
}
if (borderColor != null)
{
this.borderColor = borderColor.intValue();
}
return this;
}
public ElementTextField setFocusable(boolean focusable)
{
canFocusChange = focusable;
return this;
}
public ElementTextField setFocused(boolean focused)
{
if (canFocusChange)
{
isFocused = focused;
caretCounter = 0;
}
return this;
}
public ElementTextField setText(String text)
{
selectionStart = 0;
selectionEnd = textLength;
writeText(text);
return this;
}
public ElementTextField setMaxLength(short limit)
{
char[] oldText = text;
text = new char[limit];
textLength = Math.min(limit, textLength);
if (oldText != null)
{
System.arraycopy(oldText, 0, text, 0, textLength);
}
findRenderStart();
return this;
}
public int getMaxStringLength()
{
return text.length;
}
public boolean isFocused()
{
return isEnabled() && isFocused;
}
public boolean isFocusable()
{
return canFocusChange;
}
public int getContentWidth()
{
FontRenderer font = getFontRenderer();
int width = 0;
for (int i = 0; i < textLength; ++i)
{
width += font.getCharWidth(text[i]);
}
return width;
}
public int getPaddingLeft()
{
return paddingLeft;
}
public int getPaddingRight()
{
return paddingRight;
}
public int getPaddingTop()
{
return paddingTop;
}
public int getPaddingBottom()
{
return paddingBottom;
}
public int getVisibleWidth()
{
FontRenderer font = getFontRenderer();
int width = 0, endX = sizeX - 1;
for (int i = renderStart; i < textLength; ++i)
{
int charW = font.getCharWidth(text[i]);
if (!enableStencil && (width + charW) > endX)
{
break;
}
width += charW;
if (width >= endX)
{
width = Math.min(width, endX);
break;
}
}
return width;
}
public String getText()
{
return new String(text, 0, textLength);
}
public String getSelectedText()
{
if (selectionStart != selectionEnd)
{
return new String(text, selectionStart, selectionEnd);
}
return getText();
}
public void writeText(String text)
{
int i = 0;
for (int e = text.length(); i < e; ++i)
{
if (!insertCharacter(text.charAt(i)))
{
break;
}
}
clearSelection();
findRenderStart();
onCharacterEntered(i > 0);
}
public boolean isAllowedCharacter(char charTyped)
{
return ChatAllowedCharacters.isAllowedCharacter(charTyped);
}
protected boolean onEnter()
{
return false;
}
protected void onFocusLost()
{
}
protected void onCharacterEntered(boolean success)
{
}
protected boolean insertCharacter(char charTyped)
{
if (isAllowedCharacter(charTyped))
{
if (selectionStart != selectionEnd)
{
if (caret == selectionStart)
{
++caret;
}
text[selectionStart++] = charTyped;
return true;
}
if ((caretInsert && caret == text.length) || textLength == text.length)
{
return false;
}
if (!caretInsert)
{
if (caret < textLength)
{
System.arraycopy(text, caret, text, caret + 1, textLength - caret);
}
++textLength;
}
text[caret++] = charTyped;
return true;
}
else
{
return true;
}
}
protected void findRenderStart()
{
caret = MathHelper.clampI(caret, 0, textLength);
if (caret < renderStart)
{
renderStart = caret;
return;
}
FontRenderer font = getFontRenderer();
int endX = sizeX - 2;
for (int i = renderStart, width = 0; i < caret; ++i)
{
width += font.getCharWidth(text[i]);
while (width >= endX)
{
width -= font.getCharWidth(text[renderStart++]);
if (renderStart >= textLength)
{
return;
}
}
}
}
protected void clearSelection()
{
if (selectionStart != selectionEnd)
{
if (selectionEnd < textLength)
{
System.arraycopy(text, selectionEnd, text, selectionStart, textLength - selectionEnd);
}
textLength -= selectionEnd - selectionStart;
selectionEnd = caret = selectionStart;
findRenderStart();
}
}
protected final int seekNextCaretLocation(int pos)
{
return seekNextCaretLocation(pos, true);
}
protected int seekNextCaretLocation(int pos, boolean forward)
{
int dir = forward ? 1 : -1;
int e = forward ? textLength : 0;
if (pos == textLength)
{
--pos;
}
char prevChar = text[pos];
while (pos != e && Character.isSpaceChar(prevChar))
{
prevChar = text[pos += dir];
}
if (smartCaret)
{
for (int i = pos; i != e; i += dir)
{
char curChar = text[i];
boolean caze = Character.isUpperCase(curChar) != Character.isUpperCase(prevChar);
if (caze || Character.isSpaceChar(curChar) != Character.isSpaceChar(prevChar) ||
Character.isLetterOrDigit(curChar) != Character.isLetterOrDigit(prevChar))
{
if ((pos + dir) != i || !Character.isLetterOrDigit(curChar))
{
return i + (smartCaretCase && caze && Character.isUpperCase(prevChar) ? -dir : 0);
}
}
prevChar = curChar;
}
}
for (int i = pos; i != e; i += dir)
{
char curChar = text[i];
if (Character.isSpaceChar(curChar) != Character.isSpaceChar(prevChar))
{
return i;
}
}
return forward ? textLength : 0;
}
@Override
public boolean onKeyTyped(char charTyped, int keyTyped)
{
if (!isFocused())
{
return false;
}
switch (charTyped)
{
case 1: // ^A
selectionEnd = caret = textLength;
selectionStart = 0;
findRenderStart();
return true;
case 3: // ^C
if (selectionStart != selectionEnd)
{
GuiScreen.setClipboardString(getSelectedText());
}
return true;
case 24: // ^X
if (selectionStart != selectionEnd)
{
GuiScreen.setClipboardString(getSelectedText());
clearSelection();
}
return true;
case 22: // ^V
writeText(GuiScreen.getClipboardString());
return true;
default:
switch (keyTyped)
{
case Keyboard.KEY_ESCAPE:
setFocused(false);
return !isFocused();
case Keyboard.KEY_RETURN:
case Keyboard.KEY_NUMPADENTER:
return onEnter();
case Keyboard.KEY_INSERT:
if (GuiScreen.isShiftKeyDown())
{
writeText(GuiScreen.getClipboardString());
}
else
{
caretInsert = !caretInsert;
}
return true;
case Keyboard.KEY_CLEAR: // mac only (clear selection)
clearSelection();
return true;
case Keyboard.KEY_DELETE: // delete
if (!GuiScreen.isShiftKeyDown())
{
if (selectionStart != selectionEnd)
{
clearSelection();
}
else if (GuiScreen.isCtrlKeyDown())
{
int size = seekNextCaretLocation(caret, true) - caret;
selectionStart = caret;
selectionEnd = caret + size;
clearSelection();
}
else
{
if (caret < textLength && textLength > 0)
{
--textLength;
System.arraycopy(text, caret + 1, text, caret, textLength - caret);
}
}
if (caret <= renderStart)
{
renderStart = MathHelper.clampI(caret - 3, 0, textLength);
}
findRenderStart();
onCharacterEntered(true);
return true;
}
// continue.. (shift+delete = backspace)
case Keyboard.KEY_BACK: // backspace
if (selectionStart != selectionEnd)
{
clearSelection();
}
else if (GuiScreen.isCtrlKeyDown())
{
int size = seekNextCaretLocation(caret, false) - caret;
selectionStart = caret + size;
selectionEnd = caret;
clearSelection();
}
else
{
if (caret > 0 && textLength > 0)
{
--caret;
System.arraycopy(text, caret + 1, text, caret, textLength - caret);
--textLength;
}
}
if (caret <= renderStart)
{
renderStart = MathHelper.clampI(caret - 3, 0, textLength);
}
findRenderStart();
onCharacterEntered(true);
return true;
case Keyboard.KEY_HOME: // home
if (GuiScreen.isShiftKeyDown())
{
if (caret > selectionEnd)
{
selectionEnd = selectionStart;
}
selectionStart = 0;
}
else
{
selectionStart = selectionEnd = 0;
}
renderStart = caret = 0;
return true;
case Keyboard.KEY_END: // end
if (GuiScreen.isShiftKeyDown())
{
if (caret < selectionStart)
{
selectionStart = selectionEnd;
}
selectionEnd = textLength;
}
else
{
selectionStart = selectionEnd = textLength;
}
caret = textLength;
findRenderStart();
return true;
case Keyboard.KEY_LEFT: // left arrow
case Keyboard.KEY_RIGHT: // right arrow
int size = keyTyped == 203 ? -1 : 1;
if (GuiScreen.isCtrlKeyDown())
{
size = seekNextCaretLocation(caret, keyTyped == 205) - caret;
}
if (selectionStart == selectionEnd || !GuiScreen.isShiftKeyDown())
{
selectionStart = selectionEnd = caret;
}
{
int t = caret;
caret = MathHelper.clampI(caret + size, 0, textLength);
size = caret - t;
}
findRenderStart();
if (GuiScreen.isShiftKeyDown())
{
if (caret == selectionStart + size)
{
selectionStart = caret;
}
else if (caret == selectionEnd + size)
{
selectionEnd = caret;
}
// this logic is 'broken' in that the selection doesn't wrap
// such that a|bc|def becomes abc|def| but it will highlight
// the rest of the word the caret is on i.e., a|bc|def -> a|bcdef|
// i don't know that it matters (home+end exhibit the former)
if (selectionStart > selectionEnd)
{
int t = selectionStart;
selectionStart = selectionEnd;
selectionEnd = t;
}
}
return true;
default:
if (isAllowedCharacter(charTyped))
{
boolean typed = insertCharacter(charTyped);
clearSelection();
findRenderStart();
onCharacterEntered(typed);
return true;
}
else
{
return false;
}
}
}
}
@Override
public boolean onMousePressed(int mouseX, int mouseY, int mouseButton)
{
selecting = mouseButton == 0;
l:
if (selecting)
{
if (textLength == 0)
{
selectionStart = selectionEnd = caret = 0;
break l;
}
FontRenderer font = getFontRenderer();
int pos = mouseX - posX - 1;
for (int i = renderStart, width = 0; ; )
{
int charW = font.getCharWidth(text[i]);
if ((width += charW) > pos || ++i >= textLength)
{
selectionStart = selectionEnd = caret = i;
break;
}
}
}
setFocused(true);
return true;
}
@Override
public void update(int mouseX, int mouseY)
{
++caretCounter;
//if (selecting) {
// FontRenderer font = getFontRenderer();
// int pos = mouseX - posX - 1;
// for (int i = renderStart, width = 0; i < textLength; ++i) {
// }
//}
}
@Override
public void onMouseReleased(int mouseX, int mouseY)
{
if (!selecting)
{
boolean focus = isFocused();
setFocused(false);
if (focus && !isFocused())
{
onFocusLost();
}
}
selecting = false;
}
@Override
public void drawBackground(int mouseX, int mouseY, float gameTicks)
{
drawModalRect(posX - 1, posY - 1, posX + sizeX + 1, posY + sizeY + 1, borderColor);
drawModalRect(posX, posY, posX + sizeX, posY + sizeY, isEnabled() ? backgroundColor : disabledColor);
}
@Override
public void drawForeground(int mouseX, int mouseY)
{
if (enableStencil)
{
glEnable(GL_STENCIL_TEST);
glClear(GL_STENCIL_BUFFER_BIT);
drawStencil(posX + 1, posY + 1, posX + sizeX - 1, posY + sizeY - 1, 1);
}
FontRenderer font = getFontRenderer();
char[] text = this.text;
int startX = posX + paddingLeft, endX = sizeX - paddingRight, startY = posY + paddingTop, endY = startY + font.FONT_HEIGHT + paddingBottom;
for (int i = renderStart, width = 0; i <= textLength; ++i)
{
boolean end = i == textLength;
int charW = 2;
if (!end)
{
charW = font.getCharWidth(text[i]);
if (!enableStencil && (width + charW) > endX)
{
break;
}
}
boolean drawCaret = i == caret && (caretCounter &= 31) < 16 && isFocused();
if (drawCaret)
{
int caretEnd = width + 2;
if (caretInsert)
{
caretEnd = width + charW;
}
drawModalRect(startX + width, startY - 1, startX + caretEnd, endY, (0xFF000000 & defaultCaretColor) | (~defaultCaretColor & 0xFFFFFF));
}
if (!end)
{
boolean selected = i >= selectionStart & i < selectionEnd;
if (selected)
{
drawModalRect(startX + width, startY, startX + width + charW, endY, selectedLineColor);
}
font.drawStringWithShadow(String.valueOf(text[i]), startX + width, startY, selected ? selectedTextColor : textColor);
}
if (drawCaret)
{
int caretEnd = width + 2;
if (caretInsert)
{
caretEnd = width + charW;
}
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ZERO);
gui.drawSizedModalRect(startX + width, startY - 1, startX + caretEnd, endY, -1);
GL11.glDisable(GL11.GL_BLEND);
}
width += charW;
if (width > endX)
{
break;
}
}
if (enableStencil)
{
glDisable(GL_STENCIL_TEST);
}
}
}

View file

@ -0,0 +1,48 @@
package techreborn.cofhLib.gui.element;
import techreborn.cofhLib.util.CharacterSingleton;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ElementTextFieldFiltered extends ElementTextField
{
protected boolean includeVanilla = true;
protected CharacterSingleton seq = new CharacterSingleton();
protected Matcher filter;
public ElementTextFieldFiltered(techreborn.cofhLib.gui.GuiBase gui, int posX, int posY, int width, int height)
{
super(gui, posX, posY, width, height);
}
public ElementTextFieldFiltered(techreborn.cofhLib.gui.GuiBase gui, int posX, int posY, int width, int height, short limit)
{
super(gui, posX, posY, width, height, limit);
}
/**
* @param pattern Regex limit what characters can be typed
* @param includeVanilla Include vanilla disallowed characters
* @return this
*/
public ElementTextFieldFiltered setFilter(Pattern pattern, boolean includeVanilla)
{
filter = pattern.matcher(seq);
this.includeVanilla = includeVanilla;
return this;
}
@Override
public boolean isAllowedCharacter(char charTyped)
{
seq.character = charTyped;
return (!includeVanilla || super.isAllowedCharacter(charTyped)) && (filter == null || filter.reset().matches());
}
}

View file

@ -0,0 +1,42 @@
package techreborn.cofhLib.gui.element;
public class ElementTextFieldLimited extends ElementTextField
{
protected boolean includeVanilla = true;
protected String filter;
public ElementTextFieldLimited(techreborn.cofhLib.gui.GuiBase gui, int posX, int posY, int width, int height)
{
super(gui, posX, posY, width, height);
}
public ElementTextFieldLimited(techreborn.cofhLib.gui.GuiBase gui, int posX, int posY, int width, int height, short limit)
{
super(gui, posX, posY, width, height, limit);
}
/**
* @param pattern String containing all characters permitted
* @param includeVanilla Include vanilla disallowed characters
* @return this
*/
public ElementTextFieldLimited setFilter(String pattern, boolean includeVanilla)
{
filter = pattern;
this.includeVanilla = includeVanilla;
return this;
}
@Override
public boolean isAllowedCharacter(char charTyped)
{
return (!includeVanilla || super.isAllowedCharacter(charTyped)) && (filter == null || filter.indexOf(charTyped) >= 0);
}
}

View file

@ -0,0 +1,287 @@
package techreborn.cofhLib.gui.element;
import techreborn.cofhLib.render.RenderHelper;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
/**
* Base class for a tab element. Has self-contained rendering methods and a link back to the {@link techreborn.cofhLib.gui.GuiBase} it is a part of.
*
* @author King Lemming
*/
public abstract class TabBase extends ElementBase
{
public static int tabExpandSpeed = 8;
public static final int LEFT = 0;
public static final int RIGHT = 1;
protected int offsetX = 0;
protected int offsetY = 0;
public boolean open;
public boolean fullyOpen;
public int side = RIGHT;
public int headerColor = 0xe1c92f;
public int subheaderColor = 0xaaafb8;
public int textColor = 0x000000;
public int backgroundColor = 0xffffff;
protected int currentShiftX = 0;
protected int currentShiftY = 0;
public int minWidth = 22;
public int maxWidth = 124;
public int currentWidth = minWidth;
public int minHeight = 22;
public int maxHeight = 22;
public int currentHeight = minHeight;
public static final ResourceLocation DEFAULT_TEXTURE_LEFT = new ResourceLocation(techreborn.cofhLib.gui.GuiProps.PATH_ELEMENTS + "Tab_Left.png");
public static final ResourceLocation DEFAULT_TEXTURE_RIGHT = new ResourceLocation(techreborn.cofhLib.gui.GuiProps.PATH_ELEMENTS + "Tab_Right.png");
public TabBase(techreborn.cofhLib.gui.GuiBase gui)
{
super(gui, 0, 0);
texture = DEFAULT_TEXTURE_RIGHT;
}
public TabBase(techreborn.cofhLib.gui.GuiBase gui, int side)
{
super(gui, 0, 0);
this.side = side;
if (side == LEFT)
{
texture = DEFAULT_TEXTURE_LEFT;
}
else
{
texture = DEFAULT_TEXTURE_RIGHT;
}
}
public TabBase setOffsets(int x, int y)
{
offsetX = x;
offsetY = y;
return this;
}
public void draw(int x, int y)
{
posX = x + offsetX;
posY = y + offsetY;
draw();
}
public void draw()
{
return;
}
@Override
public void drawBackground(int mouseX, int mouseY, float gameTicks)
{
}
@Override
public void drawForeground(int mouseX, int mouseY)
{
}
@Override
public void update()
{
if (open && currentWidth < maxWidth)
{
currentWidth += tabExpandSpeed;
}
else if (!open && currentWidth > minWidth)
{
currentWidth -= tabExpandSpeed;
}
if (currentWidth > maxWidth)
{
currentWidth = maxWidth;
}
else if (currentWidth < minWidth)
{
currentWidth = minWidth;
}
if (open && currentHeight < maxHeight)
{
currentHeight += tabExpandSpeed;
}
else if (!open && currentHeight > minHeight)
{
currentHeight -= tabExpandSpeed;
}
if (currentHeight > maxHeight)
{
currentHeight = maxHeight;
}
else if (currentHeight < minHeight)
{
currentHeight = minHeight;
}
if (!fullyOpen && open && currentWidth == maxWidth && currentHeight == maxHeight)
{
setFullyOpen();
}
}
protected void drawBackground()
{
float colorR = (backgroundColor >> 16 & 255) / 255.0F;
float colorG = (backgroundColor >> 8 & 255) / 255.0F;
float colorB = (backgroundColor & 255) / 255.0F;
GL11.glColor4f(colorR, colorG, colorB, 1.0F);
RenderHelper.bindTexture(texture);
int xPosition = posX();
gui.drawTexturedModalRect(xPosition, posY + 4, 0, 256 - currentHeight + 4, 4, currentHeight - 4);
gui.drawTexturedModalRect(xPosition + 4, posY, 256 - currentWidth + 4, 0, currentWidth - 4, 4);
gui.drawTexturedModalRect(xPosition, posY, 0, 0, 4, 4);
gui.drawTexturedModalRect(xPosition + 4, posY + 4, 256 - currentWidth + 4, 256 - currentHeight + 4, currentWidth - 4, currentHeight - 4);
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0F);
}
protected void drawTabIcon(String iconName)
{
gui.drawIcon(iconName, posXOffset(), posY + 3, 1);
}
/**
* Shortcut to correct for the proper X position.
*/
protected int posX()
{
if (side == LEFT)
{
return posX - currentWidth;
}
return posX;
}
/**
* Corrects for shadowing differences in tabs to ensure that they always look nice - used in font rendering, typically.
*/
protected int posXOffset()
{
return posX() + sideOffset();
}
protected int sideOffset()
{
return (side == LEFT ? 4 : 2);
}
public boolean intersectsWith(int mouseX, int mouseY, int shiftX, int shiftY)
{
shiftX += offsetX;
shiftY += offsetY;
if (side == LEFT)
{
if (mouseX <= shiftX && mouseX >= shiftX - currentWidth && mouseY >= shiftY && mouseY <= shiftY + currentHeight)
{
return true;
}
}
else if (mouseX >= shiftX && mouseX <= shiftX + currentWidth && mouseY >= shiftY && mouseY <= shiftY + currentHeight)
{
return true;
}
return false;
}
public boolean isFullyOpened()
{
return fullyOpen;
}
public void setCurrentShift(int x, int y)
{
currentShiftX = x + offsetX;
currentShiftY = y + offsetY;
}
public void setFullyOpen()
{
open = true;
currentWidth = maxWidth;
currentHeight = maxHeight;
fullyOpen = true;
}
public void toggleOpen()
{
if (open)
{
open = false;
if (side == LEFT)
{
techreborn.cofhLib.gui.TabTracker.setOpenedLeftTab(null);
}
else
{
techreborn.cofhLib.gui.TabTracker.setOpenedRightTab(null);
}
fullyOpen = false;
}
else
{
open = true;
if (side == LEFT)
{
techreborn.cofhLib.gui.TabTracker.setOpenedLeftTab(this.getClass());
}
else
{
techreborn.cofhLib.gui.TabTracker.setOpenedRightTab(this.getClass());
}
}
}
public techreborn.cofhLib.util.Rectangle4i getBounds()
{
if (isVisible())
{
return new techreborn.cofhLib.util.Rectangle4i(posX() + gui.getGuiLeft(), posY + gui.getGuiTop(), currentWidth, currentHeight);
}
else
{
return new techreborn.cofhLib.util.Rectangle4i(posX() + gui.getGuiLeft(), posY + gui.getGuiTop(), 0, 0);
}
}
}

View file

@ -0,0 +1,15 @@
package techreborn.cofhLib.gui.element.listbox;
import techreborn.cofhLib.gui.element.ElementListBox;
public interface IListBoxElement
{
public int getHeight();
public int getWidth();
public Object getValue();
public void draw(ElementListBox listBox, int x, int y, int backColor, int textColor);
}

View file

@ -0,0 +1,45 @@
package techreborn.cofhLib.gui.element.listbox;
import techreborn.cofhLib.gui.element.ElementListBox;
import net.minecraft.client.Minecraft;
public class ListBoxElementText implements IListBoxElement
{
private final String _text;
public ListBoxElementText(String text)
{
_text = text;
}
@Override
public Object getValue()
{
return _text;
}
@Override
public int getHeight()
{
return 10;
}
@Override
public int getWidth()
{
return Minecraft.getMinecraft().fontRenderer.getStringWidth(_text);
}
@Override
public void draw(ElementListBox listBox, int x, int y, int backColor, int textColor)
{
listBox.getFontRenderer().drawStringWithShadow(_text, x, y, textColor);
}
}

View file

@ -0,0 +1,36 @@
package techreborn.cofhLib.gui.element.listbox;
public class SliderHorizontal extends techreborn.cofhLib.gui.element.ElementSlider
{
public SliderHorizontal(techreborn.cofhLib.gui.GuiBase containerScreen, int x, int y, int width, int height, int maxValue)
{
this(containerScreen, x, y, width, height, maxValue, 0);
}
public SliderHorizontal(techreborn.cofhLib.gui.GuiBase containerScreen, int x, int y, int width, int height, int maxValue, int minValue)
{
super(containerScreen, x, y, width, height, maxValue, minValue);
int dist = maxValue - minValue;
setSliderSize(dist <= 0 ? width : Math.max(width / ++dist, 9), height);
}
@Override
public int getSliderX()
{
int dist = _valueMax - _valueMin;
int maxPos = sizeX - _sliderWidth;
return Math.min(dist == 0 ? 0 : maxPos * (_value - _valueMin) / dist, maxPos);
}
@Override
public void dragSlider(int v, int y)
{
v += Math.round(_sliderWidth * (v / (float) sizeX) + (_sliderWidth * 0.25f));
setValue(_valueMin + ((_valueMax - _valueMin) * v / sizeX));
}
}

View file

@ -0,0 +1,36 @@
package techreborn.cofhLib.gui.element.listbox;
public class SliderVertical extends techreborn.cofhLib.gui.element.ElementSlider
{
public SliderVertical(techreborn.cofhLib.gui.GuiBase containerScreen, int x, int y, int width, int height, int maxValue)
{
this(containerScreen, x, y, width, height, maxValue, 0);
}
public SliderVertical(techreborn.cofhLib.gui.GuiBase containerScreen, int x, int y, int width, int height, int maxValue, int minValue)
{
super(containerScreen, x, y, width, height, maxValue, minValue);
int dist = maxValue - minValue;
setSliderSize(width, dist <= 0 ? height : Math.max(height / ++dist, 9));
}
@Override
public int getSliderY()
{
int dist = _valueMax - _valueMin;
int maxPos = sizeY - _sliderHeight;
return Math.min(dist == 0 ? 0 : maxPos * (_value - _valueMin) / dist, maxPos);
}
@Override
public void dragSlider(int x, int v)
{
v += Math.round(_sliderHeight * (v / (float) sizeY) + (_sliderHeight * 0.25f));
setValue(_valueMin + ((_valueMax - _valueMin) * v / sizeY));
}
}

View file

@ -0,0 +1,18 @@
package techreborn.cofhLib.gui.slot;
import net.minecraft.item.ItemStack;
/**
* Interface used in conjunction with {@link SlotValidated}.
*
* @author King Lemming
*/
public interface ISlotValidator
{
/**
* Essentially a passthrough so an arbitrary criterion can be checked against.
*/
boolean isItemValid(ItemStack stack);
}

View file

@ -0,0 +1,30 @@
package techreborn.cofhLib.gui.slot;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
/**
* Slot that will only accept ItemStacks whose items are a subclass of the given class.
*/
public class SlotAcceptAssignable extends Slot
{
protected Class<? extends Item> clazz;
public SlotAcceptAssignable(IInventory inventory, int index, int x, int y, Class<? extends Item> c)
{
super(inventory, index, x, y);
clazz = c;
}
@Override
public boolean isItemValid(ItemStack stack)
{
return stack != null && clazz.isInstance(stack.getItem());
}
}

View file

@ -0,0 +1,41 @@
package techreborn.cofhLib.gui.slot;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
/**
* Slot that will only accept ItemStacks when the IInventory returns true from isItemValidForSlot.
* <p/>
* If an ISidedInventory, canInsertItem (from side 6 (UNKNOWN)) must also return true.
*/
public class SlotAcceptInsertable extends SlotAcceptValid
{
protected ISidedInventory sidedInv;
public SlotAcceptInsertable(IInventory inventory, int index, int x, int y)
{
super(inventory, index, x, y);
if (inventory instanceof ISidedInventory)
{
sidedInv = (ISidedInventory) inventory;
}
else
{
sidedInv = null;
}
}
@Override
public boolean isItemValid(ItemStack stack)
{
boolean valid = super.isItemValid(stack);
return valid && sidedInv != null ? sidedInv.canInsertItem(slotNumber, stack, 6) : valid;
}
}

View file

@ -0,0 +1,26 @@
package techreborn.cofhLib.gui.slot;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
/**
* Slot that will only accept ItemStacks when the IInventory returns true from isItemValidForSlot.
*/
public class SlotAcceptValid extends Slot
{
public SlotAcceptValid(IInventory inventory, int index, int x, int y)
{
super(inventory, index, x, y);
}
@Override
public boolean isItemValid(ItemStack stack)
{
return stack != null && this.inventory.isItemValidForSlot(this.slotNumber, stack);
}
}

View file

@ -0,0 +1,36 @@
package techreborn.cofhLib.gui.slot;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.ItemStack;
/**
* Crafting result slot where the result cannot be removed.
*
* @author King Lemming
*/
public class SlotCraftingLocked extends SlotCrafting
{
public SlotCraftingLocked(EntityPlayer player, IInventory craftMatrix, IInventory inventory, int index, int x, int y)
{
super(player, craftMatrix, inventory, index, x, y);
}
@Override
public boolean isItemValid(ItemStack stack)
{
return false;
}
@Override
public boolean canTakeStack(EntityPlayer player)
{
return false;
}
}

View file

@ -0,0 +1,51 @@
package techreborn.cofhLib.gui.slot;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
/**
* Slot which copies an ItemStack when clicked on, does not decrement the ItemStack on the cursor.
*
* @author King Lemming
*/
public class SlotFalseCopy extends Slot
{
public int slotIndex = 0;
public SlotFalseCopy(IInventory inventory, int index, int x, int y)
{
super(inventory, index, x, y);
slotIndex = index;
}
@Override
public boolean canTakeStack(EntityPlayer player)
{
return false;
}
@Override
public boolean isItemValid(ItemStack stack)
{
return true;
}
@Override
public void putStack(ItemStack stack)
{
if (stack != null)
{
stack.stackSize = 1;
}
this.inventory.setInventorySlotContents(this.slotIndex, stack);
this.onSlotChanged();
}
}

View file

@ -0,0 +1,64 @@
package techreborn.cofhLib.gui.slot;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
/**
* Slot that will redirect inserts to another inventory slot (other than index), but not be visible.
* <p/>
* Used primarily for containers that have a larger internal inventory than external (e.g., DeepStorageUnit)
*/
public class SlotInvisible extends Slot
{
protected final int slotIndex;
public SlotInvisible(IInventory inventory, int index, int x, int y, int slot)
{
super(inventory, index, x, y);
slotIndex = slot;
}
@Override
public void putStack(ItemStack stack)
{
this.inventory.setInventorySlotContents(slotIndex, stack);
this.onSlotChanged();
}
@Override
public ItemStack getStack()
{
return null;
}
@Override
public ItemStack decrStackSize(int par1)
{
return null;
}
@Override
public boolean canTakeStack(EntityPlayer p)
{
return false;
}
@Override
@SideOnly(Side.CLIENT)
public boolean func_111238_b()
{
return false;
}
}

View file

@ -0,0 +1,27 @@
package techreborn.cofhLib.gui.slot;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
/**
* Slot that will only accept Potions.
*/
public class SlotPotion extends Slot
{
public SlotPotion(IInventory inventory, int index, int x, int y)
{
super(inventory, index, x, y);
}
@Override
public boolean isItemValid(ItemStack stack)
{
return stack != null && stack.getItem().equals(Items.potionitem);
}
}

View file

@ -0,0 +1,26 @@
package techreborn.cofhLib.gui.slot;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
/**
* Slot that will only accept Potion Ingredients.
*/
public class SlotPotionIngredient extends Slot
{
public SlotPotionIngredient(IInventory inventory, int index, int x, int y)
{
super(inventory, index, x, y);
}
@Override
public boolean isItemValid(ItemStack stack)
{
return stack != null && stack.getItem().isPotionIngredient(stack);
}
}

View file

@ -0,0 +1,26 @@
package techreborn.cofhLib.gui.slot;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
/**
* Slot which players can only remove items from.
*/
public class SlotRemoveOnly extends Slot
{
public SlotRemoveOnly(IInventory inventory, int index, int x, int y)
{
super(inventory, index, x, y);
}
@Override
public boolean isItemValid(ItemStack stack)
{
return false;
}
}

View file

@ -0,0 +1,31 @@
package techreborn.cofhLib.gui.slot;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
/**
* A slot where the input can be validated based on any arbitrary criteria by using a passthrough method to an {@link ISlotValidator}.
*
* @author King Lemming
*/
public class SlotValidated extends Slot
{
ISlotValidator validator;
public SlotValidated(ISlotValidator validator, IInventory inventory, int index, int x, int y)
{
super(inventory, index, x, y);
this.validator = validator;
}
@Override
public boolean isItemValid(ItemStack stack)
{
return validator.isItemValid(stack);
}
}

View file

@ -0,0 +1,66 @@
package techreborn.cofhLib.gui.slot;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
/**
* A slot that can only be used to display an item, not edited. Can optionally not highlight when moused over.
*/
public class SlotViewOnly extends Slot
{
protected boolean showHighlight;
public SlotViewOnly(IInventory inventory, int index, int x, int y)
{
this(inventory, index, x, y, false);
}
public SlotViewOnly(IInventory inventory, int index, int x, int y, boolean highlight)
{
super(inventory, index, x, y);
showHighlight = highlight;
}
@Override
public void putStack(ItemStack stack)
{
}
@Override
public ItemStack decrStackSize(int i)
{
return null;
}
@Override
public boolean canTakeStack(EntityPlayer player)
{
return false;
}
@Override
public boolean isItemValid(ItemStack stack)
{
return false;
}
@Override
@SideOnly(Side.CLIENT)
public boolean func_111238_b()
{
return showHighlight;
}
}

View file

@ -0,0 +1,200 @@
package techreborn.cofhLib.render;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import org.lwjgl.opengl.GL11;
/**
* Contains various helper functions to assist with rendering.
*
* @author King Lemming
*/
public final class RenderHelper
{
public static final double RENDER_OFFSET = 1.0D / 1024.0D;
public static final ResourceLocation MC_BLOCK_SHEET = new ResourceLocation("textures/atlas/blocks.png");
public static final ResourceLocation MC_ITEM_SHEET = new ResourceLocation("textures/atlas/items.png");
public static final ResourceLocation MC_FONT_DEFAULT = new ResourceLocation("textures/font/ascii.png");
public static final ResourceLocation MC_FONT_ALTERNATE = new ResourceLocation("textures/font/ascii_sga.png");
public static final ResourceLocation MC_ITEM_GLINT = new ResourceLocation("textures/misc/enchanted_item_glint.png");
private RenderHelper()
{
}
public static final TextureManager engine()
{
return Minecraft.getMinecraft().renderEngine;
}
public static final Tessellator tessellator()
{
return Tessellator.instance;
}
public static void setColor3ub(int color)
{
GL11.glColor3ub((byte) (color >> 16 & 0xFF), (byte) (color >> 8 & 0xFF), (byte) (color & 0xFF));
}
public static void setColor4ub(int color)
{
GL11.glColor4ub((byte) (color >> 24 & 0xFF), (byte) (color >> 16 & 0xFF), (byte) (color >> 8 & 0xFF), (byte) (color & 0xFF));
}
public static void resetColor()
{
GL11.glColor4f(1F, 1F, 1F, 1F);
}
public static void renderItemAsBlock(RenderBlocks renderer, ItemStack item, double translateX, double translateY, double translateZ)
{
renderTextureAsBlock(renderer, item.getIconIndex(), translateX, translateY, translateZ);
}
public static void renderTextureAsBlock(RenderBlocks renderer, IIcon texture, double translateX, double translateY, double translateZ)
{
Tessellator tessellator = Tessellator.instance;
Block block = Blocks.stone;
if (texture == null)
{
return;
}
renderer.setRenderBoundsFromBlock(block);
GL11.glTranslated(translateX, translateY, translateZ);
tessellator.startDrawingQuads();
tessellator.setNormal(0.0F, -1.0F, 0.0F);
renderer.renderFaceYNeg(block, 0.0D, 0.0D, 0.0D, texture);
tessellator.setNormal(0.0F, 1.0F, 0.0F);
renderer.renderFaceYPos(block, 0.0D, 0.0D, 0.0D, texture);
tessellator.setNormal(0.0F, 0.0F, -1.0F);
renderer.renderFaceZNeg(block, 0.0D, 0.0D, 0.0D, texture);
tessellator.setNormal(0.0F, 0.0F, 1.0F);
renderer.renderFaceZPos(block, 0.0D, 0.0D, 0.0D, texture);
tessellator.setNormal(-1.0F, 0.0F, 0.0F);
renderer.renderFaceXNeg(block, 0.0D, 0.0D, 0.0D, texture);
tessellator.setNormal(1.0F, 0.0F, 0.0F);
renderer.renderFaceXPos(block, 0.0D, 0.0D, 0.0D, texture);
tessellator.draw();
}
public static void renderItemIn2D(IIcon icon)
{
ItemRenderer.renderItemIn2D(Tessellator.instance, icon.getMaxU(), icon.getMinV(), icon.getMinU(), icon.getMaxV(), icon.getIconWidth(), icon.getIconHeight(), 0.0625F);
}
public static void renderIcon(IIcon icon, double z)
{
Tessellator.instance.startDrawingQuads();
Tessellator.instance.addVertexWithUV(0, 16, z, icon.getMinU(), icon.getMaxV());
Tessellator.instance.addVertexWithUV(16, 16, z, icon.getMaxU(), icon.getMaxV());
Tessellator.instance.addVertexWithUV(16, 0, z, icon.getMaxU(), icon.getMinV());
Tessellator.instance.addVertexWithUV(0, 0, z, icon.getMinU(), icon.getMinV());
Tessellator.instance.draw();
}
public static void renderIcon(double x, double y, double z, IIcon icon, int width, int height)
{
Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.addVertexWithUV(x, y + height, z, icon.getMinU(), icon.getMaxV());
tessellator.addVertexWithUV(x + width, y + height, z, icon.getMaxU(), icon.getMaxV());
tessellator.addVertexWithUV(x + width, y, z, icon.getMaxU(), icon.getMinV());
tessellator.addVertexWithUV(x, y, z, icon.getMinU(), icon.getMinV());
tessellator.draw();
}
public static final IIcon getFluidTexture(Fluid fluid)
{
if (fluid == null)
{
return FluidRegistry.LAVA.getIcon();
}
return fluid.getIcon();
}
public static final IIcon getFluidTexture(FluidStack fluid)
{
if (fluid == null || fluid.getFluid() == null || fluid.getFluid().getIcon(fluid) == null)
{
return FluidRegistry.LAVA.getIcon();
}
return fluid.getFluid().getIcon(fluid);
}
public static final void bindItemTexture(ItemStack stack)
{
engine().bindTexture(stack.getItemSpriteNumber() == 0 ? MC_BLOCK_SHEET : MC_ITEM_SHEET);
}
public static final void bindTexture(ResourceLocation texture)
{
engine().bindTexture(texture);
}
public static final void setBlockTextureSheet()
{
bindTexture(MC_BLOCK_SHEET);
}
public static final void setItemTextureSheet()
{
bindTexture(MC_ITEM_SHEET);
}
public static final void setDefaultFontTextureSheet()
{
bindTexture(MC_FONT_DEFAULT);
}
public static final void setSGAFontTextureSheet()
{
bindTexture(MC_FONT_ALTERNATE);
}
public static final void enableGUIStandardItemLighting()
{
net.minecraft.client.renderer.RenderHelper.enableGUIStandardItemLighting();
}
}

View file

@ -0,0 +1,34 @@
package techreborn.cofhLib.util;
public class CharacterSingleton implements CharSequence
{
public char character;
@Override
public int length()
{
return 1;
}
@Override
public char charAt(int index)
{
return character;
}
@Override
public CharSequence subSequence(int start, int end)
{
if (start == end)
{
return "";
}
return this;
}
}

View file

@ -0,0 +1,142 @@
package techreborn.cofhLib.util;
/**
* Generic rectangle class.
*
* @author Chicken Bones
*/
public class Rectangle4i
{
public int x;
public int y;
public int w;
public int h;
public Rectangle4i()
{
}
public Rectangle4i(int x, int y, int w, int h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public int x1()
{
return x;
}
public int y1()
{
return y;
}
public int x2()
{
return x + w - 1;
}
public int y2()
{
return y + h - 1;
}
public void set(int x, int y, int w, int h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public Rectangle4i offset(int dx, int dy)
{
x += dx;
y += dy;
return this;
}
public Rectangle4i include(int px, int py)
{
if (px < x)
{
expand(px - x, 0);
}
if (px >= x + w)
{
expand(px - x - w + 1, 0);
}
if (py < y)
{
expand(0, py - y);
}
if (py >= y + h)
{
expand(0, py - y - h + 1);
}
return this;
}
public Rectangle4i include(Rectangle4i r)
{
include(r.x, r.y);
return include(r.x2(), r.y2());
}
public Rectangle4i expand(int px, int py)
{
if (px > 0)
{
w += px;
}
else
{
x += px;
w -= px;
}
if (py > 0)
{
h += py;
}
else
{
y += py;
h -= py;
}
return this;
}
public boolean contains(int px, int py)
{
return x <= px && px < x + w && y <= py && py < y + h;
}
public boolean intersects(Rectangle4i r)
{
return r.x + r.w > x && r.x < x + w && r.y + r.h > y && r.y < y + h;
}
public int area()
{
return w * h;
}
}

View file

@ -0,0 +1,244 @@
package techreborn.cofhLib.util.helpers;
import java.util.Random;
/**
* Contains various math-related helper functions. Often faster than conventional implementations.
*
* @author King Lemming
*/
public final class MathHelper
{
private MathHelper()
{
}
public static final Random RANDOM = new Random();
public static final double PHI = 1.618034;
public static final double[] SIN_TABLE = new double[65536];
static
{
for (int i = 0; i < 65536; i++)
{
SIN_TABLE[i] = Math.sin(i / 65536D * 2 * Math.PI);
}
SIN_TABLE[0] = 0;
SIN_TABLE[16384] = 1;
SIN_TABLE[32768] = 0;
SIN_TABLE[49152] = 1;
}
public static double sin(double d)
{
return SIN_TABLE[(int) ((float) d * 10430.378F) & 65535];
}
public static double cos(double d)
{
return SIN_TABLE[(int) ((float) d * 10430.378F + 16384.0F) & 65535];
}
public static int clampI(int a, int min, int max)
{
return a < min ? min : (a > max ? max : a);
}
public static float clampF(float a, float min, float max)
{
return a < min ? min : (a > max ? max : a);
}
public static float approachLinear(float a, float b, float max)
{
return a > b ? a - b < max ? b : a - max : b - a < max ? b : a + max;
}
public static double approachLinear(double a, double b, double max)
{
return a > b ? a - b < max ? b : a - max : b - a < max ? b : a + max;
}
public static float interpolate(float a, float b, float d)
{
return a + (b - a) * d;
}
public static double interpolate(double a, double b, double d)
{
return a + (b - a) * d;
}
public static double approachExp(double a, double b, double ratio)
{
return a + (b - a) * ratio;
}
public static double approachExp(double a, double b, double ratio, double cap)
{
double d = (b - a) * ratio;
if (Math.abs(d) > cap)
{
d = Math.signum(d) * cap;
}
return a + d;
}
public static double retreatExp(double a, double b, double c, double ratio, double kick)
{
double d = (Math.abs(c - a) + kick) * ratio;
if (d > Math.abs(b - a))
{
return b;
}
return a + Math.signum(b - a) * d;
}
public static double clip(double value, double min, double max)
{
if (value > max)
{
value = max;
}
else if (value < min)
{
value = min;
}
return value;
}
public static boolean between(double a, double x, double b)
{
return a <= x && x <= b;
}
public static int approachExpI(int a, int b, double ratio)
{
int r = (int) Math.round(approachExp(a, b, ratio));
return r == a ? b : r;
}
public static int retreatExpI(int a, int b, int c, double ratio, int kick)
{
int r = (int) Math.round(retreatExp(a, b, c, ratio, kick));
return r == a ? b : r;
}
/**
* Unchecked implementation to round a number. Parameter should be known to be valid in advance.
*/
public static int round(double d)
{
return (int) (d + 0.5D);
}
/**
* Unchecked implementation to round a number up. Parameter should be known to be valid in advance.
*/
public static int ceil(double d)
{
return (int) (d + 0.9999D);
}
/**
* Unchecked implementation to round a number down. Parameter should be known to be valid in advance.
*/
public static int floor(double d)
{
int i = (int) d;
return d < i ? i - 1 : i;
}
/**
* Unchecked implementation to determine the smaller of two Floats. Parameters should be known to be valid in advance.
*/
public static float minF(float a, float b)
{
return a < b ? a : b;
}
public static float minF(int a, float b)
{
return a < b ? a : b;
}
public static float minF(float a, int b)
{
return a < b ? a : b;
}
/**
* Unchecked implementation to determine the larger of two Floats. Parameters should be known to be valid in advance.
*/
public static float maxF(float a, float b)
{
return a > b ? a : b;
}
public static float maxF(int a, float b)
{
return a > b ? a : b;
}
public static float maxF(float a, int b)
{
return a > b ? a : b;
}
public static double maxAbs(double a, double b)
{
if (a < 0.0D)
{
a = -a;
}
if (b < 0.0D)
{
b = -b;
}
return a > b ? a : b;
}
public static int setBit(int mask, int bit, boolean value)
{
mask |= (value ? 1 : 0) << bit;
return mask;
}
public static boolean isBitSet(int mask, int bit)
{
return (mask & 1 << bit) != 0;
}
}

View file

@ -0,0 +1,267 @@
package techreborn.cofhLib.util.helpers;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.StatCollector;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import org.lwjgl.input.Keyboard;
import java.util.List;
/**
* Contains various helper functions to assist with String manipulation.
*
* @author King Lemming
*/
public final class StringHelper
{
private StringHelper()
{
}
/* KEY HELPERS */
public static boolean isAltKeyDown()
{
return Keyboard.isKeyDown(Keyboard.KEY_LMENU) || Keyboard.isKeyDown(Keyboard.KEY_RMENU);
}
public static boolean isControlKeyDown()
{
return Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL);
}
public static boolean isShiftKeyDown()
{
return Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
}
/* FORMAT HELPERS */
public static int getSplitStringHeight(FontRenderer fontRenderer, String input, int width)
{
@SuppressWarnings("rawtypes") List stringRows = fontRenderer.listFormattedStringToWidth(input, width);
return stringRows.size() * fontRenderer.FONT_HEIGHT;
}
public static String camelCase(String input)
{
return input.substring(0, 1).toLowerCase() + input.substring(1);
}
public static String titleCase(String input)
{
return input.substring(0, 1).toUpperCase() + input.substring(1);
}
public static String localize(String key)
{
return StatCollector.translateToLocal(key);
}
public static String getFluidName(FluidStack stack)
{
Fluid fluid = stack.getFluid();
String name = "" + END;
if (fluid.getRarity() == EnumRarity.uncommon)
{
name += YELLOW;
}
else if (fluid.getRarity() == EnumRarity.rare)
{
name += BRIGHT_BLUE;
}
else if (fluid.getRarity() == EnumRarity.epic)
{
name += PINK;
}
name += fluid.getLocalizedName(stack) + END;
return name;
}
public static String getFluidName(FluidStack stack, String defaultName)
{
if (stack == null)
{
return defaultName;
}
return getFluidName(stack);
}
public static String getItemName(ItemStack stack)
{
String name = "" + END;
if (stack.getRarity() == EnumRarity.uncommon)
{
name += YELLOW;
}
else if (stack.getRarity() == EnumRarity.rare)
{
name += BRIGHT_BLUE;
}
else if (stack.getRarity() == EnumRarity.epic)
{
name += PINK;
}
name += stack.getDisplayName() + END;
return name;
}
public static String getScaledNumber(long number)
{
if (number >= 1000000000)
{
return number / 1000000000 + "." + (number % 1000000000 / 10000000) + "G";
}
else if (number >= 1000000)
{
return number / 1000000 + "." + (number % 1000000 / 10000) + "M";
}
else if (number >= 1000)
{
return number / 1000 + "." + (number % 1000 / 10) + "k";
}
else
{
return String.valueOf(number);
}
}
@Deprecated
public static String getScaledNumber(long number, int minDigits)
{
return getScaledNumber(number);
}
/* ITEM TEXT HELPERS */
public static String getActivationText(String key)
{
return BRIGHT_BLUE + localize(key) + END;
}
public static String getDeactivationText(String key)
{
return YELLOW + localize(key) + END;
}
public static String getInfoText(String key)
{
return BRIGHT_GREEN + localize(key) + END;
}
public static String getFlavorText(String key)
{
return LIGHT_GRAY + ITALIC + localize(key) + END;
}
public static String getRarity(int level)
{
switch (level)
{
case 2:
return StringHelper.YELLOW;
case 3:
return StringHelper.BRIGHT_BLUE;
default:
return StringHelper.LIGHT_GRAY;
}
}
public static String shiftForDetails()
{
return LIGHT_GRAY + localize("info.cofh.hold") + " " + YELLOW + ITALIC + localize("info.cofh.shift") + " " + END + LIGHT_GRAY + localize("info.cofh.forDetails") + END;
}
/* TUTORIAL TAB HELPERS */
public static String tutorialTabAugment()
{
return localize("info.cofh.tutorial.tabAugment");
}
public static String tutorialTabConfiguration()
{
return localize("info.cofh.tutorial.tabConfiguration.0");
}
public static String tutorialTabOperation()
{
return localize("info.cofh.tutorial.tabConfiguration.1");
}
public static String tutorialTabRedstone()
{
return localize("info.cofh.tutorial.tabRedstone");
}
public static String tutorialTabSecurity()
{
return localize("info.cofh.tutorial.tabSecurity");
}
public static String tutorialTabFluxRequired()
{
return localize("info.cofh.tutorial.fluxRequired");
}
/**
* When formatting a string, always apply color before font modification.
*/
public static final String BLACK = (char) 167 + "0";
public static final String BLUE = (char) 167 + "1";
public static final String GREEN = (char) 167 + "2";
public static final String TEAL = (char) 167 + "3";
public static final String RED = (char) 167 + "4";
public static final String PURPLE = (char) 167 + "5";
public static final String ORANGE = (char) 167 + "6";
public static final String LIGHT_GRAY = (char) 167 + "7";
public static final String GRAY = (char) 167 + "8";
public static final String LIGHT_BLUE = (char) 167 + "9";
public static final String BRIGHT_GREEN = (char) 167 + "a";
public static final String BRIGHT_BLUE = (char) 167 + "b";
public static final String LIGHT_RED = (char) 167 + "c";
public static final String PINK = (char) 167 + "d";
public static final String YELLOW = (char) 167 + "e";
public static final String WHITE = (char) 167 + "f";
public static final String OBFUSCATED = (char) 167 + "k";
public static final String BOLD = (char) 167 + "l";
public static final String STRIKETHROUGH = (char) 167 + "m";
public static final String UNDERLINE = (char) 167 + "n";
public static final String ITALIC = (char) 167 + "o";
public static final String END = (char) 167 + "r";
public static final String[] ROMAN_NUMERAL = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};
public static boolean displayShiftForDetail = true;
public static boolean displayStackCount = false;
}