From df5397e468591058660d6956b9d63d427a2b827e Mon Sep 17 00:00:00 2001 From: Stefania Makrygiannaki Date: Mon, 20 Jul 2020 22:52:21 +0300 Subject: [PATCH] feat: allow to rotate items in the InventoryHud (#24) Co-authored-by: jdrueckert Co-authored-by: Tobias Nett --- .../nui/layers/hud/InventoryHud.java | 30 ++++++++++++++++++- .../nui/layers/ingame/inventory/ItemCell.java | 2 ++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/terasology/rendering/nui/layers/hud/InventoryHud.java b/src/main/java/org/terasology/rendering/nui/layers/hud/InventoryHud.java index e19b0a5170..32f124c214 100644 --- a/src/main/java/org/terasology/rendering/nui/layers/hud/InventoryHud.java +++ b/src/main/java/org/terasology/rendering/nui/layers/hud/InventoryHud.java @@ -20,6 +20,7 @@ import org.terasology.logic.inventory.SelectedInventorySlotComponent; import org.terasology.logic.players.LocalPlayer; import org.terasology.registry.In; +import org.terasology.rendering.nui.LayoutConfig; import org.terasology.rendering.nui.databinding.ReadOnlyBinding; import org.terasology.rendering.nui.layers.ingame.inventory.InventoryCell; @@ -33,10 +34,19 @@ public class InventoryHud extends CoreHudWidget { private UICrosshair crosshair; + // Set "true" to use the rotating style quickslot; set "false" to get the default style quickslot + @LayoutConfig + private boolean rotateItems = false; + @Override public void initialise() { for (InventoryCell cell : findAll(InventoryCell.class)) { - cell.bindSelected(new SlotSelectedBinding(cell.getTargetSlot(), localPlayer)); + int offset = cell.getTargetSlot(); + if (rotateItems) { + cell.bindTargetSlot(new TargetSlotBinding(offset, localPlayer)); + } else { + cell.bindSelected(new SlotSelectedBinding(offset, localPlayer)); + } cell.bindTargetInventory(new ReadOnlyBinding() { @Override public EntityRef get() { @@ -69,4 +79,22 @@ public Boolean get() { return component != null && component.slot == slot; } } + + private class TargetSlotBinding extends ReadOnlyBinding { + + private int offset; + private LocalPlayer localPlayer; + + public TargetSlotBinding(int targetSlot, LocalPlayer localPlayer) { + this.offset = targetSlot; + this.localPlayer = localPlayer; + } + + @Override + public Integer get() { + SelectedInventorySlotComponent component = + localPlayer.getCharacterEntity().getComponent(SelectedInventorySlotComponent.class); + return (component.slot + offset) % 10; + } + } } diff --git a/src/main/java/org/terasology/rendering/nui/layers/ingame/inventory/ItemCell.java b/src/main/java/org/terasology/rendering/nui/layers/ingame/inventory/ItemCell.java index ead874712d..7a2fa6c631 100644 --- a/src/main/java/org/terasology/rendering/nui/layers/ingame/inventory/ItemCell.java +++ b/src/main/java/org/terasology/rendering/nui/layers/ingame/inventory/ItemCell.java @@ -16,6 +16,7 @@ package org.terasology.rendering.nui.layers.ingame.inventory; import com.google.common.primitives.UnsignedBytes; +import org.terasology.rendering.nui.LayoutConfig; import org.terasology.utilities.Assets; import org.terasology.entitySystem.entity.EntityRef; import org.terasology.logic.common.DisplayNameComponent; @@ -40,6 +41,7 @@ */ public abstract class ItemCell extends CoreWidget { protected ItemIcon icon = new ItemIcon(); + @LayoutConfig private Binding selected = new DefaultBinding<>(false); public ItemCell() {