Bit more work on gui framework

This commit is contained in:
Pahimar 2015-12-21 15:54:07 -05:00
parent bd09bc96c4
commit 6495a087a2
2 changed files with 112 additions and 10 deletions

View file

@ -11,6 +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;
@ -116,10 +117,54 @@ public abstract class GuiBase extends GuiContainer {
return guiComponentMap.get(id);
}
public GuiComponent getGuiComponentAt(int positionX, int positionY) {
for (GuiComponent guiComponent : guiComponentMap.values()) {
if (guiComponent.intersectsWith(positionX, positionY)) {
return guiComponent;
}
}
return null;
}
public GuiComponent getGuiComponentAt(int positionX, int positionY, int zIndex) {
for (GuiComponent guiComponent : guiComponentMap.values()) {
if (guiComponent.intersectsWith(positionX, positionY, zIndex)) {
return guiComponent;
}
}
return null;
}
public Collection<GuiComponent> getGuiComponents() {
return guiComponentMap.values();
}
public Collection<GuiComponent> getGuiComponentsAt(int positionX, int positionY) {
Collection<GuiComponent> intersectingGuiComponents = new ArrayList<GuiComponent>();
for (GuiComponent guiComponent : guiComponentMap.values()) {
if (guiComponent.intersectsWith(positionX, positionY)) {
intersectingGuiComponents.add(guiComponent);
}
}
return intersectingGuiComponents;
}
public Collection<GuiComponent> getGuiComponentsAt(int positionX, int positionY, int zIndex) {
Collection<GuiComponent> intersectingGuiComponents = new ArrayList<GuiComponent>();
for (GuiComponent guiComponent : guiComponentMap.values()) {
if (guiComponent.intersectsWith(positionX, positionY, zIndex)) {
intersectingGuiComponents.add(guiComponent);
}
}
return intersectingGuiComponents;
}
public GuiBase addGuiComponent(GuiComponent guiComponent) {
this.guiComponentMap.put(guiComponent.getId(), guiComponent);
return this;

View file

@ -11,14 +11,36 @@ public abstract class GuiComponent implements Comparable<GuiComponent> {
protected int positionX, positionY, componentWidth, componentHeight, textureWidth, textureHeight;
protected int ordering = 0;
protected int zIndex = 0;
protected boolean isVisible = true;
protected boolean isEnabled = true;
public GuiComponent(GuiBase parentGui, String id, int positionX, int positionY) {
public GuiComponent(GuiBase parentGui, String id) {
this(parentGui, id, null, 0, 0);
}
public GuiComponent(GuiBase parentGui, String id, ResourceLocation texture, int positionX, int positionY) {
this(parentGui, id, texture, positionX, positionY, 256, 256);
}
public GuiComponent(GuiBase parentGui, String id, ResourceLocation texture, int positionX, int positionY, int componentWidth, int componentHeight) {
this(parentGui, id, texture, positionX, positionY, componentWidth, componentHeight, 256, 256);
}
public GuiComponent(GuiBase parentGui, String id, ResourceLocation texture, int positionX, int positionY, int componentWidth, int componentHeight, int textureWidth, int textureHeight) {
this.parentGui = parentGui;
this.id = id;
this.texture = texture;
this.positionX = positionX;
this.positionY = positionY;
this.componentWidth = componentWidth;
this.componentHeight = componentHeight;
this.textureWidth = textureWidth;
this.textureHeight = textureHeight;
}
public final GuiBase getParentGui() {
return parentGui;
}
public final String getId() {
@ -81,6 +103,24 @@ public abstract class GuiComponent implements Comparable<GuiComponent> {
return this;
}
public int getOrdering() {
return ordering;
}
public GuiComponent setOrdering(int ordering) {
this.ordering = ordering;
return this;
}
public int getZIndex() {
return zIndex;
}
public GuiComponent setZIndex(int zIndex) {
this.zIndex = zIndex;
return this;
}
public boolean isVisible() {
return isVisible;
}
@ -120,14 +160,26 @@ public abstract class GuiComponent implements Comparable<GuiComponent> {
public abstract void onUpdate(int mouseX, int mouseY, float partialTicks);
/**
* Checks whether or not the mouse cursor is intersecting with this GuiComponent
* Checks whether or not the specified coordinate intersects with this GuiComponent
*
* @param mouseX x position of mouse cursor on the screen
* @param mouseY y position of mouse cursor on the screen
* @return true if the mouse cursor is intersecting with this GuiComponent, false otherwise
* @param xCoord x position
* @param yCoord y position
* @return true if the specified coordinates intersect with this GuiComponent, false otherwise
*/
public boolean intersectsWithMouse(int mouseX, int mouseY) {
return (mouseX >= this.positionX && mouseX <= this.positionX + this.componentWidth) && (mouseY >= this.positionY && mouseY <= this.positionY + this.componentHeight);
public boolean intersectsWith(int xCoord, int yCoord) {
return (xCoord >= this.positionX && xCoord <= this.positionX + this.componentWidth) && (yCoord >= this.positionY && yCoord <= this.positionY + this.componentHeight);
}
/**
* Checks whether or not the specified coordinate and z index intersects with this GuiComponent
*
* @param xCoord x position
* @param yCoord y position
* @param zIndex z index
* @return
*/
public boolean intersectsWith(int xCoord, int yCoord, int zIndex) {
return (xCoord >= this.positionX && xCoord <= this.positionX + this.componentWidth) && (yCoord >= this.positionY && yCoord <= this.positionY + this.componentHeight) && (zIndex == this.zIndex);
}
public abstract void onMouseButtonClick(int mouseX, int mouseY, int mouseButton);
@ -177,10 +229,15 @@ public abstract class GuiComponent implements Comparable<GuiComponent> {
@Override
public int compareTo(GuiComponent guiComponent) {
if (this.ordering == guiComponent.ordering) {
if (this.id != null && guiComponent.id != null) {
return this.id.compareToIgnoreCase(guiComponent.id);
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 {
return this.hashCode() - guiComponent.hashCode();
// Purposefully sorting so that higher z-indices appear first in the map
return guiComponent.zIndex - this.zIndex;
}
} else {
return this.ordering - guiComponent.ordering;