修正循环列表GetItem,增加普通列表组件。

This commit is contained in:
ALEXTANG
2023-12-06 14:40:03 +08:00
parent 818a74f437
commit 15735c3d2d
5 changed files with 154 additions and 21 deletions

View File

@@ -9,10 +9,10 @@ namespace GameLogic
/// <summary> /// <summary>
/// UI列表Item /// UI列表Item
/// </summary> /// </summary>
/// <typeparam name="DataT"></typeparam> /// <typeparam name="TData"></typeparam>
public interface IListDataItem<in DataT> public interface IListDataItem<in TData>
{ {
void SetItemData(DataT d); void SetItemData(TData d);
} }
/// <summary> /// <summary>
@@ -180,8 +180,7 @@ namespace GameLogic
/// <param name="n"></param> /// <param name="n"></param>
public void SetDatas(List<DataT> dataList, int n = -1) public void SetDatas(List<DataT> dataList, int n = -1)
{ {
AdjustItemNum(Mathf.Max(0, n >= 0 ? n : (dataList == null ? 0 : (dataList.Count - dataStartOffset))), AdjustItemNum(Mathf.Max(0, n >= 0 ? n : (dataList == null ? 0 : (dataList.Count - dataStartOffset))), dataList);
dataList);
} }
/// <summary> /// <summary>
@@ -215,8 +214,7 @@ namespace GameLogic
return; return;
} }
var listDataItem = item as IListDataItem<DataT>; if (item is IListDataItem<DataT> listDataItem)
if (listDataItem != null)
{ {
listDataItem.SetItemData(GetData(i)); listDataItem.SetItemData(GetData(i));
} }
@@ -271,8 +269,7 @@ namespace GameLogic
var preIndex = selectIndex; var preIndex = selectIndex;
m_selectIndex = i; m_selectIndex = i;
var item = GetItem(preIndex) as IListSelectItem; if (GetItem(preIndex) is IListSelectItem item)
if (item != null)
{ {
item.SetSelected(false); item.SetSelected(false);
} }
@@ -282,13 +279,20 @@ namespace GameLogic
{ {
item.SetSelected(true); item.SetSelected(true);
} }
UpdateSnapTargetItem();
if (triggerEvt && funcOnSelectChange != null) if (triggerEvt && funcOnSelectChange != null)
{ {
funcOnSelectChange.Invoke(); funcOnSelectChange.Invoke();
} }
} }
/// <summary>
/// 刷新Snap
/// </summary>
protected virtual void UpdateSnapTargetItem()
{
}
/// <summary> /// <summary>
/// 获取当前选中的数据 /// 获取当前选中的数据
/// </summary> /// </summary>
@@ -312,13 +316,18 @@ namespace GameLogic
/// <summary> /// <summary>
/// 获取item /// 获取item
/// </summary> /// </summary>
/// <param name="index"></param> /// <param name="i"></param>
/// <returns></returns> /// <returns></returns>
public virtual ItemT GetItem(int index) public virtual ItemT GetItem(int i)
{ {
return null; return null;
} }
/// <summary>
/// 点击选择
/// </summary>
public bool SelectByClick = true;
/// <summary> /// <summary>
/// item被点击 /// item被点击
/// </summary> /// </summary>
@@ -331,7 +340,10 @@ namespace GameLogic
funcOnItemClick.Invoke(i); funcOnItemClick.Invoke(i);
} }
if (SelectByClick)
{
selectIndex = i; selectIndex = i;
} }
} }
} }
}

View File

@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using TEngine;
namespace GameLogic
{
/// <summary>
/// 普通UI列表。
/// </summary>
public class UIListWidget<TItem, TData> : UIListBase<TItem, TData> where TItem : UIWidget, new()
{
/// <summary>
/// item列表。
/// </summary>
protected List<TItem> m_items = new List<TItem>();
/// <summary>
/// item列表。
/// </summary>
public List<TItem> items => m_items;
/// <summary>
/// 设置显示数据。
/// </summary>
/// <param name="n"></param>
/// <param name="datas"></param>
/// <param name="funcItem"></param>
protected override void AdjustItemNum(int n, List<TData> datas = null, Action<TItem, int> funcItem = null)
{
base.AdjustItemNum(n, datas, funcItem);
AdjustIconNum(m_items, n, gameObject.transform, itemBase);
UpdateList(funcItem);
}
/// <summary>
/// 刷新列表。
/// </summary>
/// <param name="funcItem"></param>
protected void UpdateList(Action<TItem, int> funcItem = null)
{
for (var i = 0; i < m_items.Count; i++)
{
UpdateListItem(m_items[i], i, funcItem);
}
}
/// <summary>
/// 获取item
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override TItem GetItem(int i)
{
return i >= 0 && i < m_items.Count ? m_items[i] : null;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 96f732d895e94fbc99c904d66ca844ca
timeCreated: 1701844130

View File

@@ -66,6 +66,7 @@ namespace GameLogic
LoopRectView.SetListItemCount(n); LoopRectView.SetListItemCount(n);
LoopRectView.RefreshAllShownItem(); LoopRectView.RefreshAllShownItem();
m_tpFuncItem = null; m_tpFuncItem = null;
UpdateAllItemSelect();
} }
/// <summary> /// <summary>
@@ -152,7 +153,16 @@ namespace GameLogic
/// <returns></returns> /// <returns></returns>
public override TItem GetItem(int index) public override TItem GetItem(int index)
{ {
return index >= 0 && index < m_itemCache.Count ? m_itemCache.GetValueByIndex(index) : null; for (var i = 0; i < m_itemCache.Count; i++)
{
var item = m_itemCache.GetValueByIndex(i);
if (item.GetItemIndex() == index)
{
return item;
}
}
return null;
} }
/// <summary> /// <summary>
@@ -178,5 +188,21 @@ namespace GameLogic
{ {
return m_itemCache.GetValueByIndex(index); return m_itemCache.GetValueByIndex(index);
} }
/// <summary>
/// 刷新所有item选中状态
/// </summary>
/// <returns></returns>
public void UpdateAllItemSelect()
{
var index = selectIndex;
for (var i = 0; i < m_itemCache.Count; i++)
{
if (m_itemCache.GetValueByIndex(i) is IListSelectItem item)
{
item.SetSelected(item.GetItemIndex() == index);
}
}
}
} }
} }

View File

@@ -65,8 +65,9 @@ namespace GameLogic
base.AdjustItemNum(n, datas, funcItem); base.AdjustItemNum(n, datas, funcItem);
m_tpFuncItem = funcItem; m_tpFuncItem = funcItem;
LoopRectView.SetListItemCount(n); LoopRectView.SetListItemCount(n);
// LoopRectView.RefreshAllShownItem(); LoopRectView.RefreshAllShownItem();
m_tpFuncItem = null; m_tpFuncItem = null;
UpdateAllItemSelect();
} }
/// <summary> /// <summary>
@@ -151,7 +152,16 @@ namespace GameLogic
/// <returns></returns> /// <returns></returns>
public override TItem GetItem(int index) public override TItem GetItem(int index)
{ {
return index >= 0 && index < m_itemCache.Count ? m_itemCache.GetValueByIndex(index) : null; for (var i = 0; i < m_itemCache.Count; i++)
{
var item = m_itemCache.GetValueByIndex(i);
if (item.GetItemIndex() == index)
{
return item;
}
}
return null;
} }
/// <summary> /// <summary>
@@ -186,5 +196,30 @@ namespace GameLogic
{ {
return m_itemCache.GetValueByIndex(index); return m_itemCache.GetValueByIndex(index);
} }
/// <summary>
/// 刷新所有item选中状态
/// </summary>
/// <returns></returns>
public void UpdateAllItemSelect()
{
var index = selectIndex;
for (var i = 0; i < m_itemCache.Count; i++)
{
if (m_itemCache.GetValueByIndex(i) is IListSelectItem item)
{
item.SetSelected(item.GetItemIndex() == index);
}
}
}
protected override void UpdateSnapTargetItem()
{
base.UpdateSnapTargetItem();
if (LoopRectView != null && LoopRectView.ItemSnapEnable)
{
LoopRectView.SetSnapTargetItemIndex(selectIndex);
}
}
} }
} }