using System.Collections.Generic; using UnityEngine; using UnityEngine.Serialization; namespace Amanotes.Core { partial class Widget2D // ComputedRects { [FormerlySerializedAs("_derivedRects")] [SerializeField] internal List _computedRects = new List(); internal void ComputeAllRects() { if (_computedRects == null || _computedRects.Count == 0) return; var srcSize = (Vector2)_size; var srcPivot = _size.pivot; for (int i = 0; i < _computedRects.Count; i++) { var dr = _computedRects[i]; if (dr == null) continue; dr.Compute(srcSize, srcPivot); } } /// /// Get the computed rect by ID. /// Returns the source rect (centered on pivot) if id is null/empty or not found. /// public Rect GetRect(string id) { if (string.IsNullOrEmpty(id)) return GetSourceRect(); for (int i = 0; i < _computedRects.Count; i++) { var dr = _computedRects[i]; if (dr != null && dr.id == id) return dr.cachedRect; } return GetSourceRect(); } /// /// Get the computed size of a rect by ID. /// Returns source size if id is null/empty or not found. /// public Vector2 GetRectSize(string id) { if (string.IsNullOrEmpty(id)) return _size; for (int i = 0; i < _computedRects.Count; i++) { var dr = _computedRects[i]; if (dr != null && dr.id == id) return dr.cachedRect.size; } return _size; } /// /// Get the ComputedRect definition by ID (for runtime param mutation). /// public ComputedRect GetComputedRect(string id) { if (string.IsNullOrEmpty(id)) return null; for (int i = 0; i < _computedRects.Count; i++) { var dr = _computedRects[i]; if (dr != null && dr.id == id) return dr; } return null; } [System.Obsolete("Use GetComputedRect instead")] public ComputedRect GetDerivedRectDef(string id) => GetComputedRect(id); public void AddComputedRect(ComputedRect rect) { if (rect == null) return; _computedRects ??= new List(); _computedRects.Add(rect); } [System.Obsolete("Use AddComputedRect instead")] public void AddDerivedRect(ComputedRect rect) => AddComputedRect(rect); /// /// Collects all computed rect IDs into the provided list (for editor dropdown). /// internal void GetComputedRectIds(List ids) { ids.Clear(); if (_computedRects == null) return; for (int i = 0; i < _computedRects.Count; i++) { var dr = _computedRects[i]; if (dr != null && !string.IsNullOrEmpty(dr.id)) ids.Add(dr.id); } } private Rect GetSourceRect() { var s = (Vector2)_size; var p = _size.pivot; return new Rect(-p.x * s.x, -p.y * s.y, s.x, s.y); } #if UNITY_EDITOR private static readonly Color[] _rectColors = { new Color(0.2f, 0.8f, 0.4f, 0.8f), new Color(0.3f, 0.6f, 0.9f, 0.8f), new Color(0.9f, 0.5f, 0.2f, 0.8f), new Color(0.8f, 0.2f, 0.7f, 0.8f), new Color(0.9f, 0.9f, 0.2f, 0.8f), }; internal void DrawComputedRectGizmos() { if (_computedRects == null || _computedRects.Count == 0) return; var srcSize = (Vector2)_size; var srcPivot = _size.pivot; var t = transform; for (int i = 0; i < _computedRects.Count; i++) { var dr = _computedRects[i]; if (dr == null || string.IsNullOrEmpty(dr.id)) continue; var rect = dr.Compute(srcSize, srcPivot); var color = _rectColors[i % _rectColors.Length]; Gizmos.color = color; var center = new Vector3(rect.x + rect.width * 0.5f, rect.y + rect.height * 0.5f, 0f); var size3 = _size.useZ ? new Vector3(rect.width, 0f, rect.height) : new Vector3(rect.width, rect.height, 0f); var worldCenter = t.TransformPoint(_size.useZ ? new Vector3(center.x, 0f, center.y) : center); Gizmos.matrix = Matrix4x4.TRS(worldCenter, t.rotation, Vector3.one); Gizmos.DrawWireCube(Vector3.zero, size3); } Gizmos.matrix = Matrix4x4.identity; } #endif } }