/// #pkgName FGUI包名
/// #panelName UIPanel名字
/// #UIName = $"{#pkgName}{#panelName}" UIKey名字
/// 该脚本由模板创建
/// created by cb 2024
using FairyGUI;
using XGame.Framework;
using XGame.Framework.UI;
namespace FL.FGUI
{
///
/// UI逻辑处理类
///
///
public partial class MainHomeBottomPanelCtrl : UIController
{
private UIKey _tempKey;
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;
}
#region UI事件
private void AddUIListenres()
{
VM.ClosePanelBtn.onClick.Add(OnClickClosePanelBtn);
}
private void RemoveUIListenres()
{
VM.ClosePanelBtn.onClick.Remove(OnClickClosePanelBtn);
}
private void OnClickClosePanelBtn(EventContext context)
{
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.PartnerMainPanel,
3 => UIKeys.PartnerEpiMainPanel,
6 => UIKeys.DragonEggTreasurePanel,
_ => null,
};
}
private int KeyToIndex(UIKey uiKey)
{
if (uiKey == UIKeys.PlayerMainPanel) return 1;
if (uiKey == UIKeys.PartnerMainPanel) return 2;
if (uiKey == UIKeys.PartnerEpiMainPanel) return 3;
if (uiKey == UIKeys.DragonEggTreasurePanel) return 6;
return -1;
}
private void OnBtnCtrlChanged(EventContext context)
{
XGame.Log.Debug($"OnBtnCtrlChanged. previousIndex:{VM.BtnCtrl.previousIndex} selectedIndex:{VM.BtnCtrl.selectedIndex}");
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;
XGame.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;
}
}
}
private void OnUIClosed(int eventId, object args)
{
var uiKey = args as UIKey;
XGame.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)
{ // 控制器索引是当前关闭的UI
VM.BtnCtrl.selectedIndex = 0;
}
}
}
#endregion
}
}