aboutsummaryrefslogtreecommitdiff
path: root/src/Java/binnie/craftgui/core/geometry
diff options
context:
space:
mode:
authorDraknyte1 <Draknyte1@hotmail.com>2016-01-20 14:24:34 +1000
committerDraknyte1 <Draknyte1@hotmail.com>2016-01-20 14:24:34 +1000
commit869c206c4fcc8001bd2e1d66f704290331813835 (patch)
tree96735ce8fe4665e2759c3374221d6f06f4527df2 /src/Java/binnie/craftgui/core/geometry
parentec2c72827f01dd4bb2174137f1ab162f9ddaab62 (diff)
downloadGT5-Unofficial-869c206c4fcc8001bd2e1d66f704290331813835.tar.gz
GT5-Unofficial-869c206c4fcc8001bd2e1d66f704290331813835.tar.bz2
GT5-Unofficial-869c206c4fcc8001bd2e1d66f704290331813835.zip
Initial Commit
Diffstat (limited to 'src/Java/binnie/craftgui/core/geometry')
-rw-r--r--src/Java/binnie/craftgui/core/geometry/CraftGUIUtil.java77
-rw-r--r--src/Java/binnie/craftgui/core/geometry/IArea.java143
-rw-r--r--src/Java/binnie/craftgui/core/geometry/IBorder.java126
-rw-r--r--src/Java/binnie/craftgui/core/geometry/IPoint.java83
-rw-r--r--src/Java/binnie/craftgui/core/geometry/Position.java41
-rw-r--r--src/Java/binnie/craftgui/core/geometry/TextJustification.java25
6 files changed, 495 insertions, 0 deletions
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;
+ }
+}