< 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:86
Uncovered lines:24
Coverable lines:110
Total lines:260
Line coverage:78.1% (86 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%19.0719094.23%
GetUnit(...)0%12300%
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;
 6using UnityEngine.UIElements;
 7
 8namespace DCL.ECSComponents
 9{
 10    public class UITransformHandler : IECSComponentHandler<PBUiTransform>
 11    {
 12        private readonly IInternalECSComponent<InternalUiContainer> internalUiContainer;
 13        private readonly int componentId;
 14
 1715        public UITransformHandler(IInternalECSComponent<InternalUiContainer> internalUiContainer, int componentId)
 16        {
 1717            this.internalUiContainer = internalUiContainer;
 1718            this.componentId = componentId;
 1719        }
 20
 021        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { }
 22
 23        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 24        {
 125            var containerData = internalUiContainer.GetFor(scene, entity);
 126            if (containerData != null)
 27            {
 128                var containerModel = containerData.model;
 129                containerModel.components.Remove(componentId);
 30
 31                // do parent detach only if not child of root entity
 32                // since ui element without transform should be always attached
 33                // to the root entity
 134                if (containerModel.parentId != SpecialEntityId.SCENE_ROOT_ENTITY)
 35                {
 136                    containerModel.parentElement?.Remove(containerModel.rootElement);
 137                    containerModel.parentElement = null;
 38                }
 139                containerModel.parentId = SpecialEntityId.SCENE_ROOT_ENTITY;
 140                internalUiContainer.PutFor(scene, entity, containerModel);
 41            }
 142        }
 43
 44        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBUiTransform model)
 45        {
 246            var containerModel = internalUiContainer.GetFor(scene, entity)?.model ?? new InternalUiContainer();
 47
 248            containerModel.components.Add(componentId);
 49
 250            if (containerModel.parentId != model.Parent)
 51            {
 152                containerModel.parentId = model.Parent;
 153                containerModel.parentElement?.Remove(containerModel.rootElement);
 154                containerModel.parentElement = null;
 55            }
 56
 257            VisualElement element = containerModel.rootElement;
 58
 259            SetUpVisualElement(element, model);
 260            internalUiContainer.PutFor(scene, entity, containerModel);
 261        }
 62
 63        private static void SetUpVisualElement(VisualElement element, PBUiTransform model)
 64        {
 265            element.style.display = GetDisplay(model.Display);
 266            element.style.overflow = GetOverflow(model.Overflow);
 67
 68            // Flex
 269            element.style.flexDirection = GetFlexDirection(model.FlexDirection);
 270            if (!float.IsNaN(model.FlexBasis))
 171                element.style.flexBasis = new Length(model.FlexBasis, GetUnit(model.FlexBasisUnit));
 72            else
 173                element.style.flexBasis = new StyleLength(StyleKeyword.Auto);
 74
 275            element.style.flexGrow = model.FlexGrow;
 276            element.style.flexShrink = model.FlexShrink;
 277            element.style.flexWrap = GetWrap(model.FlexWrap);
 78
 79            // Align
 280            if (model.AlignContent != YGAlign.YgaFlexStart)
 281                element.style.alignContent = GetAlign(model.AlignContent);
 282            if (model.AlignItems != YGAlign.YgaAuto)
 183                element.style.alignItems = GetAlign(model.AlignItems);
 284            if (model.AlignSelf != YGAlign.YgaAuto)
 185                element.style.alignSelf = GetAlign(model.AlignSelf);
 286            element.style.justifyContent = GetJustify(model.JustifyContent);
 87
 88            // Layout size
 289            if (!float.IsNaN(model.Height))
 290                element.style.height = new Length(model.Height, GetUnit(model.HeightUnit));
 291            if (!float.IsNaN(model.Width))
 292                element.style.width = new Length(model.Width, GetUnit(model.WidthUnit));
 93
 294            if (!float.IsNaN(model.MaxWidth))
 195                element.style.maxWidth = new Length(model.MaxWidth, GetUnit(model.MaxWidthUnit));
 96            else
 197                element.style.maxWidth = new StyleLength(StyleKeyword.Auto);
 298            if (!float.IsNaN(model.MaxHeight))
 299                element.style.maxHeight = new Length(model.MaxHeight, GetUnit(model.MaxHeightUnit));
 100            else
 0101                element.style.maxHeight = new StyleLength(StyleKeyword.Auto);
 102
 2103            if (!float.IsNaN(model.MinHeight))
 2104                element.style.minHeight = new Length(model.MinHeight, GetUnit(model.MinHeightUnit));
 2105            if (!float.IsNaN(model.MinWidth))
 2106                element.style.minWidth = new Length(model.MinWidth, GetUnit(model.MinWidthUnit));
 107
 108            // Paddings
 2109            if (!Mathf.Approximately(model.PaddingBottom, 0))
 1110                element.style.paddingBottom = new Length(model.PaddingBottom, GetUnit(model.PaddingBottomUnit));
 2111            if (!Mathf.Approximately(model.PaddingLeft, 0))
 0112                element.style.paddingLeft = new Length(model.PaddingLeft, GetUnit(model.PaddingLeftUnit));
 2113            if (!Mathf.Approximately(model.PaddingRight, 0))
 1114                element.style.paddingRight = new Length(model.PaddingRight, GetUnit(model.PaddingRightUnit));
 2115            if (!Mathf.Approximately(model.PaddingTop, 0))
 1116                element.style.paddingTop = new Length(model.PaddingTop, GetUnit(model.PaddingTopUnit));
 117
 118            // Margins
 2119            if (!Mathf.Approximately(model.MarginLeft, 0))
 0120                element.style.marginLeft = new Length(model.MarginLeft, GetUnit(model.MarginLeftUnit));
 2121            if (!Mathf.Approximately(model.MarginRight, 0))
 1122                element.style.marginRight = new Length(model.MarginRight, GetUnit(model.MarginRightUnit));
 2123            if (!Mathf.Approximately(model.MarginBottom, 0))
 1124                element.style.marginBottom = new Length(model.MarginBottom, GetUnit(model.MarginBottomUnit));
 2125            if (!Mathf.Approximately(model.MarginTop, 0))
 1126                element.style.marginTop = new Length(model.MarginTop, GetUnit(model.MarginTopUnit));
 127
 128            // Borders
 2129            element.style.borderBottomWidth = model.BorderBottom;
 2130            element.style.borderLeftWidth = model.BorderLeft;
 2131            element.style.borderRightWidth = model.BorderRight;
 2132            element.style.borderTopWidth = model.BorderTop;
 133
 134            // Position
 2135            element.style.position = GetPosition(model.PositionType);
 2136        }
 137
 138        private static LengthUnit GetUnit(YGUnit unit)
 139        {
 140            switch (unit)
 141            {
 142                case YGUnit.YguPoint:
 0143                    return LengthUnit.Pixel;
 144                case YGUnit.YguPercent:
 0145                    return LengthUnit.Percent;
 146                default:
 0147                    return LengthUnit.Pixel;
 148            }
 149        }
 150
 151        private static StyleEnum<Overflow> GetOverflow(YGOverflow overflow)
 152        {
 153            switch (overflow)
 154            {
 155                case YGOverflow.YgoVisible:
 2156                    return Overflow.Visible;
 157                case YGOverflow.YgoHidden:
 0158                    return Overflow.Hidden;
 159                default:
 0160                    return Overflow.Visible;
 161            }
 162        }
 163
 164        private static StyleEnum<DisplayStyle> GetDisplay(YGDisplay display)
 165        {
 166            switch (display)
 167            {
 168                case YGDisplay.YgdFlex:
 2169                    return DisplayStyle.Flex;
 170                case YGDisplay.YgdNone:
 0171                    return DisplayStyle.None;
 172                default:
 0173                    return DisplayStyle.Flex;
 174            }
 175        }
 176
 177        private static StyleEnum<Justify> GetJustify(YGJustify justify)
 178        {
 179            switch (justify)
 180            {
 181                case YGJustify.YgjFlexStart:
 1182                    return Justify.FlexStart;
 183                case YGJustify.YgjCenter:
 0184                    return Justify.Center;
 185                case YGJustify.YgjFlexEnd:
 0186                    return Justify.FlexEnd;
 187                case YGJustify.YgjSpaceBetween:
 0188                    return Justify.SpaceBetween;
 189                case YGJustify.YgjSpaceAround:
 1190                    return Justify.SpaceAround;
 191                default:
 0192                    return Justify.FlexStart;
 193            }
 194        }
 195
 196        private static StyleEnum<Wrap> GetWrap(YGWrap wrap)
 197        {
 198            switch (wrap)
 199            {
 200                case YGWrap.YgwNoWrap:
 1201                    return Wrap.NoWrap;
 202                case YGWrap.YgwWrap:
 0203                    return Wrap.Wrap;
 204                case YGWrap.YgwWrapReverse:
 1205                    return Wrap.WrapReverse;
 206                default:
 0207                    return Wrap.Wrap;
 208            }
 209        }
 210
 211        private static StyleEnum<FlexDirection> GetFlexDirection(YGFlexDirection direction)
 212        {
 213            switch (direction)
 214            {
 215                case YGFlexDirection.YgfdColumn:
 1216                    return FlexDirection.Column;
 217                case YGFlexDirection.YgfdColumnReverse:
 1218                    return FlexDirection.ColumnReverse;
 219                case YGFlexDirection.YgfdRow:
 0220                    return FlexDirection.Row;
 221                case YGFlexDirection.YgfdRowReverse:
 0222                    return FlexDirection.RowReverse;
 223                default:
 0224                    return FlexDirection.Row;
 225            }
 226        }
 227
 228        private static StyleEnum<UnityEngine.UIElements.Position> GetPosition(YGPositionType positionType)
 229        {
 230            switch (positionType)
 231            {
 232                case YGPositionType.YgptRelative:
 0233                    return UnityEngine.UIElements.Position.Relative;
 234                case YGPositionType.YgptAbsolute:
 1235                    return UnityEngine.UIElements.Position.Absolute;
 236                default:
 1237                    return UnityEngine.UIElements.Position.Relative;
 238            }
 239        }
 240
 241        private static StyleEnum<Align> GetAlign(YGAlign align)
 242        {
 243            switch (align)
 244            {
 245                case YGAlign.YgaAuto:
 1246                    return Align.Auto;
 247                case YGAlign.YgaFlexStart:
 0248                    return Align.FlexStart;
 249                case YGAlign.YgaCenter:
 2250                    return Align.Center;
 251                case YGAlign.YgaFlexEnd:
 0252                    return Align.FlexEnd;
 253                case YGAlign.YgaStretch:
 1254                    return Align.Stretch;
 255                default:
 0256                    return Align.Auto;
 257            }
 258        }
 259    }
 260}