172 lines
4.2 KiB
C#
172 lines
4.2 KiB
C#
using System.Collections.Generic;
|
|
using GeometryTD.CustomEvent;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class RewardItem : MonoBehaviour
|
|
{
|
|
[SerializeField] private IconArea _iconArea;
|
|
|
|
[SerializeField] private TMP_Text _titleText;
|
|
|
|
[SerializeField] private TMP_Text _typeText;
|
|
|
|
[SerializeField] private TMP_Text _descriptionText;
|
|
|
|
[SerializeField] private Transform _tagItemParent;
|
|
|
|
[SerializeField] private TagItem _tagItemTemplate;
|
|
|
|
private readonly List<TagItem> _runtimeTagItems = new List<TagItem>();
|
|
|
|
private RewardItemContext _context;
|
|
|
|
|
|
public void OnInit(RewardItemContext context)
|
|
{
|
|
if (context == null)
|
|
{
|
|
OnReset();
|
|
gameObject.SetActive(false);
|
|
return;
|
|
}
|
|
|
|
_context = context;
|
|
gameObject.SetActive(true);
|
|
|
|
if (_iconArea != null)
|
|
{
|
|
if (context.IconArea != null)
|
|
{
|
|
_iconArea.OnInit(context.IconArea);
|
|
}
|
|
else
|
|
{
|
|
_iconArea.OnReset();
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
RefreshTags(context.Tags);
|
|
}
|
|
|
|
public void OnReset()
|
|
{
|
|
_context = null;
|
|
|
|
if (_iconArea != null)
|
|
{
|
|
_iconArea.OnReset();
|
|
}
|
|
|
|
if (_titleText != null)
|
|
{
|
|
_titleText.text = string.Empty;
|
|
}
|
|
|
|
if (_typeText != null)
|
|
{
|
|
_typeText.text = string.Empty;
|
|
}
|
|
|
|
if (_descriptionText != null)
|
|
{
|
|
_descriptionText.text = string.Empty;
|
|
}
|
|
|
|
ClearTags();
|
|
}
|
|
|
|
public void OnClick()
|
|
{
|
|
if (_context == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameEntry.Event.Fire(this, RewardSelectItemSelectedEventArgs.Create(_context.Index));
|
|
}
|
|
|
|
private void RefreshTags(TagItemContext[] tagContexts)
|
|
{
|
|
ClearTags();
|
|
|
|
if (_tagItemParent == null || tagContexts == null || tagContexts.Length <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
TagItem template = ResolveTagTemplate();
|
|
if (template == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < tagContexts.Length; i++)
|
|
{
|
|
TagItemContext tagContext = tagContexts[i];
|
|
if (tagContext == null || string.IsNullOrWhiteSpace(tagContext.TagName))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
TagItem tagItem = Instantiate(template, _tagItemParent);
|
|
tagItem.gameObject.SetActive(true);
|
|
tagItem.OnInit(tagContext);
|
|
_runtimeTagItems.Add(tagItem);
|
|
}
|
|
}
|
|
|
|
private TagItem ResolveTagTemplate()
|
|
{
|
|
if (_tagItemTemplate != null)
|
|
{
|
|
return _tagItemTemplate;
|
|
}
|
|
|
|
if (_tagItemParent == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
_tagItemTemplate = _tagItemParent.GetComponentInChildren<TagItem>(true);
|
|
if (_tagItemTemplate != null)
|
|
{
|
|
_tagItemTemplate.gameObject.SetActive(false);
|
|
}
|
|
|
|
return _tagItemTemplate;
|
|
}
|
|
|
|
private void ClearTags()
|
|
{
|
|
for (int i = _runtimeTagItems.Count - 1; i >= 0; i--)
|
|
{
|
|
TagItem tagItem = _runtimeTagItems[i];
|
|
if (tagItem != null)
|
|
{
|
|
Destroy(tagItem.gameObject);
|
|
}
|
|
}
|
|
|
|
_runtimeTagItems.Clear();
|
|
}
|
|
}
|
|
}
|