diff options
author | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-06-05 00:43:59 +0300 |
---|---|---|
committer | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-06-05 00:43:59 +0300 |
commit | f15d6c10ff057d906e138a4e3c199a9a90c3a92a (patch) | |
tree | 94b84d23c0fd141ede0889900e4f4c99ab97565b | |
parent | 1a92028f3b019eca1cf2d460f598bb131dae8457 (diff) | |
download | LibGui-4.0.0-beta.4.tar.gz LibGui-4.0.0-beta.4.tar.bz2 LibGui-4.0.0-beta.4.zip |
Make insets properly swappable, add WPanelWithInsets4.0.0-beta.4
6 files changed, 88 insertions, 76 deletions
diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/client/InsetsTestGui.java b/GuiTest/src/main/java/io/github/cottonmc/test/client/InsetsTestGui.java new file mode 100644 index 0000000..abd36e6 --- /dev/null +++ b/GuiTest/src/main/java/io/github/cottonmc/test/client/InsetsTestGui.java @@ -0,0 +1,22 @@ +package io.github.cottonmc.test.client; + +import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription; +import io.github.cottonmc.cotton.gui.widget.WButton; +import io.github.cottonmc.cotton.gui.widget.WGridPanel; + +import io.github.cottonmc.cotton.gui.widget.WLabel; + +import io.github.cottonmc.cotton.gui.widget.data.Insets; + +import net.minecraft.text.LiteralText; + +public class InsetsTestGui extends LightweightGuiDescription { + public InsetsTestGui() { + WGridPanel root = (WGridPanel) rootPanel; + + root.add(new WLabel(new LiteralText("Insets demo")), 0, 0); + root.add(new WButton(new LiteralText("Default")).setOnClick(() -> root.setInsets(Insets.ROOT_PANEL)), 0, 1, 2, 1); + root.add(new WButton(new LiteralText("None")).setOnClick(() -> root.setInsets(Insets.NONE)), 2, 1, 2, 1); + root.add(new WButton(new LiteralText("Large")).setOnClick(() -> root.setInsets(new Insets(16))), 4, 1, 2, 1); + } +} diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java b/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java index b45145e..2f78fcd 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java @@ -47,6 +47,13 @@ public class LibGuiTestClient implements ClientModInitializer { }); return 0; })) + .then(literal("insets").executes(context -> { + var client = context.getSource().getClient(); + client.send(() -> { + client.openScreen(new CottonClientScreen(new InsetsTestGui())); + }); + return 0; + })) ); } 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 index a4212c9..eb447a0 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java @@ -12,7 +12,7 @@ import java.util.Objects; * * @since 2.0.0 */ -public class WBox extends WPanel { +public class WBox extends WPanelWithInsets { /** * The spacing between widgets. */ @@ -37,8 +37,6 @@ public class WBox extends WPanel { */ protected VerticalAlignment verticalAlignment = VerticalAlignment.TOP; - private Insets insets = Insets.NONE; - /** * Constructs a box. * @@ -242,28 +240,8 @@ public class WBox extends WPanel { return this; } - /** - * Gets the layout insets of this box. - * - * @return the insets - * @since 4.0.0 - */ - public Insets getInsets() { - return insets; - } - - /** - * Sets the layout insets of this box. - * - * <p>The insets should be set <i>before</i> adding any widgets - * to this box. - * - * @param insets the insets, should not be null - * @return this box - * @since 4.0.0 - */ public WBox setInsets(Insets insets) { - this.insets = Objects.requireNonNull(insets, "insets"); + super.setInsets(insets); return this; } } 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 c2bea8b..5c0c454 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 @@ -2,14 +2,10 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.widget.data.Insets; -import java.util.Objects; - /** * A panel that positions children in a grid. */ -public class WGridPanel extends WPanel { - private Insets insets = Insets.NONE; - +public class WGridPanel extends WPanelWithInsets { /** * The grid size in pixels. * Defaults to 18, which is the size of one item slot. @@ -69,28 +65,9 @@ public class WGridPanel extends WPanel { expandToFit(w, insets); } - /** - * Gets the layout insets of this panel. - * - * @return the insets - * @since 4.0.0 - */ - public Insets getInsets() { - return insets; - } - - /** - * Sets the layout insets of this panel. - * - * <p>The insets should be set <i>before</i> adding any widgets - * to this panel. - * - * @param insets the insets, should not be null - * @return this panel - * @since 4.0.0 - */ + @Override public WGridPanel setInsets(Insets insets) { - this.insets = Objects.requireNonNull(insets, "insets"); + super.setInsets(insets); return this; } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanelWithInsets.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanelWithInsets.java new file mode 100644 index 0000000..77b22be --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanelWithInsets.java @@ -0,0 +1,52 @@ +package io.github.cottonmc.cotton.gui.widget; + +import io.github.cottonmc.cotton.gui.widget.data.Insets; + +import java.util.Objects; + +/** + * A panel that has {@linkplain Insets layout insets}. + * + * @since 4.0.0 + */ +public abstract class WPanelWithInsets extends WPanel { + /** + * The layout insets of this panel. + * They control how far from the panel's edges the widgets are placed. + */ + protected Insets insets = Insets.NONE; + + /** + * Gets the layout insets of this panel. + * + * @return the insets + */ + public Insets getInsets() { + return insets; + } + + /** + * Sets the layout insets of this panel. + * Subclasses are encouraged to override this method to return their more specific type + * (such as {@link WGridPanel}). + * + * <p>If there are already widgets in this panel when the insets are modified, + * the panel is resized and the widgets are moved according to the insets. + * + * @param insets the insets, should not be null + * @return this panel + */ + public WPanelWithInsets setInsets(Insets insets) { + Insets old = this.insets; + this.insets = Objects.requireNonNull(insets, "insets"); + + setSize(getWidth() - old.left() - old.right(), getHeight() - old.top() - old.bottom()); + + for (WWidget child : children) { + child.setLocation(child.getX() - old.left() + insets.left(), child.getY() - old.top() + insets.top()); + expandToFit(child, insets); + } + + return this; + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java index 3c7bee7..7f18d5a 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java @@ -2,14 +2,10 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.widget.data.Insets; -import java.util.Objects; - /** * A panel that positions children by pixel-perfect positions. */ -public class WPlainPanel extends WPanel { - private Insets insets = Insets.NONE; - +public class WPlainPanel extends WPanelWithInsets { /** * Adds a new widget to this panel. * @@ -53,28 +49,8 @@ public class WPlainPanel extends WPanel { //valid = false; } - /** - * Gets the layout insets of this panel. - * - * @return the insets - * @since 4.0.0 - */ - public Insets getInsets() { - return insets; - } - - /** - * Sets the layout insets of this panel. - * - * <p>The insets should be set <i>before</i> adding any widgets - * to this panel. - * - * @param insets the insets, should not be null - * @return this panel - * @since 4.0.0 - */ public WPlainPanel setInsets(Insets insets) { - this.insets = Objects.requireNonNull(insets, "insets"); + super.setInsets(insets); return this; } } |