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(); } private void OnSwapOpened(IBasicItemContainer obj) { UXService.Entry(); if (obj is WorldItemContainer container) { otherLabel.text = $"{container.Name}\t{(container.AllowStorage ? "存储" : "不可存储")}\t{(container.AllowLoot ? "可拾取" : "不可拾取")}"; } ReBuild(obj); } private void OnInventory(InputAction.CallbackContext obj) { if(obj is not {interaction:PressInteraction,performed:true}) return; UXService.Entry(); swapItem.Close(); } private void OnEscape(InputAction.CallbackContext obj) { if (obj.performed) UXService.Entry(); 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(x => { swapItem.Add(item); }); } foreach (var item in inventory.GetItems()) { var container = selfContainer.Create(itemPrefab); UXUtils.CreateItemContainer(container, item); container.RegisterCallback(x => { swapItem.Remove(item); }); } } } }