Exemplo n.º 1
0
        protected virtual SpriterVarValue GetVariableValue(SpriterAnimation animation, SpriterVarDef varDef, SpriterVarline varline, float targetTime)
        {
            SpriterVarlineKey[] keys = varline.Keys;
            if (keys == null)
            {
                return(varDef.VariableValue);
            }

            SpriterVarlineKey keyA = keys.GetLastKey(targetTime) ?? keys[keys.Length - 1];

            if (keyA == null)
            {
                return(varDef.VariableValue);
            }

            SpriterVarlineKey keyB = varline.Keys.GetNextKey(keyA, animation.Looping);

            if (keyB == null)
            {
                return(keyA.VariableValue);
            }

            float adjustedTime = keyA.Time == keyB.Time ? targetTime : SpriterHelper.AdjustTime(targetTime, keyA, keyB, animation.Length);
            float factor       = SpriterHelper.GetFactor(keyA, keyB, animation.Length, adjustedTime);

            SpriterVarValue varVal = Pool.GetObject <SpriterVarValue>();

            varVal.Interpolate(keyA.VariableValue, keyB.VariableValue, factor);
            return(varVal);
        }
        private static void InitVarDefs(SpriterAnimation animation)
        {
            if (animation.Meta == null || animation.Meta.Varlines == null || animation.Meta.Varlines.Length == 0)
            {
                return;
            }

            foreach (SpriterVarline varline in animation.Meta.Varlines)
            {
                SpriterVarDef varDefs = animation.Entity.Variables[varline.Def];
                Init(varDefs, varline);
            }

            foreach (SpriterTimeline timeline in animation.Timelines)
            {
                if (timeline.Meta == null || timeline.Meta.Varlines == null || timeline.Meta.Varlines.Length == 0)
                {
                    continue;
                }
                SpriterObjectInfo objInfo = animation.Entity.ObjectInfos.First(o => o.Name == timeline.Name);
                foreach (SpriterVarline varline in timeline.Meta.Varlines)
                {
                    SpriterVarDef varDef = objInfo.Variables[varline.Def];
                    Init(varDef, varline);
                }
            }
        }
        private static SpriterVarValue GetVariableValue(SpriterAnimation animation, SpriterVarDef varDef, SpriterVarline varline, float targetTime)
        {
            SpriterVarlineKey[] keys = varline.Keys;
            if (keys == null)
            {
                return(varDef.VariableValue);
            }

            SpriterVarlineKey keyA = LastKeyForTime(keys, targetTime) ?? keys[keys.Length - 1];

            if (keyA == null)
            {
                return(varDef.VariableValue);
            }

            SpriterVarlineKey keyB = GetNextXLineKey(varline.Keys, keyA, animation.Looping);

            if (keyB == null)
            {
                return(keyA.VariableValue);
            }

            float adjustedTime = keyA.Time == keyB.Time ? targetTime : AdjustTime(keyA, keyB, animation.Length, targetTime);
            float factor       = GetFactor(keyA, keyB, animation.Length, targetTime);

            return(Interpolate(keyA.VariableValue, keyB.VariableValue, factor));
        }
        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)
            {
                SpriterObjectPool.ReturnObject(boneInfos);
                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]);
                }
                else if (parentInfo != null)
                {
                    ApplyParentTransform(interpolated, parentInfo);
                }

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

            SpriterObjectPool.ReturnObject(boneInfos);
        }
Exemplo n.º 5
0
        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;
            }
        }
        private static SpriterSpatial[] GetBoneInfos(SpriterMainlineKey key, SpriterAnimation animation, float targetTime, SpriterSpatial parentInfo = null)
        {
            if (key.BoneRefs == null)
            {
                return(null);
            }
            SpriterSpatial[] ret = new SpriterSpatial[key.BoneRefs.Length];

            for (int i = 0; i < key.BoneRefs.Length; ++i)
            {
                SpriterRef     boneRef      = key.BoneRefs[i];
                SpriterSpatial interpolated = GetBoneInfo(boneRef, animation, targetTime);

                if (boneRef.ParentId >= 0)
                {
                    ApplyParentTransform(interpolated, ret[boneRef.ParentId]);
                }
                else if (parentInfo != null)
                {
                    ApplyParentTransform(interpolated, parentInfo);
                }
                ret[i] = interpolated;
            }

            return(ret);
        }
        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);
        }
Exemplo n.º 8
0
        protected virtual SpriterSpatial[] GetBoneInfos(SpriterMainlineKey key, SpriterAnimation animation, float targetTime, SpriterSpatial parentInfo = null)
        {
            if (key.BoneRefs == null)
            {
                return(null);
            }
            SpriterSpatial[] ret = Pool.GetArray <SpriterSpatial>(key.BoneRefs.Length);

            for (int i = 0; i < key.BoneRefs.Length; ++i)
            {
                SpriterRef     boneRef      = key.BoneRefs[i];
                SpriterSpatial interpolated = GetBoneInfo(boneRef, animation, targetTime);

                if (boneRef.ParentId >= 0)
                {
                    interpolated.ApplyParentTransform(ret[boneRef.ParentId]);
                }
                else if (parentInfo != null)
                {
                    interpolated.ApplyParentTransform(parentInfo);
                }
                ret[i] = interpolated;
            }

            return(ret);
        }
        public static FrameMetadata GetFrameMetadata(SpriterAnimation animation, float targetTime, float deltaTime, SpriterSpatial parentInfo = null)
        {
            FrameMetadata metadata = new FrameMetadata();

            AddVariableAndTagData(animation, targetTime, metadata);
            AddEventData(animation, targetTime, deltaTime, metadata);
            AddSoundData(animation, targetTime, deltaTime, metadata);
            return(metadata);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Plays the given animation. Playback starts from the beginning.
        /// </summary>
        public virtual void Play(SpriterAnimation animation)
        {
            Progress = 0;

            CurrentAnimation = animation;
            Name             = animation.Name;

            NextAnimation = null;
            Length        = CurrentAnimation.Length;
        }
Exemplo n.º 11
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);
        }
        private static SpriterObjectInfo GetObjectInfo(SpriterAnimation animation, string name)
        {
            SpriterObjectInfo objInfo = null;

            foreach (SpriterObjectInfo info in animation.Entity.ObjectInfos)
            {
                if (info.Name == name)
                {
                    objInfo = info;
                    break;
                }
            }

            return(objInfo);
        }
Exemplo n.º 13
0
        private static SpriterSpatial GetBoneInfo(SpriterRef spriterRef, SpriterAnimation animation, float targetTime)
        {
            SpriterTimelineKey[] keys = animation.Timelines[spriterRef.TimelineId].Keys;
            SpriterTimelineKey   keyA = keys[spriterRef.KeyId];
            SpriterTimelineKey   keyB = GetNextXLineKey(keys, keyA, animation.Looping);

            if (keyB == null)
            {
                return(Copy(keyA.BoneInfo));
            }

            float factor = GetFactor(keyA, keyB, animation.Length, targetTime);

            return(Interpolate(keyA.BoneInfo, keyB.BoneInfo, factor, keyA.Spin));
        }
Exemplo n.º 14
0
 public static void UpdateFrameMetadata(FrameMetadata metadata, SpriterAnimation animation, float targetTime, float deltaTime, SpriterSpatial parentInfo = null)
 {
     if (SpriterConfig.VarsEnabled || SpriterConfig.TagsEnabled)
     {
         AddVariableAndTagData(animation, targetTime, metadata);
     }
     if (SpriterConfig.EventsEnabled)
     {
         AddEventData(animation, targetTime, deltaTime, metadata);
     }
     if (SpriterConfig.SoundsEnabled)
     {
         AddSoundData(animation, targetTime, deltaTime, metadata);
     }
 }
Exemplo n.º 15
0
        private static void InitInfos(SpriterAnimation animation)
        {
            var infos = from t in animation.Timelines
                        from k in t.Keys
                        let o = k.ObjectInfo
                                where o != null && (float.IsNaN(o.PivotX) || float.IsNaN(o.PivotY))
                                select o;

            foreach (SpriterObject info in infos)
            {
                SpriterFile file = animation.Entity.Spriter.Folders[info.FolderId].Files[info.FileId];
                info.PivotX = file.PivotX;
                info.PivotY = file.PivotY;
            }
        }
Exemplo n.º 16
0
 protected virtual void UpdateMetadata(SpriterAnimation animation, float targetTime, float deltaTime, SpriterSpatial parentInfo = null)
 {
     if (Config.VarsEnabled || Config.TagsEnabled)
     {
         AddVariableAndTagData(animation, targetTime);
     }
     if (Config.EventsEnabled)
     {
         AddEventData(animation, targetTime, deltaTime);
     }
     if (Config.SoundsEnabled)
     {
         AddSoundData(animation, targetTime, deltaTime);
     }
 }
Exemplo n.º 17
0
        protected virtual SpriterObject GetObjectInfo(SpriterRef spriterRef, SpriterAnimation animation, float targetTime)
        {
            SpriterTimelineKey[] keys = animation.Timelines[spriterRef.TimelineId].Keys;
            SpriterTimelineKey   keyA = keys[spriterRef.KeyId];
            SpriterTimelineKey   keyB = keys.GetNextKey(keyA, animation.Looping);

            if (keyB == null)
            {
                return(Copy(keyA.ObjectInfo));
            }

            float factor = SpriterHelper.GetFactor(keyA, keyB, animation.Length, targetTime);

            return(Interpolate(keyA.ObjectInfo, keyB.ObjectInfo, factor, keyA.Spin));
        }
Exemplo n.º 18
0
        protected virtual SpriterObjectInfo GetObjectInfo(SpriterAnimation animation, string name)
        {
            SpriterObjectInfo objInfo = null;

            for (int i = 0; i < animation.Entity.ObjectInfos.Length; ++i)
            {
                SpriterObjectInfo info = animation.Entity.ObjectInfos[i];
                if (info.Name == name)
                {
                    objInfo = info;
                    break;
                }
            }

            return(objInfo);
        }
Exemplo n.º 19
0
        private static void InitInfos(SpriterAnimation animation)
        {
            if (animation.Timelines == null) animation.Timelines = new SpriterTimeline[0];

            var infos = from t in animation.Timelines
                        from k in t.Keys
                        let o = k.ObjectInfo
                        where o != null && (float.IsNaN(o.PivotX) || float.IsNaN(o.PivotY))
                        select o;

            foreach (SpriterObject info in infos)
            {
                SpriterFile file = animation.Entity.Spriter.Folders[info.FolderId].Files[info.FileId];
                info.PivotX = file.PivotX;
                info.PivotY = file.PivotY;
            }
        }
        private static void AddEventData(SpriterAnimation animation, float targetTime, float deltaTime, FrameMetadata metadata)
        {
            if (animation.Eventlines == null)
            {
                return;
            }

            float previousTime = targetTime - deltaTime;

            foreach (SpriterEventline eventline in animation.Eventlines)
            {
                foreach (SpriterKey key in eventline.Keys)
                {
                    if (IsTriggered(key, targetTime, previousTime, animation.Length))
                    {
                        metadata.Events.Add(eventline.Name);
                    }
                }
            }
        }
        private static void AddSoundData(SpriterAnimation animation, float targetTime, float deltaTime, FrameMetadata metadata)
        {
            if (animation.Soundlines == null)
            {
                return;
            }

            float previousTime = targetTime - deltaTime;

            foreach (SpriterSoundline soundline in animation.Soundlines)
            {
                foreach (SpriterSoundlineKey key in soundline.Keys)
                {
                    SpriterSound sound = key.SoundObject;
                    if (sound.Trigger && IsTriggered(key, targetTime, previousTime, animation.Length))
                    {
                        metadata.Sounds.Add(sound);
                    }
                }
            }
        }
Exemplo n.º 22
0
        private static void InitVarDefs(SpriterAnimation animation)
        {
            if (animation.Meta == null || animation.Meta.Varlines == null || animation.Meta.Varlines.Length == 0) return;

            foreach (SpriterVarline varline in animation.Meta.Varlines)
            {
                SpriterVarDef varDefs = animation.Entity.Variables[varline.Def];
                Init(varDefs, varline);
            }

            foreach (SpriterTimeline timeline in animation.Timelines)
            {
                if (timeline.Meta == null || timeline.Meta.Varlines == null || timeline.Meta.Varlines.Length == 0) continue;
                SpriterObjectInfo objInfo = animation.Entity.ObjectInfos.First(o => o.Name == timeline.Name);
                foreach (SpriterVarline varline in timeline.Meta.Varlines)
                {
                    SpriterVarDef varDef = objInfo.Variables[varline.Def];
                    Init(varDef, varline);
                }
            }
        }
Exemplo n.º 23
0
        public static SpriterObjectInfo[] GetDrawData(SpriterAnimation animation, float targetTime)
        {
            SpriterMainLineKey[] keys = animation.MainlineKeys;
            SpriterMainLineKey keyA = keys.Last(k => k.Time <= targetTime);
            int nextKey = keyA.Id + 1;
            if (nextKey >= keys.Length) nextKey = 0;
            SpriterMainLineKey keyB = keys[nextKey];

            float nextTime = keyB.Time > keyA.Time ? keyB.Time : animation.Length;
            float factor = GetFactor(keyA, keyB, animation.Length, targetTime);
            float adjustedTime = Linear(keyA.Time, nextTime, factor);

            var boneInfos = new Dictionary<int, SpriterSpatialInfo>();

            if (keyA.BoneRefs != null)
            {
                for (int i = 0; i < keyA.BoneRefs.Length; ++i)
                {
                    SpriterRef boneRef = keyA.BoneRefs[i];
                    SpriterSpatialInfo interpolated = GetBoneInfo(boneRef, animation, adjustedTime);

                    if (boneRef.ParentId >= 0) ApplyParentTransform(interpolated, boneInfos[boneRef.ParentId]);
                    boneInfos[i] = interpolated;
                }
            }

            SpriterObjectInfo[] ret = new SpriterObjectInfo[keyA.ObjectRefs.Length];

            for (int i = 0; i < keyA.ObjectRefs.Length; ++i)
            {
                SpriterObjectRef objectRef = keyA.ObjectRefs[i];
                SpriterObjectInfo interpolated = GetObjectInfo(objectRef, animation, adjustedTime);

                if (objectRef.ParentId >= 0) ApplyParentTransform(interpolated, boneInfos[objectRef.ParentId]);
                ret[objectRef.Id] = interpolated;
            }

            return ret;
        }
Exemplo n.º 24
0
        protected virtual void AddEventData(SpriterAnimation animation, float targetTime, float deltaTime)
        {
            if (animation.Eventlines == null)
            {
                return;
            }

            float previousTime = targetTime - deltaTime;

            for (int i = 0; i < animation.Eventlines.Length; ++i)
            {
                SpriterEventline eventline = animation.Eventlines[i];
                for (int j = 0; j < eventline.Keys.Length; ++j)
                {
                    SpriterKey key = eventline.Keys[j];
                    if (IsTriggered(key, targetTime, previousTime, animation.Length))
                    {
                        FrameData.Events.Add(eventline.Name);
                    }
                }
            }
        }
Exemplo n.º 25
0
        protected virtual void AddSoundData(SpriterAnimation animation, float targetTime, float deltaTime)
        {
            if (animation.Soundlines == null)
            {
                return;
            }

            float previousTime = targetTime - deltaTime;

            for (int i = 0; i < animation.Soundlines.Length; ++i)
            {
                SpriterSoundline soundline = animation.Soundlines[i];
                for (int j = 0; j < soundline.Keys.Length; ++j)
                {
                    SpriterSoundlineKey key   = soundline.Keys[j];
                    SpriterSound        sound = key.SoundObject;
                    if (sound.Trigger && IsTriggered(key, targetTime, previousTime, animation.Length))
                    {
                        FrameData.Sounds.Add(sound);
                    }
                }
            }
        }
Exemplo n.º 26
0
        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;
            }
        }
        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);
        }
        private static void AddVariableAndTagData(SpriterAnimation animation, float targetTime, FrameMetadata metadata)
        {
            if (animation.Meta == null)
            {
                return;
            }

            if (animation.Meta.Varlines != null && animation.Meta.Varlines.Length > 0)
            {
                foreach (SpriterVarline varline in animation.Meta.Varlines)
                {
                    SpriterVarDef variable = animation.Entity.Variables[varline.Def];
                    metadata.AnimationVars[variable.Name] = GetVariableValue(animation, variable, varline, targetTime);
                }
            }

            SpriterElement[] tags    = animation.Entity.Spriter.Tags;
            SpriterTagline   tagline = animation.Meta.Tagline;

            if (tagline != null && tagline.Keys != null && tagline.Keys.Length > 0)
            {
                SpriterTaglineKey key = LastKeyForTime <SpriterTaglineKey>(tagline.Keys, targetTime);
                if (key != null && key.Tags != null)
                {
                    foreach (SpriterTag tag in key.Tags)
                    {
                        metadata.AnimationTags.Add(tags[tag.TagId].Name);
                    }
                }
            }

            foreach (SpriterTimeline timeline in animation.Timelines)
            {
                SpriterMeta meta = timeline.Meta;
                if (meta == null)
                {
                    continue;
                }

                SpriterObjectInfo objInfo = GetObjectInfo(animation, timeline.Name);

                if (meta.Varlines != null && meta.Varlines.Length > 0)
                {
                    foreach (SpriterVarline varline in timeline.Meta.Varlines)
                    {
                        SpriterVarDef variable = objInfo.Variables[varline.Def];
                        metadata.AddObjectVar(objInfo.Name, variable.Name, GetVariableValue(animation, variable, varline, targetTime));
                    }
                }

                if (meta.Tagline != null && meta.Tagline.Keys != null && meta.Tagline.Keys.Length > 0)
                {
                    SpriterTaglineKey key = LastKeyForTime <SpriterTaglineKey>(tagline.Keys, targetTime);
                    if (key != null && key.Tags != null)
                    {
                        foreach (SpriterTag tag in key.Tags)
                        {
                            metadata.AddObjectTag(objInfo.Name, tags[tag.TagId].Name);
                        }
                    }
                }
            }
        }
Exemplo n.º 29
0
        public static void GetFrameMetadata(FrameMetadata metadata, SpriterAnimation first, SpriterAnimation second, float targetTime, float deltaTime, float factor)
        {
            SpriterAnimation currentAnimation = factor < 0.5f ? first : second;

            UpdateFrameMetadata(metadata, currentAnimation, targetTime, deltaTime);
        }
Exemplo n.º 30
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);
        }
Exemplo n.º 31
0
        private static void GetKeys(SpriterRef spriterRef, SpriterAnimation animation, out SpriterTimeLineKey keyA, out SpriterTimeLineKey keyB)
        {
            int tId = spriterRef.TimelineId;
            int kId = spriterRef.KeyId;

            var timelineKeys = animation.Timelines[tId].Keys;

            keyA = timelineKeys[kId];
            keyB = null;

            if (timelineKeys.Length == 1) return;

            int keyBId = kId + 1;
            if (keyBId >= timelineKeys.Length)
            {
                if (animation.Looping) keyBId = 0;
                else return;
            }

            keyB = timelineKeys[keyBId];
        }
Exemplo n.º 32
0
        protected virtual void AddVariableAndTagData(SpriterAnimation animation, float targetTime)
        {
            if (animation.Meta == null)
            {
                return;
            }

            if (Config.VarsEnabled && animation.Meta.Varlines != null && animation.Meta.Varlines.Length > 0)
            {
                for (int i = 0; i < animation.Meta.Varlines.Length; ++i)
                {
                    SpriterVarline varline  = animation.Meta.Varlines[i];
                    SpriterVarDef  variable = animation.Entity.Variables[varline.Def];
                    FrameData.AnimationVars[variable.Name] = GetVariableValue(animation, variable, varline, targetTime);
                }
            }

            SpriterElement[] tags    = animation.Entity.Spriter.Tags;
            SpriterTagline   tagline = animation.Meta.Tagline;

            if (Config.TagsEnabled && tagline != null && tagline.Keys != null && tagline.Keys.Length > 0)
            {
                SpriterTaglineKey key = tagline.Keys.GetLastKey(targetTime);
                if (key != null && key.Tags != null)
                {
                    for (int i = 0; i < key.Tags.Length; ++i)
                    {
                        SpriterTag tag = key.Tags[i];
                        FrameData.AnimationTags.Add(tags[tag.TagId].Name);
                    }
                }
            }

            for (int i = 0; i < animation.Timelines.Length; ++i)
            {
                SpriterTimeline timeline = animation.Timelines[i];
                SpriterMeta     meta     = timeline.Meta;
                if (meta == null)
                {
                    continue;
                }

                SpriterObjectInfo objInfo = GetObjectInfo(animation, timeline.Name);

                if (Config.VarsEnabled && meta.Varlines != null && meta.Varlines.Length > 0)
                {
                    for (int j = 0; j < timeline.Meta.Varlines.Length; ++j)
                    {
                        SpriterVarline varline  = timeline.Meta.Varlines[j];
                        SpriterVarDef  variable = objInfo.Variables[varline.Def];
                        FrameData.AddObjectVar(objInfo.Name, variable.Name, GetVariableValue(animation, variable, varline, targetTime));
                    }
                }

                if (Config.TagsEnabled && meta.Tagline != null && meta.Tagline.Keys != null && meta.Tagline.Keys.Length > 0)
                {
                    SpriterTaglineKey key = tagline.Keys.GetLastKey(targetTime);
                    if (key != null && key.Tags != null)
                    {
                        for (int j = 0; j < key.Tags.Length; ++j)
                        {
                            SpriterTag tag = key.Tags[j];
                            FrameData.AddObjectTag(objInfo.Name, tags[tag.TagId].Name);
                        }
                    }
                }
            }
        }
Exemplo n.º 33
0
        private static SpriterObjectInfo GetObjectInfo(SpriterRef spriterRef, SpriterAnimation animation, float targetTime)
        {
            SpriterTimeLineKey keyA;
            SpriterTimeLineKey keyB;

            GetKeys(spriterRef, animation, out keyA, out keyB);
            if (keyB == null) return Copy(keyA.ObjectInfo);

            float factor = GetFactor(keyA, keyB, animation.Length, targetTime);
            return Interpolate(keyA.ObjectInfo, keyB.ObjectInfo, factor, keyA.Spin);
        }
        public static FrameMetadata GetFrameMetadata(SpriterAnimation first, SpriterAnimation second, float targetTime, float deltaTime, float factor)
        {
            SpriterAnimation currentAnimation = factor < 0.5f ? first : second;

            return(GetFrameMetadata(currentAnimation, targetTime, deltaTime));
        }
Exemplo n.º 35
0
        /// <summary>
        /// Plays the animation with the given name. Playback starts from the beginning.
        /// </summary>
        public virtual void Play(string name)
        {
            SpriterAnimation animation = Animations[name];

            Play(animation);
        }