From 48a165f113fbfa1d1075665087a40b56a458ce8e Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Fri, 10 Sep 2021 21:00:52 +0300 Subject: Add slot hovering narration --- .../cottonmc/cotton/gui/widget/WItemSlot.java | 30 +++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) 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 a4788a7..9ce37fa 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 @@ -19,6 +19,7 @@ import io.github.cottonmc.cotton.gui.ValidatedSlot; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import io.github.cottonmc.cotton.gui.impl.VisualLogger; import io.github.cottonmc.cotton.gui.impl.client.NarrationMessages; +import io.github.cottonmc.cotton.gui.widget.data.InputResult; import io.github.cottonmc.cotton.gui.widget.icon.Icon; import org.jetbrains.annotations.Nullable; @@ -81,10 +82,12 @@ public class WItemSlot extends WWidget { private boolean insertingAllowed = true; private boolean takingAllowed = true; private int focusedSlot = -1; + private int hoveredSlot = -1; private Predicate filter = DEFAULT_FILTER; private final Set listeners = new HashSet<>(); public WItemSlot(Inventory inventory, int startIndex, int slotsWide, int slotsHigh, boolean big) { + this(); this.inventory = inventory; this.startIndex = startIndex; this.slotsWide = slotsWide; @@ -93,7 +96,12 @@ public class WItemSlot extends WWidget { //this.ltr = ltr; } - private WItemSlot() {} + private WItemSlot() { + hoveredProperty().addListener((property, from, to) -> { + assert to != null; + if (!to) hoveredSlot = -1; + }); + } public static WItemSlot of(Inventory inventory, int index) { WItemSlot w = new WItemSlot(); @@ -425,6 +433,14 @@ public class WItemSlot extends WWidget { } } + @Override + public InputResult onMouseMove(int x, int y) { + int slotX = x / 18; + int slotY = y / 18; + hoveredSlot = slotX + slotY * slotsWide; + return InputResult.PROCESSED; + } + @Override public void onHidden() { super.onHidden(); @@ -443,15 +459,17 @@ public class WItemSlot extends WWidget { @Environment(EnvType.CLIENT) @Override public void addNarrations(NarrationMessageBuilder builder) { + List parts = new ArrayList<>(); + Text extra = getExtraNarrationMessage(); + if (extra != null) parts.add(extra); + if (focusedSlot >= 0) { - List parts = new ArrayList<>(); - Text extra = getExtraNarrationMessage(); - if (extra != null) parts.add(extra); parts.add(new TranslatableText(NarrationMessages.ITEM_SLOT_TITLE_KEY, focusedSlot + 1, slotsWide * slotsHigh)); - builder.put(NarrationPart.TITLE, parts.toArray(new Text[0])); + } else if (hoveredSlot >= 0) { + parts.add(new TranslatableText(NarrationMessages.ITEM_SLOT_TITLE_KEY, hoveredSlot + 1, slotsWide * slotsHigh)); } - // TODO: hovered slot + builder.put(NarrationPart.TITLE, parts.toArray(new Text[0])); } @Nullable -- cgit