< 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:54
Uncovered lines:13
Coverable lines:67
Total lines:153
Line coverage:80.5% (54 of 67)
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);
 53
 1854            if (model.stackOrientation == StackOrientation.VERTICAL && !(layoutGroup is VerticalLayoutGroup))
 55            {
 1156                Object.DestroyImmediate(layoutGroup, false);
 1157                layoutGroup = childHookRectTransform.gameObject.AddComponent<VerticalLayoutGroup>();
 1158            }
 759            else if (model.stackOrientation == StackOrientation.HORIZONTAL && !(layoutGroup is HorizontalLayoutGroup))
 60            {
 261                Object.DestroyImmediate(layoutGroup, false);
 262                layoutGroup = childHookRectTransform.gameObject.AddComponent<HorizontalLayoutGroup>();
 63            }
 64
 1865            layoutGroup.childControlHeight = false;
 1866            layoutGroup.childControlWidth = false;
 1867            layoutGroup.childForceExpandWidth = false;
 1868            layoutGroup.childForceExpandHeight = false;
 1869            layoutGroup.spacing = model.spacing;
 70
 1871            referencesContainer.sizeFitter.adjustHeight = model.adaptHeight;
 1872            referencesContainer.sizeFitter.adjustWidth = model.adaptWidth;
 73
 1874            MarkLayoutDirty();
 1875            return null;
 76        }
 77
 78        void RefreshContainerForShape(BaseDisposable updatedComponent)
 79        {
 4380            UIShape childComponent = updatedComponent as UIShape;
 4381            Assert.IsTrue(childComponent != null, "This should never happen!!!!");
 82
 4383            if (((UIShape.Model) childComponent.GetModel()).parentComponent != id)
 84            {
 2685                MarkLayoutDirty();
 2686                return;
 87            }
 88
 1789            GameObject stackContainer = null;
 90
 1791            if (!stackContainers.ContainsKey(childComponent.id))
 92            {
 1793                stackContainer = Object.Instantiate(Resources.Load("UIContainerStackChild")) as GameObject;
 94#if UNITY_EDITOR
 1795                stackContainer.name = "UIContainerStackChild - " + childComponent.id;
 96#endif
 1797                stackContainers.Add(childComponent.id, stackContainer);
 98
 1799                int oldSiblingIndex = childComponent.referencesContainer.transform.GetSiblingIndex();
 17100                childComponent.referencesContainer.transform.SetParent(stackContainer.transform, false);
 17101                stackContainer.transform.SetParent(referencesContainer.childHookRectTransform, false);
 17102                stackContainer.transform.SetSiblingIndex(oldSiblingIndex);
 17103            }
 104            else
 105            {
 0106                stackContainer = stackContainers[childComponent.id];
 107            }
 108
 17109            MarkLayoutDirty();
 17110        }
 111
 112        public override void OnChildAttached(UIShape parentComponent, UIShape childComponent)
 113        {
 36114            RefreshContainerForShape(childComponent);
 36115            childComponent.OnAppliedChanges -= RefreshContainerForShape;
 36116            childComponent.OnAppliedChanges += RefreshContainerForShape;
 36117        }
 118
 119        public override void RefreshDCLLayoutRecursively(bool refreshSize = true,
 120            bool refreshAlignmentAndPosition = true)
 121        {
 35122            base.RefreshDCLLayoutRecursively(refreshSize, refreshAlignmentAndPosition);
 35123            referencesContainer.sizeFitter.RefreshRecursively();
 35124        }
 125
 126        public override void OnChildDetached(UIShape parentComponent, UIShape childComponent)
 127        {
 5128            if (parentComponent != this)
 129            {
 5130                return;
 131            }
 132
 0133            if (stackContainers.ContainsKey(childComponent.id))
 134            {
 0135                Object.Destroy(stackContainers[childComponent.id]);
 0136                stackContainers[childComponent.id].transform.SetParent(null);
 0137                stackContainers[childComponent.id].name += "- Detached";
 0138                stackContainers.Remove(childComponent.id);
 139            }
 140
 0141            childComponent.OnAppliedChanges -= RefreshContainerForShape;
 0142            RefreshDCLLayout();
 0143        }
 144
 145        public override void Dispose()
 146        {
 12147            if (referencesContainer != null)
 4148                Utils.SafeDestroy(referencesContainer.gameObject);
 149
 12150            base.Dispose();
 12151        }
 152    }
 153}