S5 finish

This commit is contained in:
2026-03-11 17:24:15 +08:00
parent 9c5871b518
commit 8c8f786013
11 changed files with 558 additions and 48 deletions
@@ -12,20 +12,20 @@ namespace GeometryTD.CustomEvent
public long ItemId { get; private set; }
public Vector3 TargetPos { get; private set; }
public Vector2 ScreenPosition { get; private set; }
public static RepoItemClickedEventArgs Create(long itemId, Vector3 targetPos)
public static RepoItemClickedEventArgs Create(long itemId, Vector2 screenPosition)
{
RepoItemClickedEventArgs args = ReferencePool.Acquire<RepoItemClickedEventArgs>();
args.ItemId = itemId;
args.TargetPos = targetPos;
args.ScreenPosition = screenPosition;
return args;
}
public override void Clear()
{
ItemId = 0;
TargetPos = Vector3.zero;
ScreenPosition = Vector2.zero;
}
}
}
@@ -390,7 +390,7 @@ namespace GeometryTD.UI
TypeText = seed.TypeText,
Description = seed.Description ?? string.Empty,
Price = 0,
TargetPos = args.TargetPos,
ScreenPosition = args.ScreenPosition,
Tags = CloneTags(seed.Tags),
TagRuntimes = CloneTagRuntimes(seed.TagRuntimes)
});
@@ -152,7 +152,7 @@ namespace GeometryTD.UI
TypeText = seed.TypeText,
Description = seed.Description ?? string.Empty,
Price = 0,
TargetPos = args.TargetPos,
ScreenPosition = args.ScreenPosition,
Tags = CloneTags(seed.Tags),
TagRuntimes = CloneTagRuntimes(seed.TagRuntimes)
});
@@ -120,7 +120,7 @@ namespace GeometryTD.UI
return;
}
GameEntry.Event.Fire(this, RepoItemClickedEventArgs.Create(_context.InstanceId, transform.position));
GameEntry.Event.Fire(this, RepoItemClickedEventArgs.Create(_context.InstanceId, ResolveScreenPosition()));
}
public void OnBeginDrag(PointerEventData eventData)
@@ -239,6 +239,25 @@ namespace GeometryTD.UI
}
}
private Vector2 ResolveScreenPosition()
{
RectTransform rectTransform = transform as RectTransform;
if (rectTransform == null)
{
return Vector2.zero;
}
Canvas canvas = GetComponentInParent<Canvas>();
Canvas rootCanvas = canvas != null ? canvas.rootCanvas : null;
Camera uiCamera = null;
if (rootCanvas != null && rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay)
{
uiCamera = rootCanvas.worldCamera != null ? rootCanvas.worldCamera : Camera.main;
}
return RectTransformUtility.WorldToScreenPoint(uiCamera, rectTransform.position);
}
private RectTransform ResolveDragRoot()
{
Canvas canvas = GetComponentInParent<Canvas>();
@@ -348,4 +367,3 @@ namespace GeometryTD.UI
}
}
}
@@ -8,7 +8,7 @@ namespace GeometryTD.UI
public string TypeText;
public string Description;
public int Price;
public Vector3 TargetPos;
public Vector2 ScreenPosition;
public TagItemContext[] Tags;
}
}
@@ -34,7 +34,7 @@ namespace GeometryTD.UI
TypeText = rawData.TypeText,
Description = BuildDescription(rawData),
Price = rawData.Price,
TargetPos = rawData.TargetPos,
ScreenPosition = rawData.ScreenPosition,
Tags = BuildTags(rawData)
};
}
@@ -9,7 +9,7 @@ namespace GeometryTD.UI
public string TypeText;
public string Description;
public int Price;
public Vector3 TargetPos;
public Vector2 ScreenPosition;
public TagType[] Tags;
public TagRuntimeData[] TagRuntimes;
}
@@ -14,6 +14,8 @@ namespace GeometryTD.UI
[SerializeField] private TMP_Text _itemTypeText;
[SerializeField] private RectTransform _descScrollView;
[SerializeField] private TMP_Text _itemDescription;
[SerializeField] private TMP_Text _itemPrice;
@@ -30,6 +32,8 @@ namespace GeometryTD.UI
[SerializeField] private float _fixedWidth = 300f;
[SerializeField] private float _maxDescriptionViewportHeight = 0f;
[SerializeField] private float _screenEdgePadding = 0f;
[SerializeField] private float _sideGap = 16f;
@@ -45,7 +49,7 @@ namespace GeometryTD.UI
private readonly List<GameObject> _runtimeTagItems = new List<GameObject>();
private ItemDescFormContext _context;
private Vector3 _targetPos;
private Vector2 _targetPos;
private bool _isOpened;
public void RefreshUI(ItemDescFormContext context)
@@ -58,7 +62,7 @@ namespace GeometryTD.UI
_context = context;
_targetPos = ConvertWorldToAnchored(_context.TargetPos);
_targetPos = ConvertScreenToAnchored(_context.ScreenPosition);
if (_content != null)
{
@@ -146,16 +150,62 @@ namespace GeometryTD.UI
}
_itemDescription.ForceMeshUpdate();
var descriptionSize = _itemDescription.rectTransform.sizeDelta;
descriptionSize.y = _itemDescription.preferredHeight;
_itemDescription.rectTransform.sizeDelta = descriptionSize;
float descriptionHeight = Mathf.Max(0f, _itemDescription.preferredHeight);
float targetHeight = _fixedTopHeight + descriptionHeight + _fixedBottomHeight;
SetRectHeight(_itemDescription.rectTransform, descriptionHeight);
RectTransform scrollContent = _itemDescription.rectTransform.parent as RectTransform;
if (scrollContent != null)
{
SetRectHeight(scrollContent, descriptionHeight);
}
float viewportHeight = descriptionHeight;
if (_descScrollView != null)
{
viewportHeight = Mathf.Min(descriptionHeight, ResolveMaxDescriptionViewportHeight());
SetRectHeight(_descScrollView, viewportHeight);
if (_descScrollView.TryGetComponent(out ScrollRect scrollRect))
{
scrollRect.verticalNormalizedPosition = 1f;
}
}
float targetHeight = _fixedTopHeight + viewportHeight + _fixedBottomHeight;
Vector2 size = new Vector2(_fixedWidth, targetHeight);
_content.sizeDelta = size;
}
private float ResolveMaxDescriptionViewportHeight()
{
float maxViewportHeight = _maxDescriptionViewportHeight;
RectTransform parent = _content.parent as RectTransform;
if (parent != null)
{
float availableHeight = parent.rect.height - _fixedTopHeight - _fixedBottomHeight
- _screenEdgePadding * 2f;
availableHeight = Mathf.Max(0f, availableHeight);
maxViewportHeight = maxViewportHeight > 0f
? Mathf.Min(maxViewportHeight, availableHeight)
: availableHeight;
}
return maxViewportHeight > 0f ? maxViewportHeight : float.MaxValue;
}
private static void SetRectHeight(RectTransform rectTransform, float height)
{
if (rectTransform == null)
{
return;
}
Vector2 size = rectTransform.sizeDelta;
size.y = height;
rectTransform.sizeDelta = size;
}
private void ApplySideAnchoredTargetPos()
{
if (_content == null)
@@ -207,20 +257,20 @@ namespace GeometryTD.UI
finalX = minX <= maxX ? Mathf.Clamp(leftCandidateX, minX, maxX) : 0f;
}
_content.anchoredPosition3D = new Vector3(finalX, clampedY, _targetPos.z);
_content.anchoredPosition = new Vector2(finalX, clampedY);
}
private Vector3 ConvertWorldToAnchored(Vector3 worldPos)
private Vector2 ConvertScreenToAnchored(Vector2 screenPosition)
{
if (_content == null)
{
return worldPos;
return screenPosition;
}
RectTransform parent = _content.parent as RectTransform;
if (parent == null)
{
return worldPos;
return screenPosition;
}
Canvas canvas = _content.GetComponentInParent<Canvas>();
@@ -231,15 +281,13 @@ namespace GeometryTD.UI
uiCamera = rootCanvas.worldCamera != null ? rootCanvas.worldCamera : Camera.main;
}
Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(uiCamera, worldPos);
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, screenPoint, uiCamera,
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, screenPosition, uiCamera,
out Vector2 localPoint))
{
return worldPos;
return screenPosition;
}
float z = _content.anchoredPosition3D.z;
return new Vector3(localPoint.x, localPoint.y, z);
return localPoint;
}
private static bool IsInsideBounds(float value, float min, float max)