< Summary

Class:DCL.Components.UIScrollRect
Assembly:DCL.Components.UI
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIScrollRect/UIScrollRect.cs
Covered lines:50
Uncovered lines:28
Coverable lines:78
Total lines:160
Line coverage:64.1% (50 of 78)
Covered branches:0
Total branches:0
Covered methods:12
Total methods:15
Method coverage:80% (12 of 15)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
GetDataFromPb(...)0%5522300%
UIScrollRect()0%110100%
AttachTo(...)0%2100%
DetachFrom(...)0%2100%
OnChildAttached(...)0%110100%
OnChildDetached(...)0%110100%
RefreshContainerForShape(...)0%110100%
AdjustChildHook()0%110100%
RefreshDCLLayoutRecursively(...)0%110100%
ApplyChanges(...)0%110100%
OnChanged(...)0%110100%
Dispose()0%220100%

File(s)

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

#LineLine coverage
 1using DCL.Helpers;
 2using DCL.Interface;
 3using DCL.Models;
 4using System.Collections;
 5using UnityEngine;
 6using UnityEngine.UI;
 7using Color = UnityEngine.Color;
 8using Decentraland.Sdk.Ecs6;
 9using MainScripts.DCL.Components;
 10
 11namespace DCL.Components
 12{
 13    public class UIScrollRect : UIShape<UIScrollRectRefContainer, UIScrollRect.Model>
 14    {
 15        [System.Serializable]
 16        public new class Model : UIShape.Model
 17        {
 18            public float valueX = 0;
 19            public float valueY = 0;
 2620            public Color backgroundColor = Color.clear;
 21            public bool isHorizontal = false;
 2622            public bool isVertical = true;
 23            public float paddingTop = 0f;
 24            public float paddingRight = 0f;
 25            public float paddingBottom = 0f;
 26            public float paddingLeft = 0f;
 27            public string OnChanged;
 28
 29            public override BaseModel GetDataFromJSON(string json) =>
 830                Utils.SafeFromJson<Model>(json);
 31
 32            public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel)
 33            {
 034                if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.UiScrollRect)
 035                    return Utils.SafeUnimplemented<UIScrollRect, Model>(expected: ComponentBodyPayload.PayloadOneofCase.
 36
 037                var pb = new Model();
 038                if (pbModel.UiScrollRect.HasName) pb.name = pbModel.UiScrollRect.Name;
 039                if (pbModel.UiScrollRect.HasParentComponent) pb.parentComponent = pbModel.UiScrollRect.ParentComponent;
 040                if (pbModel.UiScrollRect.HasVisible) pb.visible = pbModel.UiScrollRect.Visible;
 041                if (pbModel.UiScrollRect.HasOpacity) pb.opacity = pbModel.UiScrollRect.Opacity;
 042                if (pbModel.UiScrollRect.HasHAlign) pb.hAlign = pbModel.UiScrollRect.HAlign;
 043                if (pbModel.UiScrollRect.HasVAlign) pb.vAlign = pbModel.UiScrollRect.VAlign;
 044                if (pbModel.UiScrollRect.Width != null) pb.width = SDK6DataMapExtensions.FromProtobuf(pb.width, pbModel.
 045                if (pbModel.UiScrollRect.Height != null) pb.height = SDK6DataMapExtensions.FromProtobuf(pb.height, pbMod
 046                if (pbModel.UiScrollRect.PositionX != null) pb.positionX = SDK6DataMapExtensions.FromProtobuf(pb.positio
 047                if (pbModel.UiScrollRect.PositionY != null) pb.positionY = SDK6DataMapExtensions.FromProtobuf(pb.positio
 048                if (pbModel.UiScrollRect.HasIsPointerBlocker) pb.isPointerBlocker = pbModel.UiScrollRect.IsPointerBlocke
 49
 050                if (pbModel.UiScrollRect.HasValueX) pb.valueX = pbModel.UiScrollRect.ValueX;
 051                if (pbModel.UiScrollRect.HasValueY) pb.valueY = pbModel.UiScrollRect.ValueY;
 052                if (pbModel.UiScrollRect.BackgroundColor != null) pb.backgroundColor = pbModel.UiScrollRect.BackgroundCo
 053                if (pbModel.UiScrollRect.IsHorizontal) pb.isHorizontal = pbModel.UiScrollRect.IsHorizontal;
 054                if (pbModel.UiScrollRect.IsVertical) pb.isVertical = pbModel.UiScrollRect.IsVertical;
 055                if (pbModel.UiScrollRect.HasPaddingTop) pb.paddingTop = pbModel.UiScrollRect.PaddingTop;
 056                if (pbModel.UiScrollRect.HasPaddingRight) pb.paddingRight = pbModel.UiScrollRect.PaddingRight;
 057                if (pbModel.UiScrollRect.HasPaddingBottom) pb.paddingBottom = pbModel.UiScrollRect.PaddingBottom;
 058                if (pbModel.UiScrollRect.HasPaddingLeft) pb.paddingLeft = pbModel.UiScrollRect.PaddingLeft;
 059                if (pbModel.UiScrollRect.HasOnChanged) pb.OnChanged = pbModel.UiScrollRect.OnChanged;
 60
 061                return pb;
 62            }
 63        }
 64
 565        public override string referencesContainerPrefabName => "UIScrollRect";
 66
 1567        public UIScrollRect() { model = new Model(); }
 68
 69        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 70        {
 071            Debug.LogError(
 72                "Aborted UIScrollRectShape attachment to an entity. UIShapes shouldn't be attached to entities.");
 073        }
 74
 075        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 76
 77        public override void OnChildAttached(UIShape parent, UIShape childComponent)
 78        {
 979            base.OnChildAttached(parent, childComponent);
 980            childComponent.OnAppliedChanges -= RefreshContainerForShape;
 981            childComponent.OnAppliedChanges += RefreshContainerForShape;
 982            RefreshContainerForShape(childComponent);
 983        }
 84
 85        public override void OnChildDetached(UIShape parent, UIShape childComponent)
 86        {
 487            base.OnChildDetached(parent, childComponent);
 488            childComponent.OnAppliedChanges -= RefreshContainerForShape;
 489        }
 90
 91        void RefreshContainerForShape(BaseDisposable updatedComponent)
 92        {
 1293            MarkLayoutDirty( () =>
 94                {
 1295                    referencesContainer.fitter.RefreshRecursively();
 1296                    AdjustChildHook();
 1297                    referencesContainer.scrollRect.Rebuild(CanvasUpdate.MaxUpdateValue);
 1298                }
 99            );
 12100        }
 101
 102        void AdjustChildHook()
 103        {
 24104            UIScrollRectRefContainer rc = referencesContainer;
 24105            rc.childHookRectTransform.SetParent(rc.layoutElementRT, false);
 24106            rc.childHookRectTransform.SetToMaxStretch();
 24107            rc.childHookRectTransform.SetParent(rc.content, true);
 24108            RefreshDCLLayoutRecursively(false, true);
 24109        }
 110
 111        public override void RefreshDCLLayoutRecursively(bool refreshSize = true,
 112            bool refreshAlignmentAndPosition = true)
 113        {
 36114            base.RefreshDCLLayoutRecursively(refreshSize, refreshAlignmentAndPosition);
 36115            referencesContainer.fitter.RefreshRecursively();
 36116        }
 117
 118        public override IEnumerator ApplyChanges(BaseModel newModel)
 119        {
 12120            UIScrollRectRefContainer rc = referencesContainer;
 121
 12122            rc.contentBackground.color = model.backgroundColor;
 123
 124            // Apply padding
 12125            rc.paddingLayoutGroup.padding.bottom = Mathf.RoundToInt(model.paddingBottom);
 12126            rc.paddingLayoutGroup.padding.top = Mathf.RoundToInt(model.paddingTop);
 12127            rc.paddingLayoutGroup.padding.left = Mathf.RoundToInt(model.paddingLeft);
 12128            rc.paddingLayoutGroup.padding.right = Mathf.RoundToInt(model.paddingRight);
 129
 12130            rc.scrollRect.horizontal = model.isHorizontal;
 12131            rc.scrollRect.vertical = model.isVertical;
 132
 12133            rc.HScrollbar.value = model.valueX;
 12134            rc.VScrollbar.value = model.valueY;
 135
 12136            rc.scrollRect.onValueChanged.AddListener(OnChanged);
 137
 12138            MarkLayoutDirty( () =>
 139                {
 12140                    referencesContainer.fitter.RefreshRecursively();
 12141                    AdjustChildHook();
 12142                }
 143            );
 12144            return null;
 145        }
 146
 30147        void OnChanged(Vector2 scrollingValues) { WebInterface.ReportOnScrollChange(scene.sceneData.sceneNumber, model.O
 148
 149        public override void Dispose()
 150        {
 1151            if (referencesContainer != null)
 152            {
 1153                referencesContainer.scrollRect.onValueChanged.RemoveAllListeners();
 1154                Utils.SafeDestroy(referencesContainer.gameObject);
 155            }
 156
 1157            base.Dispose();
 1158        }
 159    }
 160}