using System;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
//方案预设: 多方案选择组件
//控制显隐: 1.未开启时全不显示
// 2.开启后显示已解锁 + 未解锁 3+0
//a. 按元宝解锁的为逐一开启,默认开启x个,第x+1个显示条件需要【流派预设】功能开启, 可解锁后逐一保留一个锁住的(直到全开启);
//b. 按功能条件开启的,如果默认只开其一个方案的情况不显示,达到下一个方案的条件满足后全显示;默认大于1个的情况直接全显示
// 3.当小于4个的时候,没有下拉显示,如果可以超过4个的显示下拉
// 4.当大于4个的时候,选择超过方案4时,第四个显示为选中的方案; 否则重开的时候默认显示1-4 不记录历史点击
// 5.名字展开分两种情况,外层功能默认不展开,点击后切换展开状态;流派预设界面里只有展开状态不可收缩
public class FuncPresetChooseCells : MonoBehaviour
{
[SerializeField] HorizontalLayoutGroup layoutGroup;
[SerializeField] FuncPresetChooseCell[] cells;
[SerializeField] Button unFoldBtn; //展开更多
[SerializeField] Image redImg;
//被选中的方案会显示在外层,如果不是第四个则第四个会显示在最上面
[SerializeField] Transform moreCellObj;
[SerializeField] FuncPresetChooseMoreCell[] moreCells;
[SerializeField] Canvas canvas;
bool forceUnFold = false; //强制展开,不能收缩; 流派界面的需求
[NonSerialized] public int unFoldID = 0; //当前展开的方案ID,在选中的情况下才生效
int curBattleType;
int curFuncType;
///
/// 显示方案预设
///
/// 主线战斗 其他防守
/// 功能类型
/// 展开的方案ID,默认0,不展开
/// 强制展开,不能收缩
public void Display(int battleType, int funcType, bool _forceUnFold = false)
{
forceUnFold = _forceUnFold;
curBattleType = battleType;
curFuncType = funcType;
var selectID = FuncPresetManager.Instance.GetFuncPresetIDByBattleType(battleType, funcType);
if (unFoldID != 0 && selectID != unFoldID)
{
//其他地方切换了方案,需要验证当前展开是不是同一个
unFoldID = 0;
}
var showCount = FuncPresetManager.Instance.GetShowFuncPresetCount(funcType);
unFoldBtn.SetActive(showCount > 4);
unFoldBtn.AddListener(() =>
{
moreCellObj.SetActive(true);
});
for (int i = 0; i < cells.Length; i++)
{
if (i < showCount)
{
cells[i].SetActive(true);
if (i < 3)
{
var _unFoldState = forceUnFold ? i + 1 == selectID : i + 1 == unFoldID;
cells[i].Display(battleType, funcType, i + 1, _unFoldState);
}
else
{
var _unFoldState = forceUnFold ? selectID >= 4: (selectID > 4 ? selectID : 4) == unFoldID;
//第四个动态变化
cells[i].Display(battleType, funcType, selectID > 4 ? selectID : 4, _unFoldState);
}
}
else
{
cells[i].SetActive(false);
}
}
var showMoreCount = showCount - 4;
for (int i = 0; i < moreCells.Length; i++)
{
if (i < showMoreCount)
{
moreCells[i].SetActive(true);
var id = i + 5;
if (i == 0 && selectID > 4)
{
id = 4;
}
moreCells[i].Display(battleType, funcType, id);
}
else
{
moreCells[i].SetActive(false);
}
}
redImg.SetActive(FuncPresetManager.Instance.GetNeedShowRedID(funcType) > 4);
UIUtility.ForceRefreshLayout(this.transform).Forget();
}
void OnEnable()
{
canvas.sortingLayerName = "UI";
FuncPresetManager.Instance.OnSelectPresetEvent += OnSelectPresetEvent;
}
void OnDisable()
{
unFoldID = 0;
FuncPresetManager.Instance.OnSelectPresetEvent -= OnSelectPresetEvent;
}
void OnSelectPresetEvent(int battleType, int _funcType, int id, bool isUnFold)
{
if (battleType != curBattleType || curFuncType != _funcType)
return;
Display(curBattleType, _funcType, forceUnFold);
}
//默认是居中靠右
public void ChangeAlignment(TextAnchor type)
{
layoutGroup.childAlignment = type;
}
}