加入武器 tag 的羁绊效果

This commit is contained in:
2026-10-04 14:45:25 +08:00
parent 0077e3449d
commit c76a421f4a
130 changed files with 3248 additions and 25453 deletions
@@ -1,7 +1,3 @@
using System;
using Cysharp.Threading.Tasks;
using SepCore.AsyncTask;
using SepCore.Event;
using UnityEngine;
using UnityGameFramework.Runtime;
@@ -9,53 +5,31 @@ namespace SepCore.UI
{
public partial class ItemTooltipForm : UGuiForm
{
[SerializeField] private bool _autoResizeHeight = true;
[SerializeField] private float _fixedTopHeight = 150f;
[SerializeField] private float _fixedBottomHeight = 250;
[SerializeField] private float _fixedWidth = 600f;
[SerializeField] private float _screenEdgePadding = 64f;
[SerializeField] private float _panelSpacing = 16f;
[SerializeField] private float _screenEdgePadding = 32f;
[SerializeField] private float _verticalGap = 10f;
private ItemTooltipContext _context;
private Vector3 _targetPos;
private bool _listenersBound;
public async UniTaskVoid RefreshUI(ItemTooltipContext context)
public void RefreshUI(ItemTooltipContext context)
{
EnsureListeners();
_context = context;
_targetPos = ConvertWorldToAnchored(_context.TargetPos);
View.contentRectTransform.anchoredPosition3D = _targetPos;
// 1. 刷新基础信息面板(同步执行,立即完成文本排版与尺寸自适应)
View.basicForm.RefreshUI(_context.BasicContext);
View.titleText.text = _context.Title;
View.typeText.text = _context.TypeName;
View.infoText.text = _context.Description;
View.textFormatText.Set(_context.Price);
View.recycleButton.gameObject.SetActive(_context.ShowRecycleButton);
if (!string.IsNullOrEmpty(_context.IconAssetName))
// 2. 刷新扩展详情面板(Tag 羁绊 + 上一轮伤害)
bool showDetail = _context.DetailContext != null && _context.DetailContext.HasAnyContent;
View.detailForm.gameObject.SetActive(showDetail);
if (showDetail)
{
string iconAssetName = _context.IconAssetName;
var sprite = await SpriteLoader.LoadSpriteByKeyAsync(iconAssetName);
if (_context != null && _context.IconAssetName == iconAssetName)
{
View.iconArea.OnInit(sprite, context.Rarity);
ResizeToFitContent();
ApplyClampedTargetPos();
}
View.detailForm.RefreshUI(_context.DetailContext);
}
ResizeToFitContent();
ApplyClampedTargetPos();
// 3. 执行自适应排版定位
ApplyAdaptiveSidePositioning();
}
protected override void OnOpen(object userData)
@@ -68,7 +42,7 @@ namespace SepCore.UI
return;
}
RefreshUI(context).Forget();
RefreshUI(context);
}
protected override void OnClose(bool isShutdown, object userData)
@@ -77,85 +51,146 @@ namespace SepCore.UI
base.OnClose(isShutdown, userData);
}
public void OnRecycleButtonClick()
public void ApplyAdaptiveSidePositioning()
{
GameEntry.Event.Fire(this, ItemTooltipRecycleEventArgs.Create(_context.Index, _context.Price));
}
public void OnCancelButtonClick()
{
GameEntry.Event.Fire(this, ItemTooltipHideEventArgs.Create());
}
private void EnsureListeners()
{
if (_listenersBound)
RectTransform container = View.contentRectTransform;
if (container == null)
{
return;
}
_listenersBound = true;
RectTransform basicRect = View.basicForm.RectTransform;
RectTransform detailRect = View.detailForm.RectTransform;
View.recycleButton.onClick.AddListener(OnRecycleButtonClick);
View.cancelButton.onClick.AddListener(OnCancelButtonClick);
}
private void ResizeToFitContent()
{
if (!_autoResizeHeight)
Vector2 basicSize = basicRect.sizeDelta;
if (basicSize.x <= 0f || basicSize.y <= 0f)
{
basicSize = basicRect.rect.size;
}
float basicWidth = basicSize.x > 0f ? basicSize.x : 600f;
float basicHeight = basicSize.y > 0f ? basicSize.y : 500f;
float halfWidth = container.rect.width * 0.5f;
float halfHeight = container.rect.height * 0.5f;
if (halfWidth <= 0f || halfHeight <= 0f)
{
if (container.parent is RectTransform parentRt)
{
halfWidth = parentRt.rect.width * 0.5f;
halfHeight = parentRt.rect.height * 0.5f;
}
}
// --- 纵向布局(基本面板以 pivot=(0.5, 0) 放置于目标顶部上方)---
// _targetPos 是触发悬浮的物品按钮上边缘(世界转局部坐标),
// 基本面板底部对齐其上方 _verticalGap 像素,永远不覆盖物品按钮本体,杜绝 PointerExit 闪烁循环。
float basicY = _targetPos.y + _verticalGap;
float maxBasicY = (halfHeight - _screenEdgePadding) - basicHeight;
if (basicY > maxBasicY)
{
basicY = Mathf.Max(maxBasicY, _targetPos.y + _verticalGap);
}
bool showDetail = View.detailForm.gameObject.activeSelf;
if (!showDetail)
{
// 无副面板:直接以目标水平中心为准,限制在屏幕边界内
float minX = -halfWidth + _screenEdgePadding + basicWidth * 0.5f;
float maxX = halfWidth - _screenEdgePadding - basicWidth * 0.5f;
float basicX = minX <= maxX ? Mathf.Clamp(_targetPos.x, minX, maxX) : 0f;
basicRect.anchoredPosition = new Vector2(basicX, basicY);
return;
}
View.infoText.ForceMeshUpdate();
var descriptionSize = View.infoText.rectTransform.sizeDelta;
descriptionSize.y = View.infoText.preferredHeight;
View.infoText.rectTransform.sizeDelta = descriptionSize;
float descriptionHeight = Mathf.Max(0f, View.infoText.preferredHeight);
float targetHeight = _fixedTopHeight + descriptionHeight + _fixedBottomHeight;
Vector2 size = new Vector2(_fixedWidth, targetHeight);
View.contentRectTransform.sizeDelta = size;
}
private void ApplyClampedTargetPos()
{
RectTransform parent = View.contentRectTransform.parent as RectTransform;
if (parent == null)
// --- 双面板横向排版与左右自适应翻转 ---
Vector2 detailSize = detailRect.sizeDelta;
if (detailSize.x <= 0f || detailSize.y <= 0f)
{
return;
detailSize = detailRect.rect.size;
}
Vector2 size = View.contentRectTransform.sizeDelta;
if (size.x <= 0f || size.y <= 0f)
float detailWidth = detailSize.x > 0f ? detailSize.x : 420f;
float detailHeight = detailSize.y > 0f ? detailSize.y : 250f;
float screenMinX = -halfWidth + _screenEdgePadding;
float screenMaxX = halfWidth - _screenEdgePadding;
float basicCenterX = _targetPos.x;
float basicRight = basicCenterX + basicWidth * 0.5f;
float detailRight = basicRight + _panelSpacing + detailWidth;
float basicFinalX;
float detailFinalX;
if (detailRight <= screenMaxX)
{
size = View.contentRectTransform.rect.size;
// 1. 右侧空间充足,副面板放在右侧
float basicLeft = basicCenterX - basicWidth * 0.5f;
if (basicLeft < screenMinX)
{
// 左侧轻微出界,向右微调
basicCenterX += (screenMinX - basicLeft);
}
basicFinalX = basicCenterX;
float newBasicRight = basicFinalX + basicWidth * 0.5f;
detailFinalX = newBasicRight + _panelSpacing + detailWidth * 0.5f;
}
else
{
// 2. 右侧空间不足,副面板自动翻转至左侧
float basicLeft = basicCenterX - basicWidth * 0.5f;
float detailLeft = basicLeft - _panelSpacing - detailWidth;
if (detailLeft < screenMinX)
{
// 翻到左侧后若超出左屏边缘,向右贴边微调
basicCenterX += (screenMinX - detailLeft);
}
if (basicCenterX + basicWidth * 0.5f > screenMaxX)
{
// 右侧也不超界
basicCenterX -= (basicCenterX + basicWidth * 0.5f - screenMaxX);
}
basicFinalX = basicCenterX;
float newBasicLeft = basicFinalX - basicWidth * 0.5f;
detailFinalX = newBasicLeft - _panelSpacing - detailWidth * 0.5f;
}
Vector2 pivot = View.contentRectTransform.pivot;
float halfWidth = parent.rect.width * 0.5f;
float halfHeight = parent.rect.height * 0.5f;
// --- 副面板纵向对齐(以 pivot=(0.5, 1) 顶部对齐主面板顶部)---
float basicTopEdge = basicY + basicHeight;
float detailY = basicTopEdge;
float minX = -halfWidth + size.x * pivot.x + _screenEdgePadding;
float maxX = halfWidth - size.x * (1f - pivot.x) - _screenEdgePadding;
float minY = -halfHeight + size.y * pivot.y + _screenEdgePadding;
float maxY = halfHeight - size.y * (1f - pivot.y) - _screenEdgePadding;
// 限制副面板顶部不超出屏幕顶端
if (detailY > halfHeight - _screenEdgePadding)
{
detailY = halfHeight - _screenEdgePadding;
}
float clampedX = minX <= maxX ? Mathf.Clamp(_targetPos.x, minX, maxX) : 0f;
float clampedY = minY <= maxY ? Mathf.Clamp(_targetPos.y, minY, maxY) : 0f;
// 限制副面板底端不超出屏幕底端,且不低于物品按钮顶部
float detailBottom = detailY - detailHeight;
float minDetailBottom = Mathf.Max(-halfHeight + _screenEdgePadding, _targetPos.y + _verticalGap);
if (detailBottom < minDetailBottom)
{
detailY = minDetailBottom + detailHeight;
}
View.contentRectTransform.anchoredPosition3D = new Vector3(clampedX, clampedY, _targetPos.z);
basicRect.anchoredPosition = new Vector2(basicFinalX, basicY);
detailRect.anchoredPosition = new Vector2(detailFinalX, detailY);
}
private Vector3 ConvertWorldToAnchored(Vector3 worldPos)
{
RectTransform parent = View.contentRectTransform.parent as RectTransform;
if (parent == null)
RectTransform container = View.contentRectTransform;
if (container == null)
{
return worldPos;
}
Canvas canvas = View.contentRectTransform.GetComponentInParent<Canvas>();
Canvas canvas = container.GetComponentInParent<Canvas>();
Canvas rootCanvas = canvas != null ? canvas.rootCanvas : null;
Camera uiCamera = null;
if (rootCanvas != null && rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay)
@@ -164,14 +199,13 @@ namespace SepCore.UI
}
Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(uiCamera, worldPos);
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, screenPoint, uiCamera,
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(container, screenPoint, uiCamera,
out Vector2 localPoint))
{
return worldPos;
}
float z = View.contentRectTransform.anchoredPosition3D.z;
return new Vector3(localPoint.x, localPoint.y, z);
return localPoint;
}
}
}