aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io
diff options
context:
space:
mode:
authorJuuxel <6596629+Juuxel@users.noreply.github.com>2021-02-27 17:27:07 +0200
committerJuuxel <6596629+Juuxel@users.noreply.github.com>2021-02-27 17:27:07 +0200
commitae3311e993d402d0d26db5e9e0494adcbc9c59b5 (patch)
tree30e8bdb6eb27f4b117ccaaba3c89ca692d58f4ac /src/main/java/io
parentf5e0393c43e8fed42f425a9f621747f2d15fec84 (diff)
downloadLibGui-ae3311e993d402d0d26db5e9e0494adcbc9c59b5.tar.gz
LibGui-ae3311e993d402d0d26db5e9e0494adcbc9c59b5.tar.bz2
LibGui-ae3311e993d402d0d26db5e9e0494adcbc9c59b5.zip
Add insets support for WBox
Diffstat (limited to 'src/main/java/io')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java46
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/data/Axis.java12
2 files changed, 49 insertions, 9 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java
index 261e2f2..e65d27a 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java
@@ -2,6 +2,7 @@ package io.github.cottonmc.cotton.gui.widget;
import io.github.cottonmc.cotton.gui.widget.data.Axis;
import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment;
+import io.github.cottonmc.cotton.gui.widget.data.Insets;
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment;
import java.util.Objects;
@@ -36,6 +37,8 @@ public class WBox extends WPanel {
*/
protected VerticalAlignment verticalAlignment = VerticalAlignment.TOP;
+ private Insets insets = Insets.NONE;
+
/**
* Constructs a box.
*
@@ -74,7 +77,7 @@ public class WBox extends WPanel {
@Override
public void layout() {
- int dimension = 0;
+ int dimension = axis.choose(insets.left, insets.top);
// Set position offset from alignment along the box axis
if (axis == Axis.HORIZONTAL && horizontalAlignment != HorizontalAlignment.LEFT) {
@@ -110,13 +113,13 @@ public class WBox extends WPanel {
switch (verticalAlignment) {
case TOP:
default:
- y = 0;
+ y = insets.top;
break;
case CENTER:
- y = (getHeight() - child.getHeight()) / 2;
+ y = insets.top + (getHeight() - insets.top - insets.bottom - child.getHeight()) / 2;
break;
case BOTTOM:
- y = getHeight() - child.getHeight();
+ y = getHeight() - insets.bottom - child.getHeight();
break;
}
@@ -127,13 +130,13 @@ public class WBox extends WPanel {
switch (horizontalAlignment) {
case LEFT:
default:
- x = 0;
+ x = insets.left;
break;
case CENTER:
- x = (getWidth() - child.getWidth()) / 2;
+ x = insets.left + (getWidth() - insets.left - insets.right - child.getWidth()) / 2;
break;
case RIGHT:
- x = getWidth() - child.getWidth();
+ x = getWidth() - insets.right - child.getWidth();
break;
}
@@ -141,13 +144,13 @@ public class WBox extends WPanel {
}
if (child instanceof WPanel) ((WPanel) child).layout();
- expandToFit(child);
+ expandToFit(child, insets);
if (i != children.size() - 1) {
dimension += spacing;
}
- dimension += axis == Axis.HORIZONTAL ? child.getWidth() : child.getHeight();
+ dimension += axis.choose(child.getWidth(), child.getHeight());
}
}
@@ -238,4 +241,29 @@ public class WBox extends WPanel {
this.verticalAlignment = Objects.requireNonNull(alignment, "alignment");
return this;
}
+
+ /**
+ * Gets the layout insets of this box.
+ *
+ * @return the insets
+ * @since 4.0.0
+ */
+ public Insets getInsets() {
+ return insets;
+ }
+
+ /**
+ * Sets the layout insets of this box.
+ *
+ * <p>The insets should be set <i>before</i> adding any widgets
+ * to this box.
+ *
+ * @param insets the insets, should not be null
+ * @return this box
+ * @since 4.0.0
+ */
+ public WBox setInsets(Insets insets) {
+ this.insets = Objects.requireNonNull(insets, "insets");
+ return this;
+ }
}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Axis.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Axis.java
index c94624a..e4e9645 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Axis.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Axis.java
@@ -3,4 +3,16 @@ package io.github.cottonmc.cotton.gui.widget.data;
public enum Axis {
HORIZONTAL,
VERTICAL;
+
+ /**
+ * Chooses a value based on this axis.
+ *
+ * @param horizontal the value returned if this axis is horizontal
+ * @param vertical the value returned if this axis is vertical
+ * @param <T> the type of the value
+ * @return the corresponding value for this axis
+ */
+ public <T> T choose(T horizontal, T vertical) {
+ return this == HORIZONTAL ? horizontal : vertical;
+ }
}