private KeyFramePivotScaledValues GetKeyFrameValues(Key timelineKey, ObjectInfo box, KeyObjectRef objectRef) { var pivotValue = new KeyFrameValues { RelativePosition = new Vector3(timelineKey.Object.X, timelineKey.Object.Y, 0.0f), RelativeRotation = new Vector3 { Z = timelineKey.Object.Angle }, Spin = timelineKey.Spin }; var width = (int)box.Width; var height = (int)box.Height; float pivotX = timelineKey.Object.PivotX.HasValue ? timelineKey.Object.PivotX.Value : box.PivotX; float pivotY = timelineKey.Object.PivotY.HasValue ? timelineKey.Object.PivotY.Value : box.PivotY; var scaledPolygonValue = new KeyFrameValues { RelativeScaleX = timelineKey.Object.ScaleX, RelativeScaleY = timelineKey.Object.ScaleY, RelativePosition = GetPivotedRelativePosition(width, height, pivotX, pivotY, objectRef.ZIndex) }; return new KeyFramePivotScaledValues { Pivot = pivotValue, ScaledPolygon = scaledPolygonValue }; }
private KeyFrameValues GetKeyFrameValuesForPoint(Key timelineKey, KeyObjectRef objectRef) { return new KeyFrameValues { RelativePosition = new Vector3(timelineKey.Object.X, timelineKey.Object.Y, 0.0f), RelativeRotation = new Vector3 { Z = timelineKey.Object.Angle }, Spin = timelineKey.Spin }; }
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 static Key InterpolateToNewTimelineKey(Key mainlineKey, Key timelineKey, Key nextTimelineKey) { var time = mainlineKey.Time; var percent = GetPercentageIntoFrame(time, timelineKey.Time, nextTimelineKey.Time); return new Key { Bone = timelineKey.Bone == null ? null : new KeyBone { Angle = LinearAngle(timelineKey.Bone.Angle, nextTimelineKey.Bone.Angle, timelineKey.Spin, percent), ScaleX = MathHelper.Lerp(timelineKey.Bone.ScaleX, nextTimelineKey.Bone.ScaleX, percent), ScaleY = MathHelper.Lerp(timelineKey.Bone.ScaleY, nextTimelineKey.Bone.ScaleY, percent), X = MathHelper.Lerp(timelineKey.Bone.X, nextTimelineKey.Bone.X, percent), Y = MathHelper.Lerp(timelineKey.Bone.Y, nextTimelineKey.Bone.Y, percent) }, Spin = timelineKey.Spin, Object = timelineKey.Object == null ? null : new KeyObject { Alpha = MathHelper.Lerp(timelineKey.Object.Alpha, nextTimelineKey.Object.Alpha, percent), Angle = LinearAngle(timelineKey.Object.Angle, nextTimelineKey.Object.Angle, timelineKey.Spin, percent), File = timelineKey.Object.File, Folder = timelineKey.Object.Folder, PivotX = !timelineKey.Object.PivotX.HasValue || !nextTimelineKey.Object.PivotX.HasValue ? (float?)null : MathHelper.Lerp(timelineKey.Object.PivotX.Value, nextTimelineKey.Object.PivotX.Value, percent), PivotY = !timelineKey.Object.PivotY.HasValue || !nextTimelineKey.Object.PivotY.HasValue ? (float?)null : MathHelper.Lerp(timelineKey.Object.PivotY.Value, nextTimelineKey.Object.PivotY.Value, percent), ScaleX = MathHelper.Lerp(timelineKey.Object.ScaleX, nextTimelineKey.Object.ScaleX, percent), ScaleY = MathHelper.Lerp(timelineKey.Object.ScaleY, nextTimelineKey.Object.ScaleY, percent), X = MathHelper.Lerp(timelineKey.Object.X, nextTimelineKey.Object.X, percent), Y = MathHelper.Lerp(timelineKey.Object.Y, nextTimelineKey.Object.Y, percent) }, Time = time }; }
public Key(Key that) { if (that.Bone != null) { Bone = new KeyBone { Angle = that.Bone.Angle, ScaleX = that.Bone.ScaleX, ScaleY = that.Bone.ScaleY, X = that.Bone.X, Y = that.Bone.Y }; } if (that.BoneRef != null) { BoneRef = new List<KeyBoneRef>(); foreach (var keyBoneRef in that.BoneRef) { BoneRef.Add(new KeyBoneRef { Id = keyBoneRef.Id, Key = keyBoneRef.Key, Parent = keyBoneRef.Parent, Timeline = keyBoneRef.Timeline }); } } Id = that.Id; if (that.Object != null) { this.Object = new KeyObject { Alpha = that.Object.Alpha, Angle = that.Object.Angle, File = that.Object.File, Folder = that.Object.Folder, PivotX = that.Object.PivotX, PivotY = that.Object.PivotY, ScaleX = that.Object.ScaleX, ScaleY = that.Object.ScaleY, X = that.Object.X, Y = that.Object.Y }; } if (that.ObjectRef != null) { ObjectRef = new List<KeyObjectRef>(); foreach (var keyObjectRef in that.ObjectRef) { ObjectRef.Add(new KeyObjectRef { Id = keyObjectRef.Id, Key = keyObjectRef.Key, Name = keyObjectRef.Name, Parent = keyObjectRef.Parent, Timeline = keyObjectRef.Timeline, ZIndex = keyObjectRef.ZIndex }); } } Spin = that.Spin; Time = that.Time; }
private static KeyFramePivotScaledValues GetKeyFrameValues(Key timelineKey, SpriterDataFolderFile file, IDictionary<string, Texture2D> textures, string folderFileId, KeyObjectRef objectRef) { var pivotValue = new KeyFrameValues { RelativePosition = new Vector3(timelineKey.Object.X, timelineKey.Object.Y, 0.0f), RelativeRotation = new Vector3 { Z = timelineKey.Object.Angle }, Spin = timelineKey.Spin }; int width = file.Width; int height = file.Height; var pivotY = 1.0f; var pivotX = 0.0f; if (timelineKey.Object.PivotX.HasValue) { pivotX = timelineKey.Object.PivotX.Value; } else if (file.PivotX.HasValue) { pivotX = file.PivotX.Value; } if (timelineKey.Object.PivotY.HasValue) { pivotY = timelineKey.Object.PivotY.Value; } else if (file.PivotY.HasValue) { pivotY = file.PivotY.Value; } var ScaledSpriteValue = new KeyFrameValues { Texture = textures[folderFileId], RelativeScaleX = timelineKey.Object.ScaleX, RelativeScaleY = timelineKey.Object.ScaleY, RelativePosition = GetPivotedRelativePosition(width, height, pivotX, pivotY, objectRef.ZIndex), Alpha = timelineKey.Object.Alpha }; return new KeyFramePivotScaledValues { Pivot = pivotValue, ScaledSprite = ScaledSpriteValue }; }
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; } } }