aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuuz <6596629+Juuxel@users.noreply.github.com>2023-11-18 21:52:30 +0200
committerJuuz <6596629+Juuxel@users.noreply.github.com>2023-11-18 21:52:30 +0200
commitb5c5182e32f77db84dee227aadb74aece31ef2f6 (patch)
tree3a61cc886334a8732c1af52b5244f08a6459f685
parent3993b1a96704f9241b1bfceb03f9575c3c503b49 (diff)
downloadLibGui-b5c5182e32f77db84dee227aadb74aece31ef2f6.tar.gz
LibGui-b5c5182e32f77db84dee227aadb74aece31ef2f6.tar.bz2
LibGui-b5c5182e32f77db84dee227aadb74aece31ef2f6.zip
WScrollPanel: Add insets support
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java52
-rw-r--r--src/testMod/java/io/github/cottonmc/test/client/ScrollingTestGui.java44
2 files changed, 88 insertions, 8 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java
index 389df93..66cefaa 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java
@@ -6,8 +6,11 @@ import net.fabricmc.fabric.api.util.TriState;
import net.minecraft.client.gui.DrawContext;
import io.github.cottonmc.cotton.gui.GuiDescription;
+import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
+import io.github.cottonmc.cotton.gui.client.Scissors;
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 java.util.Objects;
@@ -16,7 +19,7 @@ import java.util.Objects;
*
* @since 2.0.0
*/
-public class WScrollPanel extends WClippedPanel {
+public class WScrollPanel extends WPanel {
private static final int SCROLL_BAR_SIZE = 8;
private final WWidget widget;
@@ -36,6 +39,8 @@ public class WScrollPanel extends WClippedPanel {
private int lastHorizontalScroll = -1;
private int lastVerticalScroll = -1;
+ private Insets insets = Insets.NONE;
+
/**
* Creates a vertically scrolling panel.
*
@@ -147,7 +152,21 @@ public class WScrollPanel extends WClippedPanel {
lastVerticalScroll = verticalScrollBar.getValue();
}
- super.paint(context, x, y, mouseX, mouseY);
+ BackgroundPainter backgroundPainter = getBackgroundPainter();
+ if (backgroundPainter != null) backgroundPainter.paintBackground(context, x, y, this);
+
+ Insets insets = getInsets();
+ for (WWidget child : children) {
+ if (child == widget) {
+ Scissors.push(x + insets.left(), y + insets.top(), width - insets.left() - insets.right(), height - insets.top() - insets.bottom());
+ }
+
+ child.paint(context, x + child.getX(), y + child.getY(), mouseX - child.getX(), mouseY - child.getY());
+
+ if (child == widget) {
+ Scissors.pop();
+ }
+ }
}
@Override
@@ -165,13 +184,14 @@ public class WScrollPanel extends WClippedPanel {
if (widget instanceof WPanel) ((WPanel) widget).layout();
children.add(widget);
- int x = horizontal ? -horizontalScrollBar.getValue() : 0;
- int y = vertical ? -verticalScrollBar.getValue() : 0;
+ Insets insets = getInsets();
+ int x = insets.left() + (horizontal ? -horizontalScrollBar.getValue() : 0);
+ int y = insets.top() + (vertical ? -verticalScrollBar.getValue() : 0);
widget.setLocation(x, y);
- verticalScrollBar.setWindow(this.height - (horizontal ? SCROLL_BAR_SIZE : 0));
+ verticalScrollBar.setWindow(this.height - insets.top() - insets.bottom() - (horizontal ? SCROLL_BAR_SIZE : 0));
verticalScrollBar.setMaxValue(widget.getHeight());
- horizontalScrollBar.setWindow(this.width - (vertical ? SCROLL_BAR_SIZE : 0));
+ horizontalScrollBar.setWindow(this.width - insets.left() - insets.right() - (vertical ? SCROLL_BAR_SIZE : 0));
horizontalScrollBar.setMaxValue(widget.getWidth());
if (vertical) children.add(verticalScrollBar);
@@ -213,4 +233,24 @@ public class WScrollPanel extends WClippedPanel {
this.verticalScrollBar.validate(c);
super.validate(c);
}
+
+ /**
+ * {@return the layout insets used for the viewed widget}
+ * @since 9.1.0
+ */
+ public Insets getInsets() {
+ return insets;
+ }
+
+ /**
+ * Sets the layout insets used for the viewed widget.
+ *
+ * @param insets the layout insets
+ * @return this scroll panel
+ * @since 9.1.0
+ */
+ public WScrollPanel setInsets(Insets insets) {
+ this.insets = Objects.requireNonNull(insets, "Insets cannot be null");
+ return this;
+ }
}
diff --git a/src/testMod/java/io/github/cottonmc/test/client/ScrollingTestGui.java b/src/testMod/java/io/github/cottonmc/test/client/ScrollingTestGui.java
index bfc25c4..af0d1e3 100644
--- a/src/testMod/java/io/github/cottonmc/test/client/ScrollingTestGui.java
+++ b/src/testMod/java/io/github/cottonmc/test/client/ScrollingTestGui.java
@@ -11,6 +11,7 @@ import io.github.cottonmc.cotton.gui.widget.WLabel;
import io.github.cottonmc.cotton.gui.widget.WLabeledSlider;
import io.github.cottonmc.cotton.gui.widget.WScrollPanel;
import io.github.cottonmc.cotton.gui.widget.data.Axis;
+import io.github.cottonmc.cotton.gui.widget.data.Insets;
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment;
import io.github.cottonmc.cotton.gui.widget.icon.ItemIcon;
@@ -18,6 +19,7 @@ public class ScrollingTestGui extends LightweightGuiDescription {
public ScrollingTestGui() {
WGridPanel root = (WGridPanel) rootPanel;
WBox box = new WBox(Axis.VERTICAL);
+ WScrollPanel scrollPanel = new WScrollPanel(box);
for (int i = 0; i < 20; i++) {
box.add(new WLabeledSlider(0, 10, Text.literal("Slider #" + i)));
@@ -25,8 +27,46 @@ public class ScrollingTestGui extends LightweightGuiDescription {
box.add(new WButton(new ItemIcon(Items.APPLE)));
- root.add(new WLabel(Text.literal("Scrolling test")).setVerticalAlignment(VerticalAlignment.CENTER), 0, 0, 5, 2);
- root.add(new WScrollPanel(box), 0, 2, 5, 3);
+ WLabeledSlider topSlider = new WLabeledSlider(0, 16, Axis.HORIZONTAL, Text.literal("Top insets"));
+ WLabeledSlider bottomSlider = new WLabeledSlider(0, 16, Axis.HORIZONTAL, Text.literal("Bottom insets"));
+ WLabeledSlider leftSlider = new WLabeledSlider(0, 16, Axis.HORIZONTAL, Text.literal("Left insets"));
+ WLabeledSlider rightSlider = new WLabeledSlider(0, 16, Axis.HORIZONTAL, Text.literal("Right insets"));
+
+ topSlider.setValueChangeListener(top -> {
+ Insets insets = scrollPanel.getInsets();
+ Insets newInsets = new Insets(top, insets.left(), insets.bottom(), insets.right());
+ scrollPanel.setInsets(newInsets);
+ scrollPanel.layout();
+ });
+
+ bottomSlider.setValueChangeListener(bottom -> {
+ Insets insets = scrollPanel.getInsets();
+ Insets newInsets = new Insets(insets.top(), insets.left(), bottom, insets.right());
+ scrollPanel.setInsets(newInsets);
+ scrollPanel.layout();
+ });
+
+ leftSlider.setValueChangeListener(left -> {
+ Insets insets = scrollPanel.getInsets();
+ Insets newInsets = new Insets(insets.top(), left, insets.bottom(), insets.right());
+ scrollPanel.setInsets(newInsets);
+ scrollPanel.layout();
+ });
+
+ rightSlider.setValueChangeListener(right -> {
+ Insets insets = scrollPanel.getInsets();
+ Insets newInsets = new Insets(insets.top(), insets.left(), insets.bottom(), right);
+ scrollPanel.setInsets(newInsets);
+ scrollPanel.layout();
+ });
+
+ root.setGaps(2, 2);
+ root.add(new WLabel(Text.literal("Scrolling test")).setVerticalAlignment(VerticalAlignment.CENTER), 0, 0, 6, 2);
+ root.add(topSlider, 0, 2, 3, 1);
+ root.add(bottomSlider, 3, 2, 3, 1);
+ root.add(leftSlider, 0, 3, 3, 1);
+ root.add(rightSlider, 3, 3, 3, 1);
+ root.add(scrollPanel, 0, 4, 6, 3);
root.validate(this);
}
}