BITFALL/Assets/Artists/Scripts/UX/UXInventorySwap.cs

151 lines
3.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using BITFALL.Entities.Inventory;
using BITFALL.UX;
using BITKit;
using BITKit.Entities;
using BITKit.Entities.Player;
using BITKit.UX;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Composites;
using UnityEngine.InputSystem.Interactions;
using UnityEngine.UIElements;
using UXUtils = BITFALL.UX.UXUtils;
namespace BITFALL.Items
{
public sealed class UXInventorySwap : UIToolKitPanel
{
[Header(Constant.Header.Input)]
[SerializeField] private InputActionReference escapeAction;
[SerializeField] private InputActionReference inventoryAction;
[Header(Constant.Header.Services)]
[SerializeReference,SubclassSelector] private IPlayerService playerService;
[Header(Constant.Header.Prefabs)]
[SerializeField] private VisualTreeAsset itemPrefab;
[Inject]
private IEntitySwapItem swapItem;
[Inject]
private IEntityInventory inventory;
[UXBindPath("self-container")]
private VisualElement selfContainer;
[UXBindPath("other-container")]
private VisualElement otherContainer;
[UXBindPath("other-label")]
private Label otherLabel;
protected override void Start()
{
base.Start();
BITKit.UX.UXUtils.Inject(this);
}
protected override void OnPanelEntry()
{
base.OnPanelEntry();
inputActionGroup.RegisterCallback(escapeAction, OnEscape);
inputActionGroup.RegisterCallback(inventoryAction, OnInventory);
}
protected override void OnPanelExit()
{
base.OnPanelExit();
inputActionGroup.UnRegisterCallback(escapeAction, OnEscape);
inputActionGroup.UnRegisterCallback(inventoryAction, OnInventory);
}
protected override void OnEnable()
{
base.OnEnable();
playerService.OnPlayerInitialized += OnPlayerInitialized;
}
protected override void OnDisable()
{
base.OnDisable();
playerService.OnPlayerInitialized -= OnPlayerInitialized;
}
private void OnPlayerInitialized(Entity obj)
{
obj.Inject(this);
swapItem.OnSwapOpened += OnSwapOpened;
swapItem.OnSwapClosed += OnSwapClosed;
inventory.OnAdd+=_=>ReBuild();
inventory.OnRemove+=_=>ReBuild();
inventory.OnRebuild+=_=>ReBuild();
}
private void OnSwapClosed(IBasicItemContainer obj)
{
UXService.Entry<UXHud>();
}
private void OnSwapOpened(IBasicItemContainer obj)
{
UXService.Entry<UXInventorySwap>();
if (obj is WorldItemContainer container)
{
otherLabel.text = $"{container.Name}\t{(container.AllowStorage ? "" : "<color=yellow></color>")}\t{(container.AllowLoot ? "" : "<color=yellow></color>")}";
}
ReBuild(obj);
}
private void OnInventory(InputAction.CallbackContext obj)
{
if(obj is not {interaction:PressInteraction,performed:true}) return;
UXService.Entry<UXHud>();
swapItem.Close();
}
private void OnEscape(InputAction.CallbackContext obj)
{
if (obj.performed)
UXService.Entry<UXHud>();
swapItem.Close();
}
private async void ReBuild()
{
await UniTask.NextFrame();
if(swapItem.TryGetCurrentContainer(out var container))
ReBuild(container);
}
private void ReBuild(IBasicItemContainer other)
{
otherContainer.Clear();
selfContainer.Clear();
foreach (var item in other.GetItems())
{
var container = otherContainer.Create(itemPrefab);
UXUtils.CreateItemContainer(container, item);
container.RegisterCallback<MouseDownEvent>(x =>
{
swapItem.Add(item);
});
}
foreach (var item in inventory.GetItems())
{
var container = selfContainer.Create(itemPrefab);
UXUtils.CreateItemContainer(container, item);
container.RegisterCallback<MouseDownEvent>(x =>
{
swapItem.Remove(item);
});
}
}
}
}