98 lines
3.6 KiB
C#
98 lines
3.6 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;
|
|
}
|
|
|
|
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")
|
|
};
|
|
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");
|
|
|
|
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;
|
|
}
|
|
|
|
ItemActor itemActor = m_Inventory.GetItem(slotIndex);
|
|
|
|
if (itemActor == null) {
|
|
m_InventorySlots[slotIndex].Icon.style.backgroundImage = null;
|
|
m_InventorySlots[slotIndex].Icon.style.display = DisplayStyle.None;
|
|
} else {
|
|
m_InventorySlots[slotIndex].Icon.style.backgroundImage = new StyleBackground(itemActor.Config.icon);
|
|
m_InventorySlots[slotIndex].Icon.style.display = DisplayStyle.Flex;
|
|
}
|
|
}
|
|
}
|
|
} |