< Summary

Class:GridContainerComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/GridContainer/GridContainerComponentView.cs
Covered lines:86
Uncovered lines:48
Coverable lines:134
Total lines:354
Line coverage:64.1% (86 of 134)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GridContainerComponentView()0%110100%
PostInitialization()0%110100%
Configure(...)0%110100%
RefreshControl()0%2.012085.71%
PostScreenSizeChanged()0%110100%
Dispose()0%110100%
SetConstraint(...)0%2.032080%
SetConstraintCount(...)0%2.032080%
SetItemSizeToContainerAdaptation(...)0%2100%
SetItemSize(...)0%21.6211055.56%
SetSpaceBetweenItems(...)0%2.032080%
SetMinWidthForFlexibleItems(...)0%2100%
SetItems(...)0%4.014090.91%
GetItem(...)0%2.152066.67%
GetAllItems()0%2100%
CreateItem(...)0%4.123050%
InstantiateItem(...)0%3.653058.33%
InstantiateItemOnEditor()0%12300%
DestroyInstantiatedItems(...)0%13.368056.25%
DestroyGameObjectOnEditor()0%12300%
ResizeGridContainer()0%6.226081.82%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/GridContainer/GridContainerComponentView.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using UnityEngine.UI;
 5using static UnityEngine.UI.GridLayoutGroup;
 6
 7public interface IGridContainerComponentView
 8{
 9    /// <summary>
 10    /// Fill the model and updates the grid with this data.
 11    /// </summary>
 12    /// <param name="model">Data to configure the grid.</param>
 13    void Configure(GridContainerComponentModel model);
 14
 15    /// <summary>
 16    /// Set the type of constraint of the grid.
 17    /// </summary>
 18    /// <param name="newConstraint">Type of constraint.</param>
 19    void SetConstraint(Constraint newConstraint);
 20
 21    /// <summary>
 22    /// Set the number of columns/rows of the grid depending on the type of constraint set.
 23    /// </summary>
 24    /// <param name="newConstraintCount">Number of columns or rows.</param>
 25    void SetConstraintCount(int newConstraintCount);
 26
 27    /// <summary>
 28    /// Set the item size adaptation to the container.
 29    /// </summary>
 30    /// <param name="adaptItemSizeToContainer">True for activate the size adaptation.</param>
 31    void SetItemSizeToContainerAdaptation(bool adaptItemSizeToContainer);
 32
 33    /// <summary>
 34    /// Set the size of each child item.
 35    /// </summary>
 36    /// <param name="newItemSize">Size of each child.</param>
 37    void SetItemSize(Vector2 newItemSize);
 38
 39    /// <summary>
 40    /// Set the space between child items.
 41    /// </summary>
 42    /// <param name="newSpace">Space between children.</param>
 43    void SetSpaceBetweenItems(Vector2 newSpace);
 44
 45    /// <summary>
 46    /// Set the minimum width for the items when constraint is set to flexible.
 47    /// </summary>
 48    /// <param name="minWidthForFlexibleItems">Min item width.</param>
 49    void SetMinWidthForFlexibleItems(float minWidthForFlexibleItems);
 50
 51    /// <summary>
 52    /// Set the items of the grid.
 53    /// </summary>
 54    /// <param name="items">List of UI components.</param>
 55    /// <param name="instantiateNewCopyOfItems">Indicates if the items provided will be instantiated as a new copy or no
 56    void SetItems(List<BaseComponentView> items, bool instantiateNewCopyOfItems = true);
 57
 58    /// <summary>
 59    /// Get an item of the grid.
 60    /// </summary>
 61    /// <param name="index">Index of the list of items.</param>
 62    /// <returns>A specific UI component.</returns>
 63    BaseComponentView GetItem(int index);
 64
 65    /// <summary>
 66    /// Get all the items of the grid.
 67    /// </summary>
 68    /// <returns>The list of items.</returns>
 69    List<BaseComponentView> GetAllItems();
 70}
 71
 72public class GridContainerComponentView : BaseComponentView, IGridContainerComponentView
 73{
 74    [Header("Prefab References")]
 75    [SerializeField] internal GridLayoutGroup gridLayoutGroup;
 76    [SerializeField] internal RectTransform externalParentToAdaptSize;
 77
 78    [Header("Configuration")]
 79    [SerializeField] internal GridContainerComponentModel model;
 80
 8681    internal List<BaseComponentView> instantiatedItems = new List<BaseComponentView>();
 82    internal bool destroyOnlyUnnecesaryItems = false;
 83
 884    public override void PostInitialization() { Configure(model); }
 85
 86    public void Configure(GridContainerComponentModel model)
 87    {
 588        this.model = model;
 589        RefreshControl();
 590    }
 91
 92    public override void RefreshControl()
 93    {
 694        if (model == null)
 095            return;
 96
 697        SetConstraint(model.constraint);
 698        SetItems(model.items);
 699        SetConstraintCount(model.constranitCount);
 6100        SetSpaceBetweenItems(model.spaceBetweenItems);
 6101    }
 102
 103    public override void PostScreenSizeChanged()
 104    {
 1105        base.PostScreenSizeChanged();
 106
 1107        ResizeGridContainer();
 1108        SetItemSize(model.itemSize);
 1109    }
 110
 111    public override void Dispose()
 112    {
 23113        base.Dispose();
 114
 23115        DestroyInstantiatedItems(true);
 23116    }
 117
 118    public void SetConstraint(Constraint newConstraint)
 119    {
 12120        model.constraint = newConstraint;
 121
 12122        if (gridLayoutGroup == null)
 0123            return;
 124
 12125        gridLayoutGroup.constraint = newConstraint;
 12126    }
 127
 128    public void SetConstraintCount(int newConstraintCount)
 129    {
 7130        model.constranitCount = newConstraintCount;
 131
 7132        if (gridLayoutGroup == null)
 0133            return;
 134
 7135        gridLayoutGroup.constraintCount = newConstraintCount;
 7136    }
 137
 138    public void SetItemSizeToContainerAdaptation(bool adaptItemSizeToContainer)
 139    {
 0140        model.adaptItemSizeToContainer = adaptItemSizeToContainer;
 0141        SetItemSize(model.itemSize);
 0142    }
 143
 144    public void SetItemSize(Vector2 newItemSize)
 145    {
 13146        Vector2 newSizeToApply = newItemSize;
 13147        if (model.adaptItemSizeToContainer)
 148        {
 4149            if (model.constraint == Constraint.FixedColumnCount)
 150            {
 1151                float width = externalParentToAdaptSize != null ? externalParentToAdaptSize.rect.width : ((RectTransform
 1152                float extraSpaceToRemove = (model.spaceBetweenItems.x * (model.constranitCount - 1)) / model.constranitC
 1153                newSizeToApply = new Vector2(
 154                    (width / model.constranitCount) - extraSpaceToRemove,
 155                    model.itemSize.y);
 1156            }
 3157            else if (model.constraint == Constraint.FixedRowCount)
 158            {
 0159                float height = ((RectTransform)transform).rect.height;
 0160                float extraSpaceToRemove = (model.spaceBetweenItems.y / (model.constranitCount / 2f));
 0161                newSizeToApply = new Vector2(
 162                    model.itemSize.x,
 163                    (height / model.constranitCount) - extraSpaceToRemove);
 0164            }
 3165            else if (model.constraint == Constraint.Flexible)
 166            {
 3167                float width = externalParentToAdaptSize != null ? externalParentToAdaptSize.rect.width : ((RectTransform
 3168                int numberOfPossibleItems = (int)(width / model.minWidthForFlexibleItems);
 169
 3170                SetConstraint(Constraint.FixedColumnCount);
 171
 3172                if (numberOfPossibleItems > 0)
 173                {
 0174                    for (int numColumnsToTry = 1; numColumnsToTry <= numberOfPossibleItems; numColumnsToTry++)
 175                    {
 0176                        SetConstraintCount(numColumnsToTry);
 0177                        SetItemSize(model.itemSize);
 178
 0179                        if (model.itemSize.x < model.minWidthForFlexibleItems)
 180                        {
 0181                            SetConstraintCount(numColumnsToTry - 1);
 0182                            SetItemSize(model.itemSize);
 0183                            break;
 184                        }
 185
 0186                        newSizeToApply = model.itemSize;
 187                    }
 0188                }
 189                else
 190                {
 3191                    newSizeToApply = new Vector2(model.minWidthForFlexibleItems, newSizeToApply.y);
 192                }
 193
 3194                SetConstraint(Constraint.Flexible);
 195            }
 196        }
 197
 13198        model.itemSize = newSizeToApply;
 199
 13200        if (gridLayoutGroup == null)
 0201            return;
 202
 13203        gridLayoutGroup.cellSize = newSizeToApply;
 204
 13205        ResizeGridContainer();
 13206    }
 207
 208    public void SetSpaceBetweenItems(Vector2 newSpace)
 209    {
 7210        model.spaceBetweenItems = newSpace;
 211
 7212        if (gridLayoutGroup == null)
 0213            return;
 214
 7215        gridLayoutGroup.spacing = newSpace;
 7216    }
 217
 218    public void SetMinWidthForFlexibleItems(float minWidthForFlexibleItems)
 219    {
 0220        model.minWidthForFlexibleItems = minWidthForFlexibleItems;
 0221        SetItemSize(model.itemSize);
 0222    }
 223
 224    public void SetItems(List<BaseComponentView> items, bool instantiateNewCopyOfItems = true)
 225    {
 10226        model.items = items;
 227
 10228        DestroyInstantiatedItems(!destroyOnlyUnnecesaryItems);
 229
 62230        for (int i = 0; i < items.Count; i++)
 231        {
 21232            CreateItem(items[i], $"Item{i}", instantiateNewCopyOfItems && !destroyOnlyUnnecesaryItems);
 233        }
 234
 10235        ResizeGridContainer();
 236
 10237        if (!instantiateNewCopyOfItems)
 0238            destroyOnlyUnnecesaryItems = true;
 239
 10240        SetItemSize(model.itemSize);
 10241    }
 242
 243    public BaseComponentView GetItem(int index)
 244    {
 2245        if (index >= instantiatedItems.Count)
 0246            return null;
 247
 2248        return instantiatedItems[index];
 249    }
 250
 0251    public List<BaseComponentView> GetAllItems() { return instantiatedItems; }
 252
 253    internal void CreateItem(BaseComponentView newItem, string name, bool instantiateNewCopyOfItem = true)
 254    {
 21255        if (Application.isPlaying)
 256        {
 21257            InstantiateItem(newItem, name, instantiateNewCopyOfItem);
 21258        }
 259        else
 260        {
 0261            if (isActiveAndEnabled)
 0262                StartCoroutine(InstantiateItemOnEditor(newItem, name));
 263        }
 0264    }
 265
 266    internal void InstantiateItem(BaseComponentView newItem, string name, bool instantiateNewCopyOfItem = true)
 267    {
 21268        if (newItem == null)
 0269            return;
 270
 271        BaseComponentView newGO;
 21272        if (instantiateNewCopyOfItem)
 273        {
 21274            newGO = Instantiate(newItem, transform);
 21275        }
 276        else
 277        {
 0278            newGO = newItem;
 0279            newGO.transform.SetParent(transform);
 0280            newGO.transform.localPosition = Vector3.zero;
 0281            newGO.transform.localScale = Vector3.one;
 282        }
 283
 21284        newGO.name = name;
 21285        instantiatedItems.Add(newGO);
 21286    }
 287
 288    internal IEnumerator InstantiateItemOnEditor(BaseComponentView newItem, string name)
 289    {
 0290        yield return null;
 0291        InstantiateItem(newItem, name);
 0292    }
 293
 294    internal void DestroyInstantiatedItems(bool forzeToDestroyAll)
 295    {
 34296        if (forzeToDestroyAll)
 297        {
 158298            foreach (Transform child in transform)
 299            {
 45300                if (Application.isPlaying)
 301                {
 45302                    Destroy(child.gameObject);
 45303                }
 304                else
 305                {
 0306                    if (isActiveAndEnabled)
 0307                        StartCoroutine(DestroyGameObjectOnEditor(child.gameObject));
 308                }
 309            }
 310        }
 311        else
 312        {
 0313            foreach (BaseComponentView child in instantiatedItems)
 314            {
 0315                if (!model.items.Contains(child))
 316                {
 0317                    Destroy(child.gameObject);
 318                }
 319            }
 320        }
 321
 34322        instantiatedItems.Clear();
 34323    }
 324
 325    internal IEnumerator DestroyGameObjectOnEditor(GameObject go)
 326    {
 0327        yield return null;
 0328        DestroyImmediate(go);
 0329    }
 330
 331    internal void ResizeGridContainer()
 332    {
 24333        if (model.items.Count == 0)
 334        {
 10335            ((RectTransform)transform).sizeDelta = Vector2.zero;
 10336            return;
 337        }
 338
 14339        if (model.constraint == Constraint.FixedColumnCount)
 340        {
 8341            int numRows = (int)Mathf.Ceil((float)model.items.Count / model.constranitCount);
 8342            ((RectTransform)transform).sizeDelta = new Vector2(
 343                model.adaptItemSizeToContainer ? ((RectTransform)transform).sizeDelta.x : (model.constranitCount * model
 344                (numRows * model.itemSize.y) + (model.spaceBetweenItems.y * (numRows - 1)));
 8345        }
 6346        else if (model.constraint == Constraint.FixedRowCount)
 347        {
 0348            int numCols = (int)Mathf.Ceil((float)model.items.Count / model.constranitCount);
 0349            ((RectTransform)transform).sizeDelta = new Vector2(
 350                (numCols * model.itemSize.x) + (model.spaceBetweenItems.x * (numCols - 1)),
 351                model.adaptItemSizeToContainer ? ((RectTransform)transform).sizeDelta.y : (model.constranitCount * model
 352        }
 6353    }
 354}