protected virtual void AddSpatialData(SpriterObject info, SpriterTimeline timeline, Spriter spriter, float deltaTime)
        {
            switch (timeline.ObjectType)
            {
            case SpriterObjectType.Sprite:
                FrameData.SpriteData.Add(info);
                break;

            case SpriterObjectType.Entity:
                SpriterAnimation newAnim       = spriter.Entities[info.EntityId].Animations[info.AnimationId];
                float            newTargetTime = info.T * newAnim.Length;
                GetFrameData(newAnim, newTargetTime, deltaTime, info);
                break;

            case SpriterObjectType.Point:
                info.PivotX = 0.0f;
                info.PivotY = 1.0f;
                FrameData.PointData[timeline.Name] = info;
                break;

            case SpriterObjectType.Box:
                FrameData.BoxData[timeline.ObjectId] = info;
                break;
            }
        }
        protected virtual SpriterObject Copy(SpriterObject obj)
        {
            SpriterObject copy = Pool.GetObject <SpriterObject>();

            copy.FillFrom(obj);
            return(copy);
        }
        public static FrameData GetFrameData(SpriterAnimation animation, float targetTime, SpriterSpatial parentInfo = null)
        {
            SpriterMainlineKey keyA;
            SpriterMainlineKey keyB;

            GetMainlineKeys(animation.MainlineKeys, targetTime, out keyA, out keyB);

            float adjustedTime = AdjustTime(keyA, keyB, animation.Length, targetTime);

            SpriterSpatial[] boneInfos = GetBoneInfos(keyA, animation, targetTime, parentInfo);

            FrameData frameData = new FrameData();

            foreach (SpriterObjectRef objectRef in keyA.ObjectRefs)
            {
                SpriterObject interpolated = GetObjectInfo(objectRef, animation, adjustedTime);
                if (boneInfos != null && objectRef.ParentId >= 0)
                {
                    ApplyParentTransform(interpolated, boneInfos[objectRef.ParentId]);
                }

                AddSpatialData(interpolated, animation.Timelines[objectRef.TimelineId], animation.Entity.Spriter, targetTime, frameData);
            }

            return(frameData);
        }
        /// <summary>
        /// Gets the folderId and fileId for the given SpriterObject based on the current character map or default
        /// </summary>
        protected virtual bool GetSpriteIds(SpriterObject obj, out int folderId, out int fileId)
        {
            folderId = obj.FolderId;
            fileId   = obj.FileId;

            if (CharacterMap == null)
            {
                return(true);
            }

            for (int i = 0; i < CharacterMap.Maps.Length; ++i)
            {
                SpriterMapInstruction map = CharacterMap.Maps[i];
                if (map.FolderId != folderId || map.FileId != fileId)
                {
                    continue;
                }
                if (map.TargetFolderId < 0 || map.TargetFileId < 0)
                {
                    return(false);
                }
                folderId = map.TargetFolderId;
                fileId   = map.TargetFileId;
                return(true);
            }

            return(false);
        }
        protected virtual SpriterObject Interpolate(SpriterObject a, SpriterObject b, float f, int spin)
        {
            SpriterObject so = Pool.GetObject <SpriterObject>();

            so.Interpolate(a, b, f, spin);
            return(so);
        }
        public static void UpdateFrameData(FrameData frameData, SpriterAnimation animation, float targetTime, SpriterSpatial parentInfo = null)
        {
            SpriterMainlineKey keyA;
            SpriterMainlineKey keyB;

            GetMainlineKeys(animation.MainlineKeys, targetTime, out keyA, out keyB);

            float adjustedTime = AdjustTime(keyA, keyB, animation.Length, targetTime);

            SpriterSpatial[] boneInfos = GetBoneInfos(keyA, animation, targetTime, parentInfo);

            if (keyA.ObjectRefs == null)
            {
                return;
            }

            for (int i = 0; i < keyA.ObjectRefs.Length; ++i)
            {
                SpriterObjectRef objectRef    = keyA.ObjectRefs[i];
                SpriterObject    interpolated = GetObjectInfo(objectRef, animation, adjustedTime);
                if (boneInfos != null && objectRef.ParentId >= 0)
                {
                    ApplyParentTransform(interpolated, boneInfos[objectRef.ParentId]);
                }

                AddSpatialData(interpolated, animation.Timelines[objectRef.TimelineId], animation.Entity.Spriter, targetTime, frameData);
            }

            SpriterObjectPool.ReturnObject(boneInfos);
        }
        /// <summary>
        /// Gets the transform information for all object types and calls the relevant apply method for each one.
        /// </summary>
        protected virtual void Animate(float deltaTime)
        {
            FrameData frameData = DataProvider.GetFrameData(Time, deltaTime, factor, CurrentAnimation, NextAnimation);

            if (SpriterConfig.MetadataEnabled)
            {
                Metadata = DataProvider.GetFrameMetadata(Time, deltaTime, factor, CurrentAnimation, NextAnimation);
            }

            for (int i = 0; i < frameData.SpriteData.Count; ++i)
            {
                SpriterObject info = frameData.SpriteData[i];
                int           folderId;
                int           fileId;
                if (!GetSpriteIds(info, out folderId, out fileId))
                {
                    continue;
                }
                TSprite obj = GetFromDict(folderId, fileId, sprites);
                obj = GetSwappedSprite(obj);
                ApplySpriteTransform(obj, info);
            }

            if (SpriterConfig.MetadataEnabled)
            {
                for (int i = 0; i < Metadata.Sounds.Count; ++i)
                {
                    SpriterSound info  = Metadata.Sounds[i];
                    TSound       sound = GetFromDict(info.FolderId, info.FileId, sounds);
                    PlaySound(sound, info);
                }

                var pointE = frameData.PointData.GetEnumerator();
                while (pointE.MoveNext())
                {
                    var e = pointE.Current;
                    ApplyPointTransform(e.Key, e.Value);
                }

                var boxE = frameData.BoxData.GetEnumerator();
                while (boxE.MoveNext())
                {
                    var e = boxE.Current;
                    ApplyBoxTransform(Entity.ObjectInfos[e.Key], e.Value);
                }

                for (int i = 0; i < Metadata.Events.Count; ++i)
                {
                    DispatchEvent(Metadata.Events[i]);
                }
            }
        }
Esempio n. 8
0
        public virtual FrameData GetFrameData(SpriterAnimation animation, float targetTime, float deltaTime, SpriterSpatial parentInfo = null)
        {
            if (parentInfo == null)
            {
                FrameData.Clear();
            }

            SpriterMainlineKey keyA;
            SpriterMainlineKey keyB;

            GetMainlineKeys(animation.MainlineKeys, targetTime, out keyA, out keyB);

            float adjustedTime = SpriterHelper.AdjustTime(targetTime, keyA, keyB, animation.Length);

            SpriterSpatial[] boneInfos = GetBoneInfos(keyA, animation, adjustedTime, parentInfo);

            if (keyA.ObjectRefs == null)
            {
                Pool.ReturnObject(boneInfos);
                return(FrameData);
            }

            for (int i = 0; i < keyA.ObjectRefs.Length; ++i)
            {
                SpriterObjectRef objectRef = keyA.ObjectRefs[i];
                var timeline = animation.Timelines[objectRef.TimelineId];

                SpriterObject interpolated = GetObjectInfo(objectRef, animation, adjustedTime);

                interpolated.Name = timeline.Name;

                if (boneInfos != null && objectRef.ParentId >= 0)
                {
                    interpolated.ApplyParentTransform(boneInfos[objectRef.ParentId]);
                }
                else if (parentInfo != null)
                {
                    interpolated.ApplyParentTransform(parentInfo);
                }

                AddSpatialData(interpolated, timeline, animation.Entity.Spriter, deltaTime);
            }

            Pool.ReturnObject(boneInfos);

            if (Config.MetadataEnabled)
            {
                UpdateMetadata(animation, targetTime, deltaTime);
            }

            return(FrameData);
        }
        /// <summary>
        /// Gets the folderId and fileId for the given SpriterObject based on the current character map or default
        /// </summary>
        protected virtual TSprite GetSprite(SpriterObject obj)
        {
            TSprite sprite = GetFromDict(obj.FolderId, obj.FileId, sprites);

            if (!charMapValues.ContainsKey(sprite))
            {
                return(sprite);
            }

            KeyValuePair <int, int> mapping = charMapValues[sprite];

            return(GetFromDict(mapping.Key, mapping.Value, sprites));
        }
        private static SpriterObject Copy(SpriterObject info)
        {
            SpriterObject so = SpriterObjectPool.GetObject <SpriterObject>();

            so.AnimationId = info.AnimationId;
            so.EntityId    = info.EntityId;
            so.FileId      = info.FileId;
            so.FolderId    = info.FolderId;
            so.PivotX      = info.PivotX;
            so.PivotY      = info.PivotY;
            so.T           = info.T;

            FillFrom(so, info);
            return(so);
        }
        private static SpriterObject Copy(SpriterObject info)
        {
            SpriterObject copy = new SpriterObject
            {
                AnimationId = info.AnimationId,
                EntityId    = info.EntityId,
                FileId      = info.FileId,
                FolderId    = info.FolderId,
                PivotX      = info.PivotX,
                PivotY      = info.PivotY,
                T           = info.T
            };

            FillFrom(copy, info);
            return(copy);
        }
Esempio n. 12
0
        /// <summary>
        /// Gets the transform information for all object types and calls the relevant apply method for each one.
        /// </summary>
        protected virtual void Animate(float deltaTime)
        {
            FrameData = DataProvider.GetFrameData(Time, deltaTime, factor, CurrentAnimation, NextAnimation);

            for (int i = 0; i < FrameData.SpriteData.Count; ++i)
            {
                SpriterObject info   = FrameData.SpriteData[i];
                TSprite       sprite = SpriteProvider.Get(info.FolderId, info.FileId);
                if (sprite != null)
                {
                    ApplySpriteTransform(sprite, info);
                }
            }

            for (int i = 0; i < FrameData.Sounds.Count; ++i)
            {
                SpriterSound info  = FrameData.Sounds[i];
                TSound       sound = SoundProvider.Get(info.FolderId, info.FileId);
                if (sound != null)
                {
                    PlaySound(sound, info);
                }
            }

            var pointE = FrameData.PointData.GetEnumerator();

            while (pointE.MoveNext())
            {
                var e = pointE.Current;
                ApplyPointTransform(e.Key, e.Value);
            }

            var boxE = FrameData.BoxData.GetEnumerator();

            while (boxE.MoveNext())
            {
                var e = boxE.Current;
                ApplyBoxTransform(Entity.ObjectInfos[e.Key], e.Value);
            }

            for (int i = 0; i < FrameData.Events.Count; ++i)
            {
                DispatchEvent(FrameData.Events[i]);
            }
        }
 private static SpriterObject Interpolate(SpriterObject a, SpriterObject b, float f, int spin)
 {
     return(new SpriterObject
     {
         Angle = MathHelper.AngleLinear(a.Angle, b.Angle, spin, f),
         Alpha = MathHelper.Linear(a.Alpha, b.Alpha, f),
         X = MathHelper.Linear(a.X, b.X, f),
         Y = MathHelper.Linear(a.Y, b.Y, f),
         ScaleX = MathHelper.Linear(a.ScaleX, b.ScaleX, f),
         ScaleY = MathHelper.Linear(a.ScaleY, b.ScaleY, f),
         PivotX = a.PivotX,
         PivotY = a.PivotY,
         FileId = a.FileId,
         FolderId = a.FolderId,
         EntityId = a.EntityId,
         AnimationId = a.AnimationId,
         T = MathHelper.Linear(a.T, b.T, f)
     });
 }
        private static SpriterObject Interpolate(SpriterObject a, SpriterObject b, float f, int spin)
        {
            SpriterObject so = SpriterObjectPool.GetObject <SpriterObject>();

            so.Angle       = MathHelper.AngleLinear(a.Angle, b.Angle, spin, f);
            so.Alpha       = MathHelper.Linear(a.Alpha, b.Alpha, f);
            so.X           = MathHelper.Linear(a.X, b.X, f);
            so.Y           = MathHelper.Linear(a.Y, b.Y, f);
            so.ScaleX      = MathHelper.Linear(a.ScaleX, b.ScaleX, f);
            so.ScaleY      = MathHelper.Linear(a.ScaleY, b.ScaleY, f);
            so.PivotX      = a.PivotX;
            so.PivotY      = a.PivotY;
            so.FileId      = a.FileId;
            so.FolderId    = a.FolderId;
            so.EntityId    = a.EntityId;
            so.AnimationId = a.AnimationId;
            so.T           = MathHelper.Linear(a.T, b.T, f);

            return(so);
        }
        private static void AddSpatialData(SpriterObject info, SpriterTimeline timeline, Spriter spriter, float targetTime, FrameData frameData)
        {
            switch (timeline.ObjectType)
            {
            case SpriterObjectType.Sprite:
                frameData.SpriteData.Add(info);
                break;

            case SpriterObjectType.Entity:
                SpriterAnimation newAnim       = spriter.Entities[info.EntityId].Animations[info.AnimationId];
                float            newTargetTime = info.T * newAnim.Length;
                UpdateFrameData(frameData, newAnim, newTargetTime, info);
                break;

            case SpriterObjectType.Point:
                frameData.PointData[timeline.Name] = info;
                break;

            case SpriterObjectType.Box:
                frameData.BoxData[timeline.ObjectId] = info;
                break;
            }
        }
Esempio n. 16
0
        public virtual FrameData GetFrameData(SpriterAnimation first, SpriterAnimation second, float targetTime, float deltaTime, float factor)
        {
            FrameData.Clear();

            if (first == second)
            {
                GetFrameData(first, targetTime, deltaTime);
                return(FrameData);
            }

            float targetTimeSecond = targetTime / first.Length * second.Length;

            SpriterMainlineKey firstKeyA;
            SpriterMainlineKey firstKeyB;

            GetMainlineKeys(first.MainlineKeys, targetTime, out firstKeyA, out firstKeyB);

            SpriterMainlineKey secondKeyA;
            SpriterMainlineKey secondKeyB;

            GetMainlineKeys(second.MainlineKeys, targetTimeSecond, out secondKeyA, out secondKeyB);

            if (!SpriterHelper.WillItBlend(firstKeyA, secondKeyA) || !SpriterHelper.WillItBlend(firstKeyB, secondKeyB))
            {
                GetFrameData(first, targetTime, deltaTime);
                return(FrameData);
            }

            float adjustedTimeFirst  = SpriterHelper.AdjustTime(targetTime, firstKeyA, firstKeyB, first.Length);
            float adjustedTimeSecond = SpriterHelper.AdjustTime(targetTimeSecond, secondKeyA, secondKeyB, second.Length);

            SpriterSpatial[] boneInfosA = GetBoneInfos(firstKeyA, first, adjustedTimeFirst);
            SpriterSpatial[] boneInfosB = GetBoneInfos(secondKeyA, second, adjustedTimeSecond);
            SpriterSpatial[] boneInfos  = null;
            if (boneInfosA != null && boneInfosB != null)
            {
                boneInfos = Pool.GetArray <SpriterSpatial>(boneInfosA.Length);
                for (int i = 0; i < boneInfosA.Length; ++i)
                {
                    SpriterSpatial boneA        = boneInfosA[i];
                    SpriterSpatial boneB        = boneInfosB[i];
                    SpriterSpatial interpolated = Interpolate(boneA, boneB, factor, 1);
                    interpolated.Angle = MathHelper.CloserAngleLinear(boneA.Angle, boneB.Angle, factor);
                    boneInfos[i]       = interpolated;
                }
            }

            SpriterMainlineKey baseKey          = factor < 0.5f ? firstKeyA : firstKeyB;
            SpriterAnimation   currentAnimation = factor < 0.5f ? first : second;

            for (int i = 0; i < baseKey.ObjectRefs.Length; ++i)
            {
                SpriterObjectRef objectRefFirst    = baseKey.ObjectRefs[i];
                SpriterObject    interpolatedFirst = GetObjectInfo(objectRefFirst, first, adjustedTimeFirst);

                SpriterObjectRef objectRefSecond    = secondKeyA.ObjectRefs[i];
                SpriterObject    interpolatedSecond = GetObjectInfo(objectRefSecond, second, adjustedTimeSecond);

                SpriterObject info = Interpolate(interpolatedFirst, interpolatedSecond, factor, 1);
                info.Angle  = MathHelper.CloserAngleLinear(interpolatedFirst.Angle, interpolatedSecond.Angle, factor);
                info.PivotX = MathHelper.Linear(interpolatedFirst.PivotX, interpolatedSecond.PivotX, factor);
                info.PivotY = MathHelper.Linear(interpolatedFirst.PivotY, interpolatedSecond.PivotY, factor);

                if (boneInfos != null && objectRefFirst.ParentId >= 0)
                {
                    info.ApplyParentTransform(boneInfos[objectRefFirst.ParentId]);
                }

                AddSpatialData(info, currentAnimation.Timelines[objectRefFirst.TimelineId], currentAnimation.Entity.Spriter, deltaTime);

                Pool.ReturnObject(interpolatedFirst);
                Pool.ReturnObject(interpolatedSecond);
            }

            Pool.ReturnObject(boneInfosA);
            Pool.ReturnObject(boneInfosB);
            Pool.ReturnObject(boneInfos);

            if (Config.MetadataEnabled)
            {
                UpdateMetadata(currentAnimation, targetTime, deltaTime);
            }

            return(FrameData);
        }
Esempio n. 17
0
 /// <summary>
 /// Applies the transform for the given box.
 /// </summary>
 protected virtual void ApplyBoxTransform(SpriterObjectInfo objInfo, SpriterObject info)
 {
 }
Esempio n. 18
0
 /// <summary>
 /// Applies the transforms for the point with the given name.
 /// </summary>
 protected virtual void ApplyPointTransform(string name, SpriterObject info)
 {
 }
Esempio n. 19
0
 /// <summary>
 /// Applies the transform to the concrete sprite isntance.
 /// </summary>
 protected virtual void ApplySpriteTransform(TSprite sprite, SpriterObject info)
 {
 }
        public static FrameData GetFrameData(SpriterAnimation first, SpriterAnimation second, float targetTime, float factor)
        {
            if (first == second)
            {
                return(GetFrameData(first, targetTime));
            }

            float targetTimeSecond = targetTime / first.Length * second.Length;

            SpriterMainlineKey firstKeyA;
            SpriterMainlineKey firstKeyB;

            GetMainlineKeys(first.MainlineKeys, targetTime, out firstKeyA, out firstKeyB);

            SpriterMainlineKey secondKeyA;
            SpriterMainlineKey secondKeyB;

            GetMainlineKeys(second.MainlineKeys, targetTimeSecond, out secondKeyA, out secondKeyB);

            if (firstKeyA.BoneRefs.Length != secondKeyA.BoneRefs.Length ||
                firstKeyB.BoneRefs.Length != secondKeyB.BoneRefs.Length ||
                firstKeyA.ObjectRefs.Length != secondKeyA.ObjectRefs.Length ||
                firstKeyB.ObjectRefs.Length != secondKeyB.ObjectRefs.Length)
            {
                return(GetFrameData(first, targetTime));
            }

            float adjustedTimeFirst  = AdjustTime(firstKeyA, firstKeyB, first.Length, targetTime);
            float adjustedTimeSecond = AdjustTime(secondKeyA, secondKeyB, second.Length, targetTimeSecond);

            SpriterSpatial[] boneInfosA = GetBoneInfos(firstKeyA, first, adjustedTimeFirst);
            SpriterSpatial[] boneInfosB = GetBoneInfos(secondKeyA, second, adjustedTimeSecond);
            SpriterSpatial[] boneInfos  = null;
            if (boneInfosA != null && boneInfosB != null)
            {
                boneInfos = new SpriterSpatial[boneInfosA.Length];
                for (int i = 0; i < boneInfosA.Length; ++i)
                {
                    SpriterSpatial boneA        = boneInfosA[i];
                    SpriterSpatial boneB        = boneInfosB[i];
                    SpriterSpatial interpolated = Interpolate(boneA, boneB, factor, 1);
                    interpolated.Angle = MathHelper.CloserAngleLinear(boneA.Angle, boneB.Angle, factor);
                    boneInfos[i]       = interpolated;
                }
            }

            SpriterMainlineKey baseKey          = factor < 0.5f ? firstKeyA : firstKeyB;
            SpriterAnimation   currentAnimation = factor < 0.5f ? first : second;

            FrameData frameData = new FrameData();

            for (int i = 0; i < baseKey.ObjectRefs.Length; ++i)
            {
                SpriterObjectRef objectRefFirst    = baseKey.ObjectRefs[i];
                SpriterObject    interpolatedFirst = GetObjectInfo(objectRefFirst, first, adjustedTimeFirst);

                SpriterObjectRef objectRefSecond    = secondKeyA.ObjectRefs[i];
                SpriterObject    interpolatedSecond = GetObjectInfo(objectRefSecond, second, adjustedTimeSecond);

                SpriterObject info = Interpolate(interpolatedFirst, interpolatedSecond, factor, 1);
                info.Angle = MathHelper.CloserAngleLinear(interpolatedFirst.Angle, interpolatedSecond.Angle, factor);

                if (boneInfos != null && objectRefFirst.ParentId >= 0)
                {
                    ApplyParentTransform(info, boneInfos[objectRefFirst.ParentId]);
                }

                AddSpatialData(info, currentAnimation.Timelines[objectRefFirst.TimelineId], currentAnimation.Entity.Spriter, targetTime, frameData);
            }

            return(frameData);
        }