Skip to content

Commit

Permalink
feat: allow to rotate items in the InventoryHud (#24)
Browse files Browse the repository at this point in the history
Co-authored-by: jdrueckert <jd.rueckert@googlemail.com>
Co-authored-by: Tobias Nett <skaldarnar@googlemail.com>
  • Loading branch information
3 people authored Jul 20, 2020
1 parent e4583f3 commit df5397e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<EntityRef>() {
@Override
public EntityRef get() {
Expand Down Expand Up @@ -69,4 +79,22 @@ public Boolean get() {
return component != null && component.slot == slot;
}
}

private class TargetSlotBinding extends ReadOnlyBinding<Integer> {

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,6 +41,7 @@
*/
public abstract class ItemCell extends CoreWidget {
protected ItemIcon icon = new ItemIcon();
@LayoutConfig
private Binding<Boolean> selected = new DefaultBinding<>(false);

public ItemCell() {
Expand Down

0 comments on commit df5397e

Please sign in to comment.