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(); private List allWindows = 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(); allWindows.Clear(); // 清空所有窗口集合 var uiInstances = Resources.FindObjectsOfTypeAll(); foreach (var uiBase in uiInstances) { allWindows.Add(uiBase); // 添加到所有窗口集合 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 wasTargetVisible = targetWindow.IsVisible; bool shouldCloseExclusive = false; bool exclusiveWindowWasOpen = false; // 第一次遍历:检查是否有需要关闭的独占窗口 foreach (var window in allWindows) { if (window == targetWindow) continue; if (window.IsVisible && window.exclusive) { // 记录有独占窗口打开且需要关闭 shouldCloseExclusive = true; exclusiveWindowWasOpen = true; break; } } // 第二次遍历:根据条件关闭窗口 foreach (var window in allWindows) { if (window == targetWindow) continue; if (window.IsVisible) { // 关闭所有独占窗口(无论是新窗口打开还是关闭) // 或当目标窗口是独占窗口时关闭所有其他窗口 if (window.exclusive || targetWindow.exclusive || shouldCloseExclusive) { window.Hide(); } } } // 切换目标窗口状态 if (wasTargetVisible) { targetWindow.Hide(); } else { targetWindow.Show(); } // 更新暂停状态(优化版) UpdatePauseState(exclusiveWindowWasOpen, targetWindow); } private void UpdatePauseState(bool exclusiveWindowWasOpen, UIBase targetWindow) { bool needPause = false; foreach (var window in allWindows) { if (window.IsVisible && window.needPause) { needPause = true; break; } } // 只在状态改变时更新 if (Base.Clock.Instance.Pause != needPause) { Base.Clock.Instance.Pause = needPause; } } /// /// 模拟按键输入切换窗口 /// /// 要模拟的按键 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); // 调用内部逻辑处理,标记为函数调用 } /// /// 关闭指定的窗口(无论是否有激活键) /// /// 要关闭的窗口 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); // 调用内部逻辑处理,标记为函数调用 } /// /// 切换指定窗口的显示状态(无论是否有激活键) /// /// 要切换的窗口 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); // 调用内部逻辑处理,标记为函数调用 } /// /// 在对象销毁时清理事件监听 /// private void OnDestroy() { SceneManager.sceneLoaded -= OnSceneLoaded; } /// /// 在对象启动时初始化 /// protected override void OnStart() { // 注册场景加载事件 SceneManager.sceneLoaded += OnSceneLoaded; // 初始化时调用一次 Init(); } /// /// 场景加载完成后重新初始化 /// private void OnSceneLoaded(Scene scene, LoadSceneMode mode) { // 场景加载完成后调用 Init 方法 Init(); } } }