补全 UI 设计
- `ItemDescForm` 道具详细信息面板,目前还未添加 DescText 内容的赋值 - `RepoForm` 道具入槽逻辑调整,为了适应 `ItemDescForm` 的点击打开逻辑,点击道具自动匹配槽位改成拖动道具到相应槽位 - 添加了一系列图片素材
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
using System;
|
||||
using GeometryTD.Definition;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
[Serializable]
|
||||
public class RepoItemContext
|
||||
{
|
||||
public string Title;
|
||||
public long InstanceId;
|
||||
public bool CanDrag;
|
||||
public TowerCompSlotType ComponentSlotType;
|
||||
public IconAreaContext IconAreaContext;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using GeometryTD.CustomEvent;
|
||||
using GeometryTD.Definition;
|
||||
using GameFramework.Event;
|
||||
using GeometryTD.CustomUtility;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
@@ -10,6 +13,14 @@ namespace GeometryTD.UI
|
||||
{
|
||||
private RepoFormUseCase _useCase;
|
||||
private readonly Dictionary<long, RepoItemContext> _itemContextMap = new Dictionary<long, RepoItemContext>();
|
||||
private readonly Dictionary<long, ItemDescSeed> _itemDescSeedMap = new Dictionary<long, ItemDescSeed>();
|
||||
|
||||
private sealed class ItemDescSeed
|
||||
{
|
||||
public string Title;
|
||||
public string TypeText;
|
||||
public TagType[] Tags;
|
||||
}
|
||||
|
||||
protected override UIFormType UIFormTypeId => UIFormType.RepoForm;
|
||||
|
||||
@@ -20,14 +31,18 @@ namespace GeometryTD.UI
|
||||
|
||||
protected override void SubscribeCustomEvents()
|
||||
{
|
||||
GameEntry.Event.Subscribe(RepoItemSelectedEventArgs.EventId, OnRepoItemSelected);
|
||||
GameEntry.Event.Subscribe(RepoItemDetailRequestedEventArgs.EventId, OnRepoItemDetailRequested);
|
||||
GameEntry.Event.Subscribe(RepoItemDragEndedEventArgs.EventId, OnRepoItemDragEnded);
|
||||
GameEntry.Event.Subscribe(CombineSlotClickedEventArgs.EventId, OnCombineSlotClicked);
|
||||
GameEntry.Event.Subscribe(RepoFormReturnEventArgs.EventId, OnRepoFormReturn);
|
||||
}
|
||||
|
||||
protected override void UnsubscribeCustomEvents()
|
||||
{
|
||||
GameEntry.Event.Unsubscribe(RepoItemSelectedEventArgs.EventId, OnRepoItemSelected);
|
||||
GameEntry.Event.Unsubscribe(RepoItemDetailRequestedEventArgs.EventId, OnRepoItemDetailRequested);
|
||||
GameEntry.Event.Unsubscribe(RepoItemDragEndedEventArgs.EventId, OnRepoItemDragEnded);
|
||||
GameEntry.Event.Unsubscribe(CombineSlotClickedEventArgs.EventId, OnCombineSlotClicked);
|
||||
GameEntry.Event.Unsubscribe(RepoFormReturnEventArgs.EventId, OnRepoFormReturn);
|
||||
}
|
||||
|
||||
public override int? OpenUI(object userData = null)
|
||||
@@ -78,6 +93,7 @@ namespace GeometryTD.UI
|
||||
private RepoFormContext BuildContext(RepoFormRawData rawData)
|
||||
{
|
||||
_itemContextMap.Clear();
|
||||
_itemDescSeedMap.Clear();
|
||||
if (rawData?.Inventory == null)
|
||||
{
|
||||
return null;
|
||||
@@ -87,57 +103,82 @@ namespace GeometryTD.UI
|
||||
|
||||
if (rawData.Inventory.Towers != null)
|
||||
{
|
||||
foreach (var tower in rawData.Inventory.Towers)
|
||||
foreach (DefenseTowerItemData tower in rawData.Inventory.Towers)
|
||||
{
|
||||
if (tower == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
AddItemContext(items, new RepoItemContext
|
||||
{
|
||||
Title = $"[Tower] {tower.Name} ({tower.Rarity})",
|
||||
InstanceId = tower.InstanceId,
|
||||
CanDrag = true,
|
||||
ComponentSlotType = TowerCompSlotType.None,
|
||||
IconAreaContext = BuildIconAreaContext(tower.Rarity)
|
||||
IconAreaContext = BuildIconAreaContext(tower)
|
||||
});
|
||||
AddItemDescSeed(tower.InstanceId, tower.Name, "Tower",
|
||||
tower.Stats != null ? tower.Stats.Tags : null);
|
||||
}
|
||||
}
|
||||
|
||||
if (rawData.Inventory.MuzzleComponents != null)
|
||||
{
|
||||
foreach (var item in rawData.Inventory.MuzzleComponents)
|
||||
foreach (MuzzleCompItemData item in rawData.Inventory.MuzzleComponents)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
AddItemContext(items, new RepoItemContext
|
||||
{
|
||||
Title = $"[Muzzle] {item.Name} ({item.Rarity})",
|
||||
InstanceId = item.InstanceId,
|
||||
CanDrag = true,
|
||||
ComponentSlotType = TowerCompSlotType.Muzzle,
|
||||
IconAreaContext = BuildIconAreaContext(item.Rarity)
|
||||
IconAreaContext = BuildIconAreaContext(item)
|
||||
});
|
||||
AddItemDescSeed(item.InstanceId, item.Name, BuildComponentTypeText(item.SlotType), item.Tags);
|
||||
}
|
||||
}
|
||||
|
||||
if (rawData.Inventory.BearingComponents != null)
|
||||
{
|
||||
foreach (var item in rawData.Inventory.BearingComponents)
|
||||
foreach (BearingCompItemData item in rawData.Inventory.BearingComponents)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
AddItemContext(items, new RepoItemContext
|
||||
{
|
||||
Title = $"[Bearing] {item.Name} ({item.Rarity})",
|
||||
InstanceId = item.InstanceId,
|
||||
CanDrag = true,
|
||||
ComponentSlotType = TowerCompSlotType.Bearing,
|
||||
IconAreaContext = BuildIconAreaContext(item.Rarity)
|
||||
IconAreaContext = BuildIconAreaContext(item)
|
||||
});
|
||||
AddItemDescSeed(item.InstanceId, item.Name, BuildComponentTypeText(item.SlotType), item.Tags);
|
||||
}
|
||||
}
|
||||
|
||||
if (rawData.Inventory.BaseComponents != null)
|
||||
{
|
||||
foreach (var item in rawData.Inventory.BaseComponents)
|
||||
foreach (BaseCompItemData item in rawData.Inventory.BaseComponents)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
AddItemContext(items, new RepoItemContext
|
||||
{
|
||||
Title = $"[Base] {item.Name} ({item.Rarity})",
|
||||
InstanceId = item.InstanceId,
|
||||
CanDrag = true,
|
||||
ComponentSlotType = TowerCompSlotType.Base,
|
||||
IconAreaContext = BuildIconAreaContext(item.Rarity)
|
||||
IconAreaContext = BuildIconAreaContext(item)
|
||||
});
|
||||
AddItemDescSeed(item.InstanceId, item.Name, BuildComponentTypeText(item.SlotType), item.Tags);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,36 +203,139 @@ namespace GeometryTD.UI
|
||||
_itemContextMap[itemContext.InstanceId] = itemContext;
|
||||
}
|
||||
|
||||
private static IconAreaContext BuildIconAreaContext(RarityType rarity)
|
||||
private void AddItemDescSeed(long itemId, string title, string typeText, TagType[] tags)
|
||||
{
|
||||
if (itemId <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_itemDescSeedMap[itemId] = new ItemDescSeed
|
||||
{
|
||||
Title = string.IsNullOrWhiteSpace(title) ? $"Item {itemId}" : title,
|
||||
TypeText = typeText ?? string.Empty,
|
||||
Tags = CloneTags(tags)
|
||||
};
|
||||
}
|
||||
|
||||
private static TagType[] CloneTags(TagType[] tags)
|
||||
{
|
||||
return tags != null ? (TagType[])tags.Clone() : Array.Empty<TagType>();
|
||||
}
|
||||
|
||||
private static string BuildComponentTypeText(TowerCompSlotType slotType)
|
||||
{
|
||||
return slotType switch
|
||||
{
|
||||
TowerCompSlotType.Muzzle => "Muzzle Component",
|
||||
TowerCompSlotType.Bearing => "Bearing Component",
|
||||
TowerCompSlotType.Base => "Base Component",
|
||||
TowerCompSlotType.Accessory => "Accessory",
|
||||
_ => "Component"
|
||||
};
|
||||
}
|
||||
|
||||
private static IconAreaContext BuildIconAreaContext(DefenseTowerItemData tower)
|
||||
{
|
||||
if (tower == null)
|
||||
{
|
||||
return new IconAreaContext
|
||||
{
|
||||
ComponentSlotType = TowerCompSlotType.None,
|
||||
Rarity = RarityType.None,
|
||||
Color = Color.white,
|
||||
Icon = null
|
||||
};
|
||||
}
|
||||
|
||||
return new IconAreaContext
|
||||
{
|
||||
Rarity = rarity,
|
||||
ComponentSlotType = TowerCompSlotType.None,
|
||||
Rarity = tower.Rarity,
|
||||
Color = IconColorGenerator.GenerateForTower(tower),
|
||||
Icon = null
|
||||
};
|
||||
}
|
||||
|
||||
private void OnRepoItemSelected(object sender, GameEventArgs e)
|
||||
private static IconAreaContext BuildIconAreaContext(TowerCompItemData item)
|
||||
{
|
||||
if (!(e is RepoItemSelectedEventArgs args))
|
||||
if (item == null)
|
||||
{
|
||||
return new IconAreaContext
|
||||
{
|
||||
ComponentSlotType = TowerCompSlotType.None,
|
||||
Rarity = RarityType.None,
|
||||
Color = Color.white,
|
||||
Icon = null
|
||||
};
|
||||
}
|
||||
|
||||
return new IconAreaContext
|
||||
{
|
||||
ComponentSlotType = item.SlotType,
|
||||
Rarity = item.Rarity,
|
||||
Color = IconColorGenerator.GenerateForComponent(item),
|
||||
Icon = null
|
||||
};
|
||||
}
|
||||
|
||||
#region Event Handlers
|
||||
|
||||
private void OnRepoItemDetailRequested(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!IsEventFromCurrentForm(sender))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Form == null || !_itemContextMap.TryGetValue(args.ItemId, out RepoItemContext itemContext))
|
||||
if (!(e is RepoItemDetailRequestedEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool assigned = Form.TryAssignItemToCombineArea(itemContext);
|
||||
if (assigned)
|
||||
if (!_itemDescSeedMap.TryGetValue(args.ItemId, out ItemDescSeed seed))
|
||||
{
|
||||
Form.SetRepoItemSelected(args.ItemId, true);
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.UIRouter.OpenUI(UIFormType.ItemDescForm, new ItemDescFormRawData
|
||||
{
|
||||
Title = seed.Title,
|
||||
TypeText = seed.TypeText,
|
||||
Description = string.Empty,
|
||||
Price = 0,
|
||||
TargetPos = args.TargetPos,
|
||||
Tags = CloneTags(seed.Tags)
|
||||
});
|
||||
}
|
||||
|
||||
private void OnRepoItemDragEnded(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!IsEventFromCurrentForm(sender))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(e is RepoItemDragEndedEventArgs args))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Form == null || !_itemContextMap.ContainsKey(args.ItemId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Form.SetRepoItemSelected(args.ItemId, args.Assigned);
|
||||
}
|
||||
|
||||
private void OnCombineSlotClicked(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!IsEventFromCurrentForm(sender))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(e is CombineSlotClickedEventArgs args))
|
||||
{
|
||||
return;
|
||||
@@ -207,5 +351,43 @@ namespace GeometryTD.UI
|
||||
Form.SetRepoItemSelected(removedItemId, false);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRepoFormReturn(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!IsEventFromCurrentForm(sender) || !(e is RepoFormReturnEventArgs))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Form == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.CloseUI();
|
||||
}
|
||||
|
||||
private bool IsEventFromCurrentForm(object sender)
|
||||
{
|
||||
if (Form == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(sender, Form))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (sender is Component component)
|
||||
{
|
||||
RepoForm ownerForm = component.GetComponentInParent<RepoForm>();
|
||||
return ownerForm == Form;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user