Files
Cielonos/Assets/Scripts/MainGame/UI/PlayerUI/MainGamePages/Common/ItemDetailPanel.cs
2026-05-10 11:47:55 -04:00

119 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Cielonos.MainGame.Characters.Inventory;
using Sirenix.OdinInspector;
using SLSUtilities.General;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace SLSUtilities.UI
{
/// <summary>
/// 通用物品详情面板,显示物品图标、名称、稀有度、描述、标签等信息。
/// 可被不同的 UIPage 复用(机械台、商店、背包检视等)。
/// </summary>
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;
/// <summary>当前显示的物品。</summary>
public ItemBase CurrentItem => currentItem;
/// <summary>
/// 使用指定物品的数据填充详情面板并显示。
/// </summary>
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();
}
/// <summary>清空面板内容并隐藏。</summary>
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<TMP_Text>();
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);
}
}
}
}