123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- /// #pkgName FGUI包名
- /// #panelName UIPanel名字
- /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
- /// 该脚本由模板创建
- /// created by cb 2024
- using FairyGUI;
- using XGame.Framework;
- using XGame.Framework.UI;
- namespace FL.FGUI
- {
- /// <summary>
- /// UI逻辑处理类
- /// </summary>
- /// <typeparam name=""></typeparam>
- public partial class MainHomeBottomPanelCtrl : UIController<MainHomeBottomPanelVM>
- {
- 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
- }
- }
|