111 lines
3.7 KiB
C#
111 lines
3.7 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
using DG.Tweening;
|
|
|
|
namespace Ashwild.Inventory
|
|
{
|
|
/// <summary>
|
|
/// The fixed description panel shown beside the inventory — a pure view. When the pointer
|
|
/// enters a filled slot the inventory manager hands it an <see cref="ItemDescriptionView"/> and
|
|
/// it fades its CanvasGroup in, drawing the icon, name, description and (for uses-tracked items)
|
|
/// the duration bar. It never reads ItemData or the inventory itself; it renders what it is given.
|
|
/// </summary>
|
|
[DisallowMultipleComponent]
|
|
public class HoverDescriptionUI : MonoBehaviour
|
|
{
|
|
#region Serialized Fields
|
|
|
|
[Header("References")]
|
|
[SerializeField] private CanvasGroup canvasGroup;
|
|
[SerializeField] private Image iconImage;
|
|
[SerializeField] private TextMeshProUGUI nameText;
|
|
[SerializeField] private TextMeshProUGUI descriptionText;
|
|
|
|
[Header("Duration Bar")]
|
|
[Tooltip("Root object of the duration/uses bar — shown only for items that track uses.")]
|
|
[SerializeField] private GameObject durationBarRoot;
|
|
[Tooltip("Filled image whose fillAmount maps to the remaining uses (Image Type = Filled).")]
|
|
[SerializeField] private Image durationBarFill;
|
|
[Tooltip("Label drawn over the bar, e.g. \"3 / 5\".")]
|
|
[SerializeField] private TextMeshProUGUI durationText;
|
|
|
|
[Header("Animation")]
|
|
[SerializeField] private float fadeDuration = 0.15f;
|
|
|
|
#endregion
|
|
|
|
#region State
|
|
|
|
private Tweener fadeTween;
|
|
|
|
#endregion
|
|
|
|
#region Unity Lifecycle
|
|
|
|
/// <summary>
|
|
/// Starts hidden so the panel only appears once a slot is actually hovered.
|
|
/// </summary>
|
|
private void Awake() => SetHidden();
|
|
|
|
/// <summary>
|
|
/// Kills the fade tween so it never targets the CanvasGroup after teardown.
|
|
/// </summary>
|
|
private void OnDestroy() => fadeTween?.Kill();
|
|
|
|
#endregion
|
|
|
|
#region Public API
|
|
|
|
/// <summary>
|
|
/// Draws the payload and fades the panel in. Called by the inventory manager on hover enter.
|
|
/// </summary>
|
|
public void Show(ItemDescriptionView view)
|
|
{
|
|
if (iconImage != null)
|
|
{
|
|
iconImage.sprite = view.Icon;
|
|
iconImage.enabled = view.Icon != null;
|
|
}
|
|
if (nameText != null)
|
|
nameText.text = view.Name;
|
|
if (descriptionText != null)
|
|
descriptionText.text = view.Description;
|
|
|
|
if (durationBarRoot != null)
|
|
durationBarRoot.SetActive(view.HasDuration);
|
|
if (view.HasDuration)
|
|
{
|
|
if (durationBarFill != null)
|
|
durationBarFill.fillAmount = view.DurationFill;
|
|
if (durationText != null)
|
|
durationText.text = view.DurationText;
|
|
}
|
|
|
|
fadeTween?.Kill();
|
|
fadeTween = canvasGroup.DOFade(1f, fadeDuration).SetUpdate(true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fades the panel out via its CanvasGroup only — the GameObject stays active so it can be
|
|
/// shown again instantly. Called on hover exit / inventory close.
|
|
/// </summary>
|
|
public void Hide()
|
|
{
|
|
fadeTween?.Kill();
|
|
fadeTween = canvasGroup.DOFade(0f, fadeDuration).SetUpdate(true);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Internal Helpers
|
|
|
|
/// <summary>
|
|
/// Instantly parks the panel invisible via the CanvasGroup, without deactivating it.
|
|
/// </summary>
|
|
private void SetHidden() => canvasGroup.alpha = 0f;
|
|
|
|
#endregion
|
|
}
|
|
}
|