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

125 lines
3.1 KiB
C#
Raw Normal View History

2023-10-24 23:37:59 +08:00
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.Interactions;
using UnityEngine.UIElements;
2023-11-21 18:05:18 +08:00
using UXUtils = BITFALL.UX.UXUtils;
2023-10-24 23:37:59 +08:00
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;
private VisualElement selfContainer;
private VisualElement otherContainer;
protected override void Start()
{
base.Start();
inputActionGroup.RegisterCallback(escapeAction, OnEscape);
inputActionGroup.RegisterCallback(inventoryAction, OnInventory);
selfContainer = document.rootVisualElement.Q<VisualElement>("self-container");
otherContainer = document.rootVisualElement.Q<VisualElement>("other-container");
}
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>();
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.Q<Button>().clicked += () => swapItem.Add(item);
}
foreach (var item in inventory.GetItems())
{
var container = selfContainer.Create(itemPrefab);
UXUtils.CreateItemContainer(container, item);
container.Q<Button>().clicked += () => swapItem.Remove(item);
}
}
}
}