< 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:80
Uncovered lines:30
Coverable lines:110
Total lines:286
Line coverage:72.7% (80 of 110)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UITransformHandler(...)0%110100%
OnComponentCreated(...)0%2100%
OnComponentRemoved(...)0%440100%
OnComponentModelUpdated(...)0%660100%
SetUpVisualElement(...)0%28.6323078%
GetUnit(...)0%3.333066.67%
GetOverflow(...)0%5.673033.33%
GetDisplay(...)0%5.673033.33%
GetJustify(...)0%21.527033.33%
GetWrap(...)0%8.125050%
GetFlexDirection(...)0%13.786040%
GetPosition(...)0%3.333066.67%
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
 2314        public UITransformHandler(IInternalECSComponent<InternalUiContainer> internalUiContainer, int componentId)
 15        {
 2316            this.internalUiContainer = internalUiContainer;
 2317            this.componentId = componentId;
 2318        }
 19
 020        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { }
 21
 22        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 23        {
 124            var containerData = internalUiContainer.GetFor(scene, entity);
 125            if (containerData != null)
 26            {
 127                var containerModel = containerData.model;
 128                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
 133                if (containerModel.parentId != SpecialEntityId.SCENE_ROOT_ENTITY)
 34                {
 135                    containerModel.parentElement?.Remove(containerModel.rootElement);
 136                    containerModel.parentElement = null;
 37                }
 138                containerModel.parentId = SpecialEntityId.SCENE_ROOT_ENTITY;
 139                internalUiContainer.PutFor(scene, entity, containerModel);
 40            }
 141        }
 42
 43        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBUiTransform model)
 44        {
 445            var containerModel = internalUiContainer.GetFor(scene, entity)?.model ?? new InternalUiContainer();
 46
 447            containerModel.components.Add(componentId);
 48
 449            if (containerModel.parentId != model.Parent)
 50            {
 151                containerModel.parentId = model.Parent;
 152                containerModel.parentElement?.Remove(containerModel.rootElement);
 153                containerModel.parentElement = null;
 54            }
 455            containerModel.shouldSort = containerModel.rigthOf != model.RightOf;
 456            containerModel.rigthOf = model.RightOf;
 57
 458            VisualElement element = containerModel.rootElement;
 59
 460            SetUpVisualElement(element, model);
 461            internalUiContainer.PutFor(scene, entity, containerModel);
 462        }
 63
 64        private static void SetUpVisualElement(VisualElement element, PBUiTransform model)
 65        {
 466            element.style.display = GetDisplay(model.Display);
 467            element.style.overflow = GetOverflow(model.Overflow);
 68
 69            // Flex
 470            element.style.flexDirection = GetFlexDirection(model.FlexDirection);
 471            if (model.FlexBasisUnit != YGUnit.YguUndefined)
 72            {
 073                element.style.flexBasis = model.FlexBasisUnit == YGUnit.YguAuto ? new StyleLength(StyleKeyword.Auto) : n
 74            }
 75
 476            element.style.flexGrow = model.FlexGrow;
 477            element.style.flexShrink = model.GetFlexShrink();
 478            element.style.flexWrap = GetWrap(model.GetFlexWrap());
 79
 80            // Align
 481            element.style.alignContent = GetAlign(model.GetAlignContent());
 482            element.style.alignItems = GetAlign(model.GetAlignItems());
 483            element.style.alignSelf = GetAlign(model.AlignSelf);
 484            element.style.justifyContent = GetJustify(model.JustifyContent);
 85
 86            // Layout size
 487            if (model.HeightUnit != YGUnit.YguUndefined)
 88            {
 089                element.style.height = new Length(model.Height, GetUnit(model.HeightUnit));
 90            }
 491            if (model.WidthUnit != YGUnit.YguUndefined)
 92            {
 093                element.style.width = new Length(model.Width, GetUnit(model.WidthUnit));
 94            }
 495            if (model.MaxWidthUnit != YGUnit.YguUndefined)
 96            {
 097                element.style.maxWidth = model.MaxWidthUnit == YGUnit.YguAuto ? new StyleLength(StyleKeyword.Auto) : new
 98            }
 499            if (model.MaxHeightUnit != YGUnit.YguUndefined)
 100            {
 0101                element.style.maxHeight = model.MaxHeightUnit == YGUnit.YguAuto ? new StyleLength(StyleKeyword.Auto) : n
 102            }
 4103            if (model.MinHeightUnit != YGUnit.YguUndefined)
 104            {
 0105                element.style.minHeight = new Length(model.MinHeight, GetUnit(model.MinHeightUnit));
 106            }
 4107            if (model.MinWidthUnit != YGUnit.YguUndefined)
 108            {
 0109                element.style.minWidth = new Length(model.MinWidth, GetUnit(model.MinWidthUnit));
 110            }
 111
 112            // Paddings
 4113            if (model.PaddingBottomUnit != YGUnit.YguUndefined)
 114            {
 1115                element.style.paddingBottom = new Length(model.PaddingBottom, GetUnit(model.PaddingBottomUnit));
 116            }
 4117            if (model.PaddingLeftUnit != YGUnit.YguUndefined)
 118            {
 1119                element.style.paddingLeft = new Length(model.PaddingLeft, GetUnit(model.PaddingLeftUnit));
 120            }
 4121            if (model.PaddingRightUnit != YGUnit.YguUndefined)
 122            {
 1123                element.style.paddingRight = new Length(model.PaddingRight, GetUnit(model.PaddingRightUnit));
 124            }
 4125            if (model.PaddingTopUnit != YGUnit.YguUndefined)
 126            {
 1127                element.style.paddingTop = new Length(model.PaddingTop, GetUnit(model.PaddingTopUnit));
 128            }
 129
 130            // Margins
 4131            if (model.MarginLeftUnit != YGUnit.YguUndefined)
 132            {
 1133                element.style.marginLeft = new Length(model.MarginLeft, GetUnit(model.MarginLeftUnit));
 134            }
 4135            if (model.MarginRightUnit != YGUnit.YguUndefined)
 136            {
 1137                element.style.marginRight = new Length(model.MarginRight, GetUnit(model.MarginRightUnit));
 138            }
 4139            if (model.MarginBottomUnit != YGUnit.YguUndefined)
 140            {
 1141                element.style.marginBottom = new Length(model.MarginBottom, GetUnit(model.MarginBottomUnit));
 142            }
 4143            if (model.MarginTopUnit != YGUnit.YguUndefined)
 144            {
 1145                element.style.marginTop = new Length(model.MarginTop, GetUnit(model.MarginTopUnit));
 146            }
 147
 148            // Position
 4149            element.style.position = GetPosition(model.PositionType);
 150
 4151            if (model.PositionTopUnit != YGUnit.YguUndefined)
 0152                element.style.top = new Length(model.PositionTop, GetUnit(model.PositionTopUnit));
 153
 4154            if (model.PositionBottomUnit != YGUnit.YguUndefined)
 0155                element.style.bottom = new Length(model.PositionBottom, GetUnit(model.PositionBottomUnit));
 156
 4157            if (model.PositionRightUnit != YGUnit.YguUndefined)
 0158                element.style.right = new Length(model.PositionRight, GetUnit(model.PositionRightUnit));
 159
 4160            if (model.PositionLeftUnit != YGUnit.YguUndefined)
 0161                element.style.left = new Length(model.PositionLeft, GetUnit(model.PositionLeftUnit));
 4162        }
 163
 164        private static LengthUnit GetUnit(YGUnit unit)
 165        {
 166            switch (unit)
 167            {
 168                case YGUnit.YguPoint:
 4169                    return LengthUnit.Pixel;
 170                case YGUnit.YguPercent:
 4171                    return LengthUnit.Percent;
 172                default:
 0173                    return LengthUnit.Pixel;
 174            }
 175        }
 176
 177        private static StyleEnum<Overflow> GetOverflow(YGOverflow overflow)
 178        {
 179            switch (overflow)
 180            {
 181                case YGOverflow.YgoVisible:
 4182                    return Overflow.Visible;
 183                case YGOverflow.YgoHidden:
 0184                    return Overflow.Hidden;
 185                default:
 0186                    return Overflow.Visible;
 187            }
 188        }
 189
 190        private static StyleEnum<DisplayStyle> GetDisplay(YGDisplay display)
 191        {
 192            switch (display)
 193            {
 194                case YGDisplay.YgdFlex:
 4195                    return DisplayStyle.Flex;
 196                case YGDisplay.YgdNone:
 0197                    return DisplayStyle.None;
 198                default:
 0199                    return DisplayStyle.Flex;
 200            }
 201        }
 202
 203        private static StyleEnum<Justify> GetJustify(YGJustify justify)
 204        {
 205            switch (justify)
 206            {
 207                case YGJustify.YgjFlexStart:
 3208                    return Justify.FlexStart;
 209                case YGJustify.YgjCenter:
 0210                    return Justify.Center;
 211                case YGJustify.YgjFlexEnd:
 0212                    return Justify.FlexEnd;
 213                case YGJustify.YgjSpaceBetween:
 0214                    return Justify.SpaceBetween;
 215                case YGJustify.YgjSpaceAround:
 1216                    return Justify.SpaceAround;
 217                default:
 0218                    return Justify.FlexStart;
 219            }
 220        }
 221
 222        private static StyleEnum<Wrap> GetWrap(YGWrap wrap)
 223        {
 224            switch (wrap)
 225            {
 226                case YGWrap.YgwNoWrap:
 0227                    return Wrap.NoWrap;
 228                case YGWrap.YgwWrap:
 3229                    return Wrap.Wrap;
 230                case YGWrap.YgwWrapReverse:
 1231                    return Wrap.WrapReverse;
 232                default:
 0233                    return Wrap.Wrap;
 234            }
 235        }
 236
 237        private static StyleEnum<FlexDirection> GetFlexDirection(YGFlexDirection direction)
 238        {
 239            switch (direction)
 240            {
 241                case YGFlexDirection.YgfdColumn:
 0242                    return FlexDirection.Column;
 243                case YGFlexDirection.YgfdColumnReverse:
 1244                    return FlexDirection.ColumnReverse;
 245                case YGFlexDirection.YgfdRow:
 3246                    return FlexDirection.Row;
 247                case YGFlexDirection.YgfdRowReverse:
 0248                    return FlexDirection.RowReverse;
 249                default:
 0250                    return FlexDirection.Row;
 251            }
 252        }
 253
 254        private static StyleEnum<UnityEngine.UIElements.Position> GetPosition(YGPositionType positionType)
 255        {
 256            switch (positionType)
 257            {
 258                case YGPositionType.YgptRelative:
 3259                    return UnityEngine.UIElements.Position.Relative;
 260                case YGPositionType.YgptAbsolute:
 1261                    return UnityEngine.UIElements.Position.Absolute;
 262                default:
 0263                    return UnityEngine.UIElements.Position.Relative;
 264            }
 265        }
 266
 267        private static StyleEnum<Align> GetAlign(YGAlign align)
 268        {
 269            switch (align)
 270            {
 271                case YGAlign.YgaAuto:
 3272                    return Align.Auto;
 273                case YGAlign.YgaFlexStart:
 0274                    return Align.FlexStart;
 275                case YGAlign.YgaCenter:
 2276                    return Align.Center;
 277                case YGAlign.YgaFlexEnd:
 0278                    return Align.FlexEnd;
 279                case YGAlign.YgaStretch:
 7280                    return Align.Stretch;
 281                default:
 0282                    return Align.Auto;
 283            }
 284        }
 285    }
 286}