278 lines
7.5 KiB
C#
278 lines
7.5 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Thousandto.Plugins.Common;
|
|
using Thousandto.Core.Base;
|
|
|
|
namespace Thousandto.Plugins.Common
|
|
{
|
|
/// <summary>
|
|
/// UI组件类
|
|
/// </summary>
|
|
/// <typeparam name="TUI"></typeparam>
|
|
/// <typeparam name="TData"></typeparam>
|
|
public class UComponentContainerPlus<TUI, TData> where TUI : IUIComponentPlus<TUI, TData>
|
|
{
|
|
#region//私有变量
|
|
private List<TUI> _free = new List<TUI>();
|
|
private Dictionary<int, TData> _keyDic = new Dictionary<int, TData>();
|
|
private Dictionary<TData, TUI> _containerDic = new Dictionary<TData, TUI>();
|
|
#endregion
|
|
|
|
#region//公共变量
|
|
#endregion
|
|
|
|
#region//公共接口
|
|
//添加组件
|
|
public void AddComponent(TUI ui, TData data, int key)
|
|
{
|
|
if (_containerDic != null)
|
|
{
|
|
if (_free.Count == 0)
|
|
{
|
|
if (_containerDic.Count == 0)
|
|
{
|
|
_containerDic[data] = ui;
|
|
}
|
|
else
|
|
{
|
|
_containerDic[data] = ui;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
TUI tUi = _free[0];
|
|
for (int i = 0; i < _free.Count; i++)
|
|
{
|
|
if (tUi.Index > _free[i].Index)
|
|
{
|
|
tUi = _free[i];
|
|
}
|
|
}
|
|
_containerDic[data] = tUi;
|
|
_free.Remove(tUi);
|
|
}
|
|
}
|
|
if (!_keyDic.ContainsKey(key))
|
|
{
|
|
_keyDic[key] = data;
|
|
}
|
|
}
|
|
|
|
public void CloneComponent(TData data, int key,EventDelegate.Callback call, int index = 0)
|
|
{
|
|
TUI ui = default(TUI);
|
|
if (_free.Count != 0)
|
|
{
|
|
if (!_containerDic.ContainsKey(data))
|
|
{
|
|
ui = _free[0];
|
|
for (int i = 0; i < _free.Count; i++)
|
|
{
|
|
if (ui.Index > _free[i].Index)
|
|
{
|
|
ui = _free[i];
|
|
}
|
|
}
|
|
_containerDic[data] = ui;
|
|
_free.Remove(ui);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (_containerDic.Count != 0)
|
|
{
|
|
int i = 0;
|
|
var enumer = _containerDic.GetEnumerator();
|
|
try
|
|
{
|
|
while (enumer.MoveNext())
|
|
{
|
|
if (i == 0)
|
|
{
|
|
i++;
|
|
if (!_containerDic.ContainsKey(data))
|
|
{
|
|
ui = enumer.Current.Value.Clone(call, index);
|
|
_containerDic[data] = ui;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
enumer.Dispose();
|
|
}
|
|
}
|
|
}
|
|
if (!_keyDic.ContainsKey(key))
|
|
{
|
|
_keyDic[key] = data;
|
|
}
|
|
}
|
|
|
|
//返回长度
|
|
public int GetComponentLength()
|
|
{
|
|
return _containerDic.Count;
|
|
}
|
|
|
|
//返回组件数据
|
|
public TData GetComponentKey(int key)
|
|
{
|
|
TData data = default(TData);
|
|
if (_keyDic.ContainsKey(key))
|
|
{
|
|
return _keyDic[key];
|
|
}
|
|
return data;
|
|
}
|
|
|
|
//获取组件
|
|
public TUI GetComponent(int key)
|
|
{
|
|
TUI ui = default(TUI);
|
|
TData data = GetComponentKey(key);
|
|
ui = GetComponent(data);
|
|
return ui;
|
|
}
|
|
|
|
//获取组件
|
|
public TUI GetComponent(TData data)
|
|
{
|
|
TUI ui = default(TUI);
|
|
if (_containerDic.ContainsKey(data))
|
|
{
|
|
_containerDic.TryGetValue(data, out ui);
|
|
}
|
|
return ui;
|
|
}
|
|
|
|
//移除
|
|
public void RemveComponent(int key)
|
|
{
|
|
TData data = GetComponentKey(key);
|
|
RemoveComponent(data);
|
|
if (_keyDic.ContainsKey(key))
|
|
_keyDic.Remove(key);
|
|
}
|
|
|
|
//移除
|
|
public void RemoveComponent(TData data)
|
|
{
|
|
if (_containerDic.ContainsKey(data))
|
|
{
|
|
_containerDic.Remove(data);
|
|
}
|
|
}
|
|
|
|
//刷新组件
|
|
public void RefreshComponent( TData data)
|
|
{
|
|
if (_containerDic.ContainsKey(data))
|
|
{
|
|
_containerDic[data].SetData(data);
|
|
}
|
|
}
|
|
|
|
//刷新 选中组件
|
|
public void RefreshSelect(int selectId)
|
|
{
|
|
TData data = default(TData);
|
|
if (_keyDic.ContainsKey(selectId))
|
|
{
|
|
data = _keyDic[selectId];
|
|
}
|
|
var enumer = _containerDic.GetEnumerator();
|
|
try
|
|
{
|
|
while (enumer.MoveNext())
|
|
{
|
|
enumer.Current.Value.SetSelect(false);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
enumer.Dispose();
|
|
}
|
|
if (_containerDic.ContainsKey(data))
|
|
{
|
|
_containerDic[data].SetSelect(true);
|
|
}
|
|
}
|
|
//
|
|
public void SetActive(bool b)
|
|
{
|
|
var enumer = _containerDic.GetEnumerator();
|
|
try
|
|
{
|
|
while (enumer.MoveNext())
|
|
{
|
|
enumer.Current.Value.SetActive(b);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
enumer.Dispose();
|
|
}
|
|
}
|
|
public void SetActive(TData data)
|
|
{
|
|
if (_containerDic != null)
|
|
{
|
|
if (_containerDic.ContainsKey(data))
|
|
{
|
|
_containerDic[data].SetActive(true);
|
|
}
|
|
}
|
|
}
|
|
public bool ContainsKey(int key)
|
|
{
|
|
if (_keyDic != null)
|
|
{
|
|
if (_keyDic.ContainsKey(key))
|
|
{
|
|
if (_containerDic != null)
|
|
{
|
|
return _containerDic.ContainsKey(_keyDic[key]);
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
//清除
|
|
public void OnClear( int key )
|
|
{
|
|
_containerDic.Clear();
|
|
_keyDic.Clear();
|
|
}
|
|
|
|
public void Free()
|
|
{
|
|
if (_keyDic != null)
|
|
{
|
|
_keyDic.Clear();
|
|
}
|
|
if (_containerDic != null)
|
|
{
|
|
var enumer = _containerDic.GetEnumerator();
|
|
try
|
|
{
|
|
while (enumer.MoveNext())
|
|
{
|
|
_free.Add(enumer.Current.Value);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
enumer.Dispose();
|
|
}
|
|
}
|
|
_containerDic.Clear();
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|