Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
189 lines
6.0 KiB
C#
189 lines
6.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UI;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace Base
|
|
{
|
|
public class UIInputControl : Utils.MonoSingleton<UIInputControl>, ITickUI
|
|
{
|
|
// 存储窗口及其激活键的字典
|
|
public Dictionary<KeyCode, UIBase> UIwindowKeys = new();
|
|
// 存储没有激活键的窗口列表
|
|
private List<UIBase> noKeyWindows = new();
|
|
|
|
// 每帧更新逻辑
|
|
public void TickUI()
|
|
{
|
|
foreach (var kvp in UIwindowKeys)
|
|
{
|
|
if (Input.GetKeyDown(kvp.Key))
|
|
{
|
|
HandleWindowActivation(kvp.Value);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Init()
|
|
{
|
|
UIwindowKeys.Clear();
|
|
noKeyWindows.Clear();
|
|
|
|
var uiInstances = Resources.FindObjectsOfTypeAll<UIBase>();
|
|
|
|
foreach (var uiBase in uiInstances)
|
|
{
|
|
var key = uiBase.actionButton;
|
|
if (key == KeyCode.None)
|
|
{
|
|
noKeyWindows.Add(uiBase);
|
|
uiBase.Hide();
|
|
continue;
|
|
}
|
|
if (UIwindowKeys.ContainsKey(key))
|
|
{
|
|
Debug.LogWarning($"Key '{key}' is already assigned to another window. Skipping...");
|
|
continue;
|
|
}
|
|
UIwindowKeys[key] = uiBase;
|
|
uiBase.Hide();
|
|
}
|
|
}
|
|
|
|
private void HandleWindowActivation(UIBase targetWindow, bool isFunctionCall = false)
|
|
{
|
|
bool wasTargetVisible = targetWindow.IsVisible;
|
|
bool anyOtherWindowOpen = false;
|
|
|
|
// 遍历所有窗口(包括有键和无键窗口)
|
|
foreach (var kvp in UIwindowKeys.Concat(noKeyWindows.Select(w => new KeyValuePair<KeyCode, UIBase>(KeyCode.None, w))))
|
|
{
|
|
if (kvp.Value == targetWindow)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (kvp.Value.IsVisible)
|
|
{
|
|
if (!wasTargetVisible || isFunctionCall) // 只在目标窗口要打开时才关闭其他窗口
|
|
{
|
|
kvp.Value.Hide();
|
|
}
|
|
else
|
|
{
|
|
anyOtherWindowOpen = true; // 记录是否有其他窗口打开
|
|
}
|
|
}
|
|
}
|
|
|
|
if (wasTargetVisible)
|
|
{
|
|
targetWindow.Hide();
|
|
}
|
|
else
|
|
{
|
|
targetWindow.Show();
|
|
}
|
|
|
|
bool currentWindowState = !wasTargetVisible || anyOtherWindowOpen;
|
|
if (Base.Clock.Instance.Pause != currentWindowState)
|
|
{
|
|
Base.Clock.Instance.Pause = currentWindowState;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 模拟按键输入切换窗口
|
|
/// </summary>
|
|
/// <param name="keyCode">要模拟的按键</param>
|
|
public void SimulateKeyPress(KeyCode keyCode)
|
|
{
|
|
if (UIwindowKeys.TryGetValue(keyCode, out UIBase targetWindow))
|
|
{
|
|
HandleWindowActivation(targetWindow); // 调用内部逻辑处理
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"No window is assigned to the key '{keyCode}'.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开指定的窗口(无论是否有激活键)
|
|
/// </summary>
|
|
/// <param name="window">要打开的窗口</param>
|
|
public void OpenWindow(UIBase window)
|
|
{
|
|
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
|
|
{
|
|
Debug.LogWarning("Cannot open the specified window as it is not registered.");
|
|
return;
|
|
}
|
|
|
|
HandleWindowActivation(window, true); // 调用内部逻辑处理,标记为函数调用
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭指定的窗口(无论是否有激活键)
|
|
/// </summary>
|
|
/// <param name="window">要关闭的窗口</param>
|
|
public void CloseWindow(UIBase window)
|
|
{
|
|
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
|
|
{
|
|
Debug.LogWarning("Cannot close the specified window as it is not registered.");
|
|
return;
|
|
}
|
|
|
|
HandleWindowActivation(window, true); // 调用内部逻辑处理,标记为函数调用
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换指定窗口的显示状态(无论是否有激活键)
|
|
/// </summary>
|
|
/// <param name="window">要切换的窗口</param>
|
|
public void ToggleWindow(UIBase window)
|
|
{
|
|
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
|
|
{
|
|
Debug.LogWarning("Cannot toggle the specified window as it is not registered.");
|
|
return;
|
|
}
|
|
|
|
HandleWindowActivation(window, true); // 调用内部逻辑处理,标记为函数调用
|
|
}
|
|
|
|
/// <summary>
|
|
/// 在对象销毁时清理事件监听
|
|
/// </summary>
|
|
private void OnDestroy()
|
|
{
|
|
SceneManager.sceneLoaded -= OnSceneLoaded;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 在对象启动时初始化
|
|
/// </summary>
|
|
protected override void OnStart()
|
|
{
|
|
// 注册场景加载事件
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
|
|
|
// 初始化时调用一次
|
|
Init();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 场景加载完成后重新初始化
|
|
/// </summary>
|
|
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
{
|
|
// 场景加载完成后调用 Init 方法
|
|
Init();
|
|
}
|
|
}
|
|
} |