补全 UI 设计

- `ItemDescForm` 道具详细信息面板,目前还未添加 DescText 内容的赋值
- `RepoForm` 道具入槽逻辑调整,为了适应 `ItemDescForm` 的点击打开逻辑,点击道具自动匹配槽位改成拖动道具到相应槽位
- 添加了一系列图片素材
This commit is contained in:
SepComet
2026-03-04 12:47:20 +08:00
parent 3e52a4df55
commit e12d2e73b2
147 changed files with 9866 additions and 277 deletions
@@ -1,9 +1,10 @@
using GeometryTD.Definition;
using UnityEngine;
using UnityEngine.EventSystems;
namespace GeometryTD.UI
{
public class CombineArea : MonoBehaviour
public class CombineArea : MonoBehaviour, IDropHandler
{
[SerializeField] private CombineSlotItem[] _slots;
@@ -80,6 +81,34 @@ namespace GeometryTD.UI
return true;
}
public void OnDrop(PointerEventData eventData)
{
if (eventData == null)
{
return;
}
GameObject pointerDrag = eventData.pointerDrag;
if (pointerDrag == null)
{
return;
}
RepoItem repoItem = pointerDrag.GetComponent<RepoItem>();
if (repoItem == null)
{
repoItem = pointerDrag.GetComponentInParent<RepoItem>();
}
if (repoItem == null)
{
return;
}
bool assigned = TryAssignItem(repoItem.Context);
repoItem.SetDropResult(assigned);
}
private CombineSlotItem FindSlot(TowerCompSlotType slotType)
{
if (_slots == null)
@@ -99,4 +128,4 @@ namespace GeometryTD.UI
return null;
}
}
}
}
@@ -1,6 +1,5 @@
using GeometryTD.CustomEvent;
using GeometryTD.Definition;
using TMPro;
using UnityEngine;
namespace GeometryTD.UI
@@ -9,12 +8,10 @@ namespace GeometryTD.UI
{
[SerializeField] private TowerCompSlotType _acceptSlotType = TowerCompSlotType.None;
[SerializeField] private TMP_Text _titleText;
[SerializeField] private IconArea _iconArea;
[SerializeField] private CommonButton _button;
private string _defaultTitle = string.Empty;
private long _boundItemId;
private int _slotIndex = -1;
@@ -27,18 +24,11 @@ namespace GeometryTD.UI
public long BoundItemId => _boundItemId;
private void Awake()
{
if (_titleText != null)
{
_defaultTitle = _titleText.text;
}
}
public void OnInit(int slotIndex)
{
_slotIndex = slotIndex;
ClearSlot();
_iconArea.OnReset();
}
public void BindItem(RepoItemContext itemContext)
@@ -46,9 +36,9 @@ namespace GeometryTD.UI
_boundItemId = itemContext?.InstanceId ?? 0;
_button.Interactive = true;
if (_titleText != null)
if (itemContext != null)
{
_titleText.text = itemContext?.Title ?? string.Empty;
_iconArea.OnInit(itemContext.IconAreaContext);
}
}
@@ -56,17 +46,14 @@ namespace GeometryTD.UI
{
_boundItemId = 0;
_button.Interactive = false;
if (_titleText != null)
{
_titleText.text = _defaultTitle;
}
_iconArea.OnReset();
}
public void OnReset()
{
_slotIndex = -1;
ClearSlot();
_iconArea.OnReset();
}
public void OnClick()
@@ -2,20 +2,21 @@ using System.Collections.Generic;
using GameFramework.ObjectPool;
using GeometryTD.PoolObjectBase;
using UnityEngine;
using UnityEngine.Serialization;
using UnityGameFramework.Runtime;
namespace GeometryTD.UI
{
public class CompArea : MonoBehaviour
{
[SerializeField] private Transform m_Content;
[SerializeField] private RepoItem m_ItemTemplate;
[SerializeField] private int m_InstancePoolCapacity = 64;
[FormerlySerializedAs("m_Content")] [SerializeField] private Transform _content;
[FormerlySerializedAs("m_ItemTemplate")] [SerializeField] private RepoItem _itemTemplate;
[FormerlySerializedAs("m_InstancePoolCapacity")] [SerializeField] private int _instancePoolCapacity = 64;
private readonly List<RepoItem> m_ActiveItems = new List<RepoItem>();
private readonly Dictionary<long, RepoItem> m_ItemMap = new Dictionary<long, RepoItem>();
private IObjectPool<RepoItemObject> m_ItemPool;
private string m_PoolName;
private readonly List<RepoItem> _activeItems = new List<RepoItem>();
private readonly Dictionary<long, RepoItem> _itemMap = new Dictionary<long, RepoItem>();
private IObjectPool<RepoItemObject> _itemPool;
private string _poolName;
public void OnInit(CompAreaContext context)
{
@@ -33,7 +34,7 @@ namespace GeometryTD.UI
return;
}
for (int i = 0; i < context.Items.Length; i++)
foreach (var itemContext in context.Items)
{
RepoItem item = CreateOrSpawnItem();
if (item == null)
@@ -42,17 +43,17 @@ namespace GeometryTD.UI
}
item.gameObject.SetActive(true);
item.OnInit(context.Items[i]);
m_ActiveItems.Add(item);
m_ItemMap[context.Items[i].InstanceId] = item;
item.OnInit(itemContext);
_activeItems.Add(item);
_itemMap[itemContext.InstanceId] = item;
}
}
public void OnReset()
{
for (int i = m_ActiveItems.Count - 1; i >= 0; i--)
for (int i = _activeItems.Count - 1; i >= 0; i--)
{
RepoItem item = m_ActiveItems[i];
RepoItem item = _activeItems[i];
if (item == null)
{
continue;
@@ -60,54 +61,54 @@ namespace GeometryTD.UI
item.OnReset();
item.gameObject.SetActive(false);
m_ItemPool?.Unspawn(item);
_itemPool?.Unspawn(item);
}
m_ActiveItems.Clear();
m_ItemMap.Clear();
_activeItems.Clear();
_itemMap.Clear();
}
private void OnDestroy()
{
OnReset();
m_ItemPool?.ReleaseAllUnused();
_itemPool?.ReleaseAllUnused();
}
private void EnsurePool()
{
if (m_ItemPool != null)
if (_itemPool != null)
{
return;
}
m_PoolName = $"RepoItemPool_{GetInstanceID()}";
m_ItemPool = GameEntry.ObjectPool.CreateSingleSpawnObjectPool<RepoItemObject>(m_PoolName, m_InstancePoolCapacity);
_poolName = $"RepoItemPool_{GetInstanceID()}";
_itemPool = GameEntry.ObjectPool.CreateSingleSpawnObjectPool<RepoItemObject>(_poolName, _instancePoolCapacity);
}
private RepoItem CreateOrSpawnItem()
{
if (m_ItemPool == null)
if (_itemPool == null)
{
return null;
}
RepoItemObject itemObject = m_ItemPool.Spawn();
RepoItemObject itemObject = _itemPool.Spawn();
RepoItem item = itemObject != null ? (RepoItem)itemObject.Target : null;
if (item == null)
{
if (m_ItemTemplate == null)
if (_itemTemplate == null)
{
Log.Warning("CompArea requires an item template.");
return null;
}
item = Instantiate(m_ItemTemplate);
m_ItemPool.Register(RepoItemObject.Create(item), true);
item = Instantiate(_itemTemplate);
_itemPool.Register(RepoItemObject.Create(item), true);
}
if (m_Content != null)
if (_content != null)
{
item.transform.SetParent(m_Content, false);
item.transform.SetParent(_content, false);
}
return item;
@@ -115,7 +116,7 @@ namespace GeometryTD.UI
public void SetItemSelected(long itemId, bool isSelected)
{
if (!m_ItemMap.TryGetValue(itemId, out RepoItem item))
if (!_itemMap.TryGetValue(itemId, out RepoItem item))
{
return;
}
@@ -1,3 +1,4 @@
using GeometryTD.CustomEvent;
using UnityEngine;
using UnityGameFramework.Runtime;
@@ -63,5 +64,10 @@ namespace GeometryTD.UI
_compArea?.OnReset();
base.OnClose(isShutdown, userData);
}
public void OnReturnButtonClick()
{
GameEntry.Event.Fire(this, RepoFormReturnEventArgs.Create());
}
}
}
}
+306 -16
View File
@@ -1,20 +1,49 @@
using System.Collections;
using GeometryTD.CustomEvent;
using TMPro;
using GeometryTD.Definition;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace GeometryTD.UI
{
public class RepoItem : MonoBehaviour
public class RepoItem : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
[SerializeField] private TMP_Text _titleText;
[SerializeField] private Image _highlightImage;
[SerializeField] private IconArea _iconArea;
private static readonly Color NormalColor = new Color32(40, 40, 40, 180);
private static readonly Color SelectedColor = new Color32(255, 216, 102, 255);
private static readonly Vector2 DefaultDragGhostSize = new Vector2(64f, 64f);
private RepoItemContext _context;
[SerializeField] private RepoItemContext _context;
private CanvasGroup _canvasGroup;
private RectTransform _dragRoot;
private GameObject _dragGhostObject;
private RectTransform _dragGhostRect;
private bool _isSelected;
private bool _isDragging;
private bool _dropHandled;
private bool _dropAssigned;
private Coroutine _pendingOpenDetailRoutine;
private bool _cancelPendingOpenDetail;
public RepoItemContext Context => _context;
private void Awake()
{
_canvasGroup = GetComponent<CanvasGroup>();
if (_canvasGroup == null)
{
_canvasGroup = gameObject.AddComponent<CanvasGroup>();
}
}
private void OnDisable()
{
CancelPendingOpenDetail();
ResetDragState();
}
public void OnInit(RepoItemContext context)
{
@@ -26,30 +55,25 @@ namespace GeometryTD.UI
_context = context;
if (_titleText != null)
{
_titleText.text = context.Title ?? string.Empty;
}
_iconArea.OnInit(context.IconAreaContext);
SetSelected(false);
ResetDragState();
}
public void OnReset()
{
CancelPendingOpenDetail();
_context = null;
if (_titleText != null)
{
_titleText.text = string.Empty;
}
_iconArea.OnReset();
SetSelected(false);
ResetDragState();
}
public void SetSelected(bool isSelected)
{
_isSelected = isSelected;
if (_highlightImage == null)
{
return;
@@ -65,7 +89,273 @@ namespace GeometryTD.UI
return;
}
GameEntry.Event.Fire(this, RepoItemSelectedEventArgs.Create(_context.InstanceId));
if (_isDragging)
{
return;
}
StartPendingOpenDetail();
}
public void OnBeginDrag(PointerEventData eventData)
{
if (!CanStartDrag(eventData))
{
return;
}
_dragRoot = ResolveDragRoot();
if (_dragRoot == null)
{
return;
}
_isDragging = true;
_dropHandled = false;
_dropAssigned = false;
_cancelPendingOpenDetail = true;
CloseItemDescFormIfOpen();
if (!CreateDragGhost())
{
_isDragging = false;
_dragRoot = null;
return;
}
if (_canvasGroup != null)
{
_canvasGroup.blocksRaycasts = false;
}
SetSelected(true);
UpdateDragGhostPosition(eventData);
}
public void OnDrag(PointerEventData eventData)
{
if (!_isDragging)
{
return;
}
UpdateDragGhostPosition(eventData);
}
public void OnEndDrag(PointerEventData eventData)
{
if (!_isDragging)
{
return;
}
bool assigned = _dropHandled && _dropAssigned;
_isDragging = false;
if (_canvasGroup != null)
{
_canvasGroup.blocksRaycasts = true;
}
DestroyDragGhost();
_dragRoot = null;
if (_context == null)
{
return;
}
GameEntry.Event.Fire(this, RepoItemDragEndedEventArgs.Create(_context.InstanceId, assigned));
}
public void SetDropResult(bool assigned)
{
if (!_isDragging)
{
return;
}
_dropHandled = true;
_dropAssigned = assigned;
}
private bool CanStartDrag(PointerEventData eventData)
{
if (_context == null || _isSelected || _isDragging)
{
return false;
}
if (!_context.CanDrag)
{
return false;
}
if (_context.ComponentSlotType == TowerCompSlotType.None)
{
return false;
}
if (eventData == null || eventData.button != PointerEventData.InputButton.Left)
{
return false;
}
return true;
}
private void StartPendingOpenDetail()
{
CancelPendingOpenDetail();
_cancelPendingOpenDetail = false;
_pendingOpenDetailRoutine = StartCoroutine(WaitPointerReleaseAndOpenDetail());
}
private void CancelPendingOpenDetail()
{
_cancelPendingOpenDetail = true;
if (_pendingOpenDetailRoutine != null)
{
StopCoroutine(_pendingOpenDetailRoutine);
_pendingOpenDetailRoutine = null;
}
}
private IEnumerator WaitPointerReleaseAndOpenDetail()
{
while (IsPrimaryPointerPressed())
{
if (_cancelPendingOpenDetail || _isDragging)
{
_pendingOpenDetailRoutine = null;
yield break;
}
yield return null;
}
_pendingOpenDetailRoutine = null;
if (_cancelPendingOpenDetail || _isDragging || _context == null)
{
yield break;
}
GameEntry.Event.Fire(this,
RepoItemDetailRequestedEventArgs.Create(_context.InstanceId, transform.position));
}
private static bool IsPrimaryPointerPressed()
{
if (Input.GetMouseButton(0))
{
return true;
}
return Input.touchCount > 0;
}
private static void CloseItemDescFormIfOpen()
{
var itemDescForm = GameEntry.UI.GetUIForm(UIFormType.ItemDescForm);
if (itemDescForm != null)
{
GameEntry.UI.CloseUIForm(itemDescForm);
}
}
private RectTransform ResolveDragRoot()
{
Canvas canvas = GetComponentInParent<Canvas>();
if (canvas == null || canvas.rootCanvas == null)
{
return null;
}
return canvas.rootCanvas.transform as RectTransform;
}
private bool CreateDragGhost()
{
DestroyDragGhost();
if (_dragRoot == null || _iconArea == null)
{
return false;
}
Sprite iconSprite = _iconArea.CurrentIconSprite;
if (iconSprite == null)
{
return false;
}
_dragGhostObject = new GameObject("RepoItemDragGhost", typeof(RectTransform), typeof(CanvasGroup), typeof(Image));
_dragGhostObject.layer = gameObject.layer;
_dragGhostRect = _dragGhostObject.GetComponent<RectTransform>();
_dragGhostRect.SetParent(_dragRoot, false);
_dragGhostRect.anchorMin = new Vector2(0.5f, 0.5f);
_dragGhostRect.anchorMax = new Vector2(0.5f, 0.5f);
_dragGhostRect.pivot = new Vector2(0.5f, 0.5f);
Vector2 iconSize = _iconArea.CurrentIconSize;
if (iconSize.x <= 0f || iconSize.y <= 0f)
{
iconSize = DefaultDragGhostSize;
}
_dragGhostRect.sizeDelta = iconSize;
Image ghostImage = _dragGhostObject.GetComponent<Image>();
ghostImage.raycastTarget = false;
ghostImage.sprite = iconSprite;
ghostImage.color = _iconArea.CurrentIconColor;
ghostImage.material = _iconArea.CurrentIconMaterial;
ghostImage.preserveAspect = true;
CanvasGroup ghostCanvasGroup = _dragGhostObject.GetComponent<CanvasGroup>();
ghostCanvasGroup.blocksRaycasts = false;
ghostCanvasGroup.interactable = false;
_dragGhostObject.transform.SetAsLastSibling();
return true;
}
private void UpdateDragGhostPosition(PointerEventData eventData)
{
if (_dragGhostRect == null || _dragRoot == null || eventData == null)
{
return;
}
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(_dragRoot, eventData.position,
eventData.pressEventCamera, out Vector2 localPoint))
{
_dragGhostRect.anchoredPosition = localPoint;
}
}
private void DestroyDragGhost()
{
if (_dragGhostObject != null)
{
Destroy(_dragGhostObject);
_dragGhostObject = null;
_dragGhostRect = null;
}
}
private void ResetDragState()
{
_isDragging = false;
_dropHandled = false;
_dropAssigned = false;
_dragRoot = null;
DestroyDragGhost();
if (_canvasGroup != null)
{
_canvasGroup.blocksRaycasts = true;
}
}
}
}