< Summary

Class:DCL.Components.DCLAnimator
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Animator/DCLAnimator.cs
Covered lines:63
Uncovered lines:7
Coverable lines:70
Total lines:168
Line coverage:90% (63 of 70)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DCLAnimationState()0%110100%
Clone()0%2100%
GetDataFromJSON(...)0%110100%
DCLAnimator()0%110100%
Awake()0%110100%
OnDestroy()0%110100%
ApplyChanges(...)0%110100%
GetModel()0%2100%
OnComponentUpdated(...)0%110100%
Initialize()0%770100%
UpdateAnimationState()0%13.0113096.15%
ResetAnimation(...)0%3.333066.67%
GetStateByString(...)0%3.033085.71%
GetClassId()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Animator/DCLAnimator.cs

#LineLine coverage
 1using DCL.Models;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL.Helpers;
 5using UnityEngine;
 6using DCL.Controllers;
 7
 8namespace DCL.Components
 9{
 10    public class DCLAnimator : BaseComponent
 11    {
 12        [System.Serializable]
 13        public class Model : BaseModel
 14        {
 15            [System.Serializable]
 16            public class DCLAnimationState
 17            {
 18                public string name;
 19                public string clip;
 20                public AnimationClip clipReference;
 21                public bool playing;
 22
 23                [Range(0, 1)]
 2024                public float weight = 1f;
 25
 2026                public float speed = 1f;
 2027                public bool looping = true;
 28                public bool shouldReset = false;
 29
 030                public DCLAnimationState Clone() { return (DCLAnimationState) this.MemberwiseClone(); }
 31            }
 32
 33            public DCLAnimationState[] states;
 34
 1135            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 36        }
 37
 38        [System.NonSerialized]
 39        public Animation animComponent = null;
 40
 741        Dictionary<string, AnimationClip> clipNameToClip = new Dictionary<string, AnimationClip>();
 742        Dictionary<AnimationClip, AnimationState> clipToState = new Dictionary<AnimationClip, AnimationState>();
 43
 1244        private void Awake() { model = new Model(); }
 45
 1246        private void OnDestroy() { entity.OnShapeUpdated -= OnComponentUpdated; }
 47
 48        public override IEnumerator ApplyChanges(BaseModel model)
 49        {
 1150            entity.OnShapeUpdated -= OnComponentUpdated;
 1151            entity.OnShapeUpdated += OnComponentUpdated;
 52
 1153            UpdateAnimationState();
 54
 1155            return null;
 56        }
 57
 058        new public Model GetModel() { return (Model) model; }
 59
 2060        private void OnComponentUpdated(IDCLEntity e) { UpdateAnimationState(); }
 61
 62        private void Initialize()
 63        {
 2164            if (entity == null || animComponent != null)
 965                return;
 66
 67            //NOTE(Brian): fetch all the AnimationClips in Animation component.
 1268            animComponent = transform.parent.GetComponentInChildren<Animation>(true);
 69
 1270            if (animComponent == null)
 771                return;
 72
 573            clipNameToClip.Clear();
 574            clipToState.Clear();
 575            int layerIndex = 0;
 76
 577            animComponent.playAutomatically = true;
 578            animComponent.enabled = true;
 579            animComponent.Stop(); //NOTE(Brian): When the GLTF is created by GLTFSceneImporter a frame may be elapsed,
 80            //putting the component in play state if playAutomatically was true at that point.
 581            animComponent.clip?.SampleAnimation(animComponent.gameObject, 0);
 82
 2483            foreach (AnimationState unityState in animComponent)
 84            {
 785                clipNameToClip[unityState.clip.name] = unityState.clip;
 86
 787                unityState.clip.wrapMode = WrapMode.Loop;
 788                unityState.layer = layerIndex;
 789                unityState.blendMode = AnimationBlendMode.Blend;
 790                layerIndex++;
 91            }
 592        }
 93
 94        void UpdateAnimationState()
 95        {
 2196            Initialize();
 97
 2198            if (clipNameToClip.Count == 0 || animComponent == null)
 799                return;
 100
 14101            Model model = (Model) this.model;
 102
 14103            if (model.states == null || model.states.Length == 0)
 0104                return;
 105
 68106            for (int i = 0; i < model.states.Length; i++)
 107            {
 20108                Model.DCLAnimationState state = model.states[i];
 109
 20110                if (clipNameToClip.ContainsKey(state.clip))
 111                {
 20112                    AnimationState unityState = animComponent[state.clip];
 20113                    unityState.weight = state.weight;
 20114                    unityState.wrapMode = state.looping ? WrapMode.Loop : WrapMode.Default;
 20115                    unityState.clip.wrapMode = unityState.wrapMode;
 20116                    unityState.speed = state.speed;
 117
 20118                    state.clipReference = unityState.clip;
 119
 20120                    if (state.shouldReset)
 6121                        ResetAnimation(state);
 122
 20123                    if (state.playing)
 124                    {
 17125                        if (!animComponent.IsPlaying(state.clip))
 16126                            animComponent.Play(state.clip);
 16127                    }
 128                    else
 129                    {
 3130                        if (animComponent.IsPlaying(state.clip))
 1131                            animComponent.Stop(state.clip);
 132                    }
 133                }
 134            }
 14135        }
 136
 137        public void ResetAnimation(Model.DCLAnimationState state)
 138        {
 7139            if (state == null || state.clipReference == null)
 140            {
 0141                Debug.LogError("Clip not found");
 0142                return;
 143            }
 144
 7145            animComponent.Stop(state.clip);
 146
 147            //Manually sample the animation. If the reset is not played again the frame 0 wont be applied
 7148            state.clipReference.SampleAnimation(animComponent.gameObject, 0);
 7149        }
 150
 151        public Model.DCLAnimationState GetStateByString(string stateName)
 152        {
 4153            Model model = (Model) this.model;
 154
 14155            for (var i = 0; i < model.states.Length; i++)
 156            {
 7157                if (model.states[i].name == stateName)
 158                {
 4159                    return model.states[i];
 160                }
 161            }
 162
 0163            return null;
 164        }
 165
 0166        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.ANIMATOR; }
 167    }
 168}