using Cielonos.MainGame.Inventory;
using Sirenix.OdinInspector;
using SLSUtilities.General;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace SLSUtilities.UI
{
///
/// 通用物品详情面板,显示物品图标、名称、稀有度、描述、标签等信息。
/// 可被不同的 UIPage 复用(机械台、商店、背包检视等)。
///
public class ItemDetailPanel : UIElementBase
{
[Title("Detail Panel References")]
public Image itemIcon;
public Image rarityFrame;
public TMP_Text itemNameText;
public TMP_Text itemTypeText;
public TMP_Text itemDescriptionText;
public RectTransform tagContainer;
public GameObject tagPrefab;
private ItemBase currentItem;
/// 当前显示的物品。
public ItemBase CurrentItem => currentItem;
///
/// 使用指定物品的数据填充详情面板并显示。
///
public void SetItem(ItemBase item)
{
currentItem = item;
if (item == null || item.contentData == null)
{
ClearPanel();
return;
}
ContentData data = item.contentData;
// 图标:主武器使用 rectIcon,其他使用 squareIcon
Sprite icon = data.itemType == ItemType.MainWeapon ? data.rectIcon : data.squareIcon;
if (itemIcon != null)
{
itemIcon.sprite = icon;
itemIcon.enabled = icon != null;
}
// 名称(本地化)
if (itemNameText != null)
{
itemNameText.text = data.displayNameKey.Localize();
}
// 类型
if (itemTypeText != null)
{
itemTypeText.text = data.itemType.ToString();
}
// 描述(本地化)
if (itemDescriptionText != null)
{
itemDescriptionText.text = data.descriptionKey.Localize();
}
// 标签
RefreshTags(data);
Show();
}
/// 清空面板内容并隐藏。
public void ClearPanel()
{
currentItem = null;
if (itemIcon != null) itemIcon.enabled = false;
if (itemNameText != null) itemNameText.text = string.Empty;
if (itemTypeText != null) itemTypeText.text = string.Empty;
if (itemDescriptionText != null) itemDescriptionText.text = string.Empty;
ClearTags();
Hide();
}
private void RefreshTags(ContentData data)
{
ClearTags();
if (tagContainer == null || tagPrefab == null || data.tags == null) return;
foreach (string tag in data.tags)
{
GameObject tagObj = Instantiate(tagPrefab, tagContainer);
TMP_Text tagText = tagObj.GetComponentInChildren();
if (tagText != null)
{
tagText.text = tag;
}
}
}
private void ClearTags()
{
if (tagContainer == null) return;
for (int i = tagContainer.childCount - 1; i >= 0; i--)
{
Destroy(tagContainer.GetChild(i).gameObject);
}
}
}
}