diff --git a/src/main/java/com/pahimar/ee3/client/gui/GuiBase.java b/src/main/java/com/pahimar/ee3/client/gui/GuiBase.java index 1f07736f..18a1194f 100644 --- a/src/main/java/com/pahimar/ee3/client/gui/GuiBase.java +++ b/src/main/java/com/pahimar/ee3/client/gui/GuiBase.java @@ -1,35 +1,212 @@ package com.pahimar.ee3.client.gui; +import com.pahimar.ee3.client.gui.component.GuiComponent; +import com.pahimar.ee3.client.util.RenderUtils; +import com.pahimar.repackage.cofh.lib.util.helpers.StringHelper; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.inventory.Container; +import net.minecraft.util.ResourceLocation; +import org.lwjgl.opengl.GL11; + +import java.util.Collection; +import java.util.Map; +import java.util.TreeMap; @SideOnly(Side.CLIENT) public abstract class GuiBase extends GuiContainer { + protected ResourceLocation texture; + protected Map guiComponentMap = new TreeMap(); + protected String title; + private boolean shouldDrawTitle = true; + private String activeGuiComponentId = null; + public GuiBase(Container container) { + this(null, container); + } + + public GuiBase(String title, Container container) { + this(title, container, null); + } + + public GuiBase(Container container, ResourceLocation texture) { + this(null, container, texture); + } + + public GuiBase(String title, Container container, ResourceLocation texture) { super(container); + this.title = title; + this.texture = texture; + } + + public String getActiveGuiComponentId() { + return activeGuiComponentId; + } + + public String getTitle() { + return title; + } + + public GuiBase setTitle(String title) { + this.title = title; + return this; + } + + public boolean shouldDrawTitle() { + return shouldDrawTitle; + } + + public GuiBase setShouldDrawTitle(boolean shouldDrawTitle) { + this.shouldDrawTitle = shouldDrawTitle; + return this; + } + + public int getScreenWidth() { + return width; + } + + public int getScreenHeight() { + return height; + } + + public int getGuiPositionX() { + return guiLeft; + } + + public GuiBase setGuiPositionX(int positionX) { + this.guiLeft = positionX; + return this; + } + + public int getGuiPositionY() { + return guiTop; + } + + public GuiBase setGuiPositionY(int positionY) { + this.guiTop = positionY; + return this; + } + + public int getGuiWidth() { + return xSize; + } + + public GuiBase setGuiWidth(int width) { + this.xSize = width; + return this; + } + + public int getGuiHeight() { + return ySize; + } + + public GuiBase setGuiHeight(int height) { + this.ySize = height; + return this; + } + + public FontRenderer getFontRenderer() { + return fontRendererObj; + } + + public GuiComponent getGuiComponent(String id) { + return guiComponentMap.get(id); + } + + public Collection getGuiComponents() { + return guiComponentMap.values(); + } + + public GuiBase addGuiComponent(GuiComponent guiComponent) { + this.guiComponentMap.put(guiComponent.getId(), guiComponent); + return this; + } + + public GuiBase addGuiComponents(Collection guiComponents) { + for (GuiComponent guiComponent : guiComponents) { + this.guiComponentMap.put(guiComponent.getId(), guiComponent); + } + + return this; + } + + public void clearGuiComponents() { + this.guiComponentMap.clear(); } @Override public void initGui() { super.initGui(); + + // A bunch of different impls clear the list of components here - no reason I can gather why at this point + + for (GuiComponent guiComponent : guiComponentMap.values()) { + guiComponent.onInit(); + } } @Override - public void drawScreen(int x, int y, float partialTick) { - super.drawScreen(x, y, partialTick); + public void onGuiClosed() { + super.onGuiClosed(); + + for (GuiComponent guiComponent : guiComponentMap.values()) { + guiComponent.onClose(); + } } @Override - protected void drawGuiContainerForegroundLayer(int x, int y) { + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + super.drawScreen(mouseX, mouseY, partialTicks); + for (GuiComponent guiComponent : guiComponentMap.values()) { + guiComponent.onUpdate(mouseX, mouseY, partialTicks); + } } @Override - protected void drawGuiContainerBackgroundLayer(float partialTick, int x, int y) { + protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { + + if (shouldDrawTitle && title != null) { + getFontRenderer().drawString(StringHelper.localize(title), RenderUtils.getCenteredTextOffset(getFontRenderer(), StringHelper.localize(title), getGuiWidth()), 6, 0x404040); + } + drawComponents(true); + } + + @Override + protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { + + GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); + if (texture != null) { + RenderUtils.bindTexture(texture); + int xStart = (getScreenWidth() - getGuiWidth()) / 2; + int yStart = (getScreenHeight() - getGuiHeight()) / 2; + this.drawTexturedModalRect(xStart, yStart, 0, 0, getGuiWidth(), getGuiHeight()); + } + + GL11.glPushMatrix(); + GL11.glTranslatef(getGuiPositionX(), getGuiPositionY(), 0.0F); + drawComponents(false, partialTicks); + GL11.glPopMatrix(); } + protected void drawComponents(boolean drawForeground) { + drawComponents(drawForeground, 0); + } + + protected void drawComponents(boolean drawForeground, float partialTicks) { + + for (GuiComponent guiComponent : guiComponentMap.values()) { + if (guiComponent.isVisible()) { + if (drawForeground) { + guiComponent.drawForeground(partialTicks); + } else { + guiComponent.drawBackground(partialTicks); + } + } + } + } } diff --git a/src/main/java/com/pahimar/ee3/client/gui/component/GuiComponent.java b/src/main/java/com/pahimar/ee3/client/gui/component/GuiComponent.java index 63472d55..0835264d 100644 --- a/src/main/java/com/pahimar/ee3/client/gui/component/GuiComponent.java +++ b/src/main/java/com/pahimar/ee3/client/gui/component/GuiComponent.java @@ -3,13 +3,14 @@ package com.pahimar.ee3.client.gui.component; import com.pahimar.ee3.client.gui.GuiBase; import net.minecraft.util.ResourceLocation; -public abstract class GuiComponent { +public abstract class GuiComponent implements Comparable { protected final GuiBase parentGui; protected final String id; protected ResourceLocation texture; protected int positionX, positionY, componentWidth, componentHeight, textureWidth, textureHeight; + protected int ordering = 0; protected boolean isVisible = true; protected boolean isEnabled = true; @@ -85,6 +86,13 @@ public abstract class GuiComponent { } public GuiComponent setVisible(boolean isVisible) { + + if (this.isVisible && !isVisible) { + this.onHide(); + } else if (!this.isVisible && isVisible) { + this.onShow(); + } + this.isVisible = isVisible; return this; } @@ -94,15 +102,22 @@ public abstract class GuiComponent { } public GuiComponent setEnabled(boolean isEnabled) { + + if (this.isEnabled && !isEnabled) { + this.onDisable(); + } else if (!this.isEnabled && isEnabled) { + this.onEnable(); + } + this.isEnabled = isEnabled; return this; } - public abstract void drawForeground(); + public abstract void drawForeground(float partialTicks); - public abstract void drawBackground(); + public abstract void drawBackground(float partialTicks); - public abstract void update(); + public abstract void onUpdate(int mouseX, int mouseY, float partialTicks); /** * Checks whether or not the mouse cursor is intersecting with this GuiComponent @@ -112,7 +127,7 @@ public abstract class GuiComponent { * @return true if the mouse cursor is intersecting with this GuiComponent, false otherwise */ public boolean intersectsWithMouse(int mouseX, int mouseY) { - return false; + return (mouseX >= this.positionX && mouseX <= this.positionX + this.componentWidth) && (mouseY >= this.positionY && mouseY <= this.positionY + this.componentHeight); } public abstract void onMouseButtonClick(int mouseX, int mouseY, int mouseButton); @@ -137,4 +152,38 @@ public abstract class GuiComponent { public abstract void onKeyPress(char characterTyped, int keyPressed); + public abstract void onFocusGain(); + + public abstract void onFocusLost(); + + public abstract void onInit(); + + public abstract void onClose(); + + public abstract void onHide(); + + public abstract void onShow(); + + public abstract void onEnable(); + + public abstract void onDisable(); + + /** + * Compares this GuiComponent with another one to determine sort order + * + * @param guiComponent the GuiComponent we are comparing this one against + * @return + */ + @Override + public int compareTo(GuiComponent guiComponent) { + if (this.ordering == guiComponent.ordering) { + if (this.id != null && guiComponent.id != null) { + return this.id.compareToIgnoreCase(guiComponent.id); + } else { + return this.hashCode() - guiComponent.hashCode(); + } + } else { + return this.ordering - guiComponent.ordering; + } + } } diff --git a/src/main/java/com/pahimar/ee3/client/util/RenderUtils.java b/src/main/java/com/pahimar/ee3/client/util/RenderUtils.java index d47863a8..b922ae89 100644 --- a/src/main/java/com/pahimar/ee3/client/util/RenderUtils.java +++ b/src/main/java/com/pahimar/ee3/client/util/RenderUtils.java @@ -12,6 +12,18 @@ import org.lwjgl.opengl.GL12; public class RenderUtils { + private static int pulse = 0; + private static boolean doInc = true; + + public static void bindTexture(ResourceLocation texture) { + FMLClientHandler.instance().getClient().getTextureManager().bindTexture(texture); + } + + public static int getCenteredTextOffset(FontRenderer fontRenderer, String string, int width) { + + return (width - fontRenderer.getStringWidth(string)) / 2; + } + public static void renderItemIntoGUI(FontRenderer fontRenderer, ItemStack itemStack, int x, int y, float opacity, float scale, int zLevel) { IIcon icon = itemStack.getIconIndex(); @@ -90,7 +102,4 @@ public class RenderUtils } return pulse; } - - private static int pulse = 0; - private static boolean doInc = true; }