diff options
Diffstat (limited to 'src/Java/binnie/craftgui/core')
19 files changed, 1795 insertions, 0 deletions
diff --git a/src/Java/binnie/craftgui/core/Attribute.java b/src/Java/binnie/craftgui/core/Attribute.java new file mode 100644 index 0000000000..5d73f58ae2 --- /dev/null +++ b/src/Java/binnie/craftgui/core/Attribute.java @@ -0,0 +1,9 @@ +package binnie.craftgui.core; + +public enum Attribute + implements IWidgetAttribute +{ + MouseOver, CanFocus, NeedsDeletion, AlwaysOnTop, BlockTooltip; + + private Attribute() {} +} diff --git a/src/Java/binnie/craftgui/core/CraftGUI.java b/src/Java/binnie/craftgui/core/CraftGUI.java new file mode 100644 index 0000000000..3cce3493cf --- /dev/null +++ b/src/Java/binnie/craftgui/core/CraftGUI.java @@ -0,0 +1,10 @@ +package binnie.craftgui.core; + +import binnie.craftgui.core.renderer.Renderer; +import binnie.craftgui.resource.minecraft.CraftGUIResourceManager; + +public class CraftGUI +{ + public static CraftGUIResourceManager ResourceManager; + public static Renderer Render; +} diff --git a/src/Java/binnie/craftgui/core/ITooltip.java b/src/Java/binnie/craftgui/core/ITooltip.java new file mode 100644 index 0000000000..634ce476d5 --- /dev/null +++ b/src/Java/binnie/craftgui/core/ITooltip.java @@ -0,0 +1,6 @@ +package binnie.craftgui.core; + +public abstract interface ITooltip +{ + public abstract void getTooltip(Tooltip paramTooltip); +} diff --git a/src/Java/binnie/craftgui/core/ITooltipHelp.java b/src/Java/binnie/craftgui/core/ITooltipHelp.java new file mode 100644 index 0000000000..f01744ab6f --- /dev/null +++ b/src/Java/binnie/craftgui/core/ITooltipHelp.java @@ -0,0 +1,6 @@ +package binnie.craftgui.core; + +public abstract interface ITooltipHelp +{ + public abstract void getHelpTooltip(Tooltip paramTooltip); +} diff --git a/src/Java/binnie/craftgui/core/ITopLevelWidget.java b/src/Java/binnie/craftgui/core/ITopLevelWidget.java new file mode 100644 index 0000000000..2e209b3ea6 --- /dev/null +++ b/src/Java/binnie/craftgui/core/ITopLevelWidget.java @@ -0,0 +1,29 @@ +package binnie.craftgui.core; + +import binnie.craftgui.core.geometry.IPoint; + +public abstract interface ITopLevelWidget + extends IWidget +{ + public abstract void setMousePosition(int paramInt1, int paramInt2); + + public abstract IPoint getAbsoluteMousePosition(); + + public abstract IWidget getFocusedWidget(); + + public abstract IWidget getMousedOverWidget(); + + public abstract IWidget getDraggedWidget(); + + public abstract boolean isFocused(IWidget paramIWidget); + + public abstract boolean isMouseOver(IWidget paramIWidget); + + public abstract boolean isDragged(IWidget paramIWidget); + + public abstract void updateTopLevel(); + + public abstract void widgetDeleted(IWidget paramIWidget); + + public abstract IPoint getDragDistance(); +} diff --git a/src/Java/binnie/craftgui/core/IWidget.java b/src/Java/binnie/craftgui/core/IWidget.java new file mode 100644 index 0000000000..cd85fa7a40 --- /dev/null +++ b/src/Java/binnie/craftgui/core/IWidget.java @@ -0,0 +1,138 @@ +package binnie.craftgui.core; + +import binnie.craftgui.core.geometry.IArea; +import binnie.craftgui.core.geometry.IPoint; +import binnie.craftgui.events.Event; +import binnie.craftgui.events.EventHandler; +import java.util.List; + +public abstract interface IWidget +{ + public abstract IWidget getParent(); + + public abstract void deleteChild(IWidget paramIWidget); + + public abstract void deleteAllChildren(); + + public abstract ITopLevelWidget getSuperParent(); + + public abstract boolean isTopLevel(); + + public abstract IPoint getPosition(); + + public abstract IPoint pos(); + + public abstract void setPosition(IPoint paramIPoint); + + public abstract IPoint getSize(); + + public abstract IPoint size(); + + public abstract void setSize(IPoint paramIPoint); + + public abstract IPoint getOriginalPosition(); + + public abstract IPoint getAbsolutePosition(); + + public abstract IPoint getOriginalAbsolutePosition(); + + public abstract IPoint getOffset(); + + public abstract IArea getArea(); + + public abstract IArea area(); + + public abstract void setOffset(IPoint paramIPoint); + + public abstract IPoint getMousePosition(); + + public abstract IPoint getRelativeMousePosition(); + + public abstract void setColour(int paramInt); + + public abstract int getColour(); + + public abstract void render(); + + public abstract void updateClient(); + + public abstract void enable(); + + public abstract void disable(); + + public abstract void show(); + + public abstract void hide(); + + public abstract boolean calculateIsMouseOver(); + + public abstract boolean isEnabled(); + + public abstract boolean isVisible(); + + public abstract boolean isFocused(); + + public abstract boolean isMouseOver(); + + public abstract boolean isDragged(); + + public abstract boolean isChildVisible(IWidget paramIWidget); + + public abstract boolean isChildEnabled(IWidget paramIWidget); + + public abstract boolean canMouseOver(); + + public abstract boolean canFocus(); + + public abstract IWidget addWidget(IWidget paramIWidget); + + public abstract List<IWidget> getWidgets(); + + public abstract void callEvent(Event paramEvent); + + public abstract void recieveEvent(Event paramEvent); + + public abstract void onUpdateClient(); + + public abstract void delete(); + + public abstract void onDelete(); + + public abstract <T> T getWidget(Class<T> paramClass); + + public abstract IArea getCroppedZone(); + + public abstract void setCroppedZone(IWidget paramIWidget, IArea paramIArea); + + public abstract boolean isCroppedWidet(); + + public abstract IWidget getCropWidget(); + + public abstract boolean isMouseOverWidget(IPoint paramIPoint); + + public abstract int getLevel(); + + public abstract boolean isDescendant(IWidget paramIWidget); + + public abstract List<IWidgetAttribute> getAttributes(); + + public abstract boolean hasAttribute(IWidgetAttribute paramIWidgetAttribute); + + public abstract boolean addAttribute(IWidgetAttribute paramIWidgetAttribute); + + public abstract <E extends Event> void addEventHandler(EventHandler<E> paramEventHandler); + + public abstract <E extends Event> void addSelfEventHandler(EventHandler<E> paramEventHandler); + + public abstract boolean contains(IPoint paramIPoint); + + public abstract float x(); + + public abstract float y(); + + public abstract float w(); + + public abstract float h(); + + public abstract void onRender(RenderStage paramRenderStage); +} diff --git a/src/Java/binnie/craftgui/core/IWidgetAttribute.java b/src/Java/binnie/craftgui/core/IWidgetAttribute.java new file mode 100644 index 0000000000..87d994ca43 --- /dev/null +++ b/src/Java/binnie/craftgui/core/IWidgetAttribute.java @@ -0,0 +1,3 @@ +package binnie.craftgui.core; + +public abstract interface IWidgetAttribute {} diff --git a/src/Java/binnie/craftgui/core/RenderStage.java b/src/Java/binnie/craftgui/core/RenderStage.java new file mode 100644 index 0000000000..e710c833e6 --- /dev/null +++ b/src/Java/binnie/craftgui/core/RenderStage.java @@ -0,0 +1,8 @@ +package binnie.craftgui.core; + +public enum RenderStage +{ + PreChildren, PostChildren, PostSiblings; + + private RenderStage() {} +} diff --git a/src/Java/binnie/craftgui/core/Tooltip.java b/src/Java/binnie/craftgui/core/Tooltip.java new file mode 100644 index 0000000000..4afb7f6bc5 --- /dev/null +++ b/src/Java/binnie/craftgui/core/Tooltip.java @@ -0,0 +1,84 @@ +package binnie.craftgui.core; + +import java.util.ArrayList; +import java.util.List; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.fluids.FluidStack; + +public class Tooltip +{ + public void add(String string) + { + this.tooltip.add(string); + } + + public String getLine(int index) + { + String string = (String)getList().get(index); + return string; + } + + public void add(List list) + { + for (Object obj : list) { + this.tooltip.add((String)obj); + } + } + + List<String> tooltip = new ArrayList(); + + public List<String> getList() + { + return this.tooltip; + } + + public boolean exists() + { + return this.tooltip.size() > 0; + } + + public static enum Type + implements Tooltip.ITooltipType + { + Standard, Help, Information, User, Power; + + private Type() {} + } + + public void setType(ITooltipType type) + { + this.type = type; + } + + ITooltipType type = Type.Standard; + public int maxWidth = 256; + + public void setMaxWidth(int w) + { + this.maxWidth = w; + } + + public ITooltipType getType() + { + return this.type; + } + + public void add(ItemStack item, String string) + { + NBTTagCompound nbt = new NBTTagCompound(); + item.writeToNBT(nbt); + nbt.setByte("nbt-type", (byte)105); + add("~~~" + nbt.toString() + "~~~" + string); + } + + public void add(FluidStack item, String string) + { + NBTTagCompound nbt = new NBTTagCompound(); + item.writeToNBT(nbt); + nbt.setByte("nbt-type", (byte)102); + add("~~~" + nbt.toString() + "~~~" + string); + } + + public static abstract interface ITooltipType {} +} diff --git a/src/Java/binnie/craftgui/core/TopLevelWidget.java b/src/Java/binnie/craftgui/core/TopLevelWidget.java new file mode 100644 index 0000000000..6f2cdd18a6 --- /dev/null +++ b/src/Java/binnie/craftgui/core/TopLevelWidget.java @@ -0,0 +1,248 @@ +package binnie.craftgui.core; + +import binnie.craftgui.core.geometry.IArea; +import binnie.craftgui.core.geometry.IPoint; +import binnie.craftgui.events.EventMouse.Down; +import binnie.craftgui.events.EventMouse.Down.Handler; +import binnie.craftgui.events.EventMouse.Drag; +import binnie.craftgui.events.EventMouse.Move; +import binnie.craftgui.events.EventMouse.Up; +import binnie.craftgui.events.EventMouse.Up.Handler; +import binnie.craftgui.events.EventWidget.EndDrag; +import binnie.craftgui.events.EventWidget.EndMouseOver; +import binnie.craftgui.events.EventWidget.GainFocus; +import binnie.craftgui.events.EventWidget.LoseFocus; +import binnie.craftgui.events.EventWidget.StartDrag; +import binnie.craftgui.events.EventWidget.StartDrag.Handler; +import binnie.craftgui.events.EventWidget.StartMouseOver; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Deque; +import java.util.List; +import java.util.ListIterator; +import org.lwjgl.input.Mouse; + +public abstract class TopLevelWidget + extends Widget + implements ITopLevelWidget +{ + public TopLevelWidget() + { + super(null); + + addEventHandler(new EventMouse.Down.Handler() + { + public void onEvent(EventMouse.Down event) + { + TopLevelWidget.this.setDraggedWidget(TopLevelWidget.this.mousedOverWidget, event.getButton()); + TopLevelWidget.this.setFocusedWidget(TopLevelWidget.this.mousedOverWidget); + } + }); + addEventHandler(new EventMouse.Up.Handler() + { + public void onEvent(EventMouse.Up event) + { + TopLevelWidget.this.setDraggedWidget(null); + } + }); + addEventHandler(new EventWidget.StartDrag.Handler() + { + public void onEvent(EventWidget.StartDrag event) + { + TopLevelWidget.this.dragStart = TopLevelWidget.this.getRelativeMousePosition(); + } + }); + } + + IWidget mousedOverWidget = null; + IWidget draggedWidget = null; + IWidget focusedWidget = null; + + public void setMousedOverWidget(IWidget widget) + { + if (this.mousedOverWidget == widget) { + return; + } + if (this.mousedOverWidget != null) { + callEvent(new EventWidget.EndMouseOver(this.mousedOverWidget)); + } + this.mousedOverWidget = widget; + if (this.mousedOverWidget != null) { + callEvent(new EventWidget.StartMouseOver(this.mousedOverWidget)); + } + } + + public void setDraggedWidget(IWidget widget) + { + setDraggedWidget(widget, -1); + } + + public void setDraggedWidget(IWidget widget, int button) + { + if (this.draggedWidget == widget) { + return; + } + if (this.draggedWidget != null) { + callEvent(new EventWidget.EndDrag(this.draggedWidget)); + } + this.draggedWidget = widget; + if (this.draggedWidget != null) { + callEvent(new EventWidget.StartDrag(this.draggedWidget, button)); + } + } + + public void setFocusedWidget(IWidget widget) + { + IWidget newWidget = widget; + if (this.focusedWidget == newWidget) { + return; + } + if ((newWidget != null) && (!newWidget.canFocus())) { + newWidget = null; + } + if (this.focusedWidget != null) { + callEvent(new EventWidget.LoseFocus(this.focusedWidget)); + } + this.focusedWidget = newWidget; + if (this.focusedWidget != null) { + callEvent(new EventWidget.GainFocus(this.focusedWidget)); + } + } + + public IWidget getMousedOverWidget() + { + return this.mousedOverWidget; + } + + public IWidget getDraggedWidget() + { + return this.draggedWidget; + } + + public IWidget getFocusedWidget() + { + return this.focusedWidget; + } + + public boolean isMouseOver(IWidget widget) + { + return getMousedOverWidget() == widget; + } + + public boolean isDragged(IWidget widget) + { + return getDraggedWidget() == widget; + } + + public boolean isFocused(IWidget widget) + { + return getFocusedWidget() == widget; + } + + public void updateTopLevel() + { + setMousedOverWidget(calculateMousedOverWidget()); + if ((getFocusedWidget() != null) && ((!getFocusedWidget().isVisible()) || (!getFocusedWidget().isEnabled()))) { + setFocusedWidget(null); + } + if (!Mouse.isButtonDown(0)) { + if (this.draggedWidget != null) { + setDraggedWidget(null); + } + } + } + + private IWidget calculateMousedOverWidget() + { + Deque<IWidget> queue = calculateMousedOverWidgets(); + while (!queue.isEmpty()) + { + IWidget widget = (IWidget)queue.removeFirst(); + if ((widget.isEnabled()) && (widget.isVisible()) && (widget.canMouseOver())) { + if ((widget.isEnabled()) && (widget.isVisible()) && (widget.canMouseOver()) && (widget.calculateIsMouseOver())) { + return widget; + } + } + } + return null; + } + + public Deque<IWidget> calculateMousedOverWidgets() + { + Deque<IWidget> list = new ArrayDeque(); + for (IWidget widget : getQueuedWidgets(this)) { + if (widget.calculateIsMouseOver()) { + list.addLast(widget); + } + } + return list; + } + + private Collection<IWidget> getQueuedWidgets(IWidget widget) + { + List<IWidget> widgets = new ArrayList(); + + boolean addChildren = true; + if (widget.isCroppedWidet()) { + addChildren = widget.getCroppedZone().contains(widget.getCropWidget().getRelativeMousePosition()); + } + if (addChildren) + { + ListIterator<IWidget> li = widget.getWidgets().listIterator(widget.getWidgets().size()); + while (li.hasPrevious()) + { + IWidget child = (IWidget)li.previous(); + widgets.addAll(getQueuedWidgets(child)); + } + } + widgets.add(widget); + + return widgets; + } + + protected IPoint mousePosition = new IPoint(0.0F, 0.0F); + + public void setMousePosition(int x, int y) + { + float dx = x - this.mousePosition.x(); + float dy = y - this.mousePosition.y(); + if ((dx != 0.0F) || (dy != 0.0F)) { + if (getDraggedWidget() != null) { + callEvent(new EventMouse.Drag(getDraggedWidget(), dx, dy)); + } else { + callEvent(new EventMouse.Move(this, dx, dy)); + } + } + if ((this.mousePosition.x() != x) || (this.mousePosition.y() != y)) + { + this.mousePosition = new IPoint(x, y); + setMousedOverWidget(calculateMousedOverWidget()); + } + } + + public IPoint getAbsoluteMousePosition() + { + return this.mousePosition; + } + + public void widgetDeleted(IWidget widget) + { + if (isMouseOver(widget)) { + setMousedOverWidget(null); + } + if (isDragged(widget)) { + setDraggedWidget(null); + } + if (isFocused(widget)) { + setFocusedWidget(null); + } + } + + IPoint dragStart = IPoint.ZERO; + + public IPoint getDragDistance() + { + return getRelativeMousePosition().sub(this.dragStart); + } +} diff --git a/src/Java/binnie/craftgui/core/Widget.java b/src/Java/binnie/craftgui/core/Widget.java new file mode 100644 index 0000000000..448bcbaed6 --- /dev/null +++ b/src/Java/binnie/craftgui/core/Widget.java @@ -0,0 +1,499 @@ +package binnie.craftgui.core; + +import binnie.craftgui.core.geometry.IArea; +import binnie.craftgui.core.geometry.IPoint; +import binnie.craftgui.core.renderer.Renderer; +import binnie.craftgui.events.Event; +import binnie.craftgui.events.EventHandler; +import binnie.craftgui.events.EventHandler.Origin; +import binnie.craftgui.events.EventWidget.ChangeColour; +import binnie.craftgui.events.EventWidget.ChangeOffset; +import binnie.craftgui.events.EventWidget.ChangePosition; +import binnie.craftgui.events.EventWidget.ChangeSize; +import binnie.craftgui.events.EventWidget.Disable; +import binnie.craftgui.events.EventWidget.Enable; +import binnie.craftgui.events.EventWidget.Hide; +import binnie.craftgui.events.EventWidget.Show; +import java.util.ArrayList; +import java.util.Collection; +import java.util.ConcurrentModificationException; +import java.util.List; + +public class Widget + implements IWidget +{ + public Widget(IWidget parent) + { + this.parent = parent; + if (parent != null) { + parent.addWidget(this); + } + } + + private IWidget parent = null; + private List<IWidget> subWidgets = new ArrayList(); + private List<IWidgetAttribute> attributes = new ArrayList(); + + public List<IWidgetAttribute> getAttributes() + { + return this.attributes; + } + + public boolean hasAttribute(IWidgetAttribute attribute) + { + return this.attributes.contains(attribute); + } + + public boolean addAttribute(IWidgetAttribute attribute) + { + return this.attributes.add(attribute); + } + + public final void deleteChild(IWidget child) + { + if (child == null) { + return; + } + child.delete(); + this.subWidgets.remove(child); + } + + public final void deleteAllChildren() + { + while (!this.subWidgets.isEmpty()) { + deleteChild((IWidget)this.subWidgets.get(0)); + } + } + + public final IWidget getParent() + { + return this.parent; + } + + public final ITopLevelWidget getSuperParent() + { + return isTopLevel() ? (ITopLevelWidget)this : this.parent.getSuperParent(); + } + + public final IWidget addWidget(IWidget widget) + { + if ((this.subWidgets.size() != 0) && (((IWidget)this.subWidgets.get(this.subWidgets.size() - 1)).hasAttribute(Attribute.AlwaysOnTop))) { + this.subWidgets.add(this.subWidgets.size() - 1, widget); + } else { + this.subWidgets.add(widget); + } + onAddChild(widget); + return widget; + } + + protected void onAddChild(IWidget widget) {} + + public final List<IWidget> getWidgets() + { + return this.subWidgets; + } + + public final boolean isTopLevel() + { + return this instanceof ITopLevelWidget; + } + + private IPoint position = new IPoint(0.0F, 0.0F); + private IPoint size = new IPoint(0.0F, 0.0F); + private IPoint offset = new IPoint(0.0F, 0.0F); + IArea cropArea; + IWidget cropWidget; + + public final IPoint pos() + { + return this.position.add(this.offset); + } + + public final IPoint size() + { + return this.size; + } + + public final IArea area() + { + return getArea(); + } + + public final IPoint getPosition() + { + return pos(); + } + + public final IArea getArea() + { + return new IArea(IPoint.ZERO, size()); + } + + public final IPoint getOriginalPosition() + { + return this.position; + } + + boolean cropped = false; + + public IArea getCroppedZone() + { + return this.cropArea; + } + + public void setCroppedZone(IWidget relative, IArea area) + { + this.cropArea = area; + this.cropped = true; + this.cropWidget = relative; + } + + public final IPoint getAbsolutePosition() + { + return isTopLevel() ? getPosition() : getParent().getAbsolutePosition().add(getPosition()); + } + + public final IPoint getOriginalAbsolutePosition() + { + return isTopLevel() ? getOriginalPosition() : getParent().getOriginalPosition().sub(getOriginalPosition()); + } + + public final IPoint getSize() + { + return size(); + } + + public final IPoint getOffset() + { + return this.offset; + } + + public final void setPosition(IPoint vector) + { + if (!vector.equals(this.position)) + { + this.position = new IPoint(vector); + callEvent(new EventWidget.ChangePosition(this)); + } + } + + public final void setSize(IPoint vector) + { + if (!vector.equals(this.size)) + { + this.size = new IPoint(vector); + callEvent(new EventWidget.ChangeSize(this)); + } + } + + public final void setOffset(IPoint vector) + { + if (vector != this.offset) + { + this.offset = new IPoint(vector); + callEvent(new EventWidget.ChangeOffset(this)); + } + } + + int colour = 16777215; + + public final void setColour(int colour) + { + if (this.colour != colour) + { + this.colour = colour; + callEvent(new EventWidget.ChangeColour(this)); + } + } + + public final int getColour() + { + return this.colour; + } + + public boolean canMouseOver() + { + return hasAttribute(Attribute.MouseOver); + } + + public boolean canFocus() + { + return hasAttribute(Attribute.CanFocus); + } + + private Collection<EventHandler> globalEventHandlers = new ArrayList(); + + public void addEventHandler(EventHandler handler) + { + this.globalEventHandlers.add(handler); + } + + public void addSelfEventHandler(EventHandler handler) + { + addEventHandler(handler.setOrigin(EventHandler.Origin.Self, this)); + } + + public final void callEvent(Event event) + { + getSuperParent().recieveEvent(event); + } + + public final void recieveEvent(Event event) + { + for (EventHandler handler : this.globalEventHandlers) { + if (handler.handles(event)) { + handler.onEvent(event); + } + } + try + { + for (IWidget child : getWidgets()) { + child.recieveEvent(event); + } + } + catch (ConcurrentModificationException e) {} + } + + public final IPoint getMousePosition() + { + return getSuperParent().getAbsoluteMousePosition(); + } + + public final IPoint getRelativeMousePosition() + { + return isTopLevel() ? getMousePosition() : getParent().getRelativeMousePosition().sub(getPosition()); + } + + public boolean isCroppedWidet() + { + return this.cropped; + } + + public final IWidget getCropWidget() + { + return this.cropWidget == null ? this : this.cropWidget; + } + + public final void render() + { + if (isVisible()) + { + CraftGUI.Render.preRender(this); + onRender(RenderStage.PreChildren); + for (IWidget widget : getWidgets()) { + widget.render(); + } + for (IWidget widget : getWidgets()) + { + CraftGUI.Render.preRender(widget); + widget.onRender(RenderStage.PostSiblings); + CraftGUI.Render.postRender(widget); + } + onRender(RenderStage.PostChildren); + CraftGUI.Render.postRender(this); + } + } + + public final void updateClient() + { + if (!isVisible()) { + return; + } + if (getSuperParent() == this) { + ((ITopLevelWidget)this).updateTopLevel(); + } + onUpdateClient(); + + List<IWidget> deletedWidgets = new ArrayList(); + for (IWidget widget : getWidgets()) { + if (widget.hasAttribute(Attribute.NeedsDeletion)) { + deletedWidgets.add(widget); + } else { + widget.updateClient(); + } + } + for (IWidget widget : deletedWidgets) { + deleteChild(widget); + } + } + + public final boolean calculateIsMouseOver() + { + IPoint mouse = getRelativeMousePosition(); + if (!this.cropped) { + return isMouseOverWidget(mouse); + } + IWidget cropRelative = this.cropWidget != null ? this.cropWidget : this; + IPoint pos = IPoint.sub(cropRelative.getAbsolutePosition(), getAbsolutePosition()); + IPoint size = new IPoint(this.cropArea.size().x(), this.cropArea.size().y()); + boolean inCrop = (mouse.x() > pos.x()) && (mouse.y() > pos.y()) && (mouse.x() < pos.x() + size.x()) && (mouse.y() < pos.y() + size.y()); + + return (inCrop) && (isMouseOverWidget(mouse)); + } + + public boolean isMouseOverWidget(IPoint relativeMouse) + { + return getArea().contains(relativeMouse); + } + + private boolean enabled = true; + private boolean visible = true; + + public final void enable() + { + this.enabled = true; + callEvent(new EventWidget.Enable(this)); + } + + public final void disable() + { + this.enabled = false; + callEvent(new EventWidget.Disable(this)); + } + + public final void show() + { + this.visible = true; + callEvent(new EventWidget.Show(this)); + } + + public final void hide() + { + this.visible = false; + callEvent(new EventWidget.Hide(this)); + } + + public boolean isEnabled() + { + return (this.enabled) && ((isTopLevel()) || ((getParent().isEnabled()) && (getParent().isChildEnabled(this)))); + } + + public final boolean isVisible() + { + return (this.visible) && ((isTopLevel()) || ((getParent().isVisible()) && (getParent().isChildVisible(this)))); + } + + public final boolean isFocused() + { + return getSuperParent().isFocused(this); + } + + public final boolean isDragged() + { + return getSuperParent().isDragged(this); + } + + public final boolean isMouseOver() + { + return getSuperParent().isMouseOver(this); + } + + public boolean isChildVisible(IWidget child) + { + return true; + } + + public boolean isChildEnabled(IWidget child) + { + return true; + } + + public void onRender(RenderStage stage) + { + if (stage == RenderStage.PreChildren) { + onRenderBackground(); + } + if (stage == RenderStage.PostChildren) { + onRenderForeground(); + } + if (stage == RenderStage.PostSiblings) { + onRenderOverlay(); + } + } + + public void onRenderBackground() {} + + public void onRenderForeground() {} + + public void onRenderOverlay() {} + + public void onUpdateClient() {} + + public final void delete() + { + getSuperParent().widgetDeleted(this); + onDelete(); + } + + public void onDelete() {} + + public <T> T getWidget(Class<T> x) + { + for (IWidget child : getWidgets()) + { + if (x.isInstance(child)) { + return child; + } + T found = child.getWidget(x); + if (found != null) { + return found; + } + } + return null; + } + + public final boolean contains(IPoint position) + { + return getArea().contains(position); + } + + public void scheduleDeletion() + { + addAttribute(Attribute.NeedsDeletion); + } + + public int getLevel() + { + int level = getParent() == null ? 0 : getParent().getLevel(); + int index = getParent() == null ? 0 : getParent().getWidgets().indexOf(this); + return level + index; + } + + public boolean isDescendant(IWidget widget) + { + IWidget clss = this; + do + { + if (clss == widget) { + return true; + } + clss = clss.getParent(); + } while (clss != null); + return false; + } + + public float x() + { + return pos().x(); + } + + public float y() + { + return pos().y(); + } + + public float w() + { + return size().x(); + } + + public float h() + { + return size().y(); + } + + public IWidget getWidget() + { + return this; + } +} diff --git a/src/Java/binnie/craftgui/core/geometry/CraftGUIUtil.java b/src/Java/binnie/craftgui/core/geometry/CraftGUIUtil.java new file mode 100644 index 0000000000..5a21e313d4 --- /dev/null +++ b/src/Java/binnie/craftgui/core/geometry/CraftGUIUtil.java @@ -0,0 +1,77 @@ +package binnie.craftgui.core.geometry; + +import binnie.craftgui.controls.core.IControlValue; +import binnie.craftgui.core.IWidget; +import binnie.craftgui.events.EventValueChanged; +import binnie.craftgui.events.EventValueChanged.Handler; + +public class CraftGUIUtil +{ + public static void alignToWidget(IWidget target, IWidget relativeTo) + { + IPoint startPos = target.getAbsolutePosition(); + IPoint endPos = relativeTo.getAbsolutePosition(); + moveWidget(target, endPos.sub(startPos)); + } + + public static void moveWidget(IWidget target, IPoint movement) + { + target.setPosition(target.getPosition().add(movement)); + } + + public static void horizontalGrid(float px, float py, IWidget... widgets) + { + horizontalGrid(px, py, TextJustification.MiddleCenter, 0.0F, widgets); + } + + public static void horizontalGrid(float px, float py, TextJustification just, float spacing, IWidget... widgets) + { + float x = 0.0F; + float h = 0.0F; + for (IWidget widget : widgets) { + h = Math.max(h, widget.getSize().y()); + } + for (IWidget widget : widgets) + { + widget.setPosition(new IPoint(px + x, py + (h - widget.getSize().y()) * just.yOffset)); + x += widget.getSize().x() + spacing; + } + } + + public static void verticalGrid(float px, float py, IWidget... widgets) + { + horizontalGrid(px, py, TextJustification.MiddleCenter, 0.0F, widgets); + } + + public static void verticalGrid(float px, float py, TextJustification just, float spacing, IWidget... widgets) + { + float y = 0.0F; + float w = 0.0F; + for (IWidget widget : widgets) { + w = Math.max(w, widget.getSize().x()); + } + for (IWidget widget : widgets) + { + widget.setPosition(new IPoint(px + (w - widget.getSize().x()) * just.xOffset, py + y)); + y += widget.getSize().y() + spacing; + } + } + + public static <T> void linkWidgets(IControlValue<T> tab, IControlValue<T> target) + { + tab.addSelfEventHandler(new EventValueChanged.Handler() + { + public void onEvent(EventValueChanged event) + { + this.val$target.setValue(event.getValue()); + } + }); + target.addSelfEventHandler(new EventValueChanged.Handler() + { + public void onEvent(EventValueChanged event) + { + this.val$tab.setValue(event.getValue()); + } + }); + } +} diff --git a/src/Java/binnie/craftgui/core/geometry/IArea.java b/src/Java/binnie/craftgui/core/geometry/IArea.java new file mode 100644 index 0000000000..c6b5511773 --- /dev/null +++ b/src/Java/binnie/craftgui/core/geometry/IArea.java @@ -0,0 +1,143 @@ +package binnie.craftgui.core.geometry; + +public class IArea +{ + private IPoint pos; + private IPoint size; + + public IArea(IArea area) + { + this(area.pos().x(), area.pos().y(), area.size().x(), area.size().y()); + } + + public IArea(IPoint pos, IPoint size) + { + this(pos.x(), pos.y(), size.x(), size.y()); + } + + public IArea(float xywh) + { + this(xywh, xywh, xywh, xywh); + } + + public IArea(float xy, float wh) + { + this(xy, xy, wh, wh); + } + + public IArea(float x, float y, float wh) + { + this(x, y, wh, wh); + } + + public IArea(float x, float y, float w, float h) + { + setPosition(new IPoint(x, y)); + setSize(new IPoint(w, h)); + } + + public IPoint pos() + { + return this.pos; + } + + public IPoint getPosition() + { + return this.pos; + } + + public void setPosition(IPoint position) + { + this.pos = position.copy(); + } + + public IPoint size() + { + return this.size; + } + + public IPoint getSize() + { + return this.size; + } + + public void setSize(IPoint size) + { + this.size = size.copy(); + } + + public boolean contains(IPoint position) + { + return (position.x() >= pos().x()) && (position.y() >= this.pos.y()) && (position.x() <= pos().x() + size().x()) && (position.y() <= pos().y() + size().y()); + } + + public float x() + { + return pos().x(); + } + + public float y() + { + return pos().y(); + } + + public float w() + { + return size().x(); + } + + public float h() + { + return size().y(); + } + + public float x(float n) + { + return this.pos.x(n); + } + + public float y(float n) + { + return this.pos.y(n); + } + + public float w(float n) + { + return this.size.x(n); + } + + public float h(float n) + { + return this.size.y(n); + } + + public IArea inset(IBorder border) + { + return new IArea(x() + border.l(), y() + border.t(), w() - border.l() - border.r(), h() - border.t() - border.b()); + } + + public IArea outset(int outset) + { + return outset(new IBorder(outset)); + } + + public IArea outset(IBorder border) + { + return new IArea(x() - border.l(), y() - border.t(), w() + border.l() + border.r(), h() + border.t() + border.b()); + } + + public IArea inset(int inset) + { + return inset(new IBorder(inset)); + } + + public String toString() + { + return w() + "x" + h() + "@" + x() + "," + y(); + } + + public IArea shift(float dx, float f) + { + return new IArea(x() + dx, y() + f, w(), h()); + } +} diff --git a/src/Java/binnie/craftgui/core/geometry/IBorder.java b/src/Java/binnie/craftgui/core/geometry/IBorder.java new file mode 100644 index 0000000000..e16b6d97f1 --- /dev/null +++ b/src/Java/binnie/craftgui/core/geometry/IBorder.java @@ -0,0 +1,126 @@ +package binnie.craftgui.core.geometry; + +public class IBorder +{ + public static final IBorder ZERO = new IBorder(0.0F); + float t; + float b; + float l; + float r; + + public IBorder(float pad) + { + this(pad, pad, pad, pad); + } + + public IBorder(float tb, float rl) + { + this(tb, rl, tb, rl); + } + + public IBorder(float t, float rl, float b) + { + this(t, rl, b, rl); + } + + public IBorder(float t, float r, float b, float l) + { + this.t = t; + this.b = b; + this.l = l; + this.r = r; + } + + public IBorder(Position edge, float n) + { + this(edge == Position.Top ? n : 0.0F, edge == Position.Right ? n : 0.0F, edge == Position.Bottom ? n : 0.0F, edge == Position.Left ? n : 0.0F); + } + + public IBorder(IBorder padding) + { + this(padding.t(), padding.r(), padding.b(), padding.l()); + } + + public float t() + { + return this.t; + } + + public float b() + { + return this.b; + } + + public float l() + { + return this.l; + } + + public float r() + { + return this.r; + } + + public float t(float n) + { + this.t = n; + return this.t; + } + + public float b(float n) + { + this.b = n; + return this.b; + } + + public float l(float n) + { + this.l = n; + return this.l; + } + + public float r(float n) + { + this.r = n; + return this.r; + } + + public boolean isNonZero() + { + return (this.t != 0.0F) || (this.r != 0.0F) || (this.l != 0.0F) || (this.r != 0.0F); + } + + @Deprecated + public IPoint tl() + { + return new IPoint(l(), t()); + } + + @Deprecated + public IPoint tr() + { + return new IPoint(r(), t()); + } + + @Deprecated + public IPoint bl() + { + return new IPoint(l(), b()); + } + + @Deprecated + public IPoint br() + { + return new IPoint(r(), b()); + } + + public IBorder add(IBorder o) + { + return new IBorder(t() + o.t(), r() + o.r(), b() + o.b(), l() + o.l()); + } + + public String toString() + { + return t() + "-" + r() + "-" + b() + "-" + l(); + } +} diff --git a/src/Java/binnie/craftgui/core/geometry/IPoint.java b/src/Java/binnie/craftgui/core/geometry/IPoint.java new file mode 100644 index 0000000000..53be8972fd --- /dev/null +++ b/src/Java/binnie/craftgui/core/geometry/IPoint.java @@ -0,0 +1,83 @@ +package binnie.craftgui.core.geometry; + +public class IPoint +{ + public static final IPoint ZERO = new IPoint(0.0F, 0.0F); + float x = 0.0F; + float y = 0.0F; + + public IPoint(float x, float y) + { + this.x = x; + this.y = y; + } + + public IPoint(IPoint o) + { + this.x = o.x(); + this.y = o.y(); + } + + public static IPoint add(IPoint a, IPoint b) + { + return new IPoint(a.x() + b.x(), a.y() + b.y()); + } + + public static IPoint sub(IPoint a, IPoint b) + { + return new IPoint(a.x() - b.x(), a.y() - b.y()); + } + + public IPoint sub(IPoint a) + { + return sub(this, a); + } + + public IPoint add(IPoint other) + { + return add(this, other); + } + + public IPoint add(float dx, float dy) + { + return add(this, new IPoint(dx, dy)); + } + + public IPoint copy() + { + return new IPoint(this); + } + + public float x() + { + return this.x; + } + + public float y() + { + return this.y; + } + + public void xy(float x, float y) + { + x(x); + y(y); + } + + public float x(float x) + { + this.x = x; + return x; + } + + public float y(float y) + { + this.y = y; + return y; + } + + public boolean equals(IPoint other) + { + return (x() == other.x()) && (y() == other.y()); + } +} diff --git a/src/Java/binnie/craftgui/core/geometry/Position.java b/src/Java/binnie/craftgui/core/geometry/Position.java new file mode 100644 index 0000000000..51b856f120 --- /dev/null +++ b/src/Java/binnie/craftgui/core/geometry/Position.java @@ -0,0 +1,41 @@ +package binnie.craftgui.core.geometry; + +public enum Position +{ + Top(0, -1), Bottom(0, 1), Left(-1, 0), Right(1, 0); + + int x; + int y; + + private Position(int x, int y) + { + this.x = x; + this.y = y; + } + + public int x() + { + return this.x; + } + + public int y() + { + return this.y; + } + + public Position opposite() + { + switch (1.$SwitchMap$binnie$craftgui$core$geometry$Position[ordinal()]) + { + case 1: + return Top; + case 2: + return Right; + case 3: + return Left; + case 4: + return Bottom; + } + return null; + } +} diff --git a/src/Java/binnie/craftgui/core/geometry/TextJustification.java b/src/Java/binnie/craftgui/core/geometry/TextJustification.java new file mode 100644 index 0000000000..99b759380b --- /dev/null +++ b/src/Java/binnie/craftgui/core/geometry/TextJustification.java @@ -0,0 +1,25 @@ +package binnie.craftgui.core.geometry; + +public enum TextJustification +{ + TopLeft(0.0F, 0.0F), TopCenter(0.5F, 0.0F), TopRight(1.0F, 0.0F), MiddleLeft(0.0F, 0.5F), MiddleCenter(0.5F, 0.5F), MiddleRight(1.0F, 0.5F), BottomLeft(0.0F, 1.0F), BottomCenter(0.5F, 1.0F), BottomRight(1.0F, 1.0F); + + float xOffset; + float yOffset; + + private TextJustification(float xOffset, float yOffset) + { + this.xOffset = xOffset; + this.yOffset = yOffset; + } + + public float getXOffset() + { + return this.xOffset; + } + + public float getYOffset() + { + return this.yOffset; + } +} diff --git a/src/Java/binnie/craftgui/core/renderer/Renderer.java b/src/Java/binnie/craftgui/core/renderer/Renderer.java new file mode 100644 index 0000000000..b1d099bf3a --- /dev/null +++ b/src/Java/binnie/craftgui/core/renderer/Renderer.java @@ -0,0 +1,252 @@ +package binnie.craftgui.core.renderer; + +import binnie.core.BinnieCore; +import binnie.core.proxy.BinnieProxy; +import binnie.craftgui.core.IWidget; +import binnie.craftgui.core.geometry.IArea; +import binnie.craftgui.core.geometry.IBorder; +import binnie.craftgui.core.geometry.IPoint; +import binnie.craftgui.core.geometry.Position; +import binnie.craftgui.core.geometry.TextJustification; +import binnie.craftgui.minecraft.GuiCraftGUI; +import binnie.craftgui.resource.IStyleSheet; +import binnie.craftgui.resource.Texture; +import java.util.List; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.renderer.texture.TextureMap; +import net.minecraft.item.ItemStack; +import net.minecraft.util.IIcon; +import org.lwjgl.opengl.GL11; + +public class Renderer +{ + GuiCraftGUI gui; + + public Renderer(GuiCraftGUI gui) + { + this.gui = gui; + } + + public final void preRender(IWidget widget) + { + GL11.glPushMatrix(); + GL11.glTranslatef(widget.getPosition().x(), widget.getPosition().y(), 0.0F); + colour(widget.getColour()); + if (widget.isCroppedWidet()) + { + IWidget cropRelative = widget.getCropWidget() != null ? widget.getCropWidget() : widget; + IPoint pos = cropRelative.getAbsolutePosition(); + IArea cropZone = widget.getCroppedZone(); + limitArea(new IArea(pos.add(cropZone.pos()), cropZone.size())); + GL11.glEnable(3089); + } + GL11.glDisable(2929); + } + + public final void postRender(IWidget widget) + { + if (widget.isCroppedWidet()) { + GL11.glDisable(3089); + } + GL11.glEnable(2929); + GL11.glPopMatrix(); + } + + int currentColour = 16777215; + Texture currentTexture; + IStyleSheet stylesheet; + + public void colour(int hex) + { + this.currentColour = hex; + int a = (hex & 0xFF000000) >> 24; + int r = (hex & 0xFF0000) >> 16; + int g = (hex & 0xFF00) >> 8; + int b = hex & 0xFF; + if (a < 0) { + a += 256; + } + if ((a > 0) && (a != 255)) + { + GL11.glColor4f(r / 255.0F, g / 255.0F, b / 255.0F, a / 255.0F); + GL11.glEnable(3042); + } + else + { + GL11.glColor3f(r / 255.0F, g / 255.0F, b / 255.0F); + } + } + + public Texture getTexture(Object key) + { + if ((key instanceof Texture)) { + return (Texture)key; + } + return this.stylesheet.getTexture(key); + } + + public void setTexture(Texture texture) + { + if ((texture != this.currentTexture) && (texture != null)) { + BinnieCore.proxy.bindTexture(texture.getFilename()); + } + colour(this.currentColour); + } + + public void texture(Object texture, IPoint position) + { + texture(getTexture(texture), position); + } + + public void texture(Texture texture, IPoint position) + { + if (texture == null) { + return; + } + setTexture(texture); + IPoint point = position.sub(new IPoint(texture.getBorder().l(), texture.getBorder().t())); + IArea textureArea = texture.getArea().outset(texture.getBorder()); + this.gui.renderTexture(point, textureArea); + } + + public void texture(Object window, IArea area) + { + texture(getTexture(window), area); + } + + public void texture(Texture texture, IArea area) + { + if (texture == null) { + return; + } + setTexture(texture); + IArea textureArea = texture.getArea().outset(texture.getBorder()); + IArea targetArea = area.outset(texture.getBorder()); + if ((textureArea.w() == targetArea.w()) && (textureArea.h() == targetArea.h())) { + this.gui.renderTexture(targetArea.pos(), textureArea); + } else { + this.gui.renderTexturePadded(targetArea, textureArea, texture.getTotalPadding()); + } + } + + public void stylesheet(IStyleSheet sheet) + { + this.stylesheet = sheet; + } + + public int textWidth(String text) + { + return this.gui.getFontRenderer().getStringWidth(text); + } + + public int textHeight() + { + return this.gui.getFontRenderer() == null ? 0 : this.gui.getFontRenderer().FONT_HEIGHT; + } + + public void text(IPoint pos, String text, int colour) + { + text(new IArea(pos, new IPoint(500.0F, 500.0F)), TextJustification.TopLeft, text, colour); + } + + public void text(IArea area, TextJustification justification, String text, int colour) + { + IPoint pos = area.pos(); + if (area.size().x() <= 0.0F) { + return; + } + List<String> wrappedStrings = this.gui.getFontRenderer().listFormattedStringToWidth(text, (int)area.size().x()); + float totalHeight = wrappedStrings.size() * textHeight(); + float posY = area.pos().y(); + if (area.size().y() > totalHeight) { + posY += (area.size().y() - totalHeight) * justification.getYOffset(); + } + for (String string : wrappedStrings) + { + float stringWidth = textWidth(string); + float posX = area.size().x() - stringWidth; + posX *= justification.getXOffset(); + GL11.glDisable(2929); + this.gui.getFontRenderer().drawString(string, (int)(pos.x() + posX), (int)posY, colour); + + posY += textHeight(); + } + GL11.glColor3f(1.0F, 1.0F, 1.0F); + } + + public void solid(IArea area, int colour) + { + this.gui.drawRect(area.pos().x(), area.pos().y(), area.pos().x() + area.size().x(), area.pos().y() + area.size().y(), 0xFF000000 | colour); + } + + public void solidAlpha(IArea area, int c1) + { + this.gui.drawGradientArea(area.pos().x(), area.pos().y(), area.pos().x() + area.size().x(), area.pos().y() + area.size().y(), c1, c1); + } + + public void gradientRect(IArea area, int c1, int c2) + { + this.gui.drawGradientArea(area.pos().x(), area.pos().y(), area.pos().x() + area.size().x(), area.pos().y() + area.size().y(), c1, c2); + } + + public void item(IPoint pos, ItemStack item) + { + this.gui.renderItem(pos, item, false); + } + + public void item(IPoint pos, ItemStack item, boolean rotating) + { + this.gui.renderItem(pos, item, rotating); + } + + public void iconBlock(IPoint pos, IIcon icon) + { + this.gui.renderIcon(pos, icon, TextureMap.locationBlocksTexture); + } + + public void iconItem(IPoint pos, IIcon icon) + { + this.gui.renderIcon(pos, icon, TextureMap.locationItemsTexture); + } + + public void limitArea(IArea area) + { + this.gui.limitArea(area); + } + + public float textHeight(String text, float width) + { + return this.gui.getFontRenderer().listFormattedStringToWidth(text, (int)width).size() * textHeight(); + } + + public void texturePercentage(Texture texture, IArea area, Position direction, float percentage) + { + float dist = (direction == Position.Top) || (direction == Position.Bottom) ? percentage * texture.h() : percentage * texture.w(); + + float dim = (direction == Position.Top) || (direction == Position.Bottom) ? texture.h() : texture.w(); + + float x = area.pos().x(); + float y = area.pos().y(); + float w = area.size().x(); + float h = area.size().y(); + switch (1.$SwitchMap$binnie$craftgui$core$geometry$Position[direction.ordinal()]) + { + case 1: + h *= percentage; + break; + case 2: + x += (1.0F - percentage) * w; + w *= percentage; + break; + case 3: + w *= percentage; + break; + case 4: + y += h - (int)(percentage * h); + h *= percentage; + } + texture(texture.crop(direction, dim - dist), new IArea(x, y, w, h)); + } + + public void test(IWidget widget) {} +} diff --git a/src/Java/binnie/craftgui/core/renderer/TextureType.java b/src/Java/binnie/craftgui/core/renderer/TextureType.java new file mode 100644 index 0000000000..1c9b0e0d1c --- /dev/null +++ b/src/Java/binnie/craftgui/core/renderer/TextureType.java @@ -0,0 +1,8 @@ +package binnie.craftgui.core.renderer; + +public enum TextureType +{ + Stretched, Tiled; + + private TextureType() {} +} |