60 lines
2.3 KiB
C#
60 lines
2.3 KiB
C#
using UnityEngine;
|
|
|
|
namespace Ashwild.Inventory
|
|
{
|
|
/// <summary>
|
|
/// The view payload for the hover description panel: exactly what the panel renders (icon,
|
|
/// name, description, and an optional duration/uses bar), nothing more. The inventory manager
|
|
/// builds one of these from a hovered slot and pushes it into the view, so the panel never
|
|
/// touches ItemData or the inventory model — it just draws what it is handed.
|
|
/// </summary>
|
|
public readonly struct ItemDescriptionView
|
|
{
|
|
public readonly Sprite Icon;
|
|
public readonly string Name;
|
|
public readonly string Description;
|
|
|
|
/// <summary>
|
|
/// Whether the hovered item tracks uses/durability and should show its duration bar.
|
|
/// </summary>
|
|
public readonly bool HasDuration;
|
|
|
|
/// <summary>
|
|
/// Remaining uses as a 0..1 fill for the bar's Filled image.
|
|
/// </summary>
|
|
public readonly float DurationFill;
|
|
|
|
/// <summary>
|
|
/// The remaining/max label drawn over the bar, e.g. "3 / 5".
|
|
/// </summary>
|
|
public readonly string DurationText;
|
|
|
|
public ItemDescriptionView(Sprite icon, string name, string description,
|
|
bool hasDuration, float durationFill, string durationText)
|
|
{
|
|
Icon = icon;
|
|
Name = name;
|
|
Description = description;
|
|
HasDuration = hasDuration;
|
|
DurationFill = durationFill;
|
|
DurationText = durationText;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Builds the payload for a hovered slot, including the uses bar for items that track
|
|
/// durability. Shared by the inventory grid and the hotbar so both describe an item
|
|
/// identically — they used to each build this by hand and could drift apart.
|
|
/// </summary>
|
|
public static ItemDescriptionView From(InventorySlot slot)
|
|
{
|
|
ItemData item = slot.ItemData;
|
|
bool hasDuration = item.HasUses;
|
|
float fill = hasDuration && item.MaxUses > 0 ? (float)slot.CurrentUses / item.MaxUses : 0f;
|
|
string durationText = hasDuration ? $"{slot.CurrentUses} / {item.MaxUses}" : string.Empty;
|
|
|
|
return new ItemDescriptionView(item.Icon, item.ItemName, item.Description,
|
|
hasDuration, fill, durationText);
|
|
}
|
|
}
|
|
}
|