137 lines
3.5 KiB
C#
137 lines
3.5 KiB
C#
using GeometryTD.CustomEvent;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class GoodsItem : MonoBehaviour
|
|
{
|
|
[SerializeField] private IconArea _iconArea;
|
|
|
|
[SerializeField] private TMP_Text _titleText;
|
|
|
|
[SerializeField] private TMP_Text _typeText;
|
|
|
|
[SerializeField] private TMP_Text _descriptionText;
|
|
|
|
[SerializeField] private Transform _tagsParent;
|
|
|
|
[SerializeField] private TMP_Text _purchaseButtonText;
|
|
|
|
[SerializeField] private GameObject _tagItemPrefab;
|
|
|
|
[SerializeField] private int _index;
|
|
|
|
private GoodsItemContext _context;
|
|
|
|
public void OnInit(GoodsItemContext context)
|
|
{
|
|
_context = context;
|
|
_index = context?.GoodsIndex ?? -1;
|
|
|
|
_iconArea?.OnInit(context?.IconAreaContext ?? new IconAreaContext());
|
|
|
|
if (_titleText != null)
|
|
{
|
|
_titleText.text = context?.Title ?? string.Empty;
|
|
}
|
|
|
|
if (_typeText != null)
|
|
{
|
|
_typeText.text = context?.TypeText ?? string.Empty;
|
|
}
|
|
|
|
if (_descriptionText != null)
|
|
{
|
|
_descriptionText.text = context?.Description ?? string.Empty;
|
|
}
|
|
|
|
if (_purchaseButtonText != null)
|
|
{
|
|
_purchaseButtonText.text = context?.PurchaseButtonText ?? string.Empty;
|
|
}
|
|
|
|
RefreshTags(context?.TagTexts);
|
|
}
|
|
|
|
public void OnReset()
|
|
{
|
|
_context = null;
|
|
_index = -1;
|
|
_iconArea?.OnReset();
|
|
|
|
if (_titleText != null)
|
|
{
|
|
_titleText.text = string.Empty;
|
|
}
|
|
|
|
if (_typeText != null)
|
|
{
|
|
_typeText.text = string.Empty;
|
|
}
|
|
|
|
if (_descriptionText != null)
|
|
{
|
|
_descriptionText.text = string.Empty;
|
|
}
|
|
|
|
if (_purchaseButtonText != null)
|
|
{
|
|
_purchaseButtonText.text = string.Empty;
|
|
}
|
|
|
|
ClearTags();
|
|
}
|
|
|
|
public void OnPurchaseButtonClick()
|
|
{
|
|
if (_context == null || !_context.CanPurchase || _index < 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameEntry.Event.Fire(this, ShopPurchaseRequestedEventArgs.Create(_index));
|
|
}
|
|
|
|
private void RefreshTags(string[] tagTexts)
|
|
{
|
|
ClearTags();
|
|
if (_tagsParent == null || _tagItemPrefab == null || tagTexts == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < tagTexts.Length; i++)
|
|
{
|
|
if (string.IsNullOrEmpty(tagTexts[i]))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
GameObject tagItemObject = Instantiate(_tagItemPrefab, _tagsParent);
|
|
TagItem tagItem = tagItemObject.GetComponent<TagItem>();
|
|
if (tagItem != null)
|
|
{
|
|
tagItem.OnInit(new TagItemContext
|
|
{
|
|
TagName = tagTexts[i]
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ClearTags()
|
|
{
|
|
if (_tagsParent == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (int i = _tagsParent.childCount - 1; i >= 0; i--)
|
|
{
|
|
Destroy(_tagsParent.GetChild(i).gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|