拆分 UIController 组装 Context 职责 + 描述文本滚动展示

This commit is contained in:
SepComet
2026-03-12 16:45:20 +08:00
parent 4962b98714
commit f67888a8da
26 changed files with 2522 additions and 1053 deletions
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2ec97872405fc20419b0f156bb6c7365
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,59 @@
using GeometryTD.CustomUtility;
namespace GeometryTD.UI
{
public sealed partial class ShopFormController
{
private static ShopFormContext BuildContext(ShopFormRawData rawData)
{
GoodsItemContext[] goodsItemContexts = System.Array.Empty<GoodsItemContext>();
if (rawData?.GoodsItems != null && rawData.GoodsItems.Count > 0)
{
goodsItemContexts = new GoodsItemContext[rawData.GoodsItems.Count];
for (int i = 0; i < rawData.GoodsItems.Count; i++)
{
GoodsItemRawData item = rawData.GoodsItems[i];
goodsItemContexts[i] = new GoodsItemContext
{
GoodsIndex = item?.GoodsIndex ?? i,
Title = item?.Title ?? string.Empty,
TypeText = item?.TypeText ?? string.Empty,
Description = BuildDescription(item),
TagTexts = BuildTagTexts(item),
PurchaseButtonText = item != null && item.IsPurchased ? "已购买" : $"购买 {item?.Price ?? 0}",
CanPurchase = item != null && !item.IsPurchased,
IconAreaContext = item?.IconAreaContext
};
}
}
return new ShopFormContext
{
GoldText = $"金币: {rawData?.PlayerGold ?? 0}",
GoodsItems = goodsItemContexts
};
}
private static string BuildDescription(GoodsItemRawData rawData)
{
string baseDescription = rawData?.Description ?? string.Empty;
string tagDescription = TagDisplayUtility.BuildTagDescriptionText(rawData?.Tags);
if (string.IsNullOrWhiteSpace(tagDescription))
{
return baseDescription;
}
if (string.IsNullOrWhiteSpace(baseDescription))
{
return tagDescription;
}
return $"{baseDescription}\n{tagDescription}";
}
private static string[] BuildTagTexts(GoodsItemRawData rawData)
{
return TagDisplayUtility.BuildTagTexts(rawData?.Tags);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3da82624de00938408d9de7b74f09459
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,13 +1,12 @@
using GeometryTD.CustomEvent;
using GeometryTD.Definition;
using GameFramework.Event;
using GeometryTD.CustomUtility;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace GeometryTD.UI
{
public sealed class ShopFormController : UIFormControllerCommonBase<ShopFormContext, ShopForm>
public sealed partial class ShopFormController : UIFormControllerCommonBase<ShopFormContext, ShopForm>
{
private ShopFormUseCase _useCase;
@@ -134,56 +133,5 @@ namespace GeometryTD.UI
return false;
}
private static ShopFormContext BuildContext(ShopFormRawData rawData)
{
GoodsItemContext[] goodsItemContexts = System.Array.Empty<GoodsItemContext>();
if (rawData?.GoodsItems != null && rawData.GoodsItems.Count > 0)
{
goodsItemContexts = new GoodsItemContext[rawData.GoodsItems.Count];
for (int i = 0; i < rawData.GoodsItems.Count; i++)
{
GoodsItemRawData item = rawData.GoodsItems[i];
goodsItemContexts[i] = new GoodsItemContext
{
GoodsIndex = item?.GoodsIndex ?? i,
Title = item?.Title ?? string.Empty,
TypeText = item?.TypeText ?? string.Empty,
Description = BuildDescription(item),
TagTexts = BuildTagTexts(item),
PurchaseButtonText = item != null && item.IsPurchased ? "已购买" : $"购买 {item?.Price ?? 0}",
CanPurchase = item != null && !item.IsPurchased,
IconAreaContext = item?.IconAreaContext
};
}
}
return new ShopFormContext
{
GoldText = $"金币: {rawData?.PlayerGold ?? 0}",
GoodsItems = goodsItemContexts
};
}
private static string BuildDescription(GoodsItemRawData rawData)
{
string baseDescription = rawData?.Description ?? string.Empty;
string tagDescription = TagDisplayUtility.BuildTagDescriptionText(rawData?.Tags);
if (string.IsNullOrWhiteSpace(tagDescription))
{
return baseDescription;
}
if (string.IsNullOrWhiteSpace(baseDescription))
{
return tagDescription;
}
return $"{baseDescription}\n{tagDescription}";
}
private static string[] BuildTagTexts(GoodsItemRawData rawData)
{
return TagDisplayUtility.BuildTagTexts(rawData?.Tags);
}
}
}
@@ -1,6 +1,7 @@
using GeometryTD.CustomEvent;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace GeometryTD.UI
{
@@ -13,6 +14,12 @@ namespace GeometryTD.UI
[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;
@@ -46,6 +53,8 @@ namespace GeometryTD.UI
_descriptionText.text = context?.Description ?? string.Empty;
}
ResizeDescriptionArea();
if (_purchaseButtonText != null)
{
_purchaseButtonText.text = context?.PurchaseButtonText ?? string.Empty;
@@ -75,6 +84,8 @@ namespace GeometryTD.UI
_descriptionText.text = string.Empty;
}
ResetDescriptionScroll();
if (_purchaseButtonText != null)
{
_purchaseButtonText.text = string.Empty;
@@ -120,6 +131,54 @@ namespace GeometryTD.UI
}
}
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)