2025-10-19 00:48:06 +08:00

76 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 文件位置: TH1_Presentation/Sequencer/Task/UISequencerTask.cs
using System;
using TH1_UI.Controller.Announce;
using TH1_UI.Controller.Base;
using TH1_UI.Core;
using UnityEngine;
namespace TH1_Presentation.Sequencer.Task
{
/// <summary>
/// 一个专门用于按顺序显示 UI (ViewController) 的任务。
/// </summary>
public class UISequencerTask : ISequencerTask
{
private readonly IViewControllerInterface _viewController;
private readonly object _data; // 用于传递给UI的数据可以是任何类型
private Action _onCompleteCallback; // 保存来自 PresentationManager 的完成回调
private bool _callbackRegistered = false;
/// <summary>
/// 创建一个UI显示任务。
/// </summary>
/// <param name="viewController">要显示的UI的控制器实例。</param>
/// <param name="data">可选需要传递给UI的数据例如包含标题和内容的自定义类或元组。</param>
public UISequencerTask(IViewControllerInterface viewController, object data = null)
{
if (viewController == null)
{
throw new ArgumentNullException(nameof(viewController), "UISequencerTask cannot be created with a null ViewController.");
}
_viewController = viewController;
_data = data;
}
public string GetDescString()
{
return "UISequencerTask : " + _viewController.GetType().ToString();
}
/// <summary>
/// 执行任务打开UI并等待其关闭。
/// </summary>
public void Execute(Action onComplete)
{
// 1. 保存 PresentationManager 的完成回调
_onCompleteCallback = onComplete;
// 添加调试日志
//Debug.Log($"[TASK] Execute: {_viewController.Name}. Subscribing 'OnViewClosed'. Callback is currently '{(object)_viewController.OnClosedCallback ?? "null"}'.");
// 2. 注册自己的回调到 ViewController 的关闭事件上
_viewController.OnClosedCallback += OnViewClosed;
_callbackRegistered = true;
// 添加调试日志
//Debug.Log($"[TASK] After Subscribe: {_viewController.Name}. Callback is now '{(object)_viewController.OnClosedCallback ?? "null"}'.");
// 3. 打开界面
if (_data != null)
_viewController.OpenWithParam(_data);
else
_viewController.Open();
}
private void OnViewClosed()
{
// step #1 在通知前,立刻注销自己的回调
if (_callbackRegistered)
{
_viewController.OnClosedCallback -= OnViewClosed;
_callbackRegistered = false;
}
//_viewController.OnClosedCallback -= OnViewClosed;
// step #2. 通知 PresentationManager 任务已完成
_onCompleteCallback?.Invoke();
}
}
}