From 48254df84d4e8f58f37b7bbb1ef3cb81446f6df4 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Wed, 20 May 2020 21:48:25 +0300 Subject: Add Inventory label for player inventory panels Also allows custom label widgets that can be *any* widget. --- .../io/github/cottonmc/test/TestController.java | 2 +- .../cotton/gui/CottonInventoryController.java | 22 ++++++++++++ .../cotton/gui/widget/WPlayerInvPanel.java | 40 ++++++++++++++++++++-- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/TestController.java b/GuiTest/src/main/java/io/github/cottonmc/test/TestController.java index 43a0a6f..1d3c955 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/TestController.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/TestController.java @@ -22,7 +22,7 @@ public class TestController extends CottonInventoryController { root.add(button, 0, 3, 5, 1); - root.add(new WPlayerInvPanel(playerInventory), 0, 5); + root.add(createPlayerInventoryPanel(), 0, 5); this.getRootPanel().validate(this); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java b/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java index 272d251..cbefb7a 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java @@ -335,6 +335,28 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr return new WPlayerInvPanel(this.playerInventory); } + /** + * Creates a player inventory widget from this panel's {@linkplain #playerInventory player inventory}. + * + * @param hasTitle whether the "Inventory" title should be displayed + * @return the created inventory widget + * @since 2.0.0 + */ + public WPlayerInvPanel createPlayerInventoryPanel(boolean hasTitle) { + return new WPlayerInvPanel(this.playerInventory, hasTitle); + } + + /** + * Creates a player inventory widget from this panel's {@linkplain #playerInventory player inventory}. + * + * @param title the inventory title widget + * @return the created inventory widget + * @since 2.0.0 + */ + public WPlayerInvPanel createPlayerInventoryPanel(WWidget title) { + return new WPlayerInvPanel(this.playerInventory, title); + } + /** * Gets the block inventory at the context. * diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java index 47df88f..198c708 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java @@ -5,6 +5,8 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.entity.player.PlayerInventory; +import javax.annotation.Nullable; + /** * A player inventory widget that has a visually separate hotbar. */ @@ -12,11 +14,45 @@ public class WPlayerInvPanel extends WPlainPanel { private final WItemSlot inv; private final WItemSlot hotbar; + /** + * Constructs a player inventory panel with a title. + * + * @param playerInventory the player inventory + */ public WPlayerInvPanel(PlayerInventory playerInventory) { + this(playerInventory, true); + } + + /** + * Constructs a player inventory panel. + * + * @param playerInventory the player inventory + * @param hasTitle whether there should be an "Inventory" title + * @since 2.0.0 + */ + public WPlayerInvPanel(PlayerInventory playerInventory, boolean hasTitle) { + this(playerInventory, hasTitle ? new WLabel(playerInventory.getDisplayName()) : null); + } + + /** + * Constructs a player inventory panel. + * + * @param playerInventory the player inventory + * @param title the title widget, can be null + * @since 2.0.0 + */ + public WPlayerInvPanel(PlayerInventory playerInventory, @Nullable WWidget title) { + int y = 0; + + if (title != null) { + this.add(title, 0, 0, 9*18, 11); + y += title.getHeight(); + } + inv = WItemSlot.ofPlayerStorage(playerInventory); hotbar = WItemSlot.of(playerInventory, 0, 9, 1); - this.add(inv, 0, 0); - this.add(hotbar, 0, 58); + this.add(inv, 0, y); + this.add(hotbar, 0, y + 58); } /** -- cgit