using System.Collections.Generic; using UnityEngine; public static class PlanRecentering { public static Vector2 ComputeBoundsCenter(IList pts) { if (pts == null || pts.Count == 0) return Vector2.zero; float minX = pts[0].x, maxX = pts[0].x; float minY = pts[0].y, maxY = pts[0].y; for (int i = 1; i < pts.Count; i++) { var p = pts[i]; if (p.x < minX) minX = p.x; if (p.x > maxX) maxX = p.x; if (p.y < minY) minY = p.y; if (p.y > maxY) maxY = p.y; } return new Vector2((minX + maxX) * 0.5f, (minY + maxY) * 0.5f); } static void OffsetList(List pts, Vector2 delta) { if (pts == null) return; for (int i = 0; i < pts.Count; i++) pts[i] -= delta; } /// /// Recentre le plan à (0,0) en bougeant les WallPoint UI (enfants du container) /// + met à jour les listes _G.WallsPointStart/Center + force les WallLine à se recalculer. /// public static Vector2 RecenterNow_FromCurrentUIAndLists() { RectTransform pointContainer = WallCreationManager.GetWallPointContainer(); if (pointContainer == null || pointContainer.childCount == 0) return Vector2.zero; // 1) delta basé sur les positions UI (source de vérité pour WallLine) List uiPts = new(pointContainer.childCount); foreach (RectTransform rt in pointContainer) uiPts.Add(rt.anchoredPosition); Vector2 delta = ComputeBoundsCenter(uiPts); if (delta == Vector2.zero) return Vector2.zero; Debug.Log($"[PlanRecentering] delta={delta}"); // 2) bouger les points UI foreach (RectTransform rt in pointContainer) rt.anchoredPosition -= delta; // 3) bouger les listes globales (cohérence) OffsetList(_G.WallsPointStart, delta); OffsetList(_G.WallsPointCenter, delta); // 4) forcer refresh des WallLine RectTransform lineContainer = WallCreationManager.GetWallLineContainer(); if (lineContainer != null) { foreach (RectTransform lr in lineContainer) { WallLine wl = lr.GetComponent(); if (wl != null) wl.UpdateValuesFromPoints(); } } return delta; } }