aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java26
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WGridPanel.java29
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WPanelWithInsets.java52
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java28
4 files changed, 59 insertions, 76 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 a4212c9..eb447a0 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
@@ -12,7 +12,7 @@ import java.util.Objects;
*
* @since 2.0.0
*/
-public class WBox extends WPanel {
+public class WBox extends WPanelWithInsets {
/**
* The spacing between widgets.
*/
@@ -37,8 +37,6 @@ public class WBox extends WPanel {
*/
protected VerticalAlignment verticalAlignment = VerticalAlignment.TOP;
- private Insets insets = Insets.NONE;
-
/**
* Constructs a box.
*
@@ -242,28 +240,8 @@ public class WBox extends WPanel {
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");
+ super.setInsets(insets);
return this;
}
}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WGridPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WGridPanel.java
index c2bea8b..5c0c454 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WGridPanel.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WGridPanel.java
@@ -2,14 +2,10 @@ package io.github.cottonmc.cotton.gui.widget;
import io.github.cottonmc.cotton.gui.widget.data.Insets;
-import java.util.Objects;
-
/**
* A panel that positions children in a grid.
*/
-public class WGridPanel extends WPanel {
- private Insets insets = Insets.NONE;
-
+public class WGridPanel extends WPanelWithInsets {
/**
* The grid size in pixels.
* Defaults to 18, which is the size of one item slot.
@@ -69,28 +65,9 @@ public class WGridPanel extends WPanel {
expandToFit(w, insets);
}
- /**
- * Gets the layout insets of this panel.
- *
- * @return the insets
- * @since 4.0.0
- */
- public Insets getInsets() {
- return insets;
- }
-
- /**
- * Sets the layout insets of this panel.
- *
- * <p>The insets should be set <i>before</i> adding any widgets
- * to this panel.
- *
- * @param insets the insets, should not be null
- * @return this panel
- * @since 4.0.0
- */
+ @Override
public WGridPanel setInsets(Insets insets) {
- this.insets = Objects.requireNonNull(insets, "insets");
+ super.setInsets(insets);
return this;
}
}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanelWithInsets.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanelWithInsets.java
new file mode 100644
index 0000000..77b22be
--- /dev/null
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanelWithInsets.java
@@ -0,0 +1,52 @@
+package io.github.cottonmc.cotton.gui.widget;
+
+import io.github.cottonmc.cotton.gui.widget.data.Insets;
+
+import java.util.Objects;
+
+/**
+ * A panel that has {@linkplain Insets layout insets}.
+ *
+ * @since 4.0.0
+ */
+public abstract class WPanelWithInsets extends WPanel {
+ /**
+ * The layout insets of this panel.
+ * They control how far from the panel's edges the widgets are placed.
+ */
+ protected Insets insets = Insets.NONE;
+
+ /**
+ * Gets the layout insets of this panel.
+ *
+ * @return the insets
+ */
+ public Insets getInsets() {
+ return insets;
+ }
+
+ /**
+ * Sets the layout insets of this panel.
+ * Subclasses are encouraged to override this method to return their more specific type
+ * (such as {@link WGridPanel}).
+ *
+ * <p>If there are already widgets in this panel when the insets are modified,
+ * the panel is resized and the widgets are moved according to the insets.
+ *
+ * @param insets the insets, should not be null
+ * @return this panel
+ */
+ public WPanelWithInsets setInsets(Insets insets) {
+ Insets old = this.insets;
+ this.insets = Objects.requireNonNull(insets, "insets");
+
+ setSize(getWidth() - old.left() - old.right(), getHeight() - old.top() - old.bottom());
+
+ for (WWidget child : children) {
+ child.setLocation(child.getX() - old.left() + insets.left(), child.getY() - old.top() + insets.top());
+ expandToFit(child, insets);
+ }
+
+ return this;
+ }
+}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java
index 3c7bee7..7f18d5a 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java
@@ -2,14 +2,10 @@ package io.github.cottonmc.cotton.gui.widget;
import io.github.cottonmc.cotton.gui.widget.data.Insets;
-import java.util.Objects;
-
/**
* A panel that positions children by pixel-perfect positions.
*/
-public class WPlainPanel extends WPanel {
- private Insets insets = Insets.NONE;
-
+public class WPlainPanel extends WPanelWithInsets {
/**
* Adds a new widget to this panel.
*
@@ -53,28 +49,8 @@ public class WPlainPanel extends WPanel {
//valid = false;
}
- /**
- * Gets the layout insets of this panel.
- *
- * @return the insets
- * @since 4.0.0
- */
- public Insets getInsets() {
- return insets;
- }
-
- /**
- * Sets the layout insets of this panel.
- *
- * <p>The insets should be set <i>before</i> adding any widgets
- * to this panel.
- *
- * @param insets the insets, should not be null
- * @return this panel
- * @since 4.0.0
- */
public WPlainPanel setInsets(Insets insets) {
- this.insets = Objects.requireNonNull(insets, "insets");
+ super.setInsets(insets);
return this;
}
}