143 lines
3.2 KiB
C#
143 lines
3.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using KitsuneCafe.ItemSystem;
|
|
using KitsuneCafe.SOAP;
|
|
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 focusedItem;
|
|
|
|
[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 ToggleVisibility()
|
|
{
|
|
root.visible = !root.visible;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
root.visible = false;
|
|
inventoryInput.AsObservable()
|
|
.Subscribe(_ =>
|
|
{
|
|
ToggleVisibility();
|
|
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<FocusEvent>(evt =>
|
|
{
|
|
TrySetValue(focusedItem, evt.FocusedIndex);
|
|
});
|
|
|
|
itemList.RegisterCallback<NavigationSubmitEvent>(evt =>
|
|
{
|
|
TrySetValue(selectedItem, itemList.FocusedIndex);
|
|
});
|
|
|
|
itemList.RegisterCallback<NavigationCancelEvent>(evt =>
|
|
{
|
|
selectedItem.Value = null;
|
|
});
|
|
|
|
inventory.CollectionChanged += RebindItems;
|
|
}
|
|
|
|
private void TrySetValue(ReactiveValue<Item> value, int index)
|
|
{
|
|
if (inventory.TryGetValue(index, out var item))
|
|
{
|
|
value.Value = item.Item;
|
|
}
|
|
else
|
|
{
|
|
value.Value = null;
|
|
}
|
|
}
|
|
|
|
private void RebindItems(in NotifyCollectionChangedEventArgs<InventoryItem> e)
|
|
{
|
|
itemList.Rebind();
|
|
}
|
|
}
|
|
}
|