196 lines
5.3 KiB
C#
196 lines
5.3 KiB
C#
using GeometryTD.CustomEvent;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
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 RectTransform _descriptionScrollView;
|
|
|
|
[SerializeField] private RectTransform _descriptionContent;
|
|
|
|
[SerializeField] private float _maxDescriptionViewportHeight = 0f;
|
|
|
|
[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;
|
|
}
|
|
|
|
ResizeDescriptionArea();
|
|
|
|
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;
|
|
}
|
|
|
|
ResetDescriptionScroll();
|
|
|
|
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 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 void ClearTags()
|
|
{
|
|
if (_tagsParent == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (int i = _tagsParent.childCount - 1; i >= 0; i--)
|
|
{
|
|
Destroy(_tagsParent.GetChild(i).gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|