using System.Collections.Generic; using BITFALL.Entities.Equipment; using BITFALL.Player.Inventory; using UnityEngine; using UnityEngine.UIElements; using UnityEngine.InputSystem; using BITKit; using BITKit.UX; using BITKit.Entities; using BITKit.Entities.Player; using UnityEngine.AddressableAssets; namespace BITFALL.UX { public class UXInventory : UIToolKitPanel { [Header(Constant.Header.Input)] public InputActionReference inventoryAction; public InputActionReference returnAction; public InputActionGroup inputActionGroup = new(); [Header(Constant.Header.Components)] public UXBar weightBar; [Header(Constant.Header.Providers)] [SerializeReference, SubclassSelector] private IPlayerService playerService; [Header(Constant.Header.Components)] public UXBuilder builder; [Header(Constant.Header.InternalVariables)] private readonly Dictionary itemContainers = new(); private readonly Dictionary equipContainers = new(); private readonly IntervalUpdate returnInterval = new(0.1f); [Inject] private IBasicItemContainer inventory; [Inject] private IPlayerInventory _playerInventory; [Inject] private IEntityEquipmentContainer equipContainer; private IEntity _entity; protected override async void Awake() { base.Awake(); inputActionGroup.RegisterCallback(returnAction, OnReturn); inputActionGroup.RegisterCallback(inventoryAction, OnInventory); foreach (var x in await ReflectionHelper.GetTypes()) { var element = document.rootVisualElement.Q(x.Name); if (element is null) continue; UXContainer uxContainer = new(element); equipContainers.Add(x.Name, uxContainer); element.RegisterCallback(mouseEvent => { if(_entity is null) return; switch (mouseEvent.button) { case 0: break; case 1: equipContainer.TryDeEquip(System.Activator.CreateInstance(x) as IEquipmentSlot); break; } }); } } protected override void OnEntryOrExit(bool isEntry) { inputActionGroup.allowInput.SetElements(this, isEntry); returnInterval.Reset(); } protected override void OnEnable() { base.OnEnable(); playerService.OnPlayerInitialized += OnPlayerInitializedLocalPlayer; playerService.OnPlayerDisposed += OnPlayerDisposed; } private void OnPlayerDisposed(Entity obj) { inventory = null; equipContainer = null; _playerInventory = null; } protected override void OnDisable() { base.OnDisable(); playerService.OnPlayerDisposed -= OnPlayerDisposed; playerService.OnPlayerInitialized -= OnPlayerInitializedLocalPlayer; } private void OnPlayerInitializedLocalPlayer(IEntity entity) { entity.Inject(this); itemContainers.Clear(); equipContainers.Clear(); builder.Clear(); var weighted = entity.Get(); weighted.OnWeighted += OnWeighted; equipContainer.OnEquip += OnEquip; equipContainer.OnDeEquip += DeEquip; inventory.OnAdd += OnAdd; inventory.OnRemove += OnRemove; inventory.OnUsed += OnRemove; inventory.OnSet += OnSet; _entity = entity; } private void OnInventory(InputAction.CallbackContext context) { if (context.JustPressed()) UXService.Entry(); } private static void OnReturn(InputAction.CallbackContext context) { if (context.JustPressed()) UXService.Entry(); } public void OnWeighted(double current, double max) { var value = current is 0 ? 0 : (current / max); weightBar.SetDirect((float) value,$"{current}/{max}"); } private void OnAdd(IBasicItem item) { var asset = Addressables.LoadAssetAsync(item.AddressablePath).WaitForCompletion(); var uxContainer = builder.BuildAsContainer(); uxContainer.icon.style.backgroundImage = asset.SquareIcon; uxContainer.contextLabel.text = asset.Name; uxContainer.button.clicked += () => { UseItem(item); }; uxContainer.visualElement.Q