aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io
diff options
context:
space:
mode:
authorRoman / Linnea Gräf <romangraef@gmail.com>2022-10-22 14:51:49 +0200
committerGitHub <noreply@github.com>2022-10-22 15:51:49 +0300
commitd76d6d4c9eebabc734dbd71f2838c782e8226c68 (patch)
tree5f129a8d3dd1bbb8a34af8cf028948e8b4470d69 /src/main/java/io
parent9ddc57b0db03fa7afabf23bb7ee490a5c84af393 (diff)
downloadLibGui-d76d6d4c9eebabc734dbd71f2838c782e8226c68.tar.gz
LibGui-d76d6d4c9eebabc734dbd71f2838c782e8226c68.tar.bz2
LibGui-d76d6d4c9eebabc734dbd71f2838c782e8226c68.zip
Allow WGridPanel to have gaps between widgets. (#170)
* Allow WGridPanel to have gaps between widgets. * Various formatting changes
Diffstat (limited to 'src/main/java/io')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WGridPanel.java48
1 files changed, 37 insertions, 11 deletions
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 5c0c454..22139e9 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
@@ -1,11 +1,14 @@
package io.github.cottonmc.cotton.gui.widget;
+import io.github.cottonmc.cotton.gui.impl.VisualLogger;
import io.github.cottonmc.cotton.gui.widget.data.Insets;
/**
* A panel that positions children in a grid.
*/
public class WGridPanel extends WPanelWithInsets {
+ private static final VisualLogger LOGGER = new VisualLogger(WGridPanel.class);
+
/**
* The grid size in pixels.
* Defaults to 18, which is the size of one item slot.
@@ -13,6 +16,16 @@ public class WGridPanel extends WPanelWithInsets {
protected int grid = 18;
/**
+ * The horizontal gap between two grid cells.
+ */
+ protected int horizontalGap = 0;
+
+ /**
+ * The vertical gap between two grid cells.
+ */
+ protected int verticalGap = 0;
+
+ /**
* Constructs a grid panel with the default grid size.
*/
public WGridPanel() {}
@@ -24,6 +37,26 @@ public class WGridPanel extends WPanelWithInsets {
*/
public WGridPanel(int gridSize) { this.grid = gridSize; }
+
+ /**
+ * Set the gaps between grid cells.
+ *
+ * <p><b>This method can only be called before any elements get added to this layout.</b></p>
+ *
+ * @param horizontalGap the horizontal gap between grid cells
+ * @param verticalGap the vertical gap between grid cells
+ */
+ public WGridPanel setGaps(int horizontalGap, int verticalGap) {
+ if (!this.children.isEmpty()) {
+ LOGGER.warn("You can only change gaps before adding children to a WGridPanel");
+ return this;
+ }
+ this.horizontalGap = horizontalGap;
+ this.verticalGap = verticalGap;
+ return this;
+ }
+
+
/**
* Adds a widget to this panel.
*
@@ -35,14 +68,7 @@ public class WGridPanel extends WPanelWithInsets {
* @param y the Y position in grid cells
*/
public void add(WWidget w, int x, int y) {
- children.add(w);
- w.parent = this;
- w.setLocation(x * grid + insets.left(), y * grid + insets.top());
- if (w.canResize()) {
- w.setSize(grid, grid);
- }
-
- expandToFit(w, insets);
+ add(w, x, y, 1, 1);
}
/**
@@ -57,11 +83,11 @@ public class WGridPanel extends WPanelWithInsets {
public void add(WWidget w, int x, int y, int width, int height) {
children.add(w);
w.parent = this;
- w.setLocation(x * grid + insets.left(), y * grid + insets.top());
+ w.setLocation(x * (grid + horizontalGap) + insets.left(), y * (grid + verticalGap) + insets.top());
if (w.canResize()) {
- w.setSize(width * grid, height * grid);
+ w.setSize((width-1) * (grid+horizontalGap) + grid, (height-1) * (grid+verticalGap) + grid);
}
-
+
expandToFit(w, insets);
}