This commit is contained in:
2026-04-30 19:33:16 +08:00
commit fb2252f688
556 changed files with 69721 additions and 0 deletions
+195
View File
@@ -0,0 +1,195 @@
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);
}
}
}
}
+94
View File
@@ -0,0 +1,94 @@
using GeometryTD.CustomEvent;
using TMPro;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace GeometryTD.UI
{
public class ShopForm : UGuiForm
{
[SerializeField] private TMP_Text _goldText;
[SerializeField] private GoodsItem[] _goldItems;
private ShopFormContext _context;
public void RefreshUI(ShopFormContext context)
{
_context = context;
if (_goldText != null)
{
_goldText.text = context?.GoldText ?? string.Empty;
}
RefreshGoodsItems(context?.GoodsItems);
}
public void OnInventoryButtonClick()
{
GameEntry.Event.Fire(this, ShopInventoryRequestedEventArgs.Create());
}
public void OnExitButtonClick()
{
GameEntry.Event.Fire(this, ShopExitRequestedEventArgs.Create());
}
protected override void OnOpen(object userData)
{
base.OnOpen(userData);
if (userData is ShopFormContext context)
{
RefreshUI(context);
return;
}
Log.Warning("ShopForm requires ShopFormContext as userData.");
}
protected override void OnClose(bool isShutdown, object userData)
{
_context = null;
if (_goldText != null)
{
_goldText.text = string.Empty;
}
if (_goldItems != null)
{
foreach (var item in _goldItems)
{
item?.OnReset();
}
}
base.OnClose(isShutdown, userData);
}
private void RefreshGoodsItems(GoodsItemContext[] goodsItems)
{
if (_goldItems == null)
{
return;
}
for (int i = 0; i < _goldItems.Length; i++)
{
GoodsItem goodsItem = _goldItems[i];
if (goodsItem == null)
{
continue;
}
if (goodsItems != null && i < goodsItems.Length && goodsItems[i] != null)
{
goodsItem.OnInit(goodsItems[i]);
}
else
{
goodsItem.OnReset();
}
}
}
}
}