diff options
Diffstat (limited to 'src/main/java/io')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java | 131 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WVerticalBox.java | 57 |
2 files changed, 117 insertions, 71 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 e02e415..9ff61a6 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 @@ -2,115 +2,104 @@ package io.github.cottonmc.cotton.gui.widget; import net.minecraft.client.util.math.MatrixStack; -import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.widget.data.Axis; -import java.util.ArrayList; -import java.util.List; - /** - * Similar to the JScrollPane in Swing, this widget represents a vertically scrollable list of widgets. + * Similar to the JScrollPane in Swing, this widget represents a scrollable widget. * * @since 2.0.0 */ public class WScrollPanel extends WClippedPanel { + private final WWidget widget; + + private boolean scrollingHorizontally = false; + private boolean scrollingVertically = true; + /** - * The spacing between widgets. + * The horizontal scroll bar of this panel. */ - protected int spacing = 4; + protected WScrollBar horizontalScrollBar = new WScrollBar(Axis.HORIZONTAL); /** - * The scroll bar of this panel. + * The vertical scroll bar of this panel. */ - protected WScrollBar scrollBar = new WScrollBar(Axis.VERTICAL); + protected WScrollBar verticalScrollBar = new WScrollBar(Axis.VERTICAL); - private int lastScroll = -1; + private int lastHorizontalScroll = -1; + private int lastVerticalScroll = -1; - // The list of *all* children as opposed to visible children, excluding the scroll bar. - private final List<WWidget> allChildren = new ArrayList<>(); - - public WScrollPanel() { - scrollBar.setParent(this); - children.add(scrollBar); - } + /** + * Creates a vertically scrolling panel. + * + * @param widget the viewed widget + */ + public WScrollPanel(WWidget widget) { + this.widget = widget; - public void add(WWidget widget, int width, int height) { widget.setParent(this); - allChildren.add(widget); + horizontalScrollBar.setParent(this); + verticalScrollBar.setParent(this); + children.add(widget); - if (canResize()) { - widget.setSize(width, height); - } + children.add(verticalScrollBar); // Only vertical scroll bar } - public void add(WWidget widget) { - add(widget, 18, 18); + public boolean isScrollingHorizontally() { + return scrollingHorizontally; } - @Override - public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { - if (scrollBar.getValue() != lastScroll) { + public WScrollPanel setScrollingHorizontally(boolean scrollingHorizontally) { + if (scrollingHorizontally != this.scrollingHorizontally) { + this.scrollingHorizontally = scrollingHorizontally; layout(); - lastScroll = scrollBar.getValue(); } - super.paint(matrices, x, y, mouseX, mouseY); + return this; } - @Override - public void layout() { - children.clear(); - scrollBar.setLocation(this.width - scrollBar.getWidth(), 0); - scrollBar.setSize(8, this.height); - - int offset = scrollBar.getValue(); - int height = 0; - - for (int i = 0; i < allChildren.size(); i++) { - WWidget child = allChildren.get(i); - int minY = height - offset; - child.setLocation(0, minY); - int maxY = minY + child.getHeight(); - - if ((minY >= 0 && minY < getHeight()) || (maxY >= 0 && maxY < getHeight()) || (minY < 0 && maxY >= getHeight())) { - children.add(child); - } - - if (i != allChildren.size() - 1) { - height += spacing; - } + public boolean isScrollingVertically() { + return scrollingVertically; + } - height += child.getHeight(); + public WScrollPanel setScrollingVertically(boolean scrollingVertically) { + if (scrollingVertically != this.scrollingVertically) { + this.scrollingVertically = scrollingVertically; + layout(); } - children.add(scrollBar); - scrollBar.setWindow(Math.min(height / 4, 18)); - scrollBar.setMaxValue(height); + return this; } @Override - public void validate(GuiDescription c) { - for (WWidget child : allChildren) { - child.validate(c); + public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { + if (verticalScrollBar.getValue() != lastVerticalScroll || horizontalScrollBar.getValue() != lastHorizontalScroll) { + layout(); + lastHorizontalScroll = horizontalScrollBar.getValue(); + lastVerticalScroll = verticalScrollBar.getValue(); } - super.validate(c); + + super.paint(matrices, x, y, mouseX, mouseY); } @Override - public void createPeers(GuiDescription c) { - super.createPeers(c); - for (WWidget child : allChildren) { - child.createPeers(c); - } - } + public void layout() { + children.clear(); + verticalScrollBar.setLocation(this.width - verticalScrollBar.getWidth(), 0); + verticalScrollBar.setSize(8, this.height); + horizontalScrollBar.setLocation(0, this.height - horizontalScrollBar.getHeight()); + horizontalScrollBar.setSize(scrollingVertically ? (this.width - verticalScrollBar.getWidth()) : this.width, 8); - public int getSpacing() { - return spacing; - } + children.add(widget); + int x = scrollingHorizontally ? -horizontalScrollBar.getValue() : 0; + int y = scrollingVertically ? -verticalScrollBar.getValue() : 0; + widget.setLocation(x, y); - public WScrollPanel setSpacing(int spacing) { - this.spacing = spacing; + verticalScrollBar.setWindow(this.height); + verticalScrollBar.setMaxValue(widget.getHeight() + 1); + horizontalScrollBar.setWindow(this.width); + horizontalScrollBar.setMaxValue(widget.getWidth() + 1); - return this; + if (scrollingVertically) children.add(verticalScrollBar); + if (scrollingHorizontally) children.add(horizontalScrollBar); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WVerticalBox.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WVerticalBox.java new file mode 100644 index 0000000..7987424 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WVerticalBox.java @@ -0,0 +1,57 @@ +package io.github.cottonmc.cotton.gui.widget; + +/** + * Similar to the BoxLayout in Swing, this widget represents a vertical list of widgets. + * + * @since 2.0.0 + */ +public class WVerticalBox extends WPanel { + /** + * The spacing between widgets. + */ + protected int spacing = 4; + + public WVerticalBox() { + } + + public void add(WWidget widget, int width, int height) { + widget.setParent(this); + children.add(widget); + if (canResize()) { + widget.setSize(width, height); + } + } + + public void add(WWidget widget) { + add(widget, 18, 18); + } + + @Override + public void layout() { + int height = 0; + + for (int i = 0; i < children.size(); i++) { + WWidget child = children.get(i); + child.setLocation(0, height); + + if (child instanceof WPanel) ((WPanel) child).layout(); + expandToFit(child); + + if (i != children.size() - 1) { + height += spacing; + } + + height += child.getHeight(); + } + } + + public int getSpacing() { + return spacing; + } + + public WVerticalBox setSpacing(int spacing) { + this.spacing = spacing; + + return this; + } +} |