using System; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace XericUI.VisualForm { /// /// 锚点窗口 /// [AddComponentMenu("Xeric UI Vessel/AnchorWindow/AnchorWindow")] public class AnchorWindowByDefaultEntry : AnchorWindow { } /// /// 锚点窗口 - WorldAnchor的子类,持有一组UI窗口条目的引用。 /// 每个条目包含窗口对象、屏幕偏移量和激活状态,支持一个锚点同时生成多个定位窗口。 /// 窗口对象可以是实际的场景对象,也可以是预制体。 /// public abstract class AnchorWindow : WorldAnchor, IAnchorWindowEntry where T : WindowEntry { [Header("窗口条目")] [SerializeField, Tooltip("窗口对象集合,每个条目可独立配置偏移和启用状态")] private List m_WindowEntries = new List(); public IList WindowEntries => m_WindowEntries; public IEnumerable DefaultTypeWindowEntries => m_WindowEntries; public int DefaultTypeWindowEntriesCount => m_WindowEntries.Count; /// /// 当前生效的窗口条目数(过滤掉WindowObject为null的条目) /// public int ValidEntryCount { get { int count = 0; foreach (var entry in m_WindowEntries) if (entry != null && entry.WindowObject != null) count++; return count; } } /// /// 获取第一个有效窗口对象的Transform /// [Obsolete("旧api")] public Transform GetWindowTransform() { foreach (var entry in m_WindowEntries) { if (entry != null && entry.WindowObject != null) return entry.WindowObject.transform; } return transform; } #region 工具方法 /// /// 设置指定索引条目的启用状态。写入后自动检查所有条目, /// 若全部禁用则同步标记到 WindowState(坞将跳过该窗口的位置更新)。 /// /// 条目索引 /// 是否启用 public void SetEntryEnabled(int index, bool enabled) { if (index < 0 || index >= m_WindowEntries.Count) return; SetEntryEnabled(m_WindowEntries[index], enabled); } /// /// 直接通过条目引用设置启用状态。无需索引查找。 /// /// 条目引用 /// 是否启用 public void SetEntryEnabled(T entry, bool enabled) { if (entry == null) return; entry.SetEnabledInternal(enabled); var state = GetWindowState(); state?.RefreshFlags(); state?.ApplyEntryEnabled(entry, enabled); } public void SetAllEntryEnabled(bool enabled) { for (int i = 0; i < m_WindowEntries.Count; i++) m_WindowEntries[i].SetEnabledInternal(enabled); GetWindowState()?.RefreshFlags(); } /// /// 设置指定索引条目的钳制到安全区标记。写入后自动同步 WindowState 的 HasAnyClamp。 /// /// 条目索引 /// 是否钳制 public void SetEntryClampToSafeZone(int index, bool clamp) { if (index < 0 || index >= m_WindowEntries.Count) return; SetEntryClampToSafeZone(m_WindowEntries[index], clamp); } /// /// 直接通过条目引用设置安全区钳制标记。无需索引查找。 /// /// 条目引用 /// 是否钳制 public void SetEntryClampToSafeZone(T entry, bool clamp) { if (entry == null) return; entry.SetClampToSafeZoneInternal(clamp); GetWindowState()?.RefreshFlags(); } /// /// 设置指定索引条目的碰撞参与标记。写入后自动同步 WindowState 的 HasAnyCollisionEntry。 /// 锚点坞在启用碰撞计算时会对标记为参与碰撞的条目进行屏幕空间碰撞分离。 /// /// 条目索引 /// 是否参与碰撞 public void SetEntryEnableCollision(int index, bool enable) { if (index < 0 || index >= m_WindowEntries.Count) return; SetEntryEnableCollision(m_WindowEntries[index], enable); } /// /// 直接通过条目引用设置碰撞参与标记。无需索引查找。 /// /// 条目引用 /// 是否参与碰撞 public void SetEntryEnableCollision(T entry, bool enable) { if (entry == null) return; entry.SetEnableCollisionInternal(enable); GetWindowState()?.RefreshFlags(); } /// /// 设置指定索引条目的排序顺序。Order 越小越在下层,越大越在上层。 /// 自动通知 WindowState 重新排列实例的层级顺序。 /// /// 条目索引 /// 排序值 public void SetEntryOrder(int index, int order) { if (index < 0 || index >= m_WindowEntries.Count) return; SetEntryOrder(m_WindowEntries[index], order); } /// /// 直接通过条目引用设置排序顺序。无需索引查找。 /// 自动通知 WindowState 重新排列实例的层级顺序。 /// /// 条目引用 /// 排序值 public void SetEntryOrder(T entry, int order) { if (entry == null) return; entry.SetOrderInternal(order); GetWindowState()?.ApplyEntryOrder(); } /// /// 从所属锚点坞获取自己的中间窗口状态类 /// public WindowState GetWindowState() { if (TargetDock == null) return null; return TargetDock.GetWindowState(this); } /// /// 获取第一个有效窗口的实例对象 /// public GameObject GetWindowInstance() { var state = GetWindowState(); if (state == null || state.Entries == null || state.Entries.Count == 0) return null; return state.Entries[0].Instance; } /// /// 获取所有窗口实例对象 /// public List GetAllWindowInstances() { var result = new List(); var state = GetWindowState(); if (state == null || state.Entries == null) return result; foreach (var e in state.Entries) if (e.Instance != null) result.Add(e.Instance); return result; } /// /// 获取第一个窗口当前的屏幕坐标(容器内本地坐标) /// public Vector2 GetScreenPosition() { var instance = GetWindowInstance(); if (instance == null) return Vector2.zero; var rt = instance.transform as RectTransform; if (rt != null) return rt.localPosition; return instance.transform.localPosition; } /// /// 获取锚点当前的世界坐标 /// public Vector3 GetWorldPosition() { var state = GetWindowState(); if (state != null) return state.LastWorldPosition; return CachedTransform.position; } /// /// 检查窗口实例是否处于活动状态 /// public bool IsWindowActive() { var state = GetWindowState(); return state != null && state.IsEnabled; } #endregion #region 生命周期事件 /// /// 窗口层级变更事件 — 当某个条目的溢出状态发生变化时触发。 /// 继承类可重写此方法,根据溢出状态切换不同窗口显示(如溢出后切换为指向示意器)。 /// /// 发生变化的条目索引 /// 新的溢出状态 public virtual void OnWindowLevelChange(int entryIndex, OverflowState state) { } #endregion } }