diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WGridPanel.java | 55 |
1 files changed, 42 insertions, 13 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..121d042 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,15 @@ 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,16 +17,48 @@ 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() {} + public WGridPanel() { + } /** * Constructs a grid panel with a custom grid size. * * @param gridSize the grid size in pixels */ - public WGridPanel(int gridSize) { this.grid = gridSize; } + 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 void setGaps(int horizontalGap, int verticalGap) { + if (!this.children.isEmpty()) { + LOGGER.warn("You can only change gaps before adding children to a WGridPanel"); + return; + } + this.horizontalGap = horizontalGap; + this.verticalGap = verticalGap; + } + /** * Adds a widget to this panel. @@ -35,14 +71,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 +86,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); } |