NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/UI/UIBackStrategy.cs

151 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Cysharp.Threading.Tasks;
/// <summary>
/// UI返回键策略接口
/// </summary>
public interface IUIBackStrategy
{
/// <summary>
/// 处理返回键
/// </summary>
/// <param name="window">触发返回的窗口</param>
/// <returns>是否成功处理</returns>
UniTask<bool> HandleBack(UIWindow window);
}
/// <summary>
/// 策略1不响应返回键
/// </summary>
public class UIBackStrategy_None : IUIBackStrategy
{
public async UniTask<bool> HandleBack(UIWindow window)
{
// 不响应返回false让下一个窗口处理
await UniTask.CompletedTask;
return false;
}
}
/// <summary>
/// 策略2关闭自己
/// </summary>
public class UIBackStrategy_CloseSelf : IUIBackStrategy
{
public async UniTask<bool> HandleBack(UIWindow window)
{
if (window != null)
{
window.CloseWindow(destroy: true);
DebugUtil.Log($"[BackStrategy] Close window: {window.WindowName}");
await UniTask.CompletedTask;
return true;
}
return false;
}
}
/// <summary>
/// 策略3什么都不做拦截返回键但不执行任何操作
/// 适用场景:剧情播放中、加载中等不希望用户中断的场景
/// </summary>
public class UIBackStrategy_DoNothing : IUIBackStrategy
{
public async UniTask<bool> HandleBack(UIWindow window)
{
// 拦截返回键,但什么都不做
DebugUtil.Log($"[BackStrategy] Back blocked by: {window?.WindowName}");
await UniTask.CompletedTask;
return true; // 返回true表示已处理阻止继续传递
}
}
/// <summary>
/// 策略4弹出退出游戏确认界面
/// 适用场景:主界面、登录界面等根界面
/// </summary>
public class UIBackStrategy_ShowExitConfirm : IUIBackStrategy
{
public async UniTask<bool> HandleBack(UIWindow window)
{
// 检查退出确认界面是否已经打开
if (!UIManager.Instance.CheckWindowCreated(UINameConst.UIBackReturn))
{
await UIManager.Instance.CreateAndOpenWindow(UINameConst.UIBackReturn);
DebugUtil.Log($"[BackStrategy] Show exit confirm from: {window?.WindowName}");
}
return true;
}
}
/// <summary>
/// 返回策略工厂
/// </summary>
public static class UIBackStrategyFactory
{
private static UIBackStrategy_None _noneStrategy;
private static UIBackStrategy_CloseSelf _closeSelfStrategy;
private static UIBackStrategy_DoNothing _doNothingStrategy;
private static UIBackStrategy_ShowExitConfirm _showExitConfirmStrategy;
/// <summary>
/// 根据策略类型创建策略实例
/// </summary>
public static IUIBackStrategy CreateStrategy(UIBackStrategyType strategyType)
{
switch (strategyType)
{
case UIBackStrategyType.PassThrough:
return GetNoneStrategy();
case UIBackStrategyType.CloseSelf:
return GetCloseSelfStrategy();
case UIBackStrategyType.Intercept:
return GetDoNothingStrategy();
case UIBackStrategyType.ShowExitConfirm:
return GetShowExitConfirmStrategy();
default:
return GetCloseSelfStrategy(); // 默认关闭自己
}
}
/// <summary>
/// 获取"不响应"策略实例(单例)
/// </summary>
public static IUIBackStrategy GetNoneStrategy()
{
if (_noneStrategy == null)
_noneStrategy = new UIBackStrategy_None();
return _noneStrategy;
}
/// <summary>
/// 获取"关闭自己"策略实例(单例)
/// </summary>
public static IUIBackStrategy GetCloseSelfStrategy()
{
if (_closeSelfStrategy == null)
_closeSelfStrategy = new UIBackStrategy_CloseSelf();
return _closeSelfStrategy;
}
/// <summary>
/// 获取"什么都不做"策略实例(单例)
/// </summary>
public static IUIBackStrategy GetDoNothingStrategy()
{
if (_doNothingStrategy == null)
_doNothingStrategy = new UIBackStrategy_DoNothing();
return _doNothingStrategy;
}
/// <summary>
/// 获取"弹出退出确认"策略实例(单例)
/// </summary>
public static IUIBackStrategy GetShowExitConfirmStrategy()
{
if (_showExitConfirmStrategy == null)
_showExitConfirmStrategy = new UIBackStrategy_ShowExitConfirm();
return _showExitConfirmStrategy;
}
}