101 lines
2.8 KiB
C#
101 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class InventoryScreen : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private VisualTreeAsset uiInventoryAsset;
|
|
|
|
[SerializeField]
|
|
private VisualTreeAsset uiItemAsset;
|
|
|
|
private UIDocument uiDocument;
|
|
|
|
private VisualElement root;
|
|
private VisualElement sourceInventory;
|
|
private VisualElement targetInventory;
|
|
|
|
// private InventoryHolder inventoryHolder;
|
|
// private InventoryHolder targetInventoryHolder;
|
|
|
|
void OnEnable()
|
|
{
|
|
uiDocument = GetComponent<UIDocument>();
|
|
|
|
root = uiDocument.rootVisualElement;
|
|
sourceInventory = root.Q("SourceInventory");
|
|
sourceInventory.style.display = DisplayStyle.None;
|
|
targetInventory = root.Q("TargetInventory");
|
|
targetInventory.style.display = DisplayStyle.None;
|
|
|
|
Hide();
|
|
|
|
root.RegisterCallback<ClickEvent>(OnRootClicked);
|
|
sourceInventory.RegisterCallback<ClickEvent>(evt => evt.StopPropagation());
|
|
targetInventory.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(InventoryHolder sourceInventory, InventoryHolder targetInventory = null)
|
|
{
|
|
ShowInventory(this.sourceInventory, sourceInventory);
|
|
ShowInventory(this.targetInventory, targetInventory);
|
|
|
|
Show();
|
|
}
|
|
|
|
public void ShowInventory(VisualElement inventory, InventoryHolder holder)
|
|
{
|
|
inventory.Clear();
|
|
|
|
if (holder == null)
|
|
{
|
|
inventory.style.display = DisplayStyle.None;
|
|
return;
|
|
}
|
|
inventory.style.display = DisplayStyle.Flex;
|
|
|
|
uiInventoryAsset.CloneTree(inventory);
|
|
|
|
// TemplateContainer inventory = uiInventoryAsset.Instantiate();
|
|
// container.Add(inventory);
|
|
|
|
var itemList = inventory.Q("Inventory").Q("ContainerScroll");
|
|
var headerLabel = inventory.Q<Label>("Header");
|
|
var footerLabel = inventory.Q<Label>("Footer");
|
|
|
|
itemList.Clear();
|
|
|
|
foreach (var item in holder.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 = holder.Name;
|
|
footerLabel.text = $"Свободно: {holder.Inventory.Free}";
|
|
}
|
|
}
|