diff options
author | Juuz <6596629+Juuxel@users.noreply.github.com> | 2023-11-18 21:28:00 +0200 |
---|---|---|
committer | Juuz <6596629+Juuxel@users.noreply.github.com> | 2023-11-18 21:37:01 +0200 |
commit | 13996a09b090eceb7e5a3276b1603f0459a9584e (patch) | |
tree | b42d3af92ad89dd22128edd662d89fda301e9365 /src/main | |
parent | 0e290b633ef8027b258503a05f9cb85b1a4f89b8 (diff) | |
download | LibGui-13996a09b090eceb7e5a3276b1603f0459a9584e.tar.gz LibGui-13996a09b090eceb7e5a3276b1603f0459a9584e.tar.bz2 LibGui-13996a09b090eceb7e5a3276b1603f0459a9584e.zip |
WListPanel: Split margin into insets and gaps
Resolves #221.
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java | 69 |
1 files changed, 64 insertions, 5 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java index d36fb70..5a4c35b 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java @@ -7,10 +7,13 @@ import net.minecraft.client.gui.DrawContext; import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.widget.data.Axis; import io.github.cottonmc.cotton.gui.widget.data.InputResult; +import io.github.cottonmc.cotton.gui.widget.data.Insets; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Objects; import java.util.function.BiConsumer; import java.util.function.Supplier; @@ -51,8 +54,16 @@ public class WListPanel<D, W extends WWidget> extends WClippedPanel { */ protected boolean fixedHeight = false; + /** + * @deprecated Replaced with {@link #getInsets() insets} and {@link #getGap() gap}. + */ + @Deprecated(forRemoval = true) protected int margin = 4; + // When the margin field is removed, these should get default values Insets(4, 4) and 4. + private @Nullable Insets insets = null; + private int gap = -1; + /** * The scroll bar of this list. */ @@ -153,8 +164,10 @@ public class WListPanel<D, W extends WWidget> extends WClippedPanel { } if (cellHeight<4) cellHeight=4; - int layoutHeight = this.getHeight()-(margin*2); - int cellsHigh = Math.max((layoutHeight+margin) / (cellHeight + margin), 1); // At least one cell is always visible + Insets insets = getInsets(); + int gap = getGap(); + int layoutHeight = this.getHeight() - insets.top() - insets.bottom(); + int cellsHigh = Math.max((layoutHeight + gap) / (cellHeight + gap), 1); // At least one cell is always visible //System.out.println("Adding children..."); @@ -190,10 +203,10 @@ public class WListPanel<D, W extends WWidget> extends WClippedPanel { //At this point, w is nonnull and configured by d if (w.canResize()) { - w.setSize(this.width-(margin*2) - scrollBar.getWidth(), cellHeight); + w.setSize(this.width - insets.left() - insets.right() - scrollBar.getWidth(), cellHeight); } - w.x = margin; - w.y = margin + ((cellHeight+margin) * i); + w.x = insets.left(); + w.y = insets.top() + ((cellHeight + gap) * i); this.children.add(w); } } @@ -227,4 +240,50 @@ public class WListPanel<D, W extends WWidget> extends WClippedPanel { public WScrollBar getScrollBar() { return scrollBar; } + + /** + * {@return the layout insets used for the contents of this list} + * @since 9.1.0 + */ + public Insets getInsets() { + // Returns the *effective* insets of this list for backwards compat. + return insets != null ? insets : new Insets(margin, margin); + } + + /** + * Sets the layout insets used for the contents of this list. + * + * @param insets the layout insets + * @return this list + * @since 9.1.0 + */ + public WListPanel<D, W> setInsets(Insets insets) { + this.insets = Objects.requireNonNull(insets, "Insets cannot be null"); + return this; + } + + /** + * {@return the gap between list items} + * @since 9.1.0 + */ + public int getGap() { + // Returns the *effective* gap of this list for backwards compat. + return gap >= 0 ? gap : margin; + } + + /** + * Sets the gap between list items. + * + * @param gap the gap, must be non-negative + * @return this list + * @since 9.1.0 + */ + public WListPanel<D, W> setGap(int gap) { + if (gap < 0) { + throw new IllegalArgumentException("Gap cannot be negative (was " + gap + ")"); + } + + this.gap = gap; + return this; + } } |