From a8a2d36a9bb32fe15a391a65666717ed26e0eb70 Mon Sep 17 00:00:00 2001 From: frqnny <45723631+frqnny@users.noreply.github.com> Date: Tue, 22 Jun 2021 12:19:07 -0400 Subject: Add Icon API to WItemSlot (#119) * Add Icon API to WItemSlot * Fix checkstyle * Clean up test description --- .../io/github/cottonmc/test/TestDescription.java | 4 +++ .../main/resources/assets/libgui-test/saddle.png | Bin 0 -> 179 bytes .../cottonmc/cotton/gui/widget/WItemSlot.java | 28 +++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 GuiTest/src/main/resources/assets/libgui-test/saddle.png diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java b/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java index e6c3564..ce579ae 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java @@ -4,6 +4,8 @@ import io.github.cottonmc.cotton.gui.SyncedGuiDescription; import io.github.cottonmc.cotton.gui.networking.NetworkSide; import io.github.cottonmc.cotton.gui.networking.ScreenNetworking; import io.github.cottonmc.cotton.gui.widget.*; +import io.github.cottonmc.cotton.gui.widget.icon.TextureIcon; + import net.minecraft.entity.player.PlayerInventory; import net.minecraft.screen.ScreenHandlerContext; import net.minecraft.screen.ScreenHandlerType; @@ -38,6 +40,8 @@ public class TestDescription extends SyncedGuiDescription { root.add(new WLabel(new LiteralText("Large slot:")), 0, 9); root.add(WItemSlot.outputOf(blockInventory, 0), 4, 9); + root.add(WItemSlot.of(blockInventory, 7).setIcon(new TextureIcon(new Identifier("libgui-test", "saddle.png"))), 7, 9); + root.add(createPlayerInventoryPanel(), 0, 11); System.out.println(root.toString()); diff --git a/GuiTest/src/main/resources/assets/libgui-test/saddle.png b/GuiTest/src/main/resources/assets/libgui-test/saddle.png new file mode 100644 index 0000000..84394b9 Binary files /dev/null and b/GuiTest/src/main/resources/assets/libgui-test/saddle.png differ diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java index 1667bee..e1f0bce 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java @@ -12,6 +12,7 @@ import net.minecraft.screen.slot.SlotActionType; import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.ValidatedSlot; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; +import io.github.cottonmc.cotton.gui.widget.icon.Icon; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; @@ -62,6 +63,8 @@ public class WItemSlot extends WWidget { @Nullable @Environment(EnvType.CLIENT) private BackgroundPainter backgroundPainter = null; + @Nullable + private Icon icon = null; private Inventory inventory; private int startIndex = 0; private int slotsWide = 1; @@ -148,6 +151,27 @@ public class WItemSlot extends WWidget { return big; } + /** + * Sets the icon to this slot. Can be used for labeling slots for certain activities. + * + * @param icon the icon + * @since 4.0.0 + */ + public WItemSlot setIcon(@Nullable Icon icon) { + this.icon = icon; + return this; + } + + /** + * + * @return icon if set, otherwise null + * @since 4.0.0 + */ + @Nullable + public Icon getIcon() { + return this.icon; + } + /** * Returns true if the contents of this {@code WItemSlot} can be modified by players. * @@ -330,6 +354,10 @@ public class WItemSlot extends WWidget { if (backgroundPainter != null) { backgroundPainter.paintBackground(matrices, x, y, this); } + + if (icon != null) { + icon.paint(matrices, x + 1, y + 1, 16); + } } @Nullable -- cgit