private void CreateRuntimeObjectsForSpriterObjectRef(Key key, IDictionary<int, ScaledSprite> persistentScaledSprites, SpriterObject SpriterObject, SpriterDataEntityAnimation animation, IDictionary<string, Texture2D> textures, KeyFrame keyFrame, IDictionary<KeyFrameValues, int> SpriterefParentDic, IDictionary<string, ObjectInfo> boxes, IDictionary<int, ScaledPolygon> persistentScaledPolygons, IDictionary<int, SpriterPoint> persistentPoints, Key timelineKeyOverride = null) { foreach (var objectRef in key.ObjectRef) { var timeline = animation.Timeline.Single(t => t.Id == objectRef.Timeline); Key timelineKey = timelineKeyOverride ?? timeline.Key.Single(k => k.Id == objectRef.Key); if (timelineKeyOverride == null && key.Time != timelineKey.Time) { var nextTimelineKey = timeline.Key.FirstOrDefault(k => k.Time > key.Time) ?? new Key(timeline.Key.First()) { Time = animation.Length }; timelineKey = InterpolateToNewTimelineKey(key, timelineKey, nextTimelineKey); } if (timeline.ObjectType == "box") { ScaledPolygon scaledPolygon; ScaledPositionedObject pivot; var box = boxes[timeline.Name]; if (persistentScaledPolygons.ContainsKey(objectRef.Id)) { scaledPolygon = persistentScaledPolygons[objectRef.Id]; pivot = (ScaledPositionedObject)scaledPolygon.Parent; } else { scaledPolygon = ScaledPolygon.CreateRectangle(timelineKey.Object.X, timelineKey.Object.Y, (int)box.Width, (int)box.Height); scaledPolygon.ParentScaleChangesPosition = false; scaledPolygon.Visible = false; var name = objectRef.Name ?? objectRef.Id.ToString(CultureInfo.InvariantCulture); pivot = new ScaledPositionedObject { Name = string.Format("{0}_pivot", name) }; scaledPolygon.Name = timeline.Name; scaledPolygon.AttachTo(pivot, true); pivot.AttachTo(SpriterObject, true); persistentScaledPolygons[objectRef.Id] = scaledPolygon; SpriterObject.ObjectList.Add(scaledPolygon); SpriterObject.ObjectList.Add(pivot); } var values = GetKeyFrameValues(timelineKey, box, objectRef); values.ScaledPolygon.Parent = pivot; if (objectRef.Parent.HasValue) { SpriterefParentDic[values.Pivot] = objectRef.Parent.Value; } else { values.Pivot.Parent = SpriterObject; } keyFrame.Values[pivot] = values.Pivot; keyFrame.Values[scaledPolygon] = values.ScaledPolygon; } else if (timeline.ObjectType == "point") { SpriterPoint point; if (persistentPoints.ContainsKey(objectRef.Id)) { point = persistentPoints[objectRef.Id]; } else { point = new SpriterPoint { X = timelineKey.Object.X, Y = timelineKey.Object.Y, Name = timeline.Name }; SpriterObject.ObjectList.Add(point); persistentPoints[objectRef.Id] = point; } KeyFrameValues values = GetKeyFrameValuesForPoint(timelineKey, objectRef); if (objectRef.Parent.HasValue) { SpriterefParentDic[values] = objectRef.Parent.Value; } else { values.Parent = SpriterObject; } keyFrame.Values[point] = values; } else if (string.IsNullOrEmpty(timeline.ObjectType)) { var folderFileId = string.Format("{0}_{1}", timelineKey.Object.Folder, timelineKey.Object.File); var file = this.Folder.First(f => f.Id == timelineKey.Object.Folder) .File.First(f => f.Id == timelineKey.Object.File); ScaledSprite scaledSprite; ScaledPositionedObject pivot; if (persistentScaledSprites.ContainsKey(objectRef.Id)) { scaledSprite = persistentScaledSprites[objectRef.Id]; pivot = (ScaledPositionedObject) scaledSprite.Parent; } else { var name = objectRef.Name ?? objectRef.Id.ToString(CultureInfo.InvariantCulture); pivot = new ScaledPositionedObject {Name = string.Format("{0}_pivot", name)}; scaledSprite = new ScaledSprite { Name = string.Format("{0}_sprite", name), Width = file.Width, Height = file.Height, ParentScaleChangesPosition = false }; scaledSprite.AttachTo(pivot, true); pivot.AttachTo(SpriterObject, true); persistentScaledSprites[objectRef.Id] = scaledSprite; SpriterObject.ObjectList.Add(scaledSprite); SpriterObject.ObjectList.Add(pivot); } var values = GetKeyFrameValues(timelineKey, file, textures, folderFileId, objectRef); values.ScaledSprite.Parent = pivot; if (objectRef.Parent.HasValue) { SpriterefParentDic[values.Pivot] = objectRef.Parent.Value; } else { values.Pivot.Parent = SpriterObject; } keyFrame.Values[pivot] = values.Pivot; keyFrame.Values[scaledSprite] = values.ScaledSprite; } } }
private void HandleUnreferencedTimelinekeys(SpriterDataEntityAnimation animation, SpriterDataEntityAnimationMainline mainline, List<KeyFrame> keyFrameList, IDictionary<int, ScaledSprite> persistentScaledSprites, SpriterObject SpriterObject, IDictionary<string, Texture2D> textures, IDictionary<KeyFrameValues, int> keyFrameValuesParentDictionary, IDictionary<int, SpriterBone> persistentBones, IDictionary<int, ScaledPositionedObject> boneRefDic, IDictionary<string, ObjectInfo> boxes, IDictionary<int, ScaledPolygon> persistentScaledPolygons, IDictionary<int, SpriterPoint> persistentPoints, SpriterDataEntity entity) { foreach (var timeline in animation.Timeline) { foreach (var timelineKey in timeline.Key) { // if timeline key has an object, and no mainline keys for objects reference this key if (timelineKey.Object != null && !mainline.Keys.Where(k => k.ObjectRef != null) .Any(k => k.ObjectRef.Any(r => r.Key == timelineKey.Id && r.Timeline == timeline.Id)) || timelineKey.Bone != null && !mainline.Keys.Where(k => k.BoneRef != null) .Any(k => k.BoneRef.Any(r => r.Key == timelineKey.Id && r.Timeline == timeline.Id))) { int index = keyFrameList.FindLastIndex(kf => Math.Abs(kf.Time - (timelineKey.Time/1000.0f)) < .0001f); if (index > 0) { var keyFrame = new KeyFrame {Time = timelineKey.Time/1000.0f}; var mainlineKey = mainline.Keys.Single(k => k.Time == timelineKey.Time); if (mainlineKey.ObjectRef != null && timelineKey.Object != null) { CreateRuntimeObjectsForSpriterObjectRef(mainlineKey, persistentScaledSprites, SpriterObject, animation, textures, keyFrame, keyFrameValuesParentDictionary, boxes, persistentScaledPolygons, persistentPoints, timelineKeyOverride: timelineKey); } if (mainlineKey.BoneRef != null && timelineKey.Bone != null) { CreateRuntimeObjectsForSpriterBoneRef(mainlineKey, persistentBones, SpriterObject, animation, keyFrame, boneRefDic, keyFrameValuesParentDictionary, entity, timelineKey); } keyFrameList.Insert(index, keyFrame); } } } } }
private static void CreateRuntimeObjectsForSpriterBoneRef(Key key, IDictionary<int, SpriterBone> persistentBones, SpriterObject SpriterObject, SpriterDataEntityAnimation animation, KeyFrame keyFrame, IDictionary<int, ScaledPositionedObject> boneRefDic, IDictionary<KeyFrameValues, int> boneRefParentDic, SpriterDataEntity entity, Key timelineKeyOverride = null) { IDictionary<int, KeyBone> bones = new Dictionary<int, KeyBone>(); foreach (var boneRef in key.BoneRef) { SpriterBone bone; var timeline = animation.Timeline.Single(t => t.Id == boneRef.Timeline); if (persistentBones.ContainsKey(boneRef.Id)) { bone = persistentBones[boneRef.Id]; } else { var objectInfo = entity.ObjectInfos == null ? (ObjectInfo)null : entity.ObjectInfos.FirstOrDefault(o => o.Type == "bone" && o.Name == timeline.Name); bone = new SpriterBone { Name = timeline.Name, Length = objectInfo == null ? 200 : objectInfo.Width }; bone.AttachTo(SpriterObject, true); persistentBones[boneRef.Id] = bone; SpriterObject.ObjectList.Add(bone); } var timelineKey = timelineKeyOverride ?? timeline.Key.Single(k => k.Id == boneRef.Key); if (timelineKeyOverride == null && key.Time != timelineKey.Time) { var nextTimelineKey = timeline.Key.FirstOrDefault(k => k.Time > key.Time) ?? new Key(timeline.Key.First()) { Time = animation.Length }; timelineKey = InterpolateToNewTimelineKey(key, timelineKey, nextTimelineKey); } var timelineKeyBone = new KeyBone(timelineKey.Bone); bones[boneRef.Id] = timelineKeyBone; keyFrame.Values[bone] = new KeyFrameValues { RelativePosition = new Vector3(timelineKeyBone.X, timelineKeyBone.Y, 0.0f), RelativeRotation = new Vector3(0.0f, 0.0f, timelineKeyBone.Angle), RelativeScaleX = timelineKeyBone.ScaleX, RelativeScaleY = timelineKeyBone.ScaleY, Spin = timelineKey.Spin }; boneRefDic[boneRef.Id] = bone; if (boneRef.Parent.HasValue) { boneRefParentDic[keyFrame.Values[bone]] = boneRef.Parent.Value; } } }