Bit more work on gui framework

This commit is contained in:
Pahimar 2015-12-22 15:52:20 -05:00
parent 6495a087a2
commit b5c246e65a
2 changed files with 111 additions and 55 deletions

View file

@ -11,10 +11,7 @@ import net.minecraft.inventory.Container;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
import java.util.*;
@SideOnly(Side.CLIENT)
public abstract class GuiBase extends GuiContainer {
@ -25,6 +22,9 @@ public abstract class GuiBase extends GuiContainer {
private boolean shouldDrawTitle = true;
private String activeGuiComponentId = null;
private int adjustedMouseX = 0;
private int adjustedMouseY = 0;
public GuiBase(Container container) {
this(null, container);
}
@ -65,6 +65,28 @@ public abstract class GuiBase extends GuiContainer {
return this;
}
public int getAdjustedMouseX() {
return adjustedMouseX;
}
protected GuiBase updateAdjustedMouseX(int rawMouseX) {
this.adjustedMouseX = rawMouseX - getGuiPositionX();
return this;
}
public int getAdjustedMouseY() {
return adjustedMouseY;
}
protected GuiBase updateAdjustedMouseY(int rawMouseY) {
this.adjustedMouseY = rawMouseY - getGuiPositionY();
return this;
}
public GuiBase updateAdjustedMousePosition(int rawMouseX, int rawMouseY) {
return updateAdjustedMouseX(rawMouseX).updateAdjustedMouseY(rawMouseY);
}
public int getScreenWidth() {
return width;
}
@ -117,8 +139,8 @@ public abstract class GuiBase extends GuiContainer {
return guiComponentMap.get(id);
}
public GuiComponent getGuiComponentAt(int positionX, int positionY) {
for (GuiComponent guiComponent : guiComponentMap.values()) {
public GuiComponent getFirstGuiComponentAt(int positionX, int positionY) {
for (GuiComponent guiComponent : getGuiComponents()) {
if (guiComponent.intersectsWith(positionX, positionY)) {
return guiComponent;
}
@ -127,14 +149,19 @@ public abstract class GuiBase extends GuiContainer {
return null;
}
public GuiComponent getGuiComponentAt(int positionX, int positionY, int zIndex) {
for (GuiComponent guiComponent : guiComponentMap.values()) {
if (guiComponent.intersectsWith(positionX, positionY, zIndex)) {
return guiComponent;
public GuiComponent getTopGuiComponentAt(int positionX, int positionY) {
TreeSet<GuiComponent> guiComponents = new TreeSet<GuiComponent>(GuiComponent.zIndexComparator);
for (GuiComponent guiComponent : getGuiComponents()) {
if (guiComponent.intersectsWith(positionX, positionY)) {
guiComponents.add(guiComponent);
}
}
return null;
if (!guiComponents.isEmpty()) {
return guiComponents.first();
} else {
return null;
}
}
public Collection<GuiComponent> getGuiComponents() {
@ -144,7 +171,7 @@ public abstract class GuiBase extends GuiContainer {
public Collection<GuiComponent> getGuiComponentsAt(int positionX, int positionY) {
Collection<GuiComponent> intersectingGuiComponents = new ArrayList<GuiComponent>();
for (GuiComponent guiComponent : guiComponentMap.values()) {
for (GuiComponent guiComponent : getGuiComponents()) {
if (guiComponent.intersectsWith(positionX, positionY)) {
intersectingGuiComponents.add(guiComponent);
}
@ -156,7 +183,7 @@ public abstract class GuiBase extends GuiContainer {
public Collection<GuiComponent> getGuiComponentsAt(int positionX, int positionY, int zIndex) {
Collection<GuiComponent> intersectingGuiComponents = new ArrayList<GuiComponent>();
for (GuiComponent guiComponent : guiComponentMap.values()) {
for (GuiComponent guiComponent : getGuiComponents()) {
if (guiComponent.intersectsWith(positionX, positionY, zIndex)) {
intersectingGuiComponents.add(guiComponent);
}
@ -186,9 +213,9 @@ public abstract class GuiBase extends GuiContainer {
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
// A bunch of different impls clear the list of components here - no reason I can discern why at this point
for (GuiComponent guiComponent : guiComponentMap.values()) {
for (GuiComponent guiComponent : getGuiComponents()) {
guiComponent.onInit();
}
}
@ -197,32 +224,40 @@ public abstract class GuiBase extends GuiContainer {
public void onGuiClosed() {
super.onGuiClosed();
for (GuiComponent guiComponent : guiComponentMap.values()) {
for (GuiComponent guiComponent : getGuiComponents()) {
guiComponent.onClose();
}
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
super.drawScreen(mouseX, mouseY, partialTicks);
public void drawScreen(int rawMouseX, int rawMouseY, float partialTicks) {
super.drawScreen(rawMouseX, rawMouseY, partialTicks);
for (GuiComponent guiComponent : guiComponentMap.values()) {
guiComponent.onUpdate(mouseX, mouseY, partialTicks);
updateAdjustedMousePosition(rawMouseX, rawMouseY);
for (GuiComponent guiComponent : getGuiComponents()) {
if (guiComponent.intersectsWith(getAdjustedMouseX(), getAdjustedMouseY())) {
guiComponent.onMouseOver(getAdjustedMouseX(), getAdjustedMouseY(), partialTicks);
}
}
}
@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
protected void drawGuiContainerForegroundLayer(int rawMouseX, int rawMouseY) {
// Draw text
if (shouldDrawTitle && title != null) {
getFontRenderer().drawString(StringHelper.localize(title), RenderUtils.getCenteredTextOffset(getFontRenderer(), StringHelper.localize(title), getGuiWidth()), 6, 0x404040);
}
drawComponents(true);
// Draw components
drawComponents(true, rawMouseX, rawMouseY, 0);
}
@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
protected void drawGuiContainerBackgroundLayer(float partialTicks, int rawMouseX, int rawMouseY) {
// Draw background
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
if (texture != null) {
RenderUtils.bindTexture(texture);
@ -231,25 +266,22 @@ public abstract class GuiBase extends GuiContainer {
this.drawTexturedModalRect(xStart, yStart, 0, 0, getGuiWidth(), getGuiHeight());
}
// Draw components
GL11.glPushMatrix();
GL11.glTranslatef(getGuiPositionX(), getGuiPositionY(), 0.0F);
drawComponents(false, partialTicks);
drawComponents(false, rawMouseX, rawMouseY, partialTicks);
GL11.glPopMatrix();
}
protected void drawComponents(boolean drawForeground) {
drawComponents(drawForeground, 0);
}
protected void drawComponents(boolean drawForeground, int rawMouseX, int rawMouseY, float partialTicks) {
protected void drawComponents(boolean drawForeground, float partialTicks) {
for (GuiComponent guiComponent : guiComponentMap.values()) {
for (GuiComponent guiComponent : getGuiComponents()) {
if (guiComponent.isVisible()) {
if (drawForeground) {
guiComponent.drawForeground(partialTicks);
guiComponent.drawForeground(rawMouseX, rawMouseY, partialTicks);
} else {
guiComponent.drawBackground(partialTicks);
guiComponent.drawBackground(rawMouseX, rawMouseY, partialTicks);
}
}
}

View file

@ -3,12 +3,53 @@ package com.pahimar.ee3.client.gui.component;
import com.pahimar.ee3.client.gui.GuiBase;
import net.minecraft.util.ResourceLocation;
import java.util.Comparator;
public abstract class GuiComponent implements Comparable<GuiComponent> {
public static Comparator<GuiComponent> orderingComparator = new Comparator<GuiComponent>() {
@Override
public int compare(GuiComponent guiComponent1, GuiComponent guiComponent2) {
if (guiComponent1.ordering == guiComponent2.ordering) {
if (guiComponent1.zIndex == guiComponent2.zIndex) {
if (guiComponent1.id != null && guiComponent2.id != null) {
return guiComponent1.id.compareToIgnoreCase(guiComponent2.id);
} else {
return guiComponent1.hashCode() - guiComponent2.hashCode();
}
} else {
// Purposefully sorting so that higher z-indices appear first in the map
return guiComponent2.zIndex - guiComponent1.zIndex;
}
} else {
return guiComponent1.ordering - guiComponent2.ordering;
}
}
};
public static Comparator<GuiComponent> zIndexComparator = new Comparator<GuiComponent>() {
@Override
public int compare(GuiComponent guiComponent1, GuiComponent guiComponent2) {
if (guiComponent1.zIndex == guiComponent2.zIndex) {
if (guiComponent1.ordering == guiComponent2.ordering) {
if (guiComponent1.id != null && guiComponent2.id != null) {
return guiComponent1.id.compareToIgnoreCase(guiComponent2.id);
} else {
return guiComponent1.hashCode() - guiComponent2.hashCode();
}
} else {
return guiComponent1.ordering - guiComponent2.ordering;
}
} else {
// Purposefully sorting so that higher z-indices appear first in the map
return guiComponent2.zIndex - guiComponent1.zIndex;
}
}
};
protected final GuiBase parentGui;
protected final String id;
protected ResourceLocation texture;
protected int positionX, positionY, componentWidth, componentHeight, textureWidth, textureHeight;
protected int ordering = 0;
protected int zIndex = 0;
@ -153,11 +194,11 @@ public abstract class GuiComponent implements Comparable<GuiComponent> {
return this;
}
public abstract void drawForeground(float partialTicks);
public abstract void drawForeground(int rawMouseX, int rawMouseY, float partialTicks);
public abstract void drawBackground(float partialTicks);
public abstract void drawBackground(int rawMouseX, int rawMouseY, float partialTicks);
public abstract void onUpdate(int mouseX, int mouseY, float partialTicks);
public abstract void onUpdate(int rawMouseX, int rawMouseY, float partialTicks);
/**
* Checks whether or not the specified coordinate intersects with this GuiComponent
@ -194,14 +235,10 @@ public abstract class GuiComponent implements Comparable<GuiComponent> {
public abstract void onMouseEnter(int mouseX, int mouseY);
public abstract void onMouseOver(int mouseX, int mouseY);
public abstract void onMouseOver(int mouseX, int mouseY, float partialTicks);
public abstract void onMouseLeave(int mouseX, int mouseY);
public abstract void onKeyDown(int keyPressed);
public abstract void onKeyUp(int keyPressed);
public abstract void onKeyPress(char characterTyped, int keyPressed);
public abstract void onFocusGain();
@ -228,19 +265,6 @@ public abstract class GuiComponent implements Comparable<GuiComponent> {
*/
@Override
public int compareTo(GuiComponent guiComponent) {
if (this.ordering == guiComponent.ordering) {
if (this.zIndex == guiComponent.zIndex) {
if (this.id != null && guiComponent.id != null) {
return this.id.compareToIgnoreCase(guiComponent.id);
} else {
return this.hashCode() - guiComponent.hashCode();
}
} else {
// Purposefully sorting so that higher z-indices appear first in the map
return guiComponent.zIndex - this.zIndex;
}
} else {
return this.ordering - guiComponent.ordering;
}
return orderingComparator.compare(this, guiComponent);
}
}