< 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:26
Coverable lines:106
Total lines:278
Line coverage:75.4% (80 of 106)
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%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
 2614        public UITransformHandler(IInternalECSComponent<InternalUiContainer> internalUiContainer, int componentId)
 15        {
 2616            this.internalUiContainer = internalUiContainer;
 2617            this.componentId = componentId;
 2618        }
 19
 020        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.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        {
 945            var containerModel = internalUiContainer.GetFor(scene, entity)?.model ?? new InternalUiContainer(entity.enti
 46
 947            containerModel.components.Add(componentId);
 48
 949            if (containerModel.parentId != model.Parent)
 50            {
 551                containerModel.parentId = model.Parent;
 552                containerModel.parentElement?.Remove(containerModel.rootElement);
 553                containerModel.parentElement = null;
 54            }
 955            containerModel.shouldSort = containerModel.rigthOf != model.RightOf;
 956            containerModel.rigthOf = model.RightOf;
 57
 958            VisualElement element = containerModel.rootElement;
 59
 960            SetUpVisualElement(element, model);
 961            internalUiContainer.PutFor(scene, entity, containerModel);
 962        }
 63
 64        private static void SetUpVisualElement(VisualElement element, PBUiTransform model)
 65        {
 966            element.style.display = GetDisplay(model.Display);
 967            element.style.overflow = GetOverflow(model.Overflow);
 68
 69            // Flex
 970            element.style.flexDirection = GetFlexDirection(model.FlexDirection);
 971            if (model.FlexBasisUnit != YGUnit.YguUndefined)
 72            {
 073                element.style.flexBasis = model.FlexBasisUnit == YGUnit.YguAuto ? new StyleLength(StyleKeyword.Auto) : n
 74            }
 75
 976            element.style.flexGrow = model.FlexGrow;
 977            element.style.flexShrink = model.GetFlexShrink();
 978            element.style.flexWrap = GetWrap(model.GetFlexWrap());
 79
 80            // Align
 981            element.style.alignContent = GetAlign(model.GetAlignContent());
 982            element.style.alignItems = GetAlign(model.GetAlignItems());
 983            element.style.alignSelf = GetAlign(model.AlignSelf);
 984            element.style.justifyContent = GetJustify(model.JustifyContent);
 85
 86            // Layout size
 987            if (model.HeightUnit != YGUnit.YguUndefined)
 88            {
 089                element.style.height = new Length(model.Height, GetUnit(model.HeightUnit));
 90            }
 991            if (model.WidthUnit != YGUnit.YguUndefined)
 92            {
 093                element.style.width = new Length(model.Width, GetUnit(model.WidthUnit));
 94            }
 995            if (model.MaxWidthUnit != YGUnit.YguUndefined)
 96            {
 097                element.style.maxWidth = model.MaxWidthUnit == YGUnit.YguAuto ? new StyleLength(StyleKeyword.Auto) : new
 98            }
 999            if (model.MaxHeightUnit != YGUnit.YguUndefined)
 100            {
 0101                element.style.maxHeight = model.MaxHeightUnit == YGUnit.YguAuto ? new StyleLength(StyleKeyword.Auto) : n
 102            }
 9103            if (model.MinHeightUnit != YGUnit.YguUndefined)
 104            {
 0105                element.style.minHeight = new Length(model.MinHeight, GetUnit(model.MinHeightUnit));
 106            }
 9107            if (model.MinWidthUnit != YGUnit.YguUndefined)
 108            {
 0109                element.style.minWidth = new Length(model.MinWidth, GetUnit(model.MinWidthUnit));
 110            }
 111
 112            // Paddings
 9113            if (model.PaddingBottomUnit != YGUnit.YguUndefined)
 114            {
 1115                element.style.paddingBottom = new Length(model.PaddingBottom, GetUnit(model.PaddingBottomUnit));
 116            }
 9117            if (model.PaddingLeftUnit != YGUnit.YguUndefined)
 118            {
 1119                element.style.paddingLeft = new Length(model.PaddingLeft, GetUnit(model.PaddingLeftUnit));
 120            }
 9121            if (model.PaddingRightUnit != YGUnit.YguUndefined)
 122            {
 1123                element.style.paddingRight = new Length(model.PaddingRight, GetUnit(model.PaddingRightUnit));
 124            }
 9125            if (model.PaddingTopUnit != YGUnit.YguUndefined)
 126            {
 1127                element.style.paddingTop = new Length(model.PaddingTop, GetUnit(model.PaddingTopUnit));
 128            }
 129
 130            // Margins
 9131            if (model.MarginLeftUnit != YGUnit.YguUndefined)
 132            {
 1133                element.style.marginLeft = new Length(model.MarginLeft, GetUnit(model.MarginLeftUnit));
 134            }
 9135            if (model.MarginRightUnit != YGUnit.YguUndefined)
 136            {
 1137                element.style.marginRight = new Length(model.MarginRight, GetUnit(model.MarginRightUnit));
 138            }
 9139            if (model.MarginBottomUnit != YGUnit.YguUndefined)
 140            {
 1141                element.style.marginBottom = new Length(model.MarginBottom, GetUnit(model.MarginBottomUnit));
 142            }
 9143            if (model.MarginTopUnit != YGUnit.YguUndefined)
 144            {
 1145                element.style.marginTop = new Length(model.MarginTop, GetUnit(model.MarginTopUnit));
 146            }
 147
 148            // Position
 9149            element.style.position = GetPosition(model.PositionType);
 150
 9151            if (model.PositionTopUnit != YGUnit.YguUndefined)
 0152                element.style.top = new Length(model.PositionTop, GetUnit(model.PositionTopUnit));
 153
 9154            if (model.PositionBottomUnit != YGUnit.YguUndefined)
 0155                element.style.bottom = new Length(model.PositionBottom, GetUnit(model.PositionBottomUnit));
 156
 9157            if (model.PositionRightUnit != YGUnit.YguUndefined)
 0158                element.style.right = new Length(model.PositionRight, GetUnit(model.PositionRightUnit));
 159
 9160            if (model.PositionLeftUnit != YGUnit.YguUndefined)
 0161                element.style.left = new Length(model.PositionLeft, GetUnit(model.PositionLeftUnit));
 9162        }
 163
 164        private static LengthUnit GetUnit(YGUnit unit)
 165        {
 166            switch (unit)
 167            {
 168                case YGUnit.YguPercent:
 4169                    return LengthUnit.Percent;
 170                default:
 4171                    return LengthUnit.Pixel;
 172            }
 173        }
 174
 175        private static StyleEnum<Overflow> GetOverflow(YGOverflow overflow)
 176        {
 177            switch (overflow)
 178            {
 179                case YGOverflow.YgoHidden:
 0180                    return Overflow.Hidden;
 181                default:
 9182                    return Overflow.Visible;
 183            }
 184        }
 185
 186        private static StyleEnum<DisplayStyle> GetDisplay(YGDisplay display)
 187        {
 188            switch (display)
 189            {
 190                case YGDisplay.YgdNone:
 0191                    return DisplayStyle.None;
 192                default:
 9193                    return DisplayStyle.Flex;
 194            }
 195        }
 196
 197        private static StyleEnum<Justify> GetJustify(YGJustify justify)
 198        {
 199            switch (justify)
 200            {
 201                case YGJustify.YgjFlexStart:
 8202                    return Justify.FlexStart;
 203                case YGJustify.YgjCenter:
 0204                    return Justify.Center;
 205                case YGJustify.YgjFlexEnd:
 0206                    return Justify.FlexEnd;
 207                case YGJustify.YgjSpaceBetween:
 0208                    return Justify.SpaceBetween;
 209                case YGJustify.YgjSpaceAround:
 1210                    return Justify.SpaceAround;
 211                default:
 0212                    return Justify.FlexStart;
 213            }
 214        }
 215
 216        private static StyleEnum<Wrap> GetWrap(YGWrap wrap)
 217        {
 218            switch (wrap)
 219            {
 220                case YGWrap.YgwNoWrap:
 8221                    return Wrap.NoWrap;
 222                case YGWrap.YgwWrap:
 0223                    return Wrap.Wrap;
 224                case YGWrap.YgwWrapReverse:
 1225                    return Wrap.WrapReverse;
 226                default:
 0227                    return Wrap.Wrap;
 228            }
 229        }
 230
 231        private static StyleEnum<FlexDirection> GetFlexDirection(YGFlexDirection direction)
 232        {
 233            switch (direction)
 234            {
 235                case YGFlexDirection.YgfdColumn:
 0236                    return FlexDirection.Column;
 237                case YGFlexDirection.YgfdColumnReverse:
 1238                    return FlexDirection.ColumnReverse;
 239                case YGFlexDirection.YgfdRow:
 8240                    return FlexDirection.Row;
 241                case YGFlexDirection.YgfdRowReverse:
 0242                    return FlexDirection.RowReverse;
 243                default:
 0244                    return FlexDirection.Row;
 245            }
 246        }
 247
 248        private static StyleEnum<UnityEngine.UIElements.Position> GetPosition(YGPositionType positionType)
 249        {
 250            switch (positionType)
 251            {
 252                case YGPositionType.YgptAbsolute:
 1253                    return UnityEngine.UIElements.Position.Absolute;
 254                default:
 8255                    return UnityEngine.UIElements.Position.Relative;
 256            }
 257        }
 258
 259        private static StyleEnum<Align> GetAlign(YGAlign align)
 260        {
 261            switch (align)
 262            {
 263                case YGAlign.YgaAuto:
 8264                    return Align.Auto;
 265                case YGAlign.YgaFlexStart:
 0266                    return Align.FlexStart;
 267                case YGAlign.YgaCenter:
 2268                    return Align.Center;
 269                case YGAlign.YgaFlexEnd:
 0270                    return Align.FlexEnd;
 271                case YGAlign.YgaStretch:
 17272                    return Align.Stretch;
 273                default:
 0274                    return Align.Auto;
 275            }
 276        }
 277    }
 278}