< Summary

Class:UISizeFitter
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIContainer/UISizeFitter.cs
Covered lines:58
Uncovered lines:7
Coverable lines:65
Total lines:166
Line coverage:89.2% (58 of 65)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UISizeFitter()0%110100%
Refresh()0%110100%
EnsureCanvasChildHookRectTransform()0%3.043083.33%
FitSizeToChildren(...)0%18.1418092.45%
RefreshRecursively_Node(...)0%220100%
RefreshRecursively(...)0%110100%
Awake()0%110100%
Update()0%2.52050%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIContainer/UISizeFitter.cs

#LineLine coverage
 1using DCL.Components;
 2using DCL.Helpers;
 3using System.Linq;
 4using UnityEngine;
 5using UnityEngine.UI;
 6
 7public class UISizeFitter : MonoBehaviour
 8{
 9    public static bool VERBOSE = false;
 6910    public bool adjustWidth = true;
 6911    public bool adjustHeight = true;
 12
 13    RectTransform canvasChildHookRT;
 14
 32615    public void Refresh() { FitSizeToChildren(adjustWidth, adjustHeight); }
 16
 17    void EnsureCanvasChildHookRectTransform()
 18    {
 16319        if (canvasChildHookRT == null)
 20        {
 4121            Canvas canvas = GetComponentInParent<Canvas>();
 4122            if (canvas)
 4123                canvasChildHookRT = canvas.GetComponent<RectTransform>();
 24            else
 025                canvasChildHookRT = GetComponentInParent<RectTransform>();
 26        }
 12227    }
 28
 29    public void FitSizeToChildren(bool adjustWidth = true, bool adjustHeight = true)
 30    {
 16331        UIReferencesContainer[] containers = GetComponentsInChildren<UIReferencesContainer>();
 32
 16333        EnsureCanvasChildHookRectTransform();
 34
 16335        RectTransform rt = transform as RectTransform;
 36
 16337        if (rt == null || canvasChildHookRT == null || containers == null || containers.Length == 0)
 38        {
 10139            return;
 40        }
 41
 6242        Rect finalRect = new Rect();
 6243        finalRect.xMax = float.MinValue;
 6244        finalRect.yMax = float.MinValue;
 6245        finalRect.xMin = float.MaxValue;
 6246        finalRect.yMin = float.MaxValue;
 47
 6248        Vector3[] corners = new Vector3[4];
 49
 6250        Vector3 center = Vector3.zero;
 51
 6252        Transform[] children = transform.Cast<Transform>().ToArray();
 53
 29254        for (int i = 0; i < children.Length; i++)
 55        {
 56            //NOTE(Brian): We need to remove the children because we are going to try and resize the father without alte
 57            //             their positions--we are going to re-add them later. Note that i'm setting the parent to canva
 58            //             this is because worldPositionStays parameter isn't working with rotated/scaled canvases, if I
 59            //             current canvas, this is solved.
 8460            children[i].SetParent(canvasChildHookRT, true);
 61        }
 62
 50863        foreach (UIReferencesContainer rc in containers)
 64        {
 19265            RectTransform r = rc.childHookRectTransform;
 66
 19267            if (r == transform)
 68            {
 69                continue;
 70            }
 71
 15072            if (VERBOSE)
 73            {
 074                Debug.Log($"..... container... {r.name} ... w = {r.sizeDelta.x} h = {r.sizeDelta.y}", rc.gameObject);
 75            }
 76
 15077            r.GetWorldCorners(corners);
 78
 79            //NOTE(Brian): We want the coords in canvas space to solve CanvasScaler issues and world canvas arbitrary tr
 15080            corners[0] = canvasChildHookRT.InverseTransformPoint(corners[0]);
 15081            corners[1] = canvasChildHookRT.InverseTransformPoint(corners[1]);
 15082            corners[2] = canvasChildHookRT.InverseTransformPoint(corners[2]);
 15083            corners[3] = canvasChildHookRT.InverseTransformPoint(corners[3]);
 84
 85            //TODO(Brian): This'll look cleaner with a Bounds.EncapsulateBounds solution.
 150086            for (int i = 0; i < corners.Length; i++)
 87            {
 60088                if (corners[i].x < finalRect.xMin)
 89                {
 8390                    finalRect.xMin = corners[i].x;
 91                }
 92
 60093                if (corners[i].x > finalRect.xMax)
 94                {
 17695                    finalRect.xMax = corners[i].x;
 96                }
 97
 60098                if (corners[i].y < finalRect.yMin)
 99                {
 90100                    finalRect.yMin = corners[i].y;
 101                }
 102
 600103                if (corners[i].y > finalRect.yMax)
 104                {
 132105                    finalRect.yMax = corners[i].y;
 106                }
 107            }
 108        }
 109
 62110        if (!adjustWidth)
 111        {
 0112            finalRect.width = rt.sizeDelta.x;
 113        }
 114
 62115        if (!adjustHeight)
 116        {
 0117            finalRect.height = rt.sizeDelta.y;
 118        }
 119
 62120        if (VERBOSE)
 121        {
 0122            Debug.Log($".....end! final rect... w = {finalRect.width} h = {finalRect.height}");
 123        }
 124
 125        //NOTE(Brian): In this last step, we need to transform from canvas space to world space.
 126        //             We need to use "position" because assumes its pivot in the lower right corner of the rect in worl
 127        //             This is exactly what we are looking for as we want an anchor agnostic solution.
 62128        rt.position = canvasChildHookRT.TransformPoint(finalRect.min + (finalRect.size * rt.pivot));
 62129        rt.sizeDelta = new Vector2(finalRect.width, finalRect.height);
 130
 292131        for (int i = 0; i < children.Length; i++)
 132        {
 84133            children[i].SetParent(transform, true);
 134        }
 62135    }
 136
 137    void RefreshRecursively_Node(UISizeFitter fitter)
 138    {
 163139        fitter.Refresh();
 140
 163141        LayoutGroup p = fitter.GetComponentInParent<LayoutGroup>();
 142
 163143        if (p != null)
 144        {
 163145            Utils.ForceRebuildLayoutImmediate(p.transform as RectTransform);
 146        }
 163147    }
 148
 188149    public void RefreshRecursively(Transform startTransform = null) { Utils.InverseTransformChildTraversal<UISizeFitter>
 150
 151#if UNITY_EDITOR
 132152    private void Awake() { forceRefresh = false; }
 153
 154    //NOTE(Brian): Only used for debugging
 155    public bool forceRefresh = false;
 156
 157    public void Update()
 158    {
 1009159        if (forceRefresh)
 160        {
 0161            RefreshRecursively();
 0162            forceRefresh = false;
 163        }
 1009164    }
 165#endif
 166}