/// #pkgName FGUI包名
/// #panelName UIPanel名字
/// #UIName = $"{#pkgName}{#panelName}" UIKey名字
/// 该脚本由模板创建
/// created by cb 2024
using FairyGUI;
using System.Collections.Generic;
using XGame;
using XGame.Framework;
using XGame.Framework.UI;
namespace FL.FGUI
{
///
/// UI逻辑处理类
///
///
public partial class MainHomeBottomPanelCtrl : UIController
{
private UIKey _tempKey;
///
/// 已开启的Normal层UI队列
///
private List _normalUIs = new();
protected override void OnEnable(object intent)
{
AddUIListenres();
VM.BtnCtrl.onChanged.Add(OnBtnCtrlChanged);
FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened);
FrameworkEvent.Instance.AddListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
Mock();
}
protected override void OnDisable()
{
RemoveUIListenres();
VM.BtnCtrl.onChanged.Remove(OnBtnCtrlChanged);
FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_OPENED, OnUIOpened);
FrameworkEvent.Instance.RemoveListener(XGame.Framework.EventDefine.UI_CLOSED, OnUIClosed);
_tempKey = null;
_normalUIs.Clear();
}
#region UI事件
private void AddUIListenres()
{
VM.ClosePanelBtn.onClick.Add(OnClickClosePanelBtn);
}
private void RemoveUIListenres()
{
VM.ClosePanelBtn.onClick.Remove(OnClickClosePanelBtn);
}
private void OnClickClosePanelBtn(EventContext context)
{
if (_normalUIs.Count > 0)
{
var uikey = _normalUIs[0];
_normalUIs.RemoveAt(0);
Context.UI.Close(uikey);
return;
}
VM.BtnCtrl.selectedIndex = 0;
}
#endregion
private void Mock()
{ //TODO 临时代码
VM.SetUnlockState(4, false, "主线4\r解锁");
VM.SetUnlockState(5, false, "主线5\r解锁");
}
#region UI开启/关闭监听事件
private bool IsUnlock(int index)
{ //TODO 临时代码
return index switch
{
4 => false,
5 => false,
_ => true
};
}
private UIKey IndexToKey(int index)
{
return index switch
{
1 => UIKeys.PlayerMainPanel,
2 => UIKeys.PartnerSbMainPanel,
3 => UIKeys.PartnerEpiMainPanel,
6 => UIKeys.DragonEggTreasurePanel,
_ => null,
};
}
private int KeyToIndex(UIKey uiKey)
{
if (uiKey == UIKeys.PlayerMainPanel) return 1;
if (uiKey == UIKeys.PartnerSbMainPanel) return 2;
if (uiKey == UIKeys.PartnerEpiMainPanel) return 3;
if (uiKey == UIKeys.DragonEggTreasurePanel) return 6;
return -1;
}
private void OnBtnCtrlChanged(EventContext context)
{
Log.Debug($"OnBtnCtrlChanged. previousIndex:{VM.BtnCtrl.previousIndex} selectedIndex:{VM.BtnCtrl.selectedIndex}");
if (VM.BtnCtrl.selectedIndex == 7)
{ // 7为单独显示返回按钮
return;
}
CloseAllNormalUI();
var lastUIKey = IndexToKey(VM.BtnCtrl.previousIndex);
if (lastUIKey != null)
{
_tempKey = lastUIKey;
Context.UI.Close(lastUIKey);
}
_tempKey = null;
if (!IsUnlock(VM.BtnCtrl.selectedIndex))
{
Context.ShowTips("功能未解锁!");
VM.BtnCtrl.selectedIndex = 0;
return;
}
var nextUIKey = IndexToKey(VM.BtnCtrl.selectedIndex);
if (nextUIKey != null)
{
_tempKey = nextUIKey;
var async = Context.UI.OpenAsync(nextUIKey);
async.On(_ =>
{
_tempKey = null;
});
}
}
private void OnUIOpened(int eventId, object args)
{
var uikey = args as UIKey;
Log.Debug($"收到UI开启事件 UIKey:{uikey} selectedIndex:{VM.BtnCtrl.selectedIndex}");
if (uikey == _tempKey) return; // 避免重复监听
var index = KeyToIndex(uikey);
if (index != -1)
{ // 是控制器关联的UI
if (index != VM.BtnCtrl.selectedIndex)
{ // 控制器索引不一致
VM.BtnCtrl.selectedIndex = index;
}
return;
}
if (_normalUIs.Contains(uikey))
{
Log.Error($"重复记录UI开启事件. UIKey:{uikey}");
return;
}
var layer = UIKeys.GetLayer(uikey);
if (layer != UILayer.Normal)
return;
if (VM.BtnCtrl.selectedIndex == 0)
{// 7为单独显示返回按钮
VM.BtnCtrl.selectedIndex = 7;
}
_normalUIs.Insert(0, uikey);
}
private void OnUIClosed(int eventId, object args)
{
var uikey = args as UIKey;
Log.Debug($"收到UI关闭事件 UIKey:{uikey} selectedIndex:{VM.BtnCtrl.selectedIndex}");
if (uikey == _tempKey) return; // 避免重复监听
if (UIKeys.GetLayer(uikey) == UILayer.Normal)
{
_normalUIs.Remove(uikey);
}
var index = KeyToIndex(uikey);
if (index != -1)
{ // 是控制器关联的UI
if (index == VM.BtnCtrl.selectedIndex)
{ // 控制器索引是当前关闭的UI
VM.BtnCtrl.selectedIndex = 0;
}
}
}
///
/// 关闭所有的Normal层UI
///
private void CloseAllNormalUI()
{
if (_normalUIs.Count == 0)
return;
for(var i = _normalUIs.Count - 1; i >= 0; i--)
{
var uikey = _normalUIs[i];
_normalUIs.RemoveAt(i);
_tempKey = uikey;
Context.UI.Close(uikey);
}
_tempKey = null;
}
#endregion
}
}