< Summary

Class:DCL.Components.UIContainerStack
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIContainer/UIContainerStack.cs
Covered lines:55
Uncovered lines:13
Coverable lines:68
Total lines:154
Line coverage:80.8% (55 of 68)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
UIContainerStack()0%110100%
GetClassId()0%2100%
AttachTo(...)0%2100%
DetachFrom(...)0%2100%
ApplyChanges(...)0%550100%
RefreshContainerForShape(...)0%33094.44%
OnChildAttached(...)0%110100%
RefreshDCLLayoutRecursively(...)0%110100%
OnChildDetached(...)0%7.613020%
Dispose()0%220100%

File(s)

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

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.Helpers;
 3using DCL.Models;
 4using System.Collections;
 5using System.Collections.Generic;
 6using UnityEngine;
 7using UnityEngine.Assertions;
 8using UnityEngine.UI;
 9
 10namespace DCL.Components
 11{
 12    public class UIContainerStack : UIShape<UIContainerRectReferencesContainer, UIContainerStack.Model>
 13    {
 14        [System.Serializable]
 15        new public class Model : UIShape.Model
 16        {
 4517            public Color color = Color.clear;
 18            public StackOrientation stackOrientation = StackOrientation.VERTICAL;
 4519            public bool adaptWidth = true;
 4520            public bool adaptHeight = true;
 21            public float spacing = 0;
 22
 1523            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 24        }
 25
 26        public enum StackOrientation
 27        {
 28            VERTICAL,
 29            HORIZONTAL
 30        }
 31
 1132        public override string referencesContainerPrefabName => "UIContainerRect";
 33
 1134        public Dictionary<string, GameObject> stackContainers = new Dictionary<string, GameObject>();
 35
 36        HorizontalOrVerticalLayoutGroup layoutGroup;
 37
 3338        public UIContainerStack() { model = new Model(); }
 39
 040        public override int GetClassId() { return (int) CLASS_ID.UI_CONTAINER_STACK; }
 41
 42        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 43        {
 044            Debug.LogError(
 45                "Aborted UIContainerStack attachment to an entity. UIShapes shouldn't be attached to entities.");
 046        }
 47
 048        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 49
 50        public override IEnumerator ApplyChanges(BaseModel newModel)
 51        {
 1852            referencesContainer.image.color = new Color(model.color.r, model.color.g, model.color.b, model.color.a);
 1853            referencesContainer.image.raycastTarget = model.color.a >= RAYCAST_ALPHA_THRESHOLD;
 54
 1855            if (model.stackOrientation == StackOrientation.VERTICAL && !(layoutGroup is VerticalLayoutGroup))
 56            {
 1157                Object.DestroyImmediate(layoutGroup, false);
 1158                layoutGroup = childHookRectTransform.gameObject.AddComponent<VerticalLayoutGroup>();
 1159            }
 760            else if (model.stackOrientation == StackOrientation.HORIZONTAL && !(layoutGroup is HorizontalLayoutGroup))
 61            {
 262                Object.DestroyImmediate(layoutGroup, false);
 263                layoutGroup = childHookRectTransform.gameObject.AddComponent<HorizontalLayoutGroup>();
 64            }
 65
 1866            layoutGroup.childControlHeight = false;
 1867            layoutGroup.childControlWidth = false;
 1868            layoutGroup.childForceExpandWidth = false;
 1869            layoutGroup.childForceExpandHeight = false;
 1870            layoutGroup.spacing = model.spacing;
 71
 1872            referencesContainer.sizeFitter.adjustHeight = model.adaptHeight;
 1873            referencesContainer.sizeFitter.adjustWidth = model.adaptWidth;
 74
 1875            MarkLayoutDirty();
 1876            return null;
 77        }
 78
 79        void RefreshContainerForShape(BaseDisposable updatedComponent)
 80        {
 4381            UIShape childComponent = updatedComponent as UIShape;
 4382            Assert.IsTrue(childComponent != null, "This should never happen!!!!");
 83
 4384            if (((UIShape.Model) childComponent.GetModel()).parentComponent != id)
 85            {
 2686                MarkLayoutDirty();
 2687                return;
 88            }
 89
 1790            GameObject stackContainer = null;
 91
 1792            if (!stackContainers.ContainsKey(childComponent.id))
 93            {
 1794                stackContainer = Object.Instantiate(Resources.Load("UIContainerStackChild")) as GameObject;
 95#if UNITY_EDITOR
 1796                stackContainer.name = "UIContainerStackChild - " + childComponent.id;
 97#endif
 1798                stackContainers.Add(childComponent.id, stackContainer);
 99
 17100                int oldSiblingIndex = childComponent.referencesContainer.transform.GetSiblingIndex();
 17101                childComponent.referencesContainer.transform.SetParent(stackContainer.transform, false);
 17102                stackContainer.transform.SetParent(referencesContainer.childHookRectTransform, false);
 17103                stackContainer.transform.SetSiblingIndex(oldSiblingIndex);
 17104            }
 105            else
 106            {
 0107                stackContainer = stackContainers[childComponent.id];
 108            }
 109
 17110            MarkLayoutDirty();
 17111        }
 112
 113        public override void OnChildAttached(UIShape parentComponent, UIShape childComponent)
 114        {
 36115            RefreshContainerForShape(childComponent);
 36116            childComponent.OnAppliedChanges -= RefreshContainerForShape;
 36117            childComponent.OnAppliedChanges += RefreshContainerForShape;
 36118        }
 119
 120        public override void RefreshDCLLayoutRecursively(bool refreshSize = true,
 121            bool refreshAlignmentAndPosition = true)
 122        {
 35123            base.RefreshDCLLayoutRecursively(refreshSize, refreshAlignmentAndPosition);
 35124            referencesContainer.sizeFitter.RefreshRecursively();
 35125        }
 126
 127        public override void OnChildDetached(UIShape parentComponent, UIShape childComponent)
 128        {
 5129            if (parentComponent != this)
 130            {
 5131                return;
 132            }
 133
 0134            if (stackContainers.ContainsKey(childComponent.id))
 135            {
 0136                Object.Destroy(stackContainers[childComponent.id]);
 0137                stackContainers[childComponent.id].transform.SetParent(null);
 0138                stackContainers[childComponent.id].name += "- Detached";
 0139                stackContainers.Remove(childComponent.id);
 140            }
 141
 0142            childComponent.OnAppliedChanges -= RefreshContainerForShape;
 0143            RefreshDCLLayout();
 0144        }
 145
 146        public override void Dispose()
 147        {
 12148            if (referencesContainer != null)
 4149                Utils.SafeDestroy(referencesContainer.gameObject);
 150
 12151            base.Dispose();
 12152        }
 153    }
 154}