修正循环列表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>
/// UI列表Item
/// </summary>
/// <typeparam name="DataT"></typeparam>
public interface IListDataItem<in DataT>
/// <typeparam name="TData"></typeparam>
public interface IListDataItem<in TData>
{
void SetItemData(DataT d);
void SetItemData(TData d);
}
/// <summary>
@@ -180,8 +180,7 @@ namespace GameLogic
/// <param name="n"></param>
public void SetDatas(List<DataT> dataList, int n = -1)
{
AdjustItemNum(Mathf.Max(0, n >= 0 ? n : (dataList == null ? 0 : (dataList.Count - dataStartOffset))),
dataList);
AdjustItemNum(Mathf.Max(0, n >= 0 ? n : (dataList == null ? 0 : (dataList.Count - dataStartOffset))), dataList);
}
/// <summary>
@@ -215,8 +214,7 @@ namespace GameLogic
return;
}
var listDataItem = item as IListDataItem<DataT>;
if (listDataItem != null)
if (item is IListDataItem<DataT> listDataItem)
{
listDataItem.SetItemData(GetData(i));
}
@@ -271,8 +269,7 @@ namespace GameLogic
var preIndex = selectIndex;
m_selectIndex = i;
var item = GetItem(preIndex) as IListSelectItem;
if (item != null)
if (GetItem(preIndex) is IListSelectItem item)
{
item.SetSelected(false);
}
@@ -282,13 +279,20 @@ namespace GameLogic
{
item.SetSelected(true);
}
UpdateSnapTargetItem();
if (triggerEvt && funcOnSelectChange != null)
{
funcOnSelectChange.Invoke();
}
}
/// <summary>
/// 刷新Snap
/// </summary>
protected virtual void UpdateSnapTargetItem()
{
}
/// <summary>
/// 获取当前选中的数据
/// </summary>
@@ -312,13 +316,18 @@ namespace GameLogic
/// <summary>
/// 获取item
/// </summary>
/// <param name="index"></param>
/// <param name="i"></param>
/// <returns></returns>
public virtual ItemT GetItem(int index)
public virtual ItemT GetItem(int i)
{
return null;
}
/// <summary>
/// 点击选择
/// </summary>
public bool SelectByClick = true;
/// <summary>
/// item被点击
/// </summary>
@@ -331,7 +340,10 @@ namespace GameLogic
funcOnItemClick.Invoke(i);
}
selectIndex = i;
if (SelectByClick)
{
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.RefreshAllShownItem();
m_tpFuncItem = null;
UpdateAllItemSelect();
}
/// <summary>
@@ -145,14 +146,23 @@ namespace GameLogic
return widget;
}
/// <summary>
/// 获取item
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
/// <summary>
/// 获取item
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
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>
@@ -178,5 +188,21 @@ namespace GameLogic
{
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);
m_tpFuncItem = funcItem;
LoopRectView.SetListItemCount(n);
// LoopRectView.RefreshAllShownItem();
LoopRectView.RefreshAllShownItem();
m_tpFuncItem = null;
UpdateAllItemSelect();
}
/// <summary>
@@ -151,7 +152,16 @@ namespace GameLogic
/// <returns></returns>
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>
@@ -186,5 +196,30 @@ namespace GameLogic
{
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);
}
}
}
}