231 lines
6.0 KiB
C#
231 lines
6.0 KiB
C#
using System.Collections.Generic;
|
|
using GeometryTD.CustomEvent;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
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 RectTransform _descriptionScrollView;
|
|
|
|
[SerializeField] private RectTransform _descriptionContent;
|
|
|
|
[SerializeField] private float _maxDescriptionViewportHeight = 0f;
|
|
|
|
[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;
|
|
}
|
|
|
|
ResizeDescriptionArea();
|
|
|
|
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;
|
|
}
|
|
|
|
ResetDescriptionScroll();
|
|
|
|
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 void ResizeDescriptionArea()
|
|
{
|
|
if (_descriptionText == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_descriptionText.ForceMeshUpdate();
|
|
float descriptionHeight = Mathf.Max(0f, _descriptionText.preferredHeight);
|
|
SetRectHeight(_descriptionText.rectTransform, descriptionHeight);
|
|
|
|
if (_descriptionContent != null)
|
|
{
|
|
SetRectHeight(_descriptionContent, descriptionHeight);
|
|
}
|
|
|
|
if (_descriptionScrollView != null)
|
|
{
|
|
float viewportHeight = _maxDescriptionViewportHeight > 0f
|
|
? Mathf.Min(descriptionHeight, _maxDescriptionViewportHeight)
|
|
: descriptionHeight;
|
|
SetRectHeight(_descriptionScrollView, viewportHeight);
|
|
}
|
|
|
|
ResetDescriptionScroll();
|
|
}
|
|
|
|
private void ResetDescriptionScroll()
|
|
{
|
|
if (_descriptionScrollView != null &&
|
|
_descriptionScrollView.TryGetComponent(out ScrollRect scrollRect))
|
|
{
|
|
scrollRect.verticalNormalizedPosition = 1f;
|
|
}
|
|
}
|
|
|
|
private static void SetRectHeight(RectTransform rectTransform, float height)
|
|
{
|
|
if (rectTransform == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Vector2 size = rectTransform.sizeDelta;
|
|
size.y = height;
|
|
rectTransform.sizeDelta = size;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|