MonsterShop/Assets/Scripts/UI/InventoryScreen.cs

77 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;
public class InventoryScreen : MonoBehaviour
{
[SerializeField]
private VisualTreeAsset uiItemAsset;
private UIDocument uiDocument;
private VisualElement root;
private VisualElement itemList;
private Label headerLabel;
private Label footerLabel;
private IInventoryHolder inventoryHolder;
private IInventoryHolder targetInventoryHolder;
void OnEnable()
{
uiDocument = GetComponent<UIDocument>();
root = uiDocument.rootVisualElement;
var inventory = root.Q("Inventory");
root.style.display = DisplayStyle.None;
itemList = inventory.Q("Inventory").Q("ContainerScroll");
headerLabel = inventory.Q<Label>("Header");
footerLabel = inventory.Q<Label>("Footer");
root.RegisterCallback<ClickEvent>(OnRootClicked);
inventory.RegisterCallback<ClickEvent>(evt => evt.StopPropagation());
}
private void OnRootClicked(ClickEvent evt)
{
Debug.Log("Root Clicked");
Hide();
}
private void Show()
{
root.style.display = DisplayStyle.Flex;
}
private void Hide()
{
root.style.display = DisplayStyle.None;
}
public void Display(IInventoryHolder inventory, IInventoryHolder targetInventory = null)
{
this.inventoryHolder = inventory;
this.targetInventoryHolder = inventory;
itemList.Clear();
foreach (var item in inventory.Inventory.Items)
{
var child = uiItemAsset.Instantiate();
// child.AddToClassList(".item");
child.Q<Label>("Name").text = item.Key;
child.Q<Label>("Quantity").text = item.Value.ToString();
itemList.Add(child);
}
headerLabel.text = inventory.Name;
footerLabel.text = $"Свободно: {inventory.Inventory.Free}";
Show();
}
}