146 lines
4.1 KiB
C#
146 lines
4.1 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class UIContainerMultiSelect : UIContainerSelect {
|
||
|
||
public int multiSelectNum = -1; // 多选的情况下,最多可选择数量,默认-1,表示可无限多选
|
||
public UIItemMultiSelect curHightLight; // 当前高亮选中Item。
|
||
|
||
public override void OnSelectedObj(object obj)
|
||
{
|
||
ContentPos selectPos = _ValueList.Find((pos) =>
|
||
{
|
||
if (pos.Obj == obj)
|
||
return true;
|
||
return false;
|
||
});
|
||
|
||
if (selectPos == null)
|
||
return;
|
||
|
||
if (!((UIItemMultiSelect)selectPos.ShowItem).IsCanSelect())
|
||
return;
|
||
|
||
if (curHightLight != null)
|
||
{
|
||
curHightLight.UnSelected();
|
||
((UIItemMultiSelect)selectPos.ShowItem).Selected();
|
||
curHightLight = (UIItemMultiSelect)selectPos.ShowItem;
|
||
}
|
||
else
|
||
{
|
||
if (_Selecteds.Count != 0)
|
||
{
|
||
((UIItemMultiSelect)_Selecteds[0].ShowItem).UnSelected();
|
||
}
|
||
((UIItemMultiSelect)selectPos.ShowItem).Selected();
|
||
curHightLight = (UIItemMultiSelect)selectPos.ShowItem;
|
||
}
|
||
|
||
if (_IsMultiSelect)
|
||
{
|
||
if (_Selecteds.Contains(selectPos))
|
||
{
|
||
((UIItemMultiSelect)selectPos.ShowItem).UnAdd();
|
||
_Selecteds.Remove(selectPos);
|
||
}
|
||
else
|
||
{
|
||
// 当不存在时,仅当多选数目没到上限时,添加
|
||
if (_Selecteds.Count < multiSelectNum)
|
||
{
|
||
((UIItemMultiSelect)selectPos.ShowItem).Add();
|
||
_Selecteds.Add(selectPos);
|
||
}
|
||
// 优化:当多选数目设置为1个时,点击其他视作替换
|
||
else if (multiSelectNum == 1)
|
||
{
|
||
((UIItemMultiSelect)_Selecteds[0].ShowItem).UnAdd();
|
||
_Selecteds.RemoveAt(0);
|
||
|
||
((UIItemMultiSelect)selectPos.ShowItem).Add();
|
||
_Selecteds.Add(selectPos);
|
||
}
|
||
}
|
||
|
||
if (_SelectedCallBack != null)
|
||
{
|
||
_SelectedCallBack(selectPos.Obj);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (!_Selecteds.Contains(selectPos))
|
||
{
|
||
_Selecteds.Clear();
|
||
_Selecteds.Add(selectPos);
|
||
}
|
||
}
|
||
|
||
if (_SelectedCallBack != null && _Selecteds.Contains(selectPos) && !_IsMultiSelect)
|
||
{
|
||
_SelectedCallBack(obj);
|
||
return;
|
||
}
|
||
|
||
if (_DisSelectedCallBack != null && !_Selecteds.Contains(selectPos) && !_IsMultiSelect)
|
||
{
|
||
_DisSelectedCallBack(obj);
|
||
return;
|
||
}
|
||
}
|
||
|
||
public override void InitSelectContent(IEnumerable list, IEnumerable selectedList, SelectedObjCallBack onSelect = null, SelectedObjCallBack onDisSelect = null, Hashtable exhash = null)
|
||
{
|
||
|
||
foreach (ContentPos item in _Selecteds)
|
||
{
|
||
if (item.ShowItem != null)
|
||
{
|
||
((UIItemMultiSelect)item.ShowItem).UnSelected();
|
||
((UIItemMultiSelect)item.ShowItem).UnAdd();
|
||
}
|
||
}
|
||
|
||
curHightLight = null;
|
||
|
||
_SelectedCallBack = onSelect;
|
||
_DisSelectedCallBack = onDisSelect;
|
||
|
||
gameObject.SetActive(true);
|
||
|
||
_SelectedList = selectedList;
|
||
|
||
base.InitContentItem(list, OnSelectedObj, exhash);
|
||
|
||
ShowItems();
|
||
}
|
||
|
||
public override List<T> GetSelecteds<T>()
|
||
{
|
||
List<T> selectedObjs = new List<T>();
|
||
if (_IsMultiSelect == false)
|
||
{
|
||
if (_Selecteds.Count > 0)
|
||
{
|
||
selectedObjs.Add((T)_Selecteds[0].Obj);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
for (int i = 0; i < multiSelectNum && i < _Selecteds.Count; ++i)
|
||
{
|
||
if (_Selecteds[i].Obj == null)
|
||
{
|
||
break;
|
||
}
|
||
|
||
selectedObjs.Add((T)_Selecteds[i].Obj);
|
||
}
|
||
}
|
||
|
||
return selectedObjs;
|
||
}
|
||
}
|