Files
jelito/Assets/jelycho/Code/Player/HUD/PlayerInventoryUI.cs
2025-07-11 14:40:23 +02:00

111 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using RebootReality.jelycho.Items;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UIElements;
using Logger = RebootKit.Engine.Foundation.Logger;
namespace RebootReality.jelycho.Player.HUD {
public class PlayerInventoryUI : MonoBehaviour {
static readonly Logger s_Logger = new Logger(nameof(PlayerInventoryUI));
[SerializeField] VisualTreeAsset m_InventorySlotTemplate;
Inventory m_Inventory;
VisualElement m_Root;
struct InventorySlot {
public VisualElement Root;
public VisualElement Slot;
public VisualElement Icon;
public Label QuantityLabel;
}
readonly List<InventorySlot> m_InventorySlots = new List<InventorySlot>();
public void Configure(VisualElement root, Inventory inventory) {
CleanUp();
m_Root = root;
m_Inventory = inventory;
m_Inventory.OnSlotUpdated += OnSlotUpdated;
m_Root.Clear();
for (int i = 0; i < m_Inventory.SlotsCount; i++) {
VisualElement slotRoot = m_InventorySlotTemplate.CloneTree();
m_Root.Add(slotRoot);
slotRoot .style.width = new StyleLength(new Length(72, LengthUnit.Pixel));
slotRoot.style.height = new StyleLength(new Length(72, LengthUnit.Pixel));
InventorySlot slot = new InventorySlot {
Root = slotRoot,
Slot = slotRoot.Q<VisualElement>("player-hud__inventory-slot"),
Icon = slotRoot.Q<VisualElement>("player-hud__inventory-slot-icon"),
QuantityLabel = slotRoot.Q<Label>("player-hud__inventory-slot-quantity"),
};
Assert.IsNotNull(slot.Root, "Slot root cannot be null");
Assert.IsNotNull(slot.Slot, "Slot element cannot be null");
Assert.IsNotNull(slot.Icon, "Slot icon cannot be null");
Assert.IsNotNull(slot.QuantityLabel, "Slot quantity label cannot be null");
m_InventorySlots.Add(slot);
OnSlotUpdated(i);
}
}
public void CleanUp() {
if (m_Root != null) {
m_Root.Clear();
m_Root = null;
}
if (m_Inventory != null) {
m_Inventory.OnSlotUpdated -= OnSlotUpdated;
m_Inventory = null;
}
m_InventorySlots.Clear();
}
public void SetSelectedSlot(int slotIndex) {
if (slotIndex < 0 || slotIndex >= m_InventorySlots.Count) {
s_Logger.Error($"Invalid slot index: {slotIndex}. Inventory has {m_InventorySlots.Count} slots.");
return;
}
for (int i = 0; i < m_InventorySlots.Count; i++) {
m_InventorySlots[i].Slot.EnableInClassList("inventory-slot-selected", i == slotIndex);
}
}
void OnSlotUpdated(int slotIndex) {
if (slotIndex < 0 || slotIndex >= m_InventorySlots.Count) {
s_Logger.Error($"Invalid slot index: {slotIndex}. Inventory has {m_InventorySlots.Count} slots.");
return;
}
int quantity = m_Inventory.GetQuantity(slotIndex);
if (quantity <= 0) {
m_InventorySlots[slotIndex].QuantityLabel.style.display = DisplayStyle.None;
m_InventorySlots[slotIndex].Icon.style.backgroundImage = null;
m_InventorySlots[slotIndex].Icon.style.display = DisplayStyle.None;
} else {
m_InventorySlots[slotIndex].QuantityLabel.style.display = DisplayStyle.Flex;
m_InventorySlots[slotIndex].QuantityLabel.text = quantity.ToString();
ItemActor itemActor = m_Inventory.GetFirstItem(slotIndex);
if (itemActor != null) {
m_InventorySlots[slotIndex].Icon.style.backgroundImage = new StyleBackground(itemActor.Config.icon);
m_InventorySlots[slotIndex].Icon.style.display = DisplayStyle.Flex;
} else {
m_InventorySlots[slotIndex].Icon.style.backgroundImage = null;
m_InventorySlots[slotIndex].Icon.style.display = DisplayStyle.None;
}
}
}
}
}