using GameFramework.Event; using UnityGameFramework.Runtime; namespace UI { public class BgFormController : IFormController { private BgFormContext _context; private BgForm _bgForm; private int? _formSerialId; private bool _pendingRefresh; public BgFormController() { GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OnOpenUIFormSuccess); GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, OnCloseUIFormComplete); } public int? OpenUI(BgFormContext context) { if (context == null) { Log.Warning("BgFormController open failed. context is null."); return null; } _context = context; if (_bgForm != null) { _bgForm.RefreshUI(_context); return _formSerialId; } CloseUI(); _pendingRefresh = true; _formSerialId = GameEntry.UI.OpenUIForm(UIFormId.BgForm, context); return _formSerialId; } public void CloseUI() { _pendingRefresh = false; if (_formSerialId.HasValue) { GameEntry.UI.CloseUIForm(_formSerialId.Value); return; } if (_bgForm != null) { _bgForm.Close(); } } ~BgFormController() { GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OnOpenUIFormSuccess); GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, OnCloseUIFormComplete); } private void TryRefreshUI() { if (_context == null) { return; } if (_bgForm == null) { _pendingRefresh = true; return; } _bgForm.RefreshUI(_context); _pendingRefresh = false; } private void OnOpenUIFormSuccess(object sender, GameEventArgs e) { if (!(e is OpenUIFormSuccessEventArgs args)) { return; } if (!_formSerialId.HasValue) { return; } if (args.UIForm == null || args.UIForm.SerialId != _formSerialId.Value || args.UserData != _context) { return; } _bgForm = args.UIForm.Logic as BgForm; if (_bgForm == null) { Log.Warning("AIChatFormController open success but form logic is invalid."); return; } if (_pendingRefresh) { TryRefreshUI(); } } private void OnCloseUIFormComplete(object sender, GameEventArgs e) { if (!(e is CloseUIFormCompleteEventArgs args)) { return; } if (args.SerialId != _formSerialId) { return; } _bgForm = null; _formSerialId = null; _pendingRefresh = false; } } }