修复循环列表主动GetItemByIndex和GetItemList的问题

修复循环列表主动GetItemByIndex和GetItemList的问题
This commit is contained in:
ALEXTANG
2023-10-26 13:12:35 +08:00
parent 119d9683ad
commit 6992d12c6c
6 changed files with 159 additions and 11 deletions

View File

@@ -277,7 +277,7 @@ namespace GameLogic
item.SetSelected(false);
}
item = GetItem(m_selectIndex) as IListSelectItem;
item = GetItem(selectIndex) as IListSelectItem;
if (item != null)
{
item.SetSelected(true);

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using TEngine;
using UnityEngine;
namespace GameLogic
@@ -17,7 +18,7 @@ namespace GameLogic
/// <summary>
/// Item字典
/// </summary>
private Dictionary<int, TItem> m_itemCache = new Dictionary<int, TItem>();
private GameFrameworkDictionary<int, TItem> m_itemCache = new GameFrameworkDictionary<int, TItem>();
/// <summary>
/// 计算偏差后的ItemList
@@ -163,9 +164,19 @@ namespace GameLogic
m_items.Clear();
for (int i = 0; i < m_itemCache.Count; i++)
{
m_items.Add(m_itemCache[i]);
m_items.Add(m_itemCache.GetValueByIndex(i));
}
return m_items;
}
/// <summary>
/// 获取Item。
/// </summary>
/// <param name="index">索引。</param>
/// <returns>TItem。</returns>
public TItem GetItemByIndex(int index)
{
return m_itemCache.GetValueByIndex(index);
}
}
}

View File

@@ -8,7 +8,7 @@ namespace GameLogic
{
public LoopListView LoopRectView { private set; get; }
private Dictionary<int, T> m_itemCache = new Dictionary<int, T>();
private GameFrameworkDictionary<int, T> m_itemCache = new GameFrameworkDictionary<int, T>();
public override void BindMemberProperty()
{
@@ -71,9 +71,8 @@ namespace GameLogic
List<T> list = new List<T>();
for (int i = 0; i < m_itemCache.Count; i++)
{
list.Add(m_itemCache[i]);
list.Add(m_itemCache.GetValueByIndex(i));
}
return list;
}
@@ -85,11 +84,11 @@ namespace GameLogic
/// <summary>
/// 获取Item。
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
/// <param name="index">索引。</param>
/// <returns>TItem。</returns>
public T GetItemByIndex(int index)
{
return m_itemCache[index];
return m_itemCache.GetValueByIndex(index);
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using TEngine;
using UnityEngine;
namespace GameLogic
@@ -16,8 +17,9 @@ namespace GameLogic
/// <summary>
/// Item字典
/// <remarks>Key => GameObjectHashCode | Value => TItem.</remarks>
/// </summary>
private Dictionary<int, TItem> m_itemCache = new Dictionary<int, TItem>();
private GameFrameworkDictionary<int, TItem> m_itemCache = new GameFrameworkDictionary<int, TItem>();
/// <summary>
/// 计算偏差后的ItemList
@@ -159,7 +161,10 @@ namespace GameLogic
public List<TItem> GetItemList()
{
m_items.Clear();
m_items.AddRange(m_itemCache.Values);
for (int i = 0; i < m_itemCache.Count; i++)
{
m_items.Add(m_itemCache.GetValueByIndex(i));
}
return m_items;
}
@@ -171,5 +176,15 @@ namespace GameLogic
{
return LoopRectView.GetItemStartIndex();
}
/// <summary>
/// 获取Item。
/// </summary>
/// <param name="index">索引。</param>
/// <returns>TItem。</returns>
public TItem GetItemByIndex(int index)
{
return m_itemCache.GetValueByIndex(index);
}
}
}

View File

@@ -0,0 +1,120 @@
using System.Collections.Generic;
namespace TEngine
{
/// <summary>
/// 游戏框架字典类。
/// </summary>
/// <typeparam name="TKey">指定字典Key的元素类型。</typeparam>
/// <typeparam name="TValue">指定字典Value的元素类型。</typeparam>
public class GameFrameworkDictionary<TKey, TValue>
{
protected readonly List<TKey> KeyList = new List<TKey>();
protected readonly Dictionary<TKey, TValue> Dictionary = new Dictionary<TKey, TValue>();
/// <summary>
/// 存储键的列表。
/// </summary>
public List<TKey> Keys => KeyList;
/// <summary>
/// 存储字典实例。
/// </summary>
public int Count => KeyList.Count;
/// <summary>
/// 通过KEY的数组下标获取元素。
/// </summary>
/// <param name="index">下标。</param>
/// <returns>TValue。</returns>
public TValue GetValueByIndex(int index)
{
return Dictionary[KeyList[index]];
}
/// <summary>
/// 通过KEY的数组下标设置元素。
/// </summary>
/// <param name="index">下标。</param>
/// <param name="item">TValue。</param>
public void SetValue(int index, TValue item)
{
Dictionary[KeyList[index]] = item;
}
/// <summary>
/// 字典索引器。
/// </summary>
/// <param name="key">TKey。</param>
public TValue this[TKey key]
{
get => Dictionary[key];
set
{
if (!ContainsKey(key))
{
Add(key, value);
}
else
{
Dictionary[key] = value;
}
}
}
/// <summary>Removes all keys and values from the <see cref="T:TEngine.GameFrameworkDictionary`2" />.</summary>
public void Clear()
{
KeyList.Clear();
Dictionary.Clear();
}
/// <summary>Adds the specified key and value to the dictionary.</summary>
/// <param name="key">The key of the element to add.</param>
/// <param name="item">The value of the element to add. The value can be <see langword="null" /> for reference types.</param>
public virtual void Add(TKey key, TValue item)
{
KeyList.Add(key);
Dictionary.Add(key, item);
}
/// <summary>Gets the value associated with the specified key.</summary>
/// <param name="key">The key of the value to get.</param>
/// <param name="value">When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the <paramref name="value" /> parameter. This parameter is passed uninitialized.</param>
public bool TryGetValue(TKey key, out TValue value)
{
return Dictionary.TryGetValue(key, out value);
}
/// <summary>Determines whether the <see cref="T:System.Collections.Generic.Dictionary`2" /> contains the specified key.</summary>
/// <param name="key">The key to locate in the </param>
public bool ContainsKey(TKey key)
{
return Dictionary.ContainsKey(key);
}
public TKey GetKey(int index)
{
return KeyList[index];
}
public bool Remove(TKey key)
{
return KeyList.Remove(key) && Dictionary.Remove(key);
}
}
/// <summary>
/// 游戏框架顺序字典类。
/// </summary>
/// <typeparam name="TKey">指定字典Key的元素类型。</typeparam>
/// <typeparam name="TValue">指定字典Value的元素类型。</typeparam>
public class GameFrameworkSortedDictionary<TKey, TValue> : GameFrameworkDictionary<TKey, TValue>
{
public override void Add(TKey key, TValue item)
{
base.Add(key, item);
KeyList.Sort();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3df785fede30420dbfa6acb2ff48c166
timeCreated: 1698296076