197 lines
5.1 KiB
C#
197 lines
5.1 KiB
C#
using Thousandto.Core.Base;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Thousandto.Plugins.Common
|
|
{
|
|
/// <summary>
|
|
/// UI组件的容器
|
|
/// </summary>
|
|
public class UIComponentContainer<TUI,TData>
|
|
where TUI:IUIComponent<TUI,TData>
|
|
{
|
|
#region //私有变量
|
|
//空闲队列
|
|
private List<TUI> _freeList = new List<TUI>();
|
|
|
|
//使用的队列
|
|
private Dictionary<TData, TUI> _usedList = new Dictionary<TData, TUI>();
|
|
|
|
//模板
|
|
private TUI _template = default(TUI);
|
|
|
|
//新的对象回调
|
|
private MyAction<TUI> _newCallBack = null;
|
|
|
|
//临时列表用于保存中间数据
|
|
private List<TUI> _tmpList = new List<TUI>();
|
|
#endregion
|
|
|
|
#region //公共函数
|
|
//设置创建一个新的对象回调
|
|
public void SetNewCallBack(MyAction<TUI> callBack)
|
|
{
|
|
_newCallBack = callBack;
|
|
}
|
|
|
|
//清理
|
|
public void Clear()
|
|
{
|
|
OnClear();
|
|
}
|
|
|
|
//添加新的组件
|
|
public void AddNewComponent(TUI compInfo)
|
|
{
|
|
_freeList.Add(compInfo);
|
|
compInfo.SetActive(false);
|
|
if (_freeList.Count == 1)
|
|
{
|
|
SetTemplate();
|
|
}
|
|
}
|
|
|
|
//设置模板
|
|
public void SetTemplate(TUI btn = default(TUI))
|
|
{
|
|
_template = btn;
|
|
if (btn == null)
|
|
{
|
|
if (_freeList.Count > 0)
|
|
{
|
|
_template = _freeList[0];
|
|
}
|
|
}
|
|
}
|
|
|
|
//把所有的组件都体会到Free队列
|
|
public void EnQueueAll()
|
|
{
|
|
if (_usedList.Count > 0)
|
|
{
|
|
_tmpList.Clear();
|
|
var e = _usedList.GetEnumerator();
|
|
try
|
|
{
|
|
while (e.MoveNext())
|
|
{
|
|
var button = e.Current.Value;
|
|
if (button != null)
|
|
{
|
|
button.SetActive(false);
|
|
button.SetName("_");
|
|
_tmpList.Add(button);
|
|
}
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
e.Dispose();
|
|
}
|
|
_usedList.Clear();
|
|
//这里做翻转,是为了让空闲队列保存他原来的顺序
|
|
_tmpList.Reverse();
|
|
_freeList.AddRange(_tmpList);
|
|
_tmpList.Clear();
|
|
}
|
|
}
|
|
|
|
//把某个对象回退掉队列中
|
|
public void EnQueue(TData type)
|
|
{
|
|
if (_usedList.ContainsKey(type))
|
|
{
|
|
var button = _usedList[type];
|
|
if (button != null)
|
|
{
|
|
button.SetActive(false);
|
|
button.SetName("_");
|
|
_freeList.Add(button);
|
|
}
|
|
_usedList.Remove(type);
|
|
}
|
|
}
|
|
|
|
//从队列中获取一个
|
|
public TUI DeQueue(TData type)
|
|
{
|
|
TUI result = default(TUI);
|
|
if (_freeList.Count > 0)
|
|
{
|
|
//从空闲表中读取最后一个
|
|
int idx = _freeList.Count - 1;
|
|
result = _freeList[idx];
|
|
_freeList.RemoveAt(idx);
|
|
}
|
|
else
|
|
{
|
|
if (_template != null)
|
|
{
|
|
result = _template.Clone();
|
|
if (_newCallBack != null)
|
|
{
|
|
_newCallBack(result);
|
|
}
|
|
}
|
|
}
|
|
if (result != null)
|
|
{
|
|
result.SetData(type);
|
|
result.SetActive(true);
|
|
_usedList[type] = result;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
//获取使用的UI对象
|
|
public TUI GetUsedUI(TData type)
|
|
{
|
|
TUI result;
|
|
_usedList.TryGetValue(type,out result);
|
|
return result;
|
|
}
|
|
|
|
//获取正在被使用的组件的数量
|
|
public int GetUsedCount()
|
|
{
|
|
return _usedList.Count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重新刷新所有对象的数据
|
|
/// </summary>
|
|
public void RefreshAllUIData()
|
|
{
|
|
if (_usedList.Count > 0)
|
|
{
|
|
var e = _usedList.GetEnumerator();
|
|
try
|
|
{
|
|
while (e.MoveNext())
|
|
{
|
|
e.Current.Value.RefreshData();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
e.Dispose();
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region //保护函数
|
|
//清理
|
|
protected virtual void OnClear()
|
|
{
|
|
_freeList.Clear();
|
|
_usedList.Clear();
|
|
_template = default(TUI);
|
|
}
|
|
#endregion
|
|
|
|
|
|
}
|
|
}
|