< Summary

Class:DCL.ECSComponents.ECSTransformHandler
Assembly:DCL.ECSComponents.Transform.Handler
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/Transform/Handler/ECSTransformHandler.cs
Covered lines:46
Uncovered lines:4
Coverable lines:50
Total lines:128
Line coverage:92% (46 of 50)
Covered branches:0
Total branches:0
Covered methods:5
Total methods:5
Method coverage:100% (5 of 5)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ECSTransformHandler(...)0%110100%
OnComponentCreated(...)0%110100%
OnComponentRemoved(...)0%6.146084.21%
OnComponentModelUpdated(...)0%6.016094.44%
ProcessNewParent(...)0%440100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/Transform/Handler/ECSTransformHandler.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.ECS7.InternalComponents;
 3using DCL.ECSRuntime;
 4using DCL.Helpers;
 5using DCL.Models;
 6using UnityEngine;
 7
 8namespace DCL.ECSComponents
 9{
 10    public class ECSTransformHandler : IECSComponentHandler<ECSTransform>
 11    {
 12        private readonly IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent;
 13
 1614        public ECSTransformHandler(IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent)
 15        {
 1616            ECSTransformUtils.orphanEntities = new KeyValueSet<IDCLEntity, ECSTransformUtils.OrphanEntity>(100);
 1617            this.sbcInternalComponent = sbcInternalComponent;
 1618        }
 19
 220        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { }
 21
 22        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 23        {
 324            ECSTransformUtils.orphanEntities.Remove(entity);
 25
 26            // reset transform and re-parent to the scene
 327            entity.gameObject.transform.ResetLocalTRS();
 328            ECSTransformUtils.TrySetParent(scene, entity, SpecialEntityId.SCENE_ROOT_ENTITY);
 29
 30            // if entity has any parent
 331            if (entity.parentId != SpecialEntityId.SCENE_ROOT_ENTITY)
 32            {
 33                // remove as child
 034                if (scene.entities.TryGetValue(entity.parentId, out IDCLEntity parent))
 35                {
 036                    parent.childrenId.Remove(entity.entityId);
 37                }
 38
 039                entity.parentId = SpecialEntityId.SCENE_ROOT_ENTITY;
 40            }
 41
 42            // if entity has any children
 343            int childrenCount = entity.childrenId.Count;
 44
 345            if (childrenCount > 0)
 46            {
 447                for (int i = childrenCount - 1; i >= 0; i--)
 48                {
 149                    long childId = entity.childrenId[i];
 50
 151                    if (!scene.entities.TryGetValue(childId, out IDCLEntity child))
 52                        continue;
 53
 54                    // re-parent child to the scene
 155                    ECSTransformUtils.TrySetParent(scene, child, SpecialEntityId.SCENE_ROOT_ENTITY);
 56
 57                    // add child as orphan
 158                    ECSTransformUtils.orphanEntities[child] = new ECSTransformUtils.OrphanEntity(scene, child, child.par
 59                }
 60
 161                entity.childrenId.Clear();
 62            }
 63
 64            // Reset value in SBC internal component
 365            sbcInternalComponent.SetPosition(scene, entity, Vector3.zero, false);
 366        }
 67
 68        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, ECSTransform model)
 69        {
 2070            Transform transform = entity.gameObject.transform;
 71
 2072            bool positionChange = transform.localPosition != model.position;
 2073            bool scaleChange = transform.localScale != model.scale;
 2074            bool rotationChange = transform.localRotation != model.rotation;
 75
 2076            transform.localPosition = model.position;
 2077            transform.localRotation = model.rotation;
 2078            transform.localScale = model.scale;
 79
 2080            if (entity.parentId != model.parentId)
 81            {
 1182                Vector3 previousGlobalPosition = transform.position;
 1183                ProcessNewParent(scene, entity, model.parentId);
 84
 85                // reparenting may end up changing the entity's position...
 1186                if (!positionChange)
 1087                    positionChange = transform.position != previousGlobalPosition;
 88            }
 89
 2090            if (positionChange)
 691                sbcInternalComponent.SetPosition(scene, entity, transform.position);
 92
 2093            if (scaleChange || rotationChange)
 294                sbcInternalComponent.OnTransformScaleRotationChanged(scene, entity);
 95
 96            // Same AvatarShape interpolation used at DCLTransform from SDK6
 2097            entity.OnTransformChange?.Invoke(model.position, model.rotation);
 098        }
 99
 100        private static void ProcessNewParent(IParcelScene scene, IDCLEntity entity, long parentId)
 101        {
 102            //check for cyclic parenting
 11103            if (ECSTransformUtils.IsCircularParenting(scene, entity, parentId))
 104            {
 1105                Debug.LogError($"cyclic parenting found for entity {entity.entityId} " +
 106                               $"parenting to {parentId} at scene {scene.sceneData.sceneNumber} ({scene.sceneData.basePo
 107
 1108                return;
 109            }
 110
 111            // remove as child of previous parent
 10112            if (entity.parentId != SpecialEntityId.SCENE_ROOT_ENTITY)
 113            {
 3114                if (scene.entities.TryGetValue(entity.parentId, out IDCLEntity parent))
 115                {
 2116                    parent.childrenId.Remove(entity.entityId);
 117                }
 118
 3119                ECSTransformUtils.TrySetParent(scene, entity, SpecialEntityId.SCENE_ROOT_ENTITY);
 120            }
 121
 10122            entity.parentId = parentId;
 123
 124            // add as orphan so system can parent it
 10125            ECSTransformUtils.orphanEntities[entity] = new ECSTransformUtils.OrphanEntity(scene, entity, parentId);
 10126        }
 127    }
 128}