114 lines
3.4 KiB
C#
114 lines
3.4 KiB
C#
using BriarQueen.Data.Identifiers;
|
|
using BriarQueen.Framework.Coordinators.Events;
|
|
using BriarQueen.Framework.Events.UI;
|
|
using BriarQueen.Framework.Managers.Interaction.Data;
|
|
using BriarQueen.Framework.Managers.Player;
|
|
using BriarQueen.Framework.Managers.Player.Data;
|
|
using BriarQueen.Framework.Managers.UI;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using VContainer;
|
|
|
|
namespace BriarQueen.UI.HUD
|
|
{
|
|
/// <summary>
|
|
/// UI slot is now an IInteractable so InteractManager (UI raycast path) can drive hover + click.
|
|
/// No IPointerEnter/Exit, no OnClickEvent subscription.
|
|
/// </summary>
|
|
public class UIInventorySlot : MonoBehaviour, IInteractable
|
|
{
|
|
[SerializeField]
|
|
private Image _slotBase;
|
|
|
|
[SerializeField]
|
|
private Image _selectedVisual;
|
|
|
|
[SerializeField]
|
|
private Image _icon;
|
|
|
|
public ItemDataSo Item;
|
|
private EventCoordinator _eventCoordinator;
|
|
|
|
private InventoryBar _owner;
|
|
|
|
private PlayerManager _playerManager;
|
|
|
|
// Cursor while hovering inventory slots (tweak if you have a “hand” cursor etc.)
|
|
public UICursorService.CursorStyle ApplicableCursorStyle => UICursorService.CursorStyle.UseItem;
|
|
public string InteractableName => Item.ItemName;
|
|
|
|
public UniTask EnterHover()
|
|
{
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
public UniTask ExitHover()
|
|
{
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
public async UniTask OnInteract(ItemDataSo selectedItem = null)
|
|
{
|
|
Debug.Log("[UI Inventory Slot] Interacted");
|
|
|
|
if (Item == null)
|
|
{
|
|
_eventCoordinator.Publish(new DisplayInteractEvent(InteractEventIDs.Get(UIInteractKey.EmptySlot)));
|
|
return;
|
|
}
|
|
|
|
// normal click: selection
|
|
if (selectedItem == null)
|
|
{
|
|
_owner?.OnSlotInteracted(this);
|
|
return;
|
|
}
|
|
|
|
// use selected item on this item (prefer selected item's logic)
|
|
if (selectedItem.Interaction != null)
|
|
{
|
|
var handled = await selectedItem.Interaction.TryUseWith(
|
|
selectedItem, Item, _playerManager, _eventCoordinator);
|
|
|
|
if (handled)
|
|
return;
|
|
}
|
|
|
|
// fallback: allow target to handle it
|
|
if (Item.Interaction != null)
|
|
{
|
|
var handled = await Item.Interaction.TryUseWith(
|
|
Item, selectedItem, _playerManager, _eventCoordinator);
|
|
|
|
if (handled)
|
|
return;
|
|
}
|
|
}
|
|
|
|
[Inject]
|
|
public void Construct(PlayerManager playerManager, EventCoordinator eventCoordinator)
|
|
{
|
|
_playerManager = playerManager;
|
|
_eventCoordinator = eventCoordinator;
|
|
}
|
|
|
|
public void Initialize(InventoryBar owner, ItemDataSo item)
|
|
{
|
|
Debug.Log("Initializing Slot");
|
|
_owner = owner;
|
|
Item = item;
|
|
|
|
if (_icon != null && Item != null)
|
|
_icon.sprite = Item.Icon;
|
|
|
|
SetSelected(false);
|
|
Debug.Log($"Set Slot to {Item}");
|
|
}
|
|
|
|
public void SetSelected(bool selected)
|
|
{
|
|
if (_selectedVisual != null) _selectedVisual.gameObject.SetActive(selected);
|
|
}
|
|
}
|
|
} |