diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-20 21:03:33 +0300 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-20 21:03:33 +0300 |
commit | 1a4ee8b4151d27906d16fa506f139cf1edf51c1e (patch) | |
tree | 5d3fcfaa6e7f88f693e3550c52be9abf045ebec5 | |
parent | b234590bab2866cda0ce3b48aba0206e3c4ce198 (diff) | |
download | LibGui-1a4ee8b4151d27906d16fa506f139cf1edf51c1e.tar.gz LibGui-1a4ee8b4151d27906d16fa506f139cf1edf51c1e.tar.bz2 LibGui-1a4ee8b4151d27906d16fa506f139cf1edf51c1e.zip |
Add horizontal axis support for boxes, document scroll widgets
4 files changed, 141 insertions, 57 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java new file mode 100644 index 0000000..b7fe4c2 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java @@ -0,0 +1,123 @@ +package io.github.cottonmc.cotton.gui.widget; + +import io.github.cottonmc.cotton.gui.widget.data.Axis; + +import java.util.Objects; + +/** + * Similar to the BoxLayout in Swing, this widget represents a list of widgets along an axis. + * + * @since 2.0.0 + */ +public class WBox extends WPanel { + /** + * The spacing between widgets. + */ + protected int spacing = 4; + + /** + * The axis that the widgets are laid out on. + */ + protected Axis axis; + + /** + * Constructs a box. + * + * @param axis the box axis + * @throws NullPointerException if the axis is null + */ + public WBox(Axis axis) { + this.axis = Objects.requireNonNull(axis, "axis"); + } + + /** + * Adds a widget to this box. + * If the widget is resizeable, resizes it to the provided dimensions. + * + * @param widget the widget + * @param width the new width of the widget + * @param height the new height of the widget + */ + public void add(WWidget widget, int width, int height) { + widget.setParent(this); + children.add(widget); + if (canResize()) { + widget.setSize(width, height); + } + } + + /** + * Adds a widget to this box. + * If the widget is resizeable, resizes it to 18x18. + * + * @param widget the widget + */ + public void add(WWidget widget) { + add(widget, 18, 18); + } + + @Override + public void layout() { + int dimension = 0; + + for (int i = 0; i < children.size(); i++) { + WWidget child = children.get(i); + if (axis == Axis.HORIZONTAL) { + child.setLocation(dimension, 0); + } else { + child.setLocation(0, dimension); + } + + if (child instanceof WPanel) ((WPanel) child).layout(); + expandToFit(child); + + if (i != children.size() - 1) { + dimension += spacing; + } + + dimension += axis == Axis.HORIZONTAL ? child.getWidth() : child.getHeight(); + } + } + + /** + * Gets the spacing between widgets. + * + * @return the spacing + */ + public int getSpacing() { + return spacing; + } + + /** + * Sets the spacing between widgets in this box. + * + * @param spacing the new spacing + * @return this box + */ + public WBox setSpacing(int spacing) { + this.spacing = spacing; + + return this; + } + + /** + * Gets the axis of this box. + * + * @return the axis + */ + public Axis getAxis() { + return axis; + } + + /** + * Sets the axis of this box. + * + * @param axis the new axis + * @return this box + * @throws NullPointerException if the axis is null + */ + public WBox setAxis(Axis axis) { + this.axis = Objects.requireNonNull(axis, "axis"); + return this; + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java index e940327..0ddf01a 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java @@ -6,7 +6,15 @@ import io.github.cottonmc.cotton.gui.client.LibGuiClient; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.widget.data.Axis; +/** + * A scroll bar. + * + * <p>Since LibGui 2.0.0, this widget is also a {@link WAbstractSlider}. + */ public class WScrollBar extends WAbstractSlider { + /** + * The range of values that is shown at a time. + */ protected int window = 16; /** 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 bff7987..71ab883 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 @@ -44,6 +44,11 @@ public class WScrollPanel extends WClippedPanel { children.add(verticalScrollBar); // Only vertical scroll bar } + /** + * Returns whether this scroll panels has a horizontal scroll bar. + * + * @return true if there is a horizontal scroll bar, false otherwise + */ public boolean isScrollingHorizontally() { return scrollingHorizontally; } @@ -57,6 +62,11 @@ public class WScrollPanel extends WClippedPanel { return this; } + /** + * Returns whether this scroll panels has a vertical scroll bar. + * + * @return true if there is a vertical scroll bar, false otherwise + */ public boolean isScrollingVertically() { return scrollingVertically; } 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 deleted file mode 100644 index 7987424..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WVerticalBox.java +++ /dev/null @@ -1,57 +0,0 @@ -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; - } -} |