Skip to content

Commit

Permalink
GUI stubs, still need new slot(s) and rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
AfterLifeLochie committed Oct 4, 2014
1 parent 9374eb2 commit b30ffea
Show file tree
Hide file tree
Showing 6 changed files with 596 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/main/java/lc/common/base/LCContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package lc.common.base;

import lc.common.LCLog;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public abstract class LCContainer extends Container {
public int xSize;
public int ySize;

public LCContainer(int width, int height) {
super();
xSize = width;
ySize = height;
}

public void addPlayerSlots(EntityPlayer player) {
addPlayerSlots(player, (xSize - 160) / 2, ySize - 82);
}

@Override
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) {
return null;
}

public void addPlayerSlots(EntityPlayer player, int x, int y) {
InventoryPlayer inventory = player.inventory;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 9; ++j)
addSlotToContainer(new Slot(inventory, j + i * 9 + 9, x + j * 18, y + i * 18));

for (int i = 0; i < 9; ++i)
addSlotToContainer(new Slot(inventory, i, x + i * 18, y + 58));
}

@Override
public Slot addSlotToContainer(Slot slot) {
if (0 > slot.xDisplayPosition || 0 > slot.yDisplayPosition || slot.xDisplayPosition + 16 > xSize
|| slot.yDisplayPosition + 16 > ySize)
LCLog.warn("Slot index %s in inventory %s is offscreen. Problems may occur!", slot.getSlotIndex(),
slot.inventory.getClass().getName());
return super.addSlotToContainer(slot);
}

@Override
public boolean canInteractWith(EntityPlayer var1) {
return true;
}

@Override
public void detectAndSendChanges() {
super.detectAndSendChanges();
for (int i = 0; i < crafters.size(); i++) {
ICrafting crafter = (ICrafting) crafters.get(i);
sendStateTo(crafter);
}
}

public abstract void sendStateTo(ICrafting crafter);

@Override
public void updateProgressBar(int i, int value) {
}
}
32 changes: 32 additions & 0 deletions src/main/java/lc/common/base/ux/IconButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package lc.common.base.ux;

import lc.core.ResourceAccess;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.Tessellator;

public class IconButton {
public static void drawButton(Minecraft mc, String iconName, boolean hover, boolean pressed, int x, int y,
double scale, float zLevel) {
bindAndClamp(mc, "button_" + ((pressed) ? "down" : (hover) ? "hover" : "up") + ".png");
drawTexturedRectUV(x, y, 24 * scale, 24 * scale, 0, 0, 1, 1, zLevel);
bindAndClamp(mc, "icons/" + iconName + ".png");
drawTexturedRectUV(x + (4 * scale), y + (4 * scale), 16 * scale, 16 * scale, 0, 0, 1, 1, zLevel);
}

private static void bindAndClamp(Minecraft mc, String name) {
mc.getTextureManager().bindTexture(ResourceAccess.getNamedResource("textures/gui/" + name));
}

private static void drawTexturedRectUV(double x, double y, double w, double h, double u, double v, double us,
double vs, float zLevel) {
Tessellator tess = Tessellator.instance;
tess.startDrawingQuads();
tess.setColorOpaque_F(1.0f, 1.0f, 1.0f);
tess.addVertexWithUV(x, y + h, zLevel, u, v + vs);
tess.addVertexWithUV(x + w, y + h, zLevel, u + us, v + vs);
tess.addVertexWithUV(x + w, y, zLevel, u + us, v);
tess.addVertexWithUV(x, y, zLevel, u, v);
tess.draw();
}

}
181 changes: 181 additions & 0 deletions src/main/java/lc/common/base/ux/LCContainerGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package lc.common.base.ux;

import java.util.HashMap;

import lc.common.base.LCContainer;
import lc.core.ResourceAccess;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;

public abstract class LCContainerGUI extends GuiContainer {

private final static int defaultTextColor = 0x404040;

private HashMap<Integer, LCContainerTab> tabList;
private LCContainerTab activeTab;

double uscale, vscale;
float red = 1.0F, green = 1.0F, blue = 1.0F;
protected int textColor = defaultTextColor;
boolean textShadow = false;

public LCContainerGUI(Container container, int width, int height) {
super(container);
xSize = width;
ySize = height;
}

public LCContainerGUI(LCContainer container) {
this(container, container.xSize, container.ySize);
}

protected abstract HashMap<Integer, LCContainerTab> getTabs();

public void switchTab(int to) {
LCContainerTab nextTab = tabList.get(to);
if (nextTab == null)
nextTab = tabList.get(0);
if (activeTab != null)
activeTab.onTabClosed(this);
activeTab = nextTab;
activeTab.onTabOpened(this);
}

@Override
protected void drawGuiContainerBackgroundLayer(float partialTickCount, int mouseX, int mouseY) {
// TODO Auto-generated method stub
if (activeTab != null)
activeTab.drawBackgroundLayer(partialTickCount, mouseX, mouseY);
}

@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
// TODO Auto-generated method stub
if (activeTab != null)
activeTab.drawForegroundLayer(mouseX, mouseY);
}

@Override
protected void keyTyped(char c, int key) {
if (activeTab != null)
activeTab.keyTyped(c, key);
}

@Override
protected void mouseClicked(int x, int y, int mouseButton) {
if (activeTab != null)
activeTab.mouseClicked(x, y, mouseButton);
}

@Override
protected void mouseMovedOrUp(int x, int y, int mouseButton) {
if (activeTab != null)
activeTab.mouseMovedOrUp(x, y, mouseButton);
}

@Override
public void drawScreen(int par1, int par2, float par3) {
resetColor();
textColor = defaultTextColor;
textShadow = false;
super.drawScreen(par1, par2, par3);
}

protected void close() {
if (activeTab != null)
activeTab.onTabClosed(this);
mc.thePlayer.closeScreen();
}

public void drawFramedSymbols(int x, int y, String address) {
int scale = 2;
bindTexture(ResourceAccess.getNamedResource("textures/gui/symbol_frame.png"), 512 / scale, 128 / scale);
drawTexturedRect(x - (472 / scale) / 2, y, 472 / scale, 88 / scale, 0, 0);
StargateGlyphRenderer.drawAddress(mc, address, x - (472 / scale) / 2, y, 9, scale, zLevel);
}

public void drawAddressString(int x, int y, String address, int len, String padding, String caret) {
StringBuilder result = new StringBuilder();
result.append(address);
if (len != result.length() && caret != null)
result.append(caret);
while (len > result.length())
result.append(padding);
drawCenteredString(fontRendererObj, result.toString(), x, y, 0xffffff);
}

public void bindTexture(ResourceLocation rsrc) {
bindTexture(rsrc, 1, 1);
}

public void bindTexture(ResourceLocation rsrc, int usize, int vsize) {
mc.getTextureManager().bindTexture(rsrc);
uscale = 1.0 / usize;
vscale = 1.0 / vsize;
}

public void drawTexturedRect(double x, double y, double w, double h) {
drawTexturedRectUV(x, y, w, h, 0, 0, 1, 1);
}

public void drawTexturedRect(double x, double y, double w, double h, double u, double v) {
drawTexturedRect(x, y, w, h, u, v, w, h);
}

public void drawTexturedRect(double x, double y, double w, double h, double u, double v, double us, double vs) {
drawTexturedRectUV(x, y, w, h, u * uscale, v * vscale, us * uscale, vs * vscale);
}

public void drawTexturedRectUV(double x, double y, double w, double h, double u, double v, double us, double vs) {
Tessellator tess = Tessellator.instance;
tess.startDrawingQuads();
tess.setColorOpaque_F(red, green, blue);
tess.addVertexWithUV(x, y + h, zLevel, u, v + vs);
tess.addVertexWithUV(x + w, y + h, zLevel, u + us, v + vs);
tess.addVertexWithUV(x + w, y, zLevel, u + us, v);
tess.addVertexWithUV(x, y, zLevel, u, v);
tess.draw();
}

public void setColor(int hex) {
setColor((hex >> 16) / 255.0, (hex >> 8 & 0xff) / 255.0, (hex & 0xff) / 255.0);
}

public void setColor(double r, double g, double b) {
red = (float) r;
green = (float) g;
blue = (float) b;
}

public void resetColor() {
setColor(1, 1, 1);
}

public void drawString(String s, int x, int y) {
fontRendererObj.drawString(s, x, y, textColor, textShadow);
}

public void drawCenteredString(String s, int x, int y) {
fontRendererObj.drawString(s, x - fontRendererObj.getStringWidth(s) / 2, y, textColor, textShadow);
}

public void drawInventoryName(IInventory inv, int x, int y) {
drawString(inventoryName(inv), x, y);
}

public void drawPlayerInventoryName() {
drawString(playerInventoryName(), 8, ySize - 96 + 2);
}

public static String inventoryName(IInventory inv) {
return StatCollector.translateToLocal(inv.getInventoryName());
}

public static String playerInventoryName() {
return StatCollector.translateToLocal("container.inventory");
}
}
105 changes: 105 additions & 0 deletions src/main/java/lc/common/base/ux/LCContainerTab.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package lc.common.base.ux;

import java.awt.Rectangle;

import net.minecraft.util.ResourceLocation;

/**
* Container tab stub
*
* @author AfterLifeLochie
*/
public abstract class LCContainerTab {
/**
* Called by the LCContainerGUI parent when the tab is selected.
*
* @param container
* The LCContainerGUI parent.
*/
protected abstract void onTabOpened(LCContainerGUI container);

/**
* Called by the LCContainerGUI when the tab is deselected.
*
* @param container
* The LCContainerGUI parent.
*/
protected abstract void onTabClosed(LCContainerGUI container);

/**
* Get an un-translated tab name.
*
* @return An un-translated tab name.
*/
protected abstract String getTabName();

/**
* Get the tab's icon location.
*
* @return An absolute resourcelocation to the tab's icon.
*/
protected abstract ResourceLocation getTabIcon();

/**
* Get the dimensions of the internal panel area of this tab.
*
* @return The internal panel area of this tab.
*/
protected abstract Rectangle getTabDimensions();

/**
* Draw the background layer of the tab.
*
* @param partialTickCount
* Partial render ticks
* @param mouseX
* Mouse x-coord
* @param mouseY
* Mouse y-coord
*/
protected abstract void drawBackgroundLayer(float partialTickCount, int mouseX, int mouseY);

/**
* Draw the foreground layer of the tab.
*
* @param mouseX
* Mouse x-coord
* @param mouseY
* Mouse y-coord
*/
protected abstract void drawForegroundLayer(int mouseX, int mouseY);

/**
* Called when the mouse is clicked
*
* @param x
* The x-coordinate of the click
* @param y
* The y-coordinate of the click
* @param mouseButton
* Which button was clicked
*/
protected abstract void mouseClicked(int x, int y, int mouseButton);

/**
* Called when the mouse is released
*
* @param x
* The x-coordinate of the release
* @param y
* The y-coordinate of the release
* @param mouseButton
* Which button was released
*/
protected abstract void mouseMovedOrUp(int x, int y, int mouseButton);

/**
* Called when a key is typed
*
* @param c
* The charcode of the key if any
* @param key
* The key number
*/
protected abstract void keyTyped(char c, int key);
}
Loading

0 comments on commit b30ffea

Please sign in to comment.