123 lines
2.7 KiB
C#
123 lines
2.7 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using KitsuneCafe.ItemSystem;
|
|
using KitsuneCafe.SOAP;
|
|
using KitsuneCafe.UI.MVVM;
|
|
using ObservableCollections;
|
|
using R3;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace KitsuneCafe.UI
|
|
{
|
|
public class InventoryScreen : MonoBehaviour
|
|
{
|
|
public const string ItemListIndicatorsName = "item-list-indicator-container";
|
|
public const string ItemListName = "item-list";
|
|
|
|
public const string ItemPreviewName = "item-preview";
|
|
|
|
|
|
public const string ItemDetailsTitleName = "item-details-title-label";
|
|
public const string ItemDetailsContentName = "item-details-content-label";
|
|
|
|
[SerializeField]
|
|
private UIDocument doc;
|
|
|
|
[SerializeField]
|
|
private Inventory inventory;
|
|
|
|
[SerializeField]
|
|
private VisualTreeAsset template;
|
|
|
|
[SerializeField]
|
|
private int displayCount = 5;
|
|
|
|
[SerializeField]
|
|
private ItemValue selectedItem;
|
|
|
|
[SerializeField]
|
|
private FloatEvent inventoryInput;
|
|
|
|
[SerializeField]
|
|
private Vector2Value itemPreviewDelta;
|
|
|
|
[SerializeField]
|
|
private FloatValue itemZoom;
|
|
|
|
[SerializeField]
|
|
private ActionMapValue actionMap;
|
|
|
|
private VisualElement root => doc.rootVisualElement;
|
|
|
|
private List<VisualElement> indicators;
|
|
private RecyclerView itemList;
|
|
private VisualElement itemPreview;
|
|
|
|
private void OnValidate()
|
|
{
|
|
if (doc == null)
|
|
{
|
|
doc = GetComponent<UIDocument>();
|
|
}
|
|
}
|
|
|
|
private void Reset()
|
|
{
|
|
OnValidate();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
root.visible = false;
|
|
|
|
inventoryInput.AsObservable()
|
|
.Subscribe(_ =>
|
|
{
|
|
root.visible = !root.visible;
|
|
actionMap.Value = root.visible ? Input.ActionMap.UI : Input.ActionMap.Player;
|
|
});
|
|
|
|
CreateItemList();
|
|
var preview = root.Q<PreviewView>();
|
|
|
|
preview.RegisterCallback<RotateEvent>(evt =>
|
|
{
|
|
itemPreviewDelta.Value = evt.Delta;
|
|
});
|
|
|
|
preview.RegisterCallback<ZoomEvent>(evt =>
|
|
{
|
|
itemZoom.Value += evt.Amount;
|
|
});
|
|
}
|
|
|
|
private void CreateItemList()
|
|
{
|
|
var indicatorContainer = root.Q<VisualElement>(ItemListIndicatorsName);
|
|
indicators = indicatorContainer.Children().ToList();
|
|
|
|
itemList = root.Q<RecyclerView>();
|
|
itemList.ItemSource = inventory;
|
|
|
|
itemList.RegisterCallback<SelectEvent>(evt =>
|
|
{
|
|
if (evt.SelectedItem == null)
|
|
{
|
|
selectedItem.Value = null;
|
|
}
|
|
else
|
|
{
|
|
selectedItem.Value = ((InventoryItem)evt.SelectedItem).Item;
|
|
}
|
|
});
|
|
|
|
inventory.CollectionChanged += RebindItems;
|
|
}
|
|
|
|
private void RebindItems(in NotifyCollectionChangedEventArgs<InventoryItem> e)
|
|
{
|
|
itemList.Rebind();
|
|
}
|
|
}
|
|
}
|