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

117 lines
3.1 KiB
C#

using Cielonos.MainGame.Characters.Inventory;
using SLSUtilities.General;
using SLSUtilities.UI;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Cielonos.MainGame.UI
{
/// <summary>
/// 机械台左侧列表中的单个选择器条目。
/// 点击后选中该物品,并通知 MechanicalTableUIPage 更新详情面板。
/// </summary>
public class MechanicalTableSelector : UIElementBase
{
private MechanicalTableUIPage Page => PlayerCanvas.MainGamePages.mechanicalTablePage;
public ItemBase item;
public int stack = 1;
public Button button;
public Image background, itemIcon, rarityFrame, selectorHint;
public TMP_Text itemName, itemStack;
public RectTransform tagContainer;
[Header("Visual Settings")]
public Color normalColor = new Color(0.2f, 0.2f, 0.2f, 0.8f);
public Color selectedColor = new Color(0.4f, 0.6f, 0.9f, 0.9f);
private bool isSelected;
/// <summary>
/// 用给定的物品配置此选择器的显示内容。
/// </summary>
public void Setup(ItemBase itemRef, int itemStack = 1)
{
item = itemRef;
stack = itemStack;
if (item == null || item.contentData == null)
{
Hide();
return;
}
ContentData data = item.contentData;
// 图标
Sprite icon = data.itemType == ItemType.MainWeapon ? data.rectIcon : data.squareIcon;
if (itemIcon != null)
{
itemIcon.sprite = icon;
itemIcon.enabled = icon != null;
}
// 名称(本地化)
if (itemName != null)
{
itemName.text = data.displayNameKey.Localize();
}
// 堆叠数量
RefreshStackText();
// 默认取消选中状态
SetSelected(false);
// 绑定按钮事件
if (button != null)
{
button.onClick.RemoveAllListeners();
button.onClick.AddListener(OnClicked);
}
Show();
}
/// <summary>
/// 设置选中/取消选中的视觉状态。
/// </summary>
public void SetSelected(bool selected)
{
isSelected = selected;
if (background != null)
{
background.color = isSelected ? selectedColor : normalColor;
}
if (selectorHint != null)
{
selectorHint.enabled = isSelected;
}
}
private void OnClicked()
{
Page?.SelectItem(this);
}
private void RefreshStackText()
{
if (this.itemStack == null) return;
if (stack > 1)
{
this.itemStack.text = $"x{stack}";
this.itemStack.gameObject.SetActive(true);
}
else
{
this.itemStack.gameObject.SetActive(false);
}
}
}
}