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, ITickUI { // 存储窗口及其激活键的字典 public Dictionary UIwindowKeys = new(); // 存储没有激活键的窗口列表 private List 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(); 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.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; } } /// /// 模拟按键输入切换窗口 /// /// 要模拟的按键 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}'."); } } /// /// 打开指定的窗口(无论是否有激活键) /// /// 要打开的窗口 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); // 调用内部逻辑处理,标记为函数调用 } /// /// 关闭指定的窗口(无论是否有激活键) /// /// 要关闭的窗口 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); // 调用内部逻辑处理,标记为函数调用 } /// /// 切换指定窗口的显示状态(无论是否有激活键) /// /// 要切换的窗口 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); // 调用内部逻辑处理,标记为函数调用 } /// /// 在对象销毁时清理事件监听 /// private void OnDestroy() { SceneManager.sceneLoaded -= OnSceneLoaded; } /// /// 在对象启动时初始化 /// protected override void OnStart() { // 注册场景加载事件 SceneManager.sceneLoaded += OnSceneLoaded; // 初始化时调用一次 Init(); } /// /// 场景加载完成后重新初始化 /// private void OnSceneLoaded(Scene scene, LoadSceneMode mode) { // 场景加载完成后调用 Init 方法 Init(); } } }