Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to rotate items in the InventoryHud #24

Merged
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