< Summary

Class:DCL.Components.UIContainerStack
Assembly:DCL.Components.UI
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIContainer/UIContainerStack.cs
Covered lines:53
Uncovered lines:33
Coverable lines:86
Total lines:184
Line coverage:61.6% (53 of 86)
Covered branches:0
Total branches:0
Covered methods:10
Total methods:14
Method coverage:71.4% (10 of 14)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
GetDataFromPb(...)0%3421800%
UIContainerStack()0%110100%
GetClassId()0%2100%
AttachTo(...)0%2100%
DetachFrom(...)0%2100%
ApplyChanges(...)0%550100%
RefreshContainerForShape(...)0%33094.12%
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;
 9using Decentraland.Sdk.Ecs6;
 10using MainScripts.DCL.Components;
 11
 12namespace DCL.Components
 13{
 14    public class UIContainerStack : UIShape<UIContainerRectReferencesContainer, UIContainerStack.Model>
 15    {
 16        [System.Serializable]
 17        public new class Model : UIShape.Model
 18        {
 4519            public Color color = Color.clear;
 20            public StackOrientation stackOrientation = StackOrientation.VERTICAL;
 4521            public bool adaptWidth = true;
 4522            public bool adaptHeight = true;
 23            public float spacing = 0;
 24
 25            public override BaseModel GetDataFromJSON(string json) =>
 1526                Utils.SafeFromJson<Model>(json);
 27
 28            public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel)
 29            {
 030                if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.UiContainerStack)
 031                    return Utils.SafeUnimplemented<UIContainerStack, Model>(expected: ComponentBodyPayload.PayloadOneofC
 32
 033                var pb = new Model();
 034                if (pbModel.UiContainerStack.HasName) pb.name = pbModel.UiContainerStack.Name;
 035                if (pbModel.UiContainerStack.HasParentComponent) pb.parentComponent = pbModel.UiContainerStack.ParentCom
 036                if (pbModel.UiContainerStack.HasVisible) pb.visible = pbModel.UiContainerStack.Visible;
 037                if (pbModel.UiContainerStack.HasOpacity) pb.opacity = pbModel.UiContainerStack.Opacity;
 038                if (pbModel.UiContainerStack.HasHAlign) pb.hAlign = pbModel.UiContainerStack.HAlign;
 039                if (pbModel.UiContainerStack.HasVAlign) pb.vAlign = pbModel.UiContainerStack.VAlign;
 040                if (pbModel.UiContainerStack.Width != null) pb.width = SDK6DataMapExtensions.FromProtobuf(pb.width, pbMo
 041                if (pbModel.UiContainerStack.Height != null) pb.height = SDK6DataMapExtensions.FromProtobuf(pb.height, p
 042                if (pbModel.UiContainerStack.PositionX != null) pb.positionX = SDK6DataMapExtensions.FromProtobuf(pb.pos
 043                if (pbModel.UiContainerStack.PositionY != null) pb.positionY = SDK6DataMapExtensions.FromProtobuf(pb.pos
 044                if (pbModel.UiContainerStack.HasIsPointerBlocker) pb.isPointerBlocker = pbModel.UiContainerStack.IsPoint
 45
 046                if (pbModel.UiContainerStack.HasStackOrientation) pb.stackOrientation = (StackOrientation)pbModel.UiCont
 047                if (pbModel.UiContainerStack.HasAdaptWidth) pb.adaptWidth = pbModel.UiContainerStack.AdaptWidth;
 048                if (pbModel.UiContainerStack.HasAdaptHeight) pb.adaptHeight = pbModel.UiContainerStack.AdaptHeight;
 049                if (pbModel.UiContainerStack.HasSpacing) pb.spacing = pbModel.UiContainerStack.Spacing;
 050                if (pbModel.UiContainerStack.Color != null) pb.color = pbModel.UiContainerStack.Color.AsUnityColor();
 51
 052                return pb;
 53            }
 54        }
 55
 56        public enum StackOrientation
 57        {
 58            VERTICAL,
 59            HORIZONTAL
 60        }
 61
 1162        public override string referencesContainerPrefabName => "UIContainerRect";
 63
 1164        public Dictionary<string, GameObject> stackContainers = new Dictionary<string, GameObject>();
 65
 66        HorizontalOrVerticalLayoutGroup layoutGroup;
 67
 3368        public UIContainerStack() { model = new Model(); }
 69
 070        public override int GetClassId() { return (int) CLASS_ID.UI_CONTAINER_STACK; }
 71
 72        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 73        {
 074            Debug.LogError(
 75                "Aborted UIContainerStack attachment to an entity. UIShapes shouldn't be attached to entities.");
 076        }
 77
 078        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 79
 80        public override IEnumerator ApplyChanges(BaseModel newModel)
 81        {
 1882            referencesContainer.image.color = new Color(model.color.r, model.color.g, model.color.b, model.color.a);
 1883            referencesContainer.image.raycastTarget = model.color.a >= RAYCAST_ALPHA_THRESHOLD;
 84
 1885            if (model.stackOrientation == StackOrientation.VERTICAL && !(layoutGroup is VerticalLayoutGroup))
 86            {
 1187                Object.DestroyImmediate(layoutGroup, false);
 1188                layoutGroup = childHookRectTransform.gameObject.AddComponent<VerticalLayoutGroup>();
 89            }
 790            else if (model.stackOrientation == StackOrientation.HORIZONTAL && !(layoutGroup is HorizontalLayoutGroup))
 91            {
 292                Object.DestroyImmediate(layoutGroup, false);
 293                layoutGroup = childHookRectTransform.gameObject.AddComponent<HorizontalLayoutGroup>();
 94            }
 95
 1896            layoutGroup.childControlHeight = false;
 1897            layoutGroup.childControlWidth = false;
 1898            layoutGroup.childForceExpandWidth = false;
 1899            layoutGroup.childForceExpandHeight = false;
 18100            layoutGroup.spacing = model.spacing;
 101
 18102            referencesContainer.sizeFitter.adjustHeight = model.adaptHeight;
 18103            referencesContainer.sizeFitter.adjustWidth = model.adaptWidth;
 104
 18105            MarkLayoutDirty();
 18106            return null;
 107        }
 108
 109        void RefreshContainerForShape(BaseDisposable updatedComponent)
 110        {
 43111            UIShape childComponent = updatedComponent as UIShape;
 43112            Assert.IsTrue(childComponent != null, "This should never happen!!!!");
 113
 43114            if (((UIShape.Model)childComponent.GetModel()).parentComponent != this.id)
 115            {
 26116                MarkLayoutDirty();
 26117                return;
 118            }
 119
 17120            GameObject stackContainer = null;
 121
 17122            if (!stackContainers.ContainsKey(childComponent.id))
 123            {
 17124                stackContainer = Object.Instantiate(Resources.Load("UIContainerStackChild")) as GameObject;
 125#if UNITY_EDITOR
 17126                stackContainer.name = "UIContainerStackChild - " + childComponent.id;
 127#endif
 17128                stackContainers.Add(childComponent.id, stackContainer);
 129
 17130                int oldSiblingIndex = childComponent.referencesContainer.transform.GetSiblingIndex();
 17131                childComponent.referencesContainer.transform.SetParent(stackContainer.transform, false);
 17132                stackContainer.transform.SetParent(referencesContainer.childHookRectTransform, false);
 17133                stackContainer.transform.SetSiblingIndex(oldSiblingIndex);
 134            }
 135            else
 136            {
 0137                stackContainer = stackContainers[childComponent.id];
 138            }
 139
 17140            MarkLayoutDirty();
 17141        }
 142
 143        public override void OnChildAttached(UIShape parentComponent, UIShape childComponent)
 144        {
 36145            RefreshContainerForShape(childComponent);
 36146            childComponent.OnAppliedChanges -= RefreshContainerForShape;
 36147            childComponent.OnAppliedChanges += RefreshContainerForShape;
 36148        }
 149
 150        public override void RefreshDCLLayoutRecursively(bool refreshSize = true,
 151            bool refreshAlignmentAndPosition = true)
 152        {
 34153            base.RefreshDCLLayoutRecursively(refreshSize, refreshAlignmentAndPosition);
 34154            referencesContainer.sizeFitter.RefreshRecursively();
 34155        }
 156
 157        public override void OnChildDetached(UIShape parentComponent, UIShape childComponent)
 158        {
 5159            if (parentComponent != this)
 160            {
 5161                return;
 162            }
 163
 0164            if (stackContainers.ContainsKey(childComponent.id))
 165            {
 0166                Object.Destroy(stackContainers[childComponent.id]);
 0167                stackContainers[childComponent.id].transform.SetParent(null);
 0168                stackContainers[childComponent.id].name += "- Detached";
 0169                stackContainers.Remove(childComponent.id);
 170            }
 171
 0172            childComponent.OnAppliedChanges -= RefreshContainerForShape;
 0173            RefreshDCLLayout();
 0174        }
 175
 176        public override void Dispose()
 177        {
 1178            if (referencesContainer != null)
 1179                Utils.SafeDestroy(referencesContainer.gameObject);
 180
 1181            base.Dispose();
 1182        }
 183    }
 184}