Passion & UI
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using System.Collections.Generic;
|
||||
using Cielonos.MainGame.Inventory;
|
||||
using SLSUtilities.General;
|
||||
using SLSUtilities.UI;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Cielonos.MainGame.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// 单条物品描述条目 UI 组件。
|
||||
/// 渲染管线:Localize → DisplayTextResolver.Resolve → InputGlyphParser.Parse → TMP_Text。
|
||||
/// </summary>
|
||||
public class ItemDescriptionEntry : MonoBehaviour
|
||||
{
|
||||
private const string LocalizationTable = "Items";
|
||||
|
||||
[Tooltip("描述文本,显示 descriptionKey 的本地化内容(含动态数值替换和按键图标解析)。")]
|
||||
public TMP_Text descriptionText;
|
||||
|
||||
/// <summary>
|
||||
/// 使用 <see cref="ItemDescription"/> 的数据填充描述条目。
|
||||
/// 完整渲染管线:本地化 → {key} 占位符替换 → [Token] 按键图标解析。
|
||||
/// </summary>
|
||||
/// <param name="description">描述数据。</param>
|
||||
/// <param name="descriptionArgs">可选的动态值字典,用于替换本地化文本中的 {key} 占位符。</param>
|
||||
public void SetDescription(ItemDescription description, Dictionary<string, string> descriptionArgs = null)
|
||||
{
|
||||
if (description == null)
|
||||
{
|
||||
Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
if (descriptionText == null) return;
|
||||
|
||||
if (string.IsNullOrEmpty(description.descriptionKey))
|
||||
{
|
||||
descriptionText.text = string.Empty;
|
||||
return;
|
||||
}
|
||||
|
||||
string localizedText = description.descriptionKey.Localize(LocalizationTable);
|
||||
localizedText = DisplayTextResolver.Resolve(localizedText, descriptionArgs);
|
||||
descriptionText.text = InputGlyphParser.Parse(localizedText);
|
||||
}
|
||||
|
||||
/// <summary>清空条目内容。</summary>
|
||||
public void Clear()
|
||||
{
|
||||
if (descriptionText != null)
|
||||
{
|
||||
descriptionText.text = string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4bcc59ecfb9425b4a85a1e3ad51c8739
|
||||
@@ -0,0 +1,177 @@
|
||||
using System.Collections.Generic;
|
||||
using Cielonos.MainGame.Inventory;
|
||||
using Cielonos.MainGame.UI;
|
||||
using Sirenix.OdinInspector;
|
||||
using SLSUtilities.General;
|
||||
using TMPro;
|
||||
using UniRx;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Localization.Settings;
|
||||
using UnityEngine.Serialization;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace SLSUtilities.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// 通用物品详情面板,显示物品图标、名称、类型、描述条目、标签等信息。
|
||||
/// 可被不同的 UIPage 复用(机械台、商店、背包检视等)。
|
||||
/// </summary>
|
||||
public class ItemDetailPanel : UIElementBase
|
||||
{
|
||||
[Title("Detail Panel References")]
|
||||
public TMP_Text selectionHintText;
|
||||
//public Image itemIcon;
|
||||
public TMP_Text nameText;
|
||||
public TMP_Text typeRarityText;
|
||||
public TMP_Text institutionText;
|
||||
public RectTransform tagContainer;
|
||||
public GameObject tagPrefab;
|
||||
|
||||
// ─────────────────── 描述条目 ───────────────────
|
||||
|
||||
[Title("Description Entries")]
|
||||
[Tooltip("描述条目的容器,应挂载 VerticalLayoutGroup 以实现垂直排列。")]
|
||||
public RectTransform descriptionContainer;
|
||||
|
||||
[Tooltip("描述条目预制体,需挂载 ItemDescriptionEntry 组件。")]
|
||||
public GameObject descriptionEntryPrefab;
|
||||
|
||||
// ─────────────────── 运行时数据 ───────────────────
|
||||
|
||||
private ItemBase currentItem;
|
||||
private readonly List<ItemDescriptionEntry> activeEntries = new List<ItemDescriptionEntry>();
|
||||
|
||||
/// <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;
|
||||
|
||||
// 图标
|
||||
/*Sprite icon = data.itemIcon;
|
||||
if (itemIcon != null)
|
||||
{
|
||||
itemIcon.sprite = icon;
|
||||
itemIcon.enabled = icon != null;
|
||||
}*/
|
||||
|
||||
// 名称(本地化)
|
||||
nameText.text = data.displayNameKey.Localize("Items");
|
||||
|
||||
// 类型
|
||||
typeRarityText.text = data.itemType.ToString() + " - " + data.itemRarity.ToString();
|
||||
|
||||
//机构
|
||||
institutionText.text = string.Empty;
|
||||
for (var index = 0; index < data.institutions.Count; index++)
|
||||
{
|
||||
var institution = data.institutions[index];
|
||||
string comma = LocalizationSettings.SelectedLocale.Identifier.Code.StartsWith("zh") ? "、" : ", ";
|
||||
if(index < data.institutions.Count - 1)
|
||||
institutionText.text += institution.Localize("Items") + comma;
|
||||
else
|
||||
institutionText.text += institution.Localize("Items");
|
||||
}
|
||||
|
||||
// 描述条目
|
||||
RefreshDescriptions(data);
|
||||
|
||||
// 标签
|
||||
RefreshTags(data);
|
||||
|
||||
selectionHintText.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
/// <summary>清空面板内容并隐藏。</summary>
|
||||
public void ClearPanel()
|
||||
{
|
||||
currentItem = null;
|
||||
|
||||
//if (itemIcon != null) itemIcon.enabled = false;
|
||||
if (nameText != null) nameText.text = string.Empty;
|
||||
if (typeRarityText != null) typeRarityText.text = string.Empty;
|
||||
|
||||
ClearDescriptions();
|
||||
ClearTags();
|
||||
selectionHintText.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
// ================================================================
|
||||
// 描述条目
|
||||
// ================================================================
|
||||
|
||||
private void RefreshDescriptions(ContentData data)
|
||||
{
|
||||
ClearDescriptions();
|
||||
|
||||
if (descriptionContainer == null || descriptionEntryPrefab == null) return;
|
||||
if (data.descriptions == null || data.descriptions.Count == 0) return;
|
||||
|
||||
Dictionary<string, string> descriptionArgs = currentItem?.GetDescriptionArgs();
|
||||
|
||||
foreach (ItemDescription desc in data.descriptions)
|
||||
{
|
||||
ItemDescriptionEntry entry = Instantiate(descriptionEntryPrefab, descriptionContainer).GetComponent<ItemDescriptionEntry>();
|
||||
entry.SetDescription(desc, descriptionArgs);
|
||||
activeEntries.Add(entry);
|
||||
}
|
||||
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(descriptionContainer);
|
||||
}
|
||||
|
||||
private void ClearDescriptions()
|
||||
{
|
||||
foreach (ItemDescriptionEntry entry in activeEntries)
|
||||
{
|
||||
if (entry != null)
|
||||
{
|
||||
Destroy(entry.gameObject);
|
||||
}
|
||||
}
|
||||
activeEntries.Clear();
|
||||
}
|
||||
|
||||
// ================================================================
|
||||
// 标签
|
||||
// ================================================================
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 53a161f3f95b39a4ab7a31b2b3d0bb36
|
||||
Reference in New Issue
Block a user