diff --git a/mcp/mobius/opis/gui/events/MouseEvent.java b/mcp/mobius/opis/gui/events/MouseEvent.java new file mode 100644 index 0000000..ed573b2 --- /dev/null +++ b/mcp/mobius/opis/gui/events/MouseEvent.java @@ -0,0 +1,101 @@ +package mcp.mobius.opis.gui.events; + +import net.minecraft.client.Minecraft; + +import org.lwjgl.input.Mouse; + +import mcp.mobius.opis.gui.interfaces.IWidget; + +public class MouseEvent { + + public enum EventType {NONE, MOVE, CLICK, RELEASED, DRAG, WHEEL, ENTER, LEAVE}; + + public long timestamp; + public Minecraft mc; + public IWidget srcwidget; + public IWidget trgwidget; + public double x,y; + public int z; + public static int buttonCount = Mouse.getButtonCount(); + public boolean[] buttonState = new boolean[buttonCount]; + public EventType type; + public int button = -1; + + + public MouseEvent(IWidget widget){ + this.srcwidget = widget; + this.timestamp = System.nanoTime(); + + this.mc = Minecraft.getMinecraft(); + + this.x = (double)Mouse.getEventX() * (double)this.srcwidget.getSize().getX() / (double)this.mc.displayWidth; + this.y = (double)this.srcwidget.getSize().getY() - (double)Mouse.getEventY() * (double)this.srcwidget.getSize().getY() / (double)this.mc.displayHeight - 1.0; + + //this.x = Mouse.getEventX(); + //this.y = Mouse.getEventY(); + + //System.out.printf("%s %s\n", this.x, this.y); + + //this.z = Mouse.getEventDWheel(); + this.z = Mouse.getDWheel(); + + for (int i = 0; i < buttonCount; i++) + buttonState[i] = Mouse.isButtonDown(i); + + this.trgwidget = this.srcwidget.getWidgetAtCoordinates(this.x, this.y); + } + + public String toString(){ + String retstring = String.format("MOUSE %s : [%s] [ %.2f %.2f %d ] [", this.type, this.timestamp, this.x, this.y, this.z); + if (this.buttonCount < 5) + for (int i = 0; i < this.buttonCount; i++) + retstring += String.format(" %s ", this.buttonState[i]); + else + for (int i = 0; i < 5; i++) + retstring += String.format(" %s ", this.buttonState[i]); + retstring += "]"; + + if (this.button != -1) + retstring += String.format(" Button %s", this.button); + + return retstring; + } + + // Returns the event type based on the previous mouse event. + public EventType getEventType(MouseEvent me){ + + this.type = EventType.NONE; + + if (this.trgwidget != me.trgwidget){ + this.type = EventType.ENTER; + return this.type; + } + + if (this.z != 0){ + this.type = EventType.WHEEL; + return this.type; + } + + for (int i = 0; i < this.buttonCount; i++){ + if (this.buttonState[i] != me.buttonState[i]){ + if (this.buttonState[i] == true) + this.type = EventType.CLICK; + else + this.type = EventType.RELEASED; + this.button = i; + return this.type; + } + } + + //MOVE & DRAG EVENTS (we moved the mouse and button 0 was clicked or not) + if ((this.x != me.x) || (this.y != me.y)){ + if (this.buttonState[0] == true) + this.type = EventType.DRAG; + else + this.type = EventType.MOVE; + return this.type; + } + + return this.type; + } +} diff --git a/mcp/mobius/opis/gui/helpers/ReverseIterator.java b/mcp/mobius/opis/gui/helpers/ReverseIterator.java new file mode 100644 index 0000000..b29068f --- /dev/null +++ b/mcp/mobius/opis/gui/helpers/ReverseIterator.java @@ -0,0 +1,37 @@ +package mcp.mobius.opis.gui.helpers; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +import com.google.common.collect.Lists; + +public class ReverseIterator implements Iterable { + + private ListIterator listIterator; + + public ReverseIterator(Collection wrappedList) { + List list = Lists.newArrayList(wrappedList); + this.listIterator = list.listIterator(wrappedList.size()); + } + + public Iterator iterator() { + return new Iterator() { + + public boolean hasNext() { + return listIterator.hasPrevious(); + } + + public T next() { + return listIterator.previous(); + } + + public void remove() { + listIterator.remove(); + } + + }; + } + +} diff --git a/mcp/mobius/opis/gui/helpers/UIException.java b/mcp/mobius/opis/gui/helpers/UIException.java new file mode 100644 index 0000000..529ed9c --- /dev/null +++ b/mcp/mobius/opis/gui/helpers/UIException.java @@ -0,0 +1,7 @@ +package mcp.mobius.opis.gui.helpers; + +public class UIException extends RuntimeException { + public UIException(String message) { + super(message); + } +} diff --git a/mcp/mobius/opis/gui/helpers/UIHelper.java b/mcp/mobius/opis/gui/helpers/UIHelper.java new file mode 100644 index 0000000..43dfd46 --- /dev/null +++ b/mcp/mobius/opis/gui/helpers/UIHelper.java @@ -0,0 +1,58 @@ +package mcp.mobius.opis.gui.helpers; + +import org.lwjgl.opengl.GL11; + +import net.minecraft.client.renderer.Tessellator; + +public class UIHelper { + + public static void drawTexture(int posX, int posY, int sizeX, int sizeY) + { + UIHelper.drawTexture(posX, posY, sizeX, sizeY, 0, 0, 256, 256); + } + + public static void drawTexture(int posX, int posY, int sizeX, int sizeY, int texU, int texV, int texSizeU, int texSizeV) + { + float zLevel = 0.0F; + float f = 0.00390625F; + + Tessellator tess = Tessellator.instance; + tess.startDrawingQuads(); + tess.addVertexWithUV((double)(posX + 0), (double)(posY + sizeY), (double)zLevel, texU*f, (texV + texSizeV)*f); + tess.addVertexWithUV((double)(posX + sizeX), (double)(posY + sizeY), (double)zLevel, (texU + texSizeU)*f, (texV + texSizeV)*f); + tess.addVertexWithUV((double)(posX + sizeX), (double)(posY + 0), (double)zLevel, (texU + texSizeU)*f, texV*f); + tess.addVertexWithUV((double)(posX + 0), (double)(posY + 0), (double)zLevel, texU*f, texV*f); + tess.draw(); + } + + public static void drawGradientRect(int minx, int miny, int maxx, int maxy, int zlevel, int color1, int color2) + { + float alpha1 = (float)(color1 >> 24 & 255) / 255.0F; + float red1 = (float)(color1 >> 16 & 255) / 255.0F; + float green1 = (float)(color1 >> 8 & 255) / 255.0F; + float blue1 = (float)(color1 & 255) / 255.0F; + float alpha2 = (float)(color2 >> 24 & 255) / 255.0F; + float red2 = (float)(color2 >> 16 & 255) / 255.0F; + float green2 = (float)(color2 >> 8 & 255) / 255.0F; + float blue2 = (float)(color2 & 255) / 255.0F; + GL11.glDisable(GL11.GL_TEXTURE_2D); + GL11.glEnable(GL11.GL_BLEND); + GL11.glDisable(GL11.GL_ALPHA_TEST); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + GL11.glShadeModel(GL11.GL_SMOOTH); + Tessellator tessellator = Tessellator.instance; + tessellator.startDrawingQuads(); + tessellator.setColorRGBA_F(red1, green1, blue1, alpha1); + tessellator.addVertex((double)maxx, (double)miny, (double)zlevel); + tessellator.addVertex((double)minx, (double)miny, (double)zlevel); + tessellator.setColorRGBA_F(red2, green2, blue2, alpha2); + tessellator.addVertex((double)minx, (double)maxy, (double)zlevel); + tessellator.addVertex((double)maxx, (double)maxy, (double)zlevel); + tessellator.draw(); + GL11.glShadeModel(GL11.GL_FLAT); + GL11.glDisable(GL11.GL_BLEND); + GL11.glEnable(GL11.GL_ALPHA_TEST); + GL11.glEnable(GL11.GL_TEXTURE_2D); + } + +} diff --git a/mcp/mobius/opis/gui/interfaces/CType.java b/mcp/mobius/opis/gui/interfaces/CType.java new file mode 100644 index 0000000..b55cd84 --- /dev/null +++ b/mcp/mobius/opis/gui/interfaces/CType.java @@ -0,0 +1,8 @@ +package mcp.mobius.opis.gui.interfaces; + +public enum CType { +REL_X, +REL_Y, +RELXY, +ABSXY; +} diff --git a/mcp/mobius/opis/gui/interfaces/IWidget.java b/mcp/mobius/opis/gui/interfaces/IWidget.java new file mode 100644 index 0000000..bcafd7b --- /dev/null +++ b/mcp/mobius/opis/gui/interfaces/IWidget.java @@ -0,0 +1,64 @@ +package mcp.mobius.opis.gui.interfaces; + +import mcp.mobius.opis.gui.events.MouseEvent; +import mcp.mobius.opis.gui.widgets.WidgetGeometry; + +import org.lwjgl.util.Point; + +public interface IWidget { + // Should all the coordinates, sizes, etc be % or absolute values ? + // Should we allow 2 methods for an absolute referencing (screen pos) and relative one (parent pos) ? + + IWidget getParent(); + void setParent(IWidget parent); + + IWidget setGeometry(WidgetGeometry geom); + WidgetGeometry getGeometry(); + + IWidget setPos(double x, double y); + IWidget setPos(double x, double y, boolean fracX, boolean fracY); + IWidget setSize(double sx, double sy); + IWidget setSize(double sx, double sy, boolean fracX, boolean fracY); + IWidget adjustSize(); + + Point getPos(); + Point getSize(); + int getRight(); + int getLeft(); + int getTop(); + int getBottom(); + + void draw(); + void draw(Point pos); + void show(); + void hide(); + void setAlpha(float alpha); + float getAlpha(); + boolean shouldRender(); + + IWidget addWidget(String name, IWidget widget); + IWidget addWidget(String name, IWidget widget, RenderPriority priority); + IWidget getWidget(String name); + IWidget delWidget(String name); + IWidget getWidgetAtCoordinates(double posX, double posY); + + boolean isWidgetAtCoordinates(double posx, double posy); + + void handleMouseInput(); + + void onMouseClick(MouseEvent event); + void onMouseDrag(MouseEvent event); + void onMouseMove(MouseEvent event); + void onMouseRelease(MouseEvent event); + void onMouseWheel(MouseEvent event); + void onMouseEnter(MouseEvent event); + void onMouseLeave(MouseEvent event); + + void emit(Signal signal, Object... params); + void onWidgetEvent(IWidget srcwidget, Signal signal, Object... params); + + //boolean onMouseEnter(); + //boolean onMouseMoved(); + //boolean onMouseLeave(); + +} diff --git a/mcp/mobius/opis/gui/interfaces/RenderPriority.java b/mcp/mobius/opis/gui/interfaces/RenderPriority.java new file mode 100644 index 0000000..1bc9f68 --- /dev/null +++ b/mcp/mobius/opis/gui/interfaces/RenderPriority.java @@ -0,0 +1,7 @@ +package mcp.mobius.opis.gui.interfaces; + +public enum RenderPriority { + LOW, + MEDIUM, + HIGH; +} diff --git a/mcp/mobius/opis/gui/interfaces/Signal.java b/mcp/mobius/opis/gui/interfaces/Signal.java new file mode 100644 index 0000000..96dcdfc --- /dev/null +++ b/mcp/mobius/opis/gui/interfaces/Signal.java @@ -0,0 +1,8 @@ +package mcp.mobius.opis.gui.interfaces; + +public enum Signal { + DRAGGED, + CLICKED, + VALUE_CHANGED, + GEOM_CHANGED; +} diff --git a/mcp/mobius/opis/gui/interfaces/WAlign.java b/mcp/mobius/opis/gui/interfaces/WAlign.java new file mode 100644 index 0000000..536d666 --- /dev/null +++ b/mcp/mobius/opis/gui/interfaces/WAlign.java @@ -0,0 +1,5 @@ +package mcp.mobius.opis.gui.interfaces; + +public enum WAlign { +LEFT,CENTER,RIGHT,TOP,BOTTOM; +} diff --git a/mcp/mobius/opis/gui/screens/ScreenBase.java b/mcp/mobius/opis/gui/screens/ScreenBase.java new file mode 100644 index 0000000..e1aa2a9 --- /dev/null +++ b/mcp/mobius/opis/gui/screens/ScreenBase.java @@ -0,0 +1,90 @@ +package mcp.mobius.opis.gui.screens; + +import java.util.HashMap; + +import org.lwjgl.input.Mouse; + +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.widgets.LayoutCanvas; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; + +public abstract class ScreenBase extends GuiScreen{ + protected GuiScreen parent; // Return screen if available. + protected Minecraft mc; // Minecraft instance + protected HashMap widgets; // List of widgets on this ui + + public ScreenBase(GuiScreen parent) { + this.parent = parent; + this.mc = Minecraft.getMinecraft(); + this.widgets = new HashMap(); + + this.addWidget("canvas", new LayoutCanvas()); + + Mouse.getDWheel(); // We init the DWheel method (getDWheel returns the value since the last call, so we have to call it once on ui creation) + } + + ///////////////////// + // WIDGET HANDLING // + ///////////////////// + + public IWidget addWidget(String name, IWidget widget){ + this.widgets.put(name, widget); + return this.getWidget(name); + } + + public IWidget getWidget(String name){ + return this.widgets.get(name); + } + + public IWidget delWidget(String name){ + IWidget widget = this.getWidget(name); + this.widgets.remove(widget); + return widget; + } + + public IWidget getRoot(){ + return this.getWidget("canvas"); + } + + ///////////////////// + // DRAWING METHODS // + ///////////////////// + + @Override + public void drawScreen(int i, int j, float f) { + this.drawDefaultBackground(); //The dark background common to most of the UIs + super.drawScreen(i, j, f); + + for (IWidget widget: this.widgets.values()) + widget.draw(); + } + + // Used to easily display a UI without having to mess with mc handle. + public void display(){ + this.mc.displayGuiScreen(this); + } + + ///////////////////// + // INPUT METHODS // + ///////////////////// + + //Keyboard handling. Basic one is just returning to the previous UI when Esc is pressed + @Override + public void keyTyped(char keyChar, int keyID) + { + if (keyID == 1) + if (this.parent == null){ + this.mc.displayGuiScreen((GuiScreen)null); + this.mc.setIngameFocus(); + } else + this.mc.displayGuiScreen(this.parent); + } + + @Override + public void handleMouseInput() + { + this.getRoot().handleMouseInput(); + } + +} diff --git a/mcp/mobius/opis/gui/widgets/LabelFixedFont.java b/mcp/mobius/opis/gui/widgets/LabelFixedFont.java new file mode 100644 index 0000000..82ec1c2 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/LabelFixedFont.java @@ -0,0 +1,57 @@ +package mcp.mobius.opis.gui.widgets; + +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.Signal; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.util.ResourceLocation; + +import org.lwjgl.util.Point; + +public class LabelFixedFont extends WidgetBase { + + protected String text = ""; + protected int color; + + public LabelFixedFont(IWidget parent, String text){ + super(parent); + this.setText(text); + this.color = 0xFFFFFF; + } + + public LabelFixedFont(IWidget parent, String text, int color){ + super(parent); + this.setText(text); + this.color = color; + } + + @Override + public IWidget setGeometry(WidgetGeometry geom){ + this.geom = geom; + this.updateGeometry(); + return this; + } + + public void setText(String text){ + this.text = text; + this.updateGeometry(); + } + + public void setColor(int color){ + this.color = color; + } + + private void updateGeometry(){ + if (this.geom == null) + this.geom = new WidgetGeometry(0,0,50,50, CType.ABSXY, CType.ABSXY); + + this.geom = new WidgetGeometry(this.geom.x, this.geom.y, this.mc.fontRenderer.getStringWidth(this.text), 8, this.geom.posType, CType.ABSXY, this.geom.alignX, this.geom.alignY); + } + + @Override + public void draw(Point pos) { + this.saveGLState(); + this.mc.fontRenderer.drawString(this.text, pos.getX(), pos.getY(), this.color); + this.loadGLState(); + } +} diff --git a/mcp/mobius/opis/gui/widgets/LayoutBase.java b/mcp/mobius/opis/gui/widgets/LayoutBase.java new file mode 100644 index 0000000..c884c1d --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/LayoutBase.java @@ -0,0 +1,47 @@ +package mcp.mobius.opis.gui.widgets; + +import mcp.mobius.opis.gui.events.MouseEvent; +import mcp.mobius.opis.gui.helpers.UIHelper; +import mcp.mobius.opis.gui.interfaces.IWidget; + +import org.lwjgl.util.Point; + +public class LayoutBase extends WidgetBase { + + protected int bgcolor1 = 0x00000000; + protected int bgcolor2 = 0x00000000; + protected boolean renderBG; + + public LayoutBase(IWidget parent){ + super(parent); + } + + @Override + public void draw(Point pos) { + if (this.renderBG) + UIHelper.drawGradientRect(this.getLeft(), this.getTop(), this.getRight(), this.getBottom(), 0, this.bgcolor1, this.bgcolor2); + } + + public void setBackgroundColors(int bg1, int bg2){ + this.bgcolor1 = bg1; + this.bgcolor2 = bg2; + this.renderBG = true; + } + + /* + @Override + public void onMouseEnter(MouseEvent event) { + IWidget widget = this.getWidgetAtCoordinates(event.x, event.y); + if (widget != null && widget != this) + widget.onMouseEnter(event); + } + + @Override + public void onMouseLeave(MouseEvent event) { + IWidget widget = this.getWidgetAtCoordinates(event.x, event.y); + if (widget != null && widget != this) + widget.onMouseLeave(event); + } + */ + +} diff --git a/mcp/mobius/opis/gui/widgets/LayoutCanvas.java b/mcp/mobius/opis/gui/widgets/LayoutCanvas.java new file mode 100644 index 0000000..233d874 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/LayoutCanvas.java @@ -0,0 +1,122 @@ +package mcp.mobius.opis.gui.widgets; + +import net.minecraft.client.gui.ScaledResolution; + +import org.lwjgl.input.Mouse; +import org.lwjgl.util.Point; + +import mcp.mobius.opis.gui.events.MouseEvent; +import mcp.mobius.opis.gui.events.MouseEvent.EventType; +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.Signal; + +public class LayoutCanvas extends LayoutBase{ + + private MouseEvent lastMouseEvent; + private IWidget draggedWidget = null; + + public LayoutCanvas(){ + super(null); + this.setGeometry(0, 0, this.rez.getScaledWidth(), this.rez.getScaledHeight(), CType.ABSXY, CType.ABSXY); + Mouse.getDWheel(); // This is to "calibrate" the DWheel + this.lastMouseEvent = new MouseEvent(this); + } + + @Override + public void draw(){ + this.rez = new ScaledResolution(mc.gameSettings, mc.displayWidth, mc.displayHeight); + this.setGeometry(0, 0, this.rez.getScaledWidth(), this.rez.getScaledHeight(), CType.ABSXY, CType.ABSXY); + + /* + this.draw(this.getPos()); + + for (IWidget widget: this.widgets.values()) + if (widget.shouldRender()) + widget.draw(); + */ + + super.draw(); + + this.handleMouseInput(); + } + + @Override + public void draw(Point pos) { super.draw(pos); } + + @Override + public void handleMouseInput(){ + // Here we are going to generate the require mouse events we will pass down to all the other widgets + // This is more or less where the magic happens. If you want mouse support for a widget, it should be + // attached to a canvas like this one, or any inheriting one. + + MouseEvent event = new MouseEvent(this); + EventType type = event.getEventType(this.lastMouseEvent); + IWidget targetWidget = this.getWidgetAtCoordinates(event.x, event.y); + + switch (type){ + case CLICK: + if (targetWidget != null) + targetWidget.onMouseClick(event); + //this.onMouseClick(event); + break; + case DRAG: + if (targetWidget != null) + targetWidget.onMouseDrag(event); + //this.onMouseDrag(event); + break; + case MOVE: + if (targetWidget != null) + targetWidget.onMouseMove(event); + //this.onMouseMove(event); + break; + case RELEASED: + if (targetWidget != null) + targetWidget.onMouseRelease(event); + //this.onMouseRelease(event); + break; + case WHEEL: + if (targetWidget != null) + targetWidget.onMouseWheel(event); + //this.onMouseWheel(event); + break; + case ENTER: + if (event.trgwidget != null) + event.trgwidget.onMouseEnter(event); + if (this.lastMouseEvent.trgwidget != null){ + event.type = EventType.LEAVE; + this.lastMouseEvent.trgwidget.onMouseLeave(event); + } + break; + case NONE: + break; + default: + break; + } + + this.lastMouseEvent = event; + } + + @Override + public void onWidgetEvent(IWidget srcwidget, Signal signal, Object... params){ + if (signal == Signal.DRAGGED) + this.draggedWidget = srcwidget; + else + super.onWidgetEvent(srcwidget, signal, params); + } + + @Override + public void onMouseRelease(MouseEvent event){ + if(event.button == 0) + this.draggedWidget = null; + super.onMouseRelease(event); + } + + @Override + public void onMouseDrag(MouseEvent event){ + if (this.draggedWidget != null) + this.draggedWidget.onMouseDrag(event); + else + super.onMouseDrag(event); + } +} diff --git a/mcp/mobius/opis/gui/widgets/LayoutCropping.java b/mcp/mobius/opis/gui/widgets/LayoutCropping.java new file mode 100644 index 0000000..771198f --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/LayoutCropping.java @@ -0,0 +1,68 @@ +package mcp.mobius.opis.gui.widgets; + +import mcp.mobius.opis.gui.interfaces.IWidget; +import net.minecraft.client.gui.ScaledResolution; + +import org.lwjgl.opengl.GL11; +import org.lwjgl.util.Point; + +public class LayoutCropping extends LayoutBase { + + int xOffset = 0; + int yOffset = 0; + + public LayoutCropping(IWidget parent){ + super(parent); + } + + public void setOffsets(int xoffset, int yoffset){ + this.xOffset = xoffset; + this.yOffset = yoffset; + } + + @Override + public void draw(){ + this.rez = new ScaledResolution(mc.gameSettings, mc.displayWidth, mc.displayHeight); + this.saveGLState(); + + GL11.glEnable(GL11.GL_BLEND); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA); + GL11.glColor4f(1.0f, 1.0f, 1.0f, this.alpha); + + this.draw(this.getPos()); + + GL11.glEnable(GL11.GL_SCISSOR_TEST); + GL11.glScissor(this.getPos().getX()*this.rez.getScaleFactor(), (this.rez.getScaledHeight() - (this.getPos().getY() + this.getSize().getY())) * this.rez.getScaleFactor(), this.getSize().getX()*this.rez.getScaleFactor(), this.getSize().getY()*this.rez.getScaleFactor()); + //GL11.glScissor(this.getPos().getX()*this.rez.getScaleFactor(), this.getPos().getY()*this.rez.getScaleFactor(), this.getSize().getX()*this.rez.getScaleFactor(), this.getSize().getY()*this.rez.getScaleFactor()); + + GL11.glTranslatef(xOffset, yOffset, 0.0f); + + /* + for (IWidget widget: this.widgets.values()) + if (widget.shouldRender()) + widget.draw(); + */ + + + for (IWidget widget: this.renderQueue_LOW.values()) + if (widget.shouldRender()) + widget.draw(); + + for (IWidget widget: this.renderQueue_MEDIUM.values()) + if (widget.shouldRender()) + widget.draw(); + + for (IWidget widget: this.renderQueue_HIGH.values()) + if (widget.shouldRender()) + widget.draw(); + + + GL11.glDisable(GL11.GL_SCISSOR_TEST); + + this.loadGLState(); + } + + @Override + public void draw(Point pos) { super.draw(pos); } + +} diff --git a/mcp/mobius/opis/gui/widgets/LayoutMargin.java b/mcp/mobius/opis/gui/widgets/LayoutMargin.java new file mode 100644 index 0000000..78ee2aa --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/LayoutMargin.java @@ -0,0 +1,43 @@ +package mcp.mobius.opis.gui.widgets; + +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.WAlign; + +import org.lwjgl.util.Point; + +public class LayoutMargin extends LayoutBase { + + int left = 0; + int right = 0; + int top = 0; + int bottom = 0; + + public LayoutMargin(IWidget parent){ + super (parent); + this.setGeometry(new WidgetGeometry(0,0,0,0,CType.ABSXY,CType.ABSXY,WAlign.CENTER,WAlign.CENTER)); + } + + public void setMargins(int left, int right, int top, int bottom){ + this.left = left; + this.right = right; + this.top = top; + this.bottom = bottom; + } + + @Override + public Point getSize(){ + Point parentSize = this.parent.getSize(); + return new Point(parentSize.getX() - left - right, parentSize.getY() - top - bottom); + } + + @Override + public Point getPos(){ + Point parentPos = this.parent.getPos(); + return new Point(parentPos.getX() + left, parentPos.getY() + top); + } + + @Override + public void draw(Point pos) { super.draw(pos); } + +} diff --git a/mcp/mobius/opis/gui/widgets/PictureDisplay.java b/mcp/mobius/opis/gui/widgets/PictureDisplay.java new file mode 100644 index 0000000..1977030 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/PictureDisplay.java @@ -0,0 +1,31 @@ +package mcp.mobius.opis.gui.widgets; + +import mcp.mobius.opis.gui.helpers.UIHelper; +import mcp.mobius.opis.gui.interfaces.IWidget; +import net.minecraft.client.renderer.Tessellator; +import net.minecraft.util.ResourceLocation; + +import org.lwjgl.opengl.GL11; +import org.lwjgl.util.Point; + +public class PictureDisplay extends WidgetBase { + + protected ResourceLocation texture; + + public PictureDisplay(IWidget parent, String uri){ + super(parent); + this.texture = new ResourceLocation(uri); + } + + @Override + public void draw(Point pos) { + this.saveGLState(); + + GL11.glPushMatrix(); + this.texManager.bindTexture(texture); + UIHelper.drawTexture(pos.getX(), pos.getY(), this.getSize().getX(), this.getSize().getY()); + GL11.glPopMatrix(); + + this.loadGLState(); + } +} diff --git a/mcp/mobius/opis/gui/widgets/PictureSwitch.java b/mcp/mobius/opis/gui/widgets/PictureSwitch.java new file mode 100644 index 0000000..c9b9b2d --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/PictureSwitch.java @@ -0,0 +1,49 @@ +package mcp.mobius.opis.gui.widgets; + +import mcp.mobius.opis.gui.events.MouseEvent; +import mcp.mobius.opis.gui.helpers.UIHelper; +import mcp.mobius.opis.gui.interfaces.IWidget; +import net.minecraft.util.ResourceLocation; + +import org.lwjgl.opengl.GL11; +import org.lwjgl.util.Point; + +public class PictureSwitch extends WidgetBase { + + private ResourceLocation texture1; + private ResourceLocation texture2; + private boolean mouseOver = false; + + public PictureSwitch(IWidget parent, String uri1, String uri2){ + super(parent); + this.texture1 = new ResourceLocation(uri1); + this.texture2 = new ResourceLocation(uri2); + } + + @Override + public void draw(Point pos) { + this.saveGLState(); + + ResourceLocation texture = mouseOver ? this.texture2 : this.texture1; + + GL11.glPushMatrix(); + this.texManager.bindTexture(texture); + UIHelper.drawTexture(pos.getX(), pos.getY(), this.getSize().getX(), this.getSize().getY()); + GL11.glPopMatrix(); + + this.loadGLState(); + } + + @Override + public void onMouseEnter(MouseEvent event){ + //System.out.printf("%s %s\n", this, event); + mouseOver = true; + } + + @Override + public void onMouseLeave(MouseEvent event){ + //System.out.printf("%s %s\n", this, event); + mouseOver = false; + } + +} diff --git a/mcp/mobius/opis/gui/widgets/ViewTable.java b/mcp/mobius/opis/gui/widgets/ViewTable.java new file mode 100644 index 0000000..8290d15 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/ViewTable.java @@ -0,0 +1,201 @@ +package mcp.mobius.opis.gui.widgets; + +import mcp.mobius.opis.gui.helpers.UIException; +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.WAlign; + +import org.lwjgl.util.Point; + +public class ViewTable extends WidgetBase{ + + public class Cell extends WidgetBase{ + public Cell(IWidget parent, String text, WAlign align){ + super(parent); + + //this.addWidget("Crop", new LayoutCropping(null)).setGeometry(new WidgetGeometry(50.0, 50.0, 90.0, 100.0, CType.RELXY, CType.RELXY, WAlign.CENTER, WAlign.CENTER)); + + switch(align){ + case CENTER: + //this.getWidget("Crop").addWidget("Text", new LabelFixedFont(null, text)) + this.addWidget("Text", new LabelFixedFont(null, text)) + .setGeometry(new WidgetGeometry(50.0, 50.0, 100.0, 100.0, CType.RELXY, CType.RELXY, WAlign.CENTER, WAlign.CENTER)); + break; + case LEFT: + //this.getWidget("Crop").addWidget("Text", new LabelFixedFont(null, text)) + this.addWidget("Text", new LabelFixedFont(null, text)) + .setGeometry(new WidgetGeometry(5.0, 50.0, 95.0, 100.0, CType.RELXY, CType.RELXY, WAlign.LEFT, WAlign.CENTER)); + break; + case RIGHT: + //this.getWidget("Crop").addWidget("Text", new LabelFixedFont(null, text)) + this.addWidget("Text", new LabelFixedFont(null, text)) + .setGeometry(new WidgetGeometry(5.0, 50.0, 95.0, 100.0, CType.RELXY, CType.RELXY, WAlign.RIGHT, WAlign.CENTER)); + break; + default: + throw new UIException(String.format("Unexpected align value : %s", align)); + } + } + + @Override + public void draw(Point pos){} + } + + public class Row extends WidgetBase{ + int ncolumns = -1; + double[] widths; + String[] texts; + WAlign[] aligns; + boolean init = false; + int bgcolor1 = 0x50505050; + int bgcolor2 = 0x50505050; + + + public Row(IWidget parent){ + super(parent); + + this.addWidget("Background", new LayoutBase(null)).setGeometry(new WidgetGeometry(50.0, 50.0, 100.0, 100.0, CType.RELXY, CType.RELXY, WAlign.CENTER, WAlign.CENTER)); + ((LayoutBase)this.getWidget("Background")).setBackgroundColors(this.bgcolor1, this.bgcolor2); + + } + + public Row setColumnsWidth(double... widths){ + if (this.ncolumns == -1) + this.ncolumns = widths.length; + else if(this.ncolumns != widths.length){ + throw new UIException(String.format("Number of columns mismatch. Expecting %d, got %d", this.ncolumns, widths.length)); + } + + this.widths = widths; + + return this; + } + + public Row setColumnsText(String...strings ){ + if (this.ncolumns == -1) + this.ncolumns = strings.length; + else if(this.ncolumns != strings.length){ + throw new UIException(String.format("Number of columns mismatch. Expecting %d, got %d", this.ncolumns, strings.length)); + } + + this.texts = strings; + + return this; + } + + public Row setColumnsAlign(WAlign... aligns ){ + if (this.ncolumns == -1) + this.ncolumns = aligns.length; + else if(this.ncolumns != aligns.length){ + throw new UIException(String.format("Number of columns mismatch. Expecting %d, got %d", this.ncolumns, aligns.length)); + } + + this.aligns = aligns; + + return this; + } + + public Row setColors(int bg1, int bg2){ + this.bgcolor1 = bg1; + this.bgcolor2 = bg2; + ((LayoutBase)this.getWidget("Background")).setBackgroundColors(this.bgcolor1, this.bgcolor2); + return this; + } + + @Override + public void draw(){ + if (!init) + { + double currentOffset = 0.0; + for (int i = 0; i < this.ncolumns; i++){ + if (!this.widgets.containsKey(String.format("Cell_%02d", i))){ + Cell cell = (Cell)(this.addWidget(String.format("Cell_%02d", i), new Cell(null, this.texts[i], this.aligns[i]))); + cell.setGeometry(new WidgetGeometry(currentOffset, 50.0, this.widths[i], 100.0, CType.RELXY, CType.RELXY, WAlign.LEFT, WAlign.CENTER)); + currentOffset += this.widths[i]; + } + } + init = true; + } + + super.draw(); + } + + @Override + public void draw(Point pos){} + } + + int ncolumns = -1; + int nrows = 0; + double[] widths; + String[] texts; + WAlign[] aligns; + boolean init = false; + + public ViewTable(IWidget parent){ + super(parent); + this.addWidget("Titles", new Row(null)).setGeometry(new WidgetGeometry(0.0, 0.0, 100.0, 16.0, CType.REL_X, CType.REL_X, WAlign.LEFT, WAlign.TOP)); + ((Row)this.getWidget("Titles")).setColors(0x00000000, 0x00000000); + this.addWidget("Viewport", new ViewportScrollable(null)).setGeometry(new WidgetGeometry(0.0, 16.0, 100.0, 90.0, CType.REL_X, CType.RELXY, WAlign.LEFT, WAlign.TOP)); + ((ViewportScrollable)(this.getWidget("Viewport"))).attachWidget(new LayoutBase(null)).setGeometry(new WidgetGeometry(0.0, 0.0, 100.0, 0.0, CType.RELXY, CType.REL_X, WAlign.LEFT, WAlign.TOP)); + + } + + @Override + public void draw(Point pos){} + + public ViewTable setColumnsWidth(double... widths){ + if (this.ncolumns == -1) + this.ncolumns = widths.length; + else if(this.ncolumns != widths.length){ + throw new UIException(String.format("Number of columns mismatch. Expecting %d, got %d", this.ncolumns, widths.length)); + } + + this.widths = widths; + ((Row)this.getWidget("Titles")).setColumnsWidth(widths); + return this; + } + + public ViewTable setColumnsTitle(String...strings ){ + if (this.ncolumns == -1) + this.ncolumns = strings.length; + else if(this.ncolumns != strings.length){ + throw new UIException(String.format("Number of columns mismatch. Expecting %d, got %d", this.ncolumns, strings.length)); + } + + this.texts = strings; + ((Row)this.getWidget("Titles")).setColumnsText(strings); + + return this; + } + + public ViewTable setColumnsAlign(WAlign... aligns ){ + if (this.ncolumns == -1) + this.ncolumns = aligns.length; + else if(this.ncolumns != aligns.length){ + throw new UIException(String.format("Number of columns mismatch. Expecting %d, got %d", this.ncolumns, aligns.length)); + } + + this.aligns = aligns; + ((Row)this.getWidget("Titles")).setColumnsAlign(aligns); + + return this; + } + + public ViewTable addRow(String...strings ){ + IWidget tableLayout = ((ViewportScrollable)(this.getWidget("Viewport"))).getAttachedWidget(); + tableLayout.setSize(100.0, (this.nrows + 1) * 16); + + Row newRow = (Row)new Row(null); + newRow.setColumnsWidth(this.widths); + newRow.setColumnsText(strings); + newRow.setColumnsAlign(this.aligns); + if(this.nrows % 2 == 1) + newRow.setColors(0x50808080, 0x50808080); + newRow.setGeometry(new WidgetGeometry(0.0, 16*this.nrows, 100.0, 16, CType.REL_X, CType.REL_X, WAlign.LEFT, WAlign.TOP)); + + tableLayout.addWidget(String.format("Row_%03d", this.nrows), newRow); + + this.nrows += 1; + + return this; + } +} diff --git a/mcp/mobius/opis/gui/widgets/ViewportScrollable.java b/mcp/mobius/opis/gui/widgets/ViewportScrollable.java new file mode 100644 index 0000000..26bdf31 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/ViewportScrollable.java @@ -0,0 +1,179 @@ +package mcp.mobius.opis.gui.widgets; + +import mcp.mobius.opis.gui.events.MouseEvent; +import mcp.mobius.opis.gui.helpers.UIHelper; +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.Signal; +import mcp.mobius.opis.gui.interfaces.WAlign; + +import org.lwjgl.opengl.Display; +import org.lwjgl.util.Point; + +public class ViewportScrollable extends WidgetBase { + + public class Escalator extends WidgetBase{ + int yOffset = 0; + int sizeCursor = 8; + int maxValue = 0; + int step = 0; + boolean drag = false; + + public Escalator(IWidget parent, int step){ + this.parent = parent; + this.step = step; + } + + public void setOffset(int yoffset){ + this.yOffset = yoffset; + } + + public void setMaxValue(int value){ + this.maxValue = value; + } + + public void setStep(int step){ + this.step = step; + } + + @Override + public void draw(Point pos) { + UIHelper.drawGradientRect(this.getLeft(), this.getTop(), this.getRight(), this.getBottom(), 1, 0xff999999, 0xff999999); + int offsetScaled = (int)(((double)this.getSize().getY() - (double)sizeCursor + 1) / (double)this.maxValue * (yOffset)); + UIHelper.drawGradientRect(this.getLeft(), this.getTop() + offsetScaled, this.getRight(), this.getTop() + offsetScaled + sizeCursor, 1, 0xffffffff, 0xffffffff); + } + + @Override + public void onMouseClick(MouseEvent event){ + if (event.button == 0){ + int offsetScaled = this.getTop() + (int)(((double)this.getSize().getY() - (double)sizeCursor + 1) / (double)this.maxValue * (yOffset)); + + this.drag = false; + + if (event.y < offsetScaled) + this.yOffset += this.step; + else if (event.y > offsetScaled + sizeCursor) + this.yOffset -= this.step; + else + this.drag = true; + + this.emit(Signal.VALUE_CHANGED, this.yOffset); + + } else { + super.onMouseClick(event); + } + } + + @Override + public void onMouseRelease(MouseEvent event){ + if (event.button == 0) + this.drag = false; + super.onMouseRelease(event); + } + + @Override + public void onMouseDrag(MouseEvent event){ + if (this.drag){ + int relativeY = (int)event.y - this.getTop(); + double factor = ((double)this.getSize().getY() - (double)sizeCursor + 1) / (double)this.maxValue; + this.yOffset = (int)(relativeY / factor); + this.yOffset = Math.max(this.yOffset, this.maxValue); + this.yOffset = Math.min(this.yOffset, 0); + this.emit(Signal.VALUE_CHANGED, this.yOffset); + this.emit(Signal.DRAGGED, this); + } + else + super.onMouseDrag(event); + } + + } + + IWidget attachedWidget = null; + int yOffset = 0; + int step = 5; + + public ViewportScrollable( IWidget parent){ + super(parent); + this.addWidget("Cropping", new LayoutCropping(null)).setGeometry(new WidgetGeometry(0.0, 0.0, 100.0, 100.0, CType.RELXY, CType.RELXY, WAlign.LEFT, WAlign.TOP)); + this.addWidget("Escalator", new Escalator(null, this.step * 5)).setGeometry(new WidgetGeometry(100.0, 0, 8, 100.0, CType.RELXY, CType.REL_Y, WAlign.RIGHT, WAlign.TOP)).hide(); + } + + public IWidget attachWidget(IWidget widget){ + this.attachedWidget = this.getWidget("Cropping").addWidget("Cropped", widget); + return this.attachedWidget; + } + + public IWidget getAttachedWidget(){ + return this.attachedWidget; + } + + public IWidget setStep(int step){ + this.step = step; + ((Escalator)this.getWidget("Escalator")).setStep(this.step * 5); + return this; + } + + @Override + public void draw(Point pos) {} + + @Override + public void draw(){ + //if (Display.wasResized()) + + if ((this.attachedWidget != null) && (this.attachedWidget.getSize().getY() > this.getSize().getY())) + this.getWidget("Escalator").show(); + else + this.getWidget("Escalator").hide(); + + super.draw(); + } + + @Override + public void onMouseWheel(MouseEvent event){ + this.yOffset += event.z / 120.0 * this.step; + + this.yOffset = Math.max(this.yOffset, this.getSize().getY() - this.attachedWidget.getSize().getY()); + this.yOffset = Math.min(this.yOffset, 0); + + ((LayoutCropping)this.getWidget("Cropping")).setOffsets(0, this.yOffset); + ((Escalator)this.getWidget("Escalator")).setOffset(this.yOffset); + } + + @Override + public void onWidgetEvent(IWidget srcwidget, Signal signal, Object... params){ + if (srcwidget.equals(this.attachedWidget) && signal == Signal.GEOM_CHANGED){ + ((Escalator)this.getWidget("Escalator")).setMaxValue(this.getSize().getY() - srcwidget.getSize().getY()); + } + + else if (srcwidget.equals(this.getWidget("Escalator")) && signal == Signal.VALUE_CHANGED){ + this.yOffset = (Integer)params[0]; + ((LayoutCropping)this.getWidget("Cropping")).setOffsets(0, this.yOffset); + } + else + super.onWidgetEvent(srcwidget, signal, params); + + } + + @Override + public void onMouseDrag(MouseEvent event){ + if (this.getWidget("Escalator").shouldRender() && ((Escalator)this.getWidget("Escalator")).drag) + this.getWidget("Escalator").onMouseDrag(event); + else + super.onMouseDrag(event); + } + + @Override + public void onMouseClick(MouseEvent event){ + if (event.button == 0){ + if (!this.getWidget("Escalator").isWidgetAtCoordinates(event.x, event.y)){ + ((Escalator)this.getWidget("Escalator")).drag = false; + super.onMouseClick(event); + } + else + this.getWidget("Escalator").onMouseClick(event); + } + else + super.onMouseClick(event); + } + +} diff --git a/mcp/mobius/opis/gui/widgets/WidgetBase.java b/mcp/mobius/opis/gui/widgets/WidgetBase.java new file mode 100644 index 0000000..f0ed324 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/WidgetBase.java @@ -0,0 +1,362 @@ +package mcp.mobius.opis.gui.widgets; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; + +import org.lwjgl.opengl.GL11; +import org.lwjgl.util.Point; + +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.Table; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.client.renderer.texture.TextureManager; +import mcp.mobius.opis.gui.events.MouseEvent; +import mcp.mobius.opis.gui.helpers.ReverseIterator; +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.RenderPriority; +import mcp.mobius.opis.gui.interfaces.Signal; + +public abstract class WidgetBase implements IWidget { + + protected IWidget parent; + protected WidgetGeometry geom; + protected LinkedHashMap widgets = new LinkedHashMap(); + protected LinkedHashMap renderQueue_HIGH = new LinkedHashMap(); + protected LinkedHashMap renderQueue_MEDIUM = new LinkedHashMap(); + protected LinkedHashMap renderQueue_LOW = new LinkedHashMap(); + protected Minecraft mc; + protected TextureManager texManager; + protected ScaledResolution rez; + + protected boolean hasBlending; + protected boolean hasLight; + protected int boundTexIndex; + + protected boolean isRendering = true; + + protected float alpha = 1.0f; + + public WidgetBase(){ + this.setParent(null); + this.mc = Minecraft.getMinecraft(); + this.rez = new ScaledResolution(mc.gameSettings, mc.displayWidth, mc.displayHeight); + this.texManager = this.mc.renderEngine; + this.setGeometry(new WidgetGeometry(0, 0, 50, 50, CType.ABSXY, CType.ABSXY)); + } + + public WidgetBase(IWidget parent){ + this.setParent(parent); + this.mc = Minecraft.getMinecraft(); + this.rez = new ScaledResolution(mc.gameSettings, mc.displayWidth, mc.displayHeight); + this.texManager = this.mc.renderEngine; + this.setGeometry(new WidgetGeometry(0, 0, 50, 50, CType.ABSXY, CType.ABSXY)); + } + + ///////////////////// + // WIDGET HANDLING // + ///////////////////// + + @Override + public IWidget addWidget(String name, IWidget widget){ + return this.addWidget(name, widget, RenderPriority.MEDIUM); + } + + @Override + public IWidget addWidget(String name, IWidget widget, RenderPriority priority){ + widget.setParent(this); + this.widgets.put(name, widget); + + switch(priority){ + case LOW: + this.renderQueue_LOW.put(name, widget); + break; + case HIGH: + this.renderQueue_HIGH.put(name, widget); + break; + case MEDIUM: + this.renderQueue_MEDIUM.put(name, widget); + break; + } + + + return this.getWidget(name); + } + + + @Override + public IWidget getWidget(String name){ + return this.widgets.get(name); + } + + @Override + public IWidget delWidget(String name){ + IWidget widget = this.getWidget(name); + this.widgets.remove(widget); + return widget; + } + + @Override + public IWidget getWidgetAtCoordinates(double posX, double posY){ + for (IWidget widget : new ReverseIterator(this.widgets.values())) + if ((posX >= widget.getPos().getX()) && + (posX <= widget.getPos().getX() + widget.getSize().getX()) && + (posY >= widget.getPos().getY()) && + (posY <= widget.getPos().getY() + widget.getSize().getY())) + return widget.getWidgetAtCoordinates(posX, posY); + + if ((posX >= this.getPos().getX()) && + (posX <= this.getPos().getX() + this.getSize().getX()) && + (posY >= this.getPos().getY()) && + (posY <= this.getPos().getY() + this.getSize().getY())) + return this; + + return null; + } + + @Override + public boolean isWidgetAtCoordinates(double posx, double posy){ + if (this.getLeft() > posx) return false; + if (this.getRight() < posx) return false; + if (this.getTop() > posy) return false; + if (this.getBottom() < posy) return false; + return true; + } + + /////////////////////// + // IWIDGET INTERFACE // + /////////////////////// + + @Override + public IWidget getParent() { return this.parent; } + @Override + public void setParent(IWidget parent) { this.parent = parent; } + + @Override + public void draw(){ + this.saveGLState(); + GL11.glEnable(GL11.GL_BLEND); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA); + GL11.glColor4f(1.0f, 1.0f, 1.0f, this.alpha); + + this.draw(this.getPos()); + + /* + for (IWidget widget: this.widgets.values()) + if (widget.shouldRender()) + widget.draw(); + */ + + for (IWidget widget: this.renderQueue_LOW.values()) + if (widget.shouldRender()) + widget.draw(); + + for (IWidget widget: this.renderQueue_MEDIUM.values()) + if (widget.shouldRender()) + widget.draw(); + + for (IWidget widget: this.renderQueue_HIGH.values()) + if (widget.shouldRender()) + widget.draw(); + + this.loadGLState(); + } + + @Override + public abstract void draw(Point pos); + + @Override + public IWidget setGeometry(WidgetGeometry geom) { + this.geom = geom; + this.emit(Signal.GEOM_CHANGED, this.geom); + return this; + } + + public IWidget setGeometry(double x, double y, double sx, double sy, CType fp, CType fs){ + this.setGeometry(new WidgetGeometry (x, y, sx, sy, fp, fs)); + this.emit(Signal.GEOM_CHANGED, this.geom); + return this; + } + + @Override + public WidgetGeometry getGeometry() { return this.geom; } + @Override + public Point getPos() { return this.geom.getPos(this.parent); } + @Override + public Point getSize() { return this.geom.getSize(this.parent); } + @Override + public int getLeft() { return this.getPos().getX(); } + @Override + public int getRight() { return this.getPos().getX() + this.getSize().getX(); } + @Override + public int getTop() { return this.getPos().getY(); } + @Override + public int getBottom() { return this.getPos().getY() + this.getSize().getY(); } + + @Override + public IWidget setPos(double x, double y){ + return this.setPos(x, y, this.geom.fracPosX, this.geom.fracPosY); + } + + @Override + public IWidget setPos(double x, double y, boolean fracX, boolean fracY){ + this.geom.setPos(x, y, fracX, fracY); + this.emit(Signal.GEOM_CHANGED, this.geom); + return this; + }; + + @Override + public IWidget setSize(double sx, double sy){ + return this.setSize(sx, sy, this.geom.fracSizeX, this.geom.fracSizeY); + } + + @Override + public IWidget setSize(double sx, double sy, boolean fracX, boolean fracY){ + this.geom.setSize(sx, sy, fracX, fracY); + this.emit(Signal.GEOM_CHANGED, this.geom); + return this; + }; + + @Override + public IWidget adjustSize(){ + int minleft = 9999; + int maxright = 0; + int mintop = 9999; + int maxbottom = 0; + + for (IWidget widget : this.widgets.values()){ + minleft = Math.min(minleft, widget.getLeft()); + maxright = Math.max(maxright, widget.getRight()); + + mintop = Math.min(mintop, widget.getTop()); + maxbottom = Math.max(maxbottom, widget.getBottom()); + } + + this.setSize(maxright - minleft, maxbottom - mintop, false, false); + + this.emit(Signal.GEOM_CHANGED, this.geom); + return this; + } + + //////////////////////////// + // SOME RENDERING HELPERS // + //////////////////////////// + + protected void saveGLState(){ + hasBlending = GL11.glGetBoolean(GL11.GL_BLEND); + hasLight = GL11.glGetBoolean(GL11.GL_LIGHTING); + boundTexIndex = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D); + GL11.glPushAttrib(GL11.GL_CURRENT_BIT); + GL11.glPushMatrix(); + } + + protected void loadGLState(){ + if (hasBlending) GL11.glEnable(GL11.GL_BLEND); else GL11.glDisable(GL11.GL_BLEND); + if (hasLight) GL11.glEnable(GL11.GL_LIGHTING); else GL11.glDisable(GL11.GL_LIGHTING); + GL11.glBindTexture(GL11.GL_TEXTURE_2D, boundTexIndex); + GL11.glPopMatrix(); + GL11.glPopAttrib(); + //GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + } + + @Override + public void setAlpha(float alpha){this.alpha = alpha;}; + + @Override + public float getAlpha(){return this.alpha;}; + + @Override + public void show(){this.isRendering = true;}; + @Override + public void hide(){this.isRendering = false;}; + @Override + public boolean shouldRender(){return this.isRendering;}; + + //////////////////// + // INPUT HANDLING // + //////////////////// + + @Override + public void handleMouseInput(){} + + @Override + public void onMouseClick(MouseEvent event) { + //System.out.printf("%s %s\n", this, event); + if (this.parent != null) + this.parent.onMouseClick(event); + + //IWidget widget = this.getWidgetAtCoordinates(event.x, event.y); + //if (widget != null && widget != this) + // widget.onMouseClick(event); + } + + @Override + public void onMouseDrag(MouseEvent event) { + //System.out.printf("%s %s\n", this, event); + if (this.parent != null) + this.parent.onMouseDrag(event); + + //IWidget widget = this.getWidgetAtCoordinates(event.x, event.y); + //if (widget != null && widget != this) + // widget.onMouseDrag(event); + } + + @Override + public void onMouseMove(MouseEvent event) { + //System.out.printf("%s %s\n", this, event); + if (this.parent != null) + this.parent.onMouseMove(event); + + + //IWidget widget = this.getWidgetAtCoordinates(event.x, event.y); + //if (widget != null && widget != this) + // widget.onMouseMove(event); + } + + @Override + public void onMouseRelease(MouseEvent event) { + //System.out.printf("%s %s\n", this, event); + if (this.parent != null) + this.parent.onMouseRelease(event); + + //IWidget widget = this.getWidgetAtCoordinates(event.x, event.y); + //if (widget != null && widget != this) + // widget.onMouseRelease(event); + } + + @Override + public void onMouseWheel(MouseEvent event) { + //System.out.printf("%s %s\n", this, event); + if (this.parent != null) + this.parent.onMouseWheel(event); + } + + @Override + public void onMouseEnter(MouseEvent event) { + //System.out.printf("%s %s\n", this, event); + if (this.parent != null) + this.parent.onMouseEnter(event); + } + + @Override + public void onMouseLeave(MouseEvent event) { + //System.out.printf("%s %s\n", this, event); + if (this.parent != null) + this.parent.onMouseLeave(event); + } + + @Override + public void emit(Signal signal, Object... params) { + if(this.parent != null) + this.parent.onWidgetEvent(this, signal, params); + } + + @Override + public void onWidgetEvent(IWidget srcwidget, Signal signal, Object... params) { + if (this.parent != null) + this.parent.onWidgetEvent(srcwidget, signal, params); + } +} diff --git a/mcp/mobius/opis/gui/widgets/WidgetGeometry.java b/mcp/mobius/opis/gui/widgets/WidgetGeometry.java new file mode 100644 index 0000000..15ec5c0 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/WidgetGeometry.java @@ -0,0 +1,186 @@ +package mcp.mobius.opis.gui.widgets; + +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.WAlign; +import net.minecraft.util.MathHelper; + +import org.lwjgl.util.Point; + + + +//public class WidgetGeometry implements Cloneable{ +public class WidgetGeometry{ + + //public enum Align {LEFT, CENTER, RIGHT, TOP, BOTTOM}; + + + double x = -1; + double y = -1; + double sx = -1; + double sy = -1; + + CType posType; + CType sizeType; + + boolean fracPosX = false; + boolean fracPosY = false; + boolean fracSizeX = false; + boolean fracSizeY = false; + + WAlign alignX; + WAlign alignY; + + public class PointDouble{ + double x; double y; + public PointDouble(double x, double y){ this.x = x; this.y = y;}; + public double getX(){return this.x;} + public double getY(){return this.y;} + public String toString(){return String.format("PointDouble : %.5f %.5f", this.x, this.y);} + } + + public WidgetGeometry(double x, double y, double sx, double sy, CType fracPos, CType fracSize){ + this(x, y, sx, sy, fracPos, fracSize, WAlign.LEFT, WAlign.TOP); + } + + public WidgetGeometry(double x, double y, double sx, double sy, CType fracPos, CType fracSize, WAlign alignX, WAlign alignY){ + this.x = x; + this.y = y; + this.sx = sx; + this.sy = sy; + this.posType = fracPos; + this.sizeType = fracSize; + + switch(fracPos){ + case REL_X: + this.fracPosX = true; + break; + case RELXY: + this.fracPosX = true; + this.fracPosY = true; + break; + case REL_Y: + this.fracPosY = true; + break; + default: + break; + + } + + switch(fracSize){ + case REL_X: + this.fracSizeX = true; + break; + case RELXY: + this.fracSizeX = true; + this.fracSizeY = true; + break; + case REL_Y: + this.fracSizeY = true; + break; + default: + break; + + } + + this.alignX = alignX; + this.alignY = alignY; + } + + public void setPos(double x, double y, boolean fracX, boolean fracY){ + this.x = x; + this.y = y; + this.fracPosX = fracX; + this.fracPosY = fracY; + } + + public void setSize(double sx, double sy, boolean fracSizeX, boolean fracSizeY){ + this.sx = sx; + this.sy = sy; + this.fracSizeX = fracSizeX; + this.fracSizeY = fracSizeY; + } + + public PointDouble getRawPos(){ + return new PointDouble(this.x, this.y); + } + + public Point getUnalignedPos(IWidget parent){ + int x = -1; + if (this.fracPosX) + x = MathHelper.ceiling_double_int(parent.getPos().getX() + parent.getSize().getX() * this.x / 100D); + if (!this.fracPosX && parent != null) + x = parent.getPos().getX() + (int)this.x; + if (!this.fracPosX && parent == null) + x = (int)this.x; + + int y = -1; + if (this.fracPosY) + y = MathHelper.ceiling_double_int(parent.getPos().getY() + parent.getSize().getY() * this.y / 100D); + if (!this.fracPosY && parent != null) + y = parent.getPos().getY() + (int)this.y; + if (!this.fracPosY && parent == null) + y = (int)this.y; + + return new Point(x,y); + } + + public Point getPos(IWidget parent){ + + int x = -1; + if (this.fracPosX) + x = MathHelper.ceiling_double_int(parent.getPos().getX() + parent.getSize().getX() * this.x / 100D); + if (!this.fracPosX && parent != null) + x = parent.getPos().getX() + (int)this.x; + if (!this.fracPosX && parent == null) + x = (int)this.x; + + int y = -1; + if (this.fracPosY) + y = MathHelper.ceiling_double_int(parent.getPos().getY() + parent.getSize().getY() * this.y / 100D); + if (!this.fracPosY && parent != null) + y = parent.getPos().getY() + (int)this.y; + if (!this.fracPosY && parent == null) + y = (int)this.y; + + + if (this.alignX == WAlign.CENTER) + x -= this.getSize(parent).getX() / 2; + + if (this.alignY == WAlign.CENTER) + y -= this.getSize(parent).getY() / 2; + + if (this.alignX == WAlign.RIGHT) + x -= this.getSize(parent).getX() - 1; + + if (this.alignY == WAlign.BOTTOM) + y -= this.getSize(parent).getY() - 1; + + return new Point(x,y); + } + + public Point getSize(IWidget parent){ + int sx = -1; + if (this.fracSizeX) + sx = MathHelper.ceiling_double_int(parent.getSize().getX() * this.sx / 100D); + if (!this.fracSizeX) + sx = (int)this.sx; + + int sy = -1; + if (this.fracSizeY) + sy = MathHelper.ceiling_double_int(parent.getSize().getY() * this.sy / 100D); + if (!this.fracSizeY) + sy = (int)this.sy; + + return new Point(sx,sy); + } + + public String toString(){ + return String.format("Geometry : [%s %s] [%s %s] [%s %s] [%s %s]", this.x, this.y, this.sx, this.sy, this.posType, this.sizeType, this.alignX, this.alignY); + } + + //@Override + //public WidgetGeometry clone() throws CloneNotSupportedException{ + // return (WidgetGeometry)super.clone(); + //} +} diff --git a/mcp/mobius/opis/gui/widgets/buttons/ButtonBase.java b/mcp/mobius/opis/gui/widgets/buttons/ButtonBase.java new file mode 100644 index 0000000..9dd7e02 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/buttons/ButtonBase.java @@ -0,0 +1,73 @@ +package mcp.mobius.opis.gui.widgets.buttons; + +import mcp.mobius.opis.gui.events.MouseEvent; +import mcp.mobius.opis.gui.helpers.UIHelper; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.Signal; +import mcp.mobius.opis.gui.widgets.LabelFixedFont; +import mcp.mobius.opis.gui.widgets.WidgetBase; +import net.minecraft.util.ResourceLocation; + +import org.lwjgl.opengl.GL11; +import org.lwjgl.util.Point; + +public abstract class ButtonBase extends WidgetBase { + + protected boolean mouseOver = false; + protected static ResourceLocation widgetsTexture = new ResourceLocation("textures/gui/widgets.png"); + + public ButtonBase(IWidget parent){ + super(parent); + } + + @Override + public void draw() { + GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); + + for (IWidget widget : this.widgets.values()) + if (widget instanceof LabelFixedFont) + if (this.mouseOver) + ((LabelFixedFont)widget).setColor(0xffffa0); + else + ((LabelFixedFont)widget).setColor(0xffffff); + + super.draw(); + } + + @Override + public void draw(Point pos) { + this.saveGLState(); + int texOffset = 0; + + if (this.mouseOver) + texOffset = 1; + + this.mc.getTextureManager().bindTexture(widgetsTexture); + UIHelper.drawTexture(this.getPos().getX(), this.getPos().getY(), this.getSize().getX(), this.getSize().getY(), 0, 66 + texOffset*20, 200, 20); + + this.loadGLState(); + } + + @Override + public void onMouseEnter(MouseEvent event){ + mouseOver = true; + } + + @Override + public void onMouseLeave(MouseEvent event){ + mouseOver = false; + } + + @Override + public IWidget getWidgetAtCoordinates(double posX, double posY){ + return this; + } + + @Override + public void onMouseClick(MouseEvent event){ + if (event.button == 0) + this.mc.sndManager.playSoundFX("random.click", 1.0F, 1.0F); + + this.emit(Signal.CLICKED, event.button); + } +} diff --git a/mcp/mobius/opis/gui/widgets/buttons/ButtonBoolean.java b/mcp/mobius/opis/gui/widgets/buttons/ButtonBoolean.java new file mode 100644 index 0000000..0ce8754 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/buttons/ButtonBoolean.java @@ -0,0 +1,43 @@ +package mcp.mobius.opis.gui.widgets.buttons; + +import mcp.mobius.opis.gui.events.MouseEvent; +import mcp.mobius.opis.gui.helpers.UIHelper; +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.WAlign; +import mcp.mobius.opis.gui.widgets.LabelFixedFont; +import mcp.mobius.opis.gui.widgets.WidgetGeometry; +import net.minecraft.util.ResourceLocation; + +import org.lwjgl.util.Point; + +public class ButtonBoolean extends ButtonBase { + + protected boolean state = false; + + public ButtonBoolean(IWidget parent, String textFalse, String textTrue){ + super(parent); + + this.addWidget("LabelFalse", new LabelFixedFont(this, textFalse)); + this.getWidget("LabelFalse").setGeometry(new WidgetGeometry(50.0D, 50.0D, 100.0D, 20.0D, CType.RELXY, CType.ABSXY, WAlign.CENTER, WAlign.CENTER)); + this.addWidget("LabelTrue", new LabelFixedFont(this, textTrue)); + this.getWidget("LabelTrue").hide(); + this.getWidget("LabelTrue").setGeometry(new WidgetGeometry(50.0D, 50.0D, 100.0D, 20.0D, CType.RELXY, CType.ABSXY, WAlign.CENTER, WAlign.CENTER)); + } + + @Override + public void onMouseClick(MouseEvent event){ + super.onMouseClick(event); + + if (event.button == 0) + this.state = !this.state; + + if (this.state){ + this.getWidget("LabelTrue").show(); + this.getWidget("LabelFalse").hide(); + } else { + this.getWidget("LabelTrue").hide(); + this.getWidget("LabelFalse").show(); + } + } +} diff --git a/mcp/mobius/opis/gui/widgets/buttons/ButtonContainer.java b/mcp/mobius/opis/gui/widgets/buttons/ButtonContainer.java new file mode 100644 index 0000000..5cefdd5 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/buttons/ButtonContainer.java @@ -0,0 +1,50 @@ +package mcp.mobius.opis.gui.widgets.buttons; + +import org.lwjgl.util.Point; + +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.WAlign; +import mcp.mobius.opis.gui.widgets.LabelFixedFont; +import mcp.mobius.opis.gui.widgets.LayoutBase; +import mcp.mobius.opis.gui.widgets.WidgetBase; +import mcp.mobius.opis.gui.widgets.WidgetGeometry; + +public class ButtonContainer extends WidgetBase { + + private int nButtons = 0; + private int columns; + private int buttonSize; + private double spacing; + + public ButtonContainer(IWidget parent, int columns, int buttonSize, double spacing){ + super (parent); + this.columns = columns; + this.spacing = spacing; + this.buttonSize = buttonSize; + } + + public void addButton(ButtonBase button){ + String buttonName = String.format("Button_%d", nButtons); + String layoutName = String.format("Layout_%d", nButtons); + String layoutLabelName = String.format("LayoutLabel_%d", nButtons); + String labelName = String.format("Label_%d", nButtons); + + this.addWidget(layoutName, new LayoutBase(this)); + this.addWidget(layoutLabelName, new LayoutBase(this)); + + int column = this.nButtons % this.columns; + int row = this.nButtons / this.columns; + double sizeColumn = 100.0 / this.columns; + + this.getWidget(layoutName).setGeometry(new WidgetGeometry(sizeColumn * column, spacing * row, sizeColumn, spacing, CType.REL_X, CType.REL_X, WAlign.LEFT, WAlign.TOP)); + this.getWidget(layoutName).addWidget(buttonName, button); + this.getWidget(layoutName).getWidget(buttonName).setGeometry(new WidgetGeometry(50.0, 50.0, buttonSize, 20.0, CType.RELXY, CType.ABSXY, WAlign.CENTER, WAlign.CENTER)); + + this.nButtons += 1; + } + + @Override + public void draw(Point pos) {} + +} diff --git a/mcp/mobius/opis/gui/widgets/buttons/ButtonContainerLabel.java b/mcp/mobius/opis/gui/widgets/buttons/ButtonContainerLabel.java new file mode 100644 index 0000000..1abe0fd --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/buttons/ButtonContainerLabel.java @@ -0,0 +1,55 @@ +package mcp.mobius.opis.gui.widgets.buttons; + +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.WAlign; +import mcp.mobius.opis.gui.widgets.LabelFixedFont; +import mcp.mobius.opis.gui.widgets.LayoutBase; +import mcp.mobius.opis.gui.widgets.WidgetBase; +import mcp.mobius.opis.gui.widgets.WidgetGeometry; + +import org.lwjgl.util.Point; + +//TODO : THIS CLASS IS WIP AND CAN'T WORK RIGHT NOW + +public class ButtonContainerLabel extends WidgetBase { + + private int nButtons = 0; + private int columns; + private int buttonSize; + private double spacing; + + public ButtonContainerLabel(IWidget parent, int columns, int buttonSize, double spacing){ + super (parent); + this.columns = columns; + this.spacing = spacing; + this.buttonSize = buttonSize; + } + + public void addButton(ButtonBase button, String label){ + String buttonName = String.format("Button_%d", nButtons); + String layoutName = String.format("Layout_%d", nButtons); + String layoutLabelName = String.format("LayoutLabel_%d", nButtons); + String labelName = String.format("Label_%d", nButtons); + + this.addWidget(layoutName, new LayoutBase(this)); + this.addWidget(layoutLabelName, new LayoutBase(this)); + + int column = (this.nButtons % this.columns) * 2; + int row = this.nButtons / this.columns; + double sizeColumn = 100.0 / (this.columns * 2); + + this.getWidget(layoutName).setGeometry(new WidgetGeometry(sizeColumn * (column + 1), spacing * row, sizeColumn, spacing, CType.REL_X, CType.REL_X, WAlign.LEFT, WAlign.TOP)); + this.getWidget(layoutName).addWidget(buttonName, button); + this.getWidget(layoutName).getWidget(buttonName).setGeometry(new WidgetGeometry(50.0, 50.0, buttonSize, 20.0, CType.RELXY, CType.ABSXY, WAlign.CENTER, WAlign.CENTER)); + + this.getWidget(layoutLabelName).setGeometry(new WidgetGeometry(sizeColumn * column, spacing * row, sizeColumn, spacing, CType.REL_X, CType.REL_X, WAlign.LEFT, WAlign.TOP)); + this.getWidget(layoutLabelName).addWidget(labelName, new LabelFixedFont(this, label)); + this.getWidget(layoutLabelName).getWidget(labelName).setGeometry(new WidgetGeometry(50.0, 50.0, buttonSize, 20.0, CType.RELXY, CType.ABSXY, WAlign.CENTER, WAlign.CENTER)); + + this.nButtons += 1; + } + + @Override + public void draw(Point pos) {} +} diff --git a/mcp/mobius/opis/gui/widgets/buttons/ButtonInteger.java b/mcp/mobius/opis/gui/widgets/buttons/ButtonInteger.java new file mode 100644 index 0000000..7797bd6 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/buttons/ButtonInteger.java @@ -0,0 +1,49 @@ +package mcp.mobius.opis.gui.widgets.buttons; + +import mcp.mobius.opis.gui.events.MouseEvent; +import mcp.mobius.opis.gui.helpers.UIHelper; +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.WAlign; +import mcp.mobius.opis.gui.widgets.LabelFixedFont; +import mcp.mobius.opis.gui.widgets.WidgetGeometry; +import net.minecraft.util.ResourceLocation; + +import org.lwjgl.util.Point; + +public class ButtonInteger extends ButtonBase { + + protected int state = 0; + protected int nStates; + + public ButtonInteger(IWidget parent, String... texts){ + super(parent); + + this.nStates = texts.length; + + for (int i = 0; i < texts.length; i++){ + String labelName = String.format("Label_%d", i); + this.addWidget(labelName, new LabelFixedFont(this, texts[i])); + this.getWidget(labelName).hide(); + this.getWidget(labelName).setGeometry(new WidgetGeometry(50.0D, 50.0D, 100.0D, 20.0D, CType.RELXY, CType.ABSXY, WAlign.CENTER, WAlign.CENTER)); + } + this.getWidget("Label_0").show(); + } + + @Override + public void onMouseClick(MouseEvent event){ + super.onMouseClick(event); + + if (event.button == 0) + this.state += 1; + + if (this.state >= this.nStates) + this.state = 0; + + for (int i = 0; i < this.nStates; i++) + this.getWidget(String.format("Label_%d", i)).hide(); + + this.getWidget(String.format("Label_%d", state)).show(); + } + +} diff --git a/mcp/mobius/opis/gui/widgets/buttons/ButtonLabel.java b/mcp/mobius/opis/gui/widgets/buttons/ButtonLabel.java new file mode 100644 index 0000000..917f512 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/buttons/ButtonLabel.java @@ -0,0 +1,24 @@ +package mcp.mobius.opis.gui.widgets.buttons; + +import mcp.mobius.opis.gui.events.MouseEvent; +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.WAlign; +import mcp.mobius.opis.gui.widgets.LabelFixedFont; +import mcp.mobius.opis.gui.widgets.WidgetGeometry; +import net.minecraft.client.gui.GuiScreen; + +public class ButtonLabel extends ButtonBase { + + public ButtonLabel(IWidget parent, String text){ + super(parent); + + this.addWidget("Label", new LabelFixedFont(this, text)); + this.getWidget("Label").setGeometry(new WidgetGeometry(50.0D, 50.0D, 100.0D, 20.0D, CType.RELXY, CType.ABSXY, WAlign.CENTER, WAlign.CENTER)); + } + + @Override + public void onMouseClick(MouseEvent event){ + super.onMouseClick(event); + } +} diff --git a/mcp/mobius/opis/gui/widgets/buttons/ButtonScreenChange.java b/mcp/mobius/opis/gui/widgets/buttons/ButtonScreenChange.java new file mode 100644 index 0000000..7a6b4b0 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/buttons/ButtonScreenChange.java @@ -0,0 +1,31 @@ +package mcp.mobius.opis.gui.widgets.buttons; + +import net.minecraft.client.gui.GuiScreen; +import mcp.mobius.opis.gui.events.MouseEvent; +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.WAlign; +import mcp.mobius.opis.gui.screens.ScreenBase; +import mcp.mobius.opis.gui.widgets.LabelFixedFont; +import mcp.mobius.opis.gui.widgets.WidgetGeometry; + +public class ButtonScreenChange extends ButtonBase { + + GuiScreen linkedScreen; + + public ButtonScreenChange(IWidget parent, String text, GuiScreen linkedscreen){ + super(parent); + this.linkedScreen = linkedscreen; + + this.addWidget("Label", new LabelFixedFont(this, text)); + this.getWidget("Label").setGeometry(new WidgetGeometry(50.0D, 50.0D, 100.0D, 20.0D, CType.RELXY, CType.ABSXY, WAlign.CENTER, WAlign.CENTER)); + } + + @Override + public void onMouseClick(MouseEvent event){ + super.onMouseClick(event); + + if (event.button == 0) + this.mc.displayGuiScreen(this.linkedScreen); + } +} diff --git a/mcp/mobius/opis/gui/widgets/movable/PictureMovableRC.java b/mcp/mobius/opis/gui/widgets/movable/PictureMovableRC.java new file mode 100644 index 0000000..93447e2 --- /dev/null +++ b/mcp/mobius/opis/gui/widgets/movable/PictureMovableRC.java @@ -0,0 +1,68 @@ +package mcp.mobius.opis.gui.widgets.movable; + +import mcp.mobius.opis.gui.events.MouseEvent; +import mcp.mobius.opis.gui.helpers.UIHelper; +import mcp.mobius.opis.gui.interfaces.CType; +import mcp.mobius.opis.gui.interfaces.IWidget; +import mcp.mobius.opis.gui.interfaces.Signal; +import mcp.mobius.opis.gui.interfaces.WAlign; +import mcp.mobius.opis.gui.widgets.WidgetBase; +import mcp.mobius.opis.gui.widgets.WidgetGeometry; +import mcp.mobius.opis.gui.widgets.WidgetGeometry.PointDouble; +import net.minecraft.util.ResourceLocation; + +import org.lwjgl.opengl.GL11; +import org.lwjgl.util.Point; + + +// A movable picture setup especially for centered relative positions. + +public class PictureMovableRC extends WidgetBase { + + private double offsetX, offsetY; + protected ResourceLocation texture; + + public PictureMovableRC(IWidget parent, String uri) { + super(parent); + this.texture = new ResourceLocation(uri); + } + + @Override + public void onMouseClick(MouseEvent event){ + this.offsetX = event.x - this.geom.getUnalignedPos(this.parent).getX(); + this.offsetY = event.y - this.geom.getUnalignedPos(this.parent).getY(); + //System.out.printf("%s %s\n", this.offsetX, this.offsetY); + } + + @Override + public void onMouseDrag(MouseEvent event){ + double newX = event.x - this.offsetX ; + double newY = event.y - this.offsetY ; + + newX = Math.max(newX, this.parent.getLeft()); + newY = Math.max(newY, this.parent.getTop()); + + newX = Math.min(newX, this.parent.getRight()); + newY = Math.min(newY, this.parent.getBottom()); + + this.setPos(((newX - this.parent.getLeft()) / this.parent.getSize().getX()) * 100.0, ((newY - this.parent.getTop())/ this.parent.getSize().getY()) * 100.0); + + //System.out.printf("%s\n", this.parent.getGeometry()); + //System.out.printf("%s %s\n", this.parent.getPos(), this.parent.getSize()); + + this.emit(Signal.DRAGGED, this.getPos()); + } + + @Override + public void draw(Point pos) { + this.saveGLState(); + + GL11.glPushMatrix(); + this.texManager.bindTexture(texture); + UIHelper.drawTexture(pos.getX(), pos.getY(), this.getSize().getX(), this.getSize().getY()); + GL11.glPopMatrix(); + + this.loadGLState(); + } +} +