From d76d6d4c9eebabc734dbd71f2838c782e8226c68 Mon Sep 17 00:00:00 2001 From: Roman / Linnea Gräf Date: Sat, 22 Oct 2022 14:51:49 +0200 Subject: Allow WGridPanel to have gaps between widgets. (#170) * Allow WGridPanel to have gaps between widgets. * Various formatting changes --- .../cottonmc/cotton/gui/widget/WGridPanel.java | 48 +++++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) (limited to 'src') 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,17 +1,30 @@ 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. */ 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. */ @@ -24,6 +37,26 @@ public class WGridPanel extends WPanelWithInsets { */ public WGridPanel(int gridSize) { this.grid = gridSize; } + + /** + * Set the gaps between grid cells. + * + *

This method can only be called before any elements get added to this layout.

+ * + * @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); } -- cgit