< Summary

Class:DCL.ECSComponents.UITransformHandler
Assembly:DCL.ECSComponents.UITransform
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/UITransform/UITransformHandler.cs
Covered lines:84
Uncovered lines:23
Coverable lines:107
Total lines:281
Line coverage:78.5% (84 of 107)
Covered branches:0
Total branches:0
Covered methods:13
Total methods:13
Method coverage:100% (13 of 13)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UITransformHandler(...)0%110100%
OnComponentCreated(...)0%110100%
OnComponentRemoved(...)0%440100%
OnComponentModelUpdated(...)0%440100%
SetUpVisualElement(...)0%31.0127082.35%
GetUnit(...)0%220100%
GetOverflow(...)0%2.52050%
GetDisplay(...)0%2.52050%
GetJustify(...)0%21.527033.33%
GetWrap(...)0%8.125050%
GetFlexDirection(...)0%13.786040%
GetPosition(...)0%220100%
GetAlign(...)0%13.127050%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/UITransform/UITransformHandler.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.ECS7.InternalComponents;
 3using DCL.ECSRuntime;
 4using DCL.Models;
 5using UnityEngine.UIElements;
 6
 7namespace DCL.ECSComponents
 8{
 9    public class UITransformHandler : IECSComponentHandler<PBUiTransform>
 10    {
 11        private readonly IInternalECSComponent<InternalUiContainer> internalUiContainer;
 12        private readonly int componentId;
 13
 914        public UITransformHandler(IInternalECSComponent<InternalUiContainer> internalUiContainer, int componentId)
 15        {
 916            this.internalUiContainer = internalUiContainer;
 917            this.componentId = componentId;
 918        }
 19
 220        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { }
 21
 22        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 23        {
 224            var containerData = internalUiContainer.GetFor(scene, entity);
 225            if (containerData != null)
 26            {
 227                var containerModel = containerData.Value.model;
 228                containerModel.components.Remove(componentId);
 29
 30                // do parent detach only if not child of root entity
 31                // since ui element without transform should be always attached
 32                // to the root entity
 233                if (containerModel.parentId != SpecialEntityId.SCENE_ROOT_ENTITY)
 34                {
 235                    containerModel.parentElement?.Remove(containerModel.rootElement);
 236                    containerModel.parentElement = null;
 37                }
 238                containerModel.parentId = SpecialEntityId.SCENE_ROOT_ENTITY;
 239                internalUiContainer.PutFor(scene, entity, containerModel);
 40            }
 241        }
 42
 43        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBUiTransform model)
 44        {
 1345            var containerModel = internalUiContainer.GetFor(scene, entity)?.model ?? new InternalUiContainer(entity.enti
 46
 1347            containerModel.components.Add(componentId);
 48
 1349            if (containerModel.parentId != model.Parent)
 50            {
 551                containerModel.parentId = model.Parent;
 552                containerModel.parentElement?.Remove(containerModel.rootElement);
 553                containerModel.parentElement = null;
 54            }
 1355            containerModel.shouldSort = containerModel.rightOf != model.RightOf;
 1356            containerModel.rightOf = model.RightOf;
 57
 1358            VisualElement element = containerModel.rootElement;
 59
 1360            SetUpVisualElement(element, model);
 1361            internalUiContainer.PutFor(scene, entity, containerModel);
 1362        }
 63
 64        private static void SetUpVisualElement(VisualElement element, PBUiTransform model)
 65        {
 1366            element.style.display = GetDisplay(model.Display);
 1367            element.style.overflow = GetOverflow(model.Overflow);
 68
 69            // Pointer blocking
 1370            element.pickingMode = model.PointerFilter == PointerFilterMode.PfmBlock ? PickingMode.Position : PickingMode
 71
 72            // Flex
 1373            element.style.flexDirection = GetFlexDirection(model.FlexDirection);
 1374            if (model.FlexBasisUnit != YGUnit.YguUndefined)
 75            {
 076                element.style.flexBasis = model.FlexBasisUnit == YGUnit.YguAuto ? new StyleLength(StyleKeyword.Auto) : n
 77            }
 78
 1379            element.style.flexGrow = model.FlexGrow;
 1380            element.style.flexShrink = model.GetFlexShrink();
 1381            element.style.flexWrap = GetWrap(model.GetFlexWrap());
 82
 83            // Align
 1384            element.style.alignContent = GetAlign(model.GetAlignContent());
 1385            element.style.alignItems = GetAlign(model.GetAlignItems());
 1386            element.style.alignSelf = GetAlign(model.AlignSelf);
 1387            element.style.justifyContent = GetJustify(model.JustifyContent);
 88
 89            // Layout size
 1390            if (model.HeightUnit != YGUnit.YguUndefined)
 91            {
 192                element.style.height = model.HeightUnit == YGUnit.YguAuto ? new StyleLength(StyleKeyword.Auto) : new Len
 93            }
 1394            if (model.WidthUnit != YGUnit.YguUndefined)
 95            {
 196                element.style.width = model.WidthUnit == YGUnit.YguAuto ? new StyleLength(StyleKeyword.Auto) : new Lengt
 97            }
 1398            if (model.MaxWidthUnit != YGUnit.YguUndefined)
 99            {
 0100                element.style.maxWidth = model.MaxWidthUnit == YGUnit.YguAuto ? new StyleLength(StyleKeyword.Auto) : new
 101            }
 13102            if (model.MaxHeightUnit != YGUnit.YguUndefined)
 103            {
 0104                element.style.maxHeight = model.MaxHeightUnit == YGUnit.YguAuto ? new StyleLength(StyleKeyword.Auto) : n
 105            }
 13106            if (model.MinHeightUnit != YGUnit.YguUndefined)
 107            {
 0108                element.style.minHeight = new Length(model.MinHeight, GetUnit(model.MinHeightUnit));
 109            }
 13110            if (model.MinWidthUnit != YGUnit.YguUndefined)
 111            {
 0112                element.style.minWidth = new Length(model.MinWidth, GetUnit(model.MinWidthUnit));
 113            }
 114
 115            // Paddings
 13116            if (model.PaddingBottomUnit != YGUnit.YguUndefined)
 117            {
 1118                element.style.paddingBottom = new Length(model.PaddingBottom, GetUnit(model.PaddingBottomUnit));
 119            }
 13120            if (model.PaddingLeftUnit != YGUnit.YguUndefined)
 121            {
 1122                element.style.paddingLeft = new Length(model.PaddingLeft, GetUnit(model.PaddingLeftUnit));
 123            }
 13124            if (model.PaddingRightUnit != YGUnit.YguUndefined)
 125            {
 1126                element.style.paddingRight = new Length(model.PaddingRight, GetUnit(model.PaddingRightUnit));
 127            }
 13128            if (model.PaddingTopUnit != YGUnit.YguUndefined)
 129            {
 1130                element.style.paddingTop = new Length(model.PaddingTop, GetUnit(model.PaddingTopUnit));
 131            }
 132
 133            // Margins
 13134            if (model.MarginLeftUnit != YGUnit.YguUndefined)
 135            {
 1136                element.style.marginLeft = new Length(model.MarginLeft, GetUnit(model.MarginLeftUnit));
 137            }
 13138            if (model.MarginRightUnit != YGUnit.YguUndefined)
 139            {
 1140                element.style.marginRight = new Length(model.MarginRight, GetUnit(model.MarginRightUnit));
 141            }
 13142            if (model.MarginBottomUnit != YGUnit.YguUndefined)
 143            {
 1144                element.style.marginBottom = new Length(model.MarginBottom, GetUnit(model.MarginBottomUnit));
 145            }
 13146            if (model.MarginTopUnit != YGUnit.YguUndefined)
 147            {
 1148                element.style.marginTop = new Length(model.MarginTop, GetUnit(model.MarginTopUnit));
 149            }
 150
 151            // Position
 13152            element.style.position = GetPosition(model.PositionType);
 153
 13154            if (model.PositionTopUnit != YGUnit.YguUndefined)
 0155                element.style.top = new Length(model.PositionTop, GetUnit(model.PositionTopUnit));
 156
 13157            if (model.PositionBottomUnit != YGUnit.YguUndefined)
 0158                element.style.bottom = new Length(model.PositionBottom, GetUnit(model.PositionBottomUnit));
 159
 13160            if (model.PositionRightUnit != YGUnit.YguUndefined)
 0161                element.style.right = new Length(model.PositionRight, GetUnit(model.PositionRightUnit));
 162
 13163            if (model.PositionLeftUnit != YGUnit.YguUndefined)
 0164                element.style.left = new Length(model.PositionLeft, GetUnit(model.PositionLeftUnit));
 13165        }
 166
 167        private static LengthUnit GetUnit(YGUnit unit)
 168        {
 169            switch (unit)
 170            {
 171                case YGUnit.YguPercent:
 4172                    return LengthUnit.Percent;
 173                default:
 4174                    return LengthUnit.Pixel;
 175            }
 176        }
 177
 178        private static StyleEnum<Overflow> GetOverflow(YGOverflow overflow)
 179        {
 180            switch (overflow)
 181            {
 182                case YGOverflow.YgoHidden:
 0183                    return Overflow.Hidden;
 184                default:
 13185                    return Overflow.Visible;
 186            }
 187        }
 188
 189        private static StyleEnum<DisplayStyle> GetDisplay(YGDisplay display)
 190        {
 191            switch (display)
 192            {
 193                case YGDisplay.YgdNone:
 0194                    return DisplayStyle.None;
 195                default:
 13196                    return DisplayStyle.Flex;
 197            }
 198        }
 199
 200        private static StyleEnum<Justify> GetJustify(YGJustify justify)
 201        {
 202            switch (justify)
 203            {
 204                case YGJustify.YgjFlexStart:
 12205                    return Justify.FlexStart;
 206                case YGJustify.YgjCenter:
 0207                    return Justify.Center;
 208                case YGJustify.YgjFlexEnd:
 0209                    return Justify.FlexEnd;
 210                case YGJustify.YgjSpaceBetween:
 0211                    return Justify.SpaceBetween;
 212                case YGJustify.YgjSpaceAround:
 1213                    return Justify.SpaceAround;
 214                default:
 0215                    return Justify.FlexStart;
 216            }
 217        }
 218
 219        private static StyleEnum<Wrap> GetWrap(YGWrap wrap)
 220        {
 221            switch (wrap)
 222            {
 223                case YGWrap.YgwNoWrap:
 12224                    return Wrap.NoWrap;
 225                case YGWrap.YgwWrap:
 0226                    return Wrap.Wrap;
 227                case YGWrap.YgwWrapReverse:
 1228                    return Wrap.WrapReverse;
 229                default:
 0230                    return Wrap.Wrap;
 231            }
 232        }
 233
 234        private static StyleEnum<FlexDirection> GetFlexDirection(YGFlexDirection direction)
 235        {
 236            switch (direction)
 237            {
 238                case YGFlexDirection.YgfdColumn:
 0239                    return FlexDirection.Column;
 240                case YGFlexDirection.YgfdColumnReverse:
 1241                    return FlexDirection.ColumnReverse;
 242                case YGFlexDirection.YgfdRow:
 12243                    return FlexDirection.Row;
 244                case YGFlexDirection.YgfdRowReverse:
 0245                    return FlexDirection.RowReverse;
 246                default:
 0247                    return FlexDirection.Row;
 248            }
 249        }
 250
 251        private static StyleEnum<UnityEngine.UIElements.Position> GetPosition(YGPositionType positionType)
 252        {
 253            switch (positionType)
 254            {
 255                case YGPositionType.YgptAbsolute:
 5256                    return UnityEngine.UIElements.Position.Absolute;
 257                default:
 8258                    return UnityEngine.UIElements.Position.Relative;
 259            }
 260        }
 261
 262        private static StyleEnum<Align> GetAlign(YGAlign align)
 263        {
 264            switch (align)
 265            {
 266                case YGAlign.YgaAuto:
 12267                    return Align.Auto;
 268                case YGAlign.YgaFlexStart:
 0269                    return Align.FlexStart;
 270                case YGAlign.YgaCenter:
 2271                    return Align.Center;
 272                case YGAlign.YgaFlexEnd:
 0273                    return Align.FlexEnd;
 274                case YGAlign.YgaStretch:
 25275                    return Align.Stretch;
 276                default:
 0277                    return Align.Auto;
 278            }
 279        }
 280    }
 281}