internal void OnKeyFrameReached(CompositeKeyFrame keyFrame)
 {
     if (KeyFrameReached != null)
     {
         KeyFrameReached(keyFrame, EventArgs.Empty);
     }
 }
        public void LerpKeyFrameWith(CompositeKeyFrame nextFrame, float currentLife)
        {
            float amount = MathHelper.Clamp(currentLife / this._duration, 0, 1);

            //Console.WriteLine("--- Lerping frame with amount: " + amount);
            if (Parent.Parent.RootBone != null)
            {
                // call the LerpBone on the root, and it will spread to all child bones using Recursivity
                CompositeBoneTransform boneTransform = GetBoneTransformFromKeyFrame(this, Parent.Parent.RootBone.Name);
                LerpBone(boneTransform, nextFrame, amount);
            }
        }
 public CompositeBoneTransform GetBoneTransformFromKeyFrame(CompositeKeyFrame keyFrame, String boneReference)
 {
     foreach (CompositeBoneTransform boneTrans in keyFrame.BoneTransforms)
     {
         if (boneTrans.BoneReference == boneReference)
         {
             return(boneTrans);
         }
     }
     throw new Exception("BoneReference \"" + boneReference
                         + "\" not found in keyframe \"" + keyFrame.Name + "\"");
 }
Example #4
0
 /// <summary>
 /// Removes a children from this bone
 /// </summary>
 public void RemoveChildBone(CompositeBone childBone)
 {
     // sync transforms
     for (int i = 0; i < Parent.Animations.Count; i++)
     {
         // loop through every keyframe to sync them
         for (int j = 0; j < Parent.Animations[i].KeyFrames.Count; j++)
         {
             CompositeKeyFrame keyframe = Parent.Animations[i].KeyFrames[j];
             // remove the entry AND its children entries
             RemoveBoneTransformEntry(keyframe.BoneTransforms, childBone);
         }
     }
     // remove the bone
     this.ChildBones.Remove(childBone);
 }
Example #5
0
 public void CopyValuesTo(CompositeBoneTransform target, CompositeKeyFrame newParent)
 {
     target.Parent            = newParent;
     target.SceneItem         = this.SceneItem;
     target.SubItem           = this.SubItem;
     target.Position          = this.Position;
     target.Scale             = this.Scale;
     target.Rotation          = this.Rotation;
     target.Opacity           = this.Opacity;
     target.FlipHorizontal    = this.FlipHorizontal;
     target.FlipVertical      = this.FlipVertical;
     target.IsVisible         = this.IsVisible;
     target.BoneReference     = this.BoneReference;
     target.InheritPosition   = this.InheritPosition;
     target.InheritRotation   = this.InheritRotation;
     target.InheritScale      = this.InheritScale;
     target.InheritVisibility = this.InheritVisibility;
 }
        public void LerpBone(CompositeBoneTransform boneTransform, CompositeKeyFrame nextFrame, float amount)
        {
            CompositeBoneTransform nextTransform;

            if (boneTransform.Bone.Interpolate == true)
            {
                nextTransform = GetBoneTransformFromKeyFrame(nextFrame, boneTransform.BoneReference);
            }
            else
            {
                nextTransform = GetBoneTransformFromKeyFrame(
                    this.Parent.Parent.Animations[0].KeyFrames[0], boneTransform.BoneReference);
            }
            boneTransform.LerpSceneItemWith(nextTransform, amount, !boneTransform.Bone.Interpolate);
            foreach (CompositeBone bone in boneTransform.Bone.ChildBones)
            {
                CompositeBoneTransform childBoneTransform = GetBoneTransformFromKeyFrame(this, bone.Name);
                LerpBone(childBoneTransform, nextFrame, amount);
            }
        }
 public void CopyValuesTo(CompositeKeyFrame target, CompositeAnimation newParent)
 {
     target.Parent   = newParent;
     target.Name     = this.Name;
     target.Duration = this.Duration;
     // copy CompositeBoneTransforms
     for (int i = 0; i < this.BoneTransforms.Count; i++)
     {
         // if no transform is available
         if (target.BoneTransforms.Count <= i)
         {
             target.BoneTransforms.Add(new CompositeBoneTransform());
         }
         this.BoneTransforms[i].CopyValuesTo(target.BoneTransforms[i], target);
     }
     // Remove remaining types (can cause garbage!)
     for (int i = target.BoneTransforms.Count; i > this.BoneTransforms.Count; i--)
     {
         target.BoneTransforms.RemoveAt(i - 1);
     }
 }
Example #8
0
        /// <summary>
        /// Insert a children bone at specific index
        /// </summary>
        public void InsertChildBone(int index, CompositeBone childBone)
        {
            index = SquidMath.Clamp(index, 0, _childBones.Count);
            _childBones.Insert(index, childBone);
            childBone.ParentBone = this;
            childBone.Parent     = this.Parent;
            CompositeBone precedingBone;

            if (index == 0)
            {
                precedingBone = this;
            }
            else
            {
                precedingBone = this.ChildBones[index - 1];
            }
            // sync transforms
            for (int i = 0; i < Parent.Animations.Count; i++)
            {
                // loop through every keyframe to sync them
                for (int j = 0; j < Parent.Animations[i].KeyFrames.Count; j++)
                {
                    CompositeKeyFrame keyframe = Parent.Animations[i].KeyFrames[j];
                    // loop to find the previous bone
                    for (int k = 0; k < keyframe.BoneTransforms.Count; k++)
                    {
                        CompositeBoneTransform transform = keyframe.BoneTransforms[k];
                        if (transform.Bone.Equals(precedingBone))
                        {
                            CompositeBoneTransform newTransform = new CompositeBoneTransform();
                            newTransform.Parent        = keyframe;
                            newTransform.BoneReference = childBone.Name;
                            // insert the new bone just after the preceding bone
                            keyframe.BoneTransforms.Insert(k + 1, newTransform);
                        }
                    }
                }
            }
        }
        public void Update(float elapsed)
        {
            if (Parent == null)
            {
                throw new Exception("The Parent of this animation isn't set");
            }
            int lastFrameIndex = _currentKeyFrameIndex;

            if (_keyFrames.Count > 0 && this.IsPlaying == true)
            {
                // Get the current frame index
                int _lifeAccumulator = 0;
                for (int i = 0; i < _keyFrames.Count; i++)
                {
                    _currentKeyFrameIndex = i;
                    int duration = _keyFrames[i].Duration;
                    if (duration < 1)
                    {
                        duration = 1;
                    }
                    _lifeAccumulator += duration;
                    if (_lifeAccumulator > _currentLife)
                    {
                        break;
                    }
                    // if we went over the maxium
                    else if (i == _keyFrames.Count - 1)
                    {
                        _currentLife = _lifeAccumulator;
                        break;
                    }
                }
                CompositeKeyFrame currentKeyFrame = _keyFrames[_currentKeyFrameIndex];
                if (_currentKeyFrameIndex != lastFrameIndex)
                {
                    this.Parent.OnKeyFrameReached(currentKeyFrame);
                }
                CompositeKeyFrame nextKeyFrame = null;
                // get the next key frame
                if (_currentKeyFrameIndex < _keyFrames.Count - 1)
                {
                    nextKeyFrame = _keyFrames[_currentKeyFrameIndex + 1];
                }
                // if we've reached the last frame
                else if (_currentKeyFrameIndex == _keyFrames.Count - 1)
                {
                    if (this.LerpLastFrameWithFirst == true)
                    {
                        nextKeyFrame = _keyFrames[0];
                    }
                    else
                    {
                        nextKeyFrame = currentKeyFrame;
                    }
                }
                int currentKeyFrameDuration = currentKeyFrame.Duration;
                if (currentKeyFrameDuration < 1)
                {
                    currentKeyFrameDuration = 1;
                }
                float life = _currentLife - (_lifeAccumulator - currentKeyFrameDuration);
                //if (Parent.CurrentAnimationID == 1)
                //{
                //    Console.WriteLine(Parent.CurrentAnimation.Name + "] Update key: "
                //        + _currentKeyFrameIndex + "] life: " + life + "/" + currentKeyFrameDuration);
                //}
                currentKeyFrame.LerpKeyFrameWith(nextKeyFrame, life);
                currentKeyFrame.Update(elapsed);

                _currentLife += _speed;
                // if we reach the end of the last keyframe, we need to loop
                if (_currentKeyFrameIndex == _keyFrames.Count - 1 && _lifeAccumulator <= _currentLife)
                {
                    if (_loopMax > 0 && _loopCounter >= _loopMax - 1)
                    {
                        EndAnim();
                    }
                    else
                    {
                        _loopCounter++;
                        _currentLife = 0;
                    }
                    this.Parent.OnEndOfAnimLoopReached(this);
                }
            }
        }