public virtual SpriterBone getBoneFor(SpriterBone bone) { foreach (SpriterBone b in this.bones) { if (b.equals(bone)) { return b; } } return null; }
/// <summary>Manipulates the given bone.</summary> /// <remarks>Manipulates the given bone.</remarks> /// <param name="bone"></param> public virtual void modSpriterBone(SpriterBone bone ) { this.modObject(bone); }
/// <summary>Manipulates the given bone.</summary> /// <remarks>Manipulates the given bone.</remarks> /// <param name="bone"></param> public virtual void modSpriterBone(SpriterBone bone ) { this.modObject(bone); }
public virtual void addChildBone(SpriterBone bone ) { bone.setParent(this); childBones.Add(bone); }
//import Com.Brashmonkey.Spriter.converters.SpriterObjectConverter; //import AnimationObject; //final private SpriterObjectConverter objectConverter = new SpriterObjectConverter(); public virtual SpriterAnimation buildAnimation( Animation animation) { MainLine mainline = animation.getMainline(); IList<TimeLine> timeLines = animation.getTimeline(); IList<Key> keyFrames = mainline .getKey(); bonesToTween = new Dictionary<SpriterBone , int>(); objectsToTween = new Dictionary<SpriterObject , int>(); SpriterAnimation spriterAnimation = new SpriterAnimation(animation.getId(), animation.getName(), animation.getLength()); for (int k = 0; k < keyFrames.Count; k++) { Key mainlineKey = keyFrames[k]; IList<SpriterObject> tempObjects = new List<SpriterObject >(); IList<SpriterBone> tempBones = new List<SpriterBone >(); SpriterKeyFrame frame = new SpriterKeyFrame (); frame.setTime(mainlineKey.getTime()); frame.setId(mainlineKey.getId()); foreach (BoneRef boneRef in mainlineKey.getBoneRef()) { TimeLine timeline = timeLines[boneRef.getTimeline()]; Key timelineKey = timeline.getKey()[boneRef.getKey()]; SpriterBone bone = boneMerger.merge(boneRef, timelineKey ); bone.setName(timeline.getName()); if (mainlineKey.getTime() != timelineKey.getTime()) { bonesToTween.Add(bone, k); } else { tempBones.Add(bone); } } //} foreach (AnimationObjectRef objectRef in mainlineKey.getObjectRef ()) { TimeLine timeline = timeLines[objectRef.getTimeline()]; Key timelineKey = timeline.getKey()[objectRef.getKey() ]; SpriterObject @object = objectMerger.merge(objectRef , timelineKey); @object.setName(timeline.getName()); if (mainlineKey.getTime() != timelineKey.getTime()) { objectsToTween.Add(@object, k); } else { tempObjects.Add(@object); } } //} SpriterObject[] objArray = new SpriterObject[tempObjects.Count]; tempObjects.CopyTo(objArray,0); frame.setObjects(objArray); SpriterBone[] boneArray = new SpriterBone[tempBones.Count]; tempBones.CopyTo(boneArray, 0); frame.setBones(boneArray); spriterAnimation.frames.Add(frame); } this.tweenBones(spriterAnimation); this.tweenObjects(spriterAnimation); return spriterAnimation; }
public virtual void tweenBones(SpriterAnimation animation) { foreach (SpriterBone key in bonesToTween.Keys) { SpriterBone toTween = key; SpriterKeyFrame frame = animation.frames[bonesToTween[key]]; long time = frame.getTime(); SpriterKeyFrame currentFrame = animation.getPreviousFrameForBone (toTween, time); SpriterKeyFrame nextFrame = animation.getNextFrameFor (toTween, currentFrame, 1); if (nextFrame != currentFrame) { SpriterBone bone1 = currentFrame.getBoneFor(toTween ); SpriterBone bone2 = nextFrame.getBoneFor(toTween); this.interpolateAbstractObject(toTween, bone1, bone2, currentFrame.getTime(), nextFrame .getTime(), time); } SpriterBone[] bones = new SpriterBone [frame.getBones().Length + 1]; for (int i = 0; i < bones.Length - 1; i++) { bones[i] = frame.getBones()[i]; } bones[bones.Length - 1] = toTween; frame.setBones(bones); } }
private void tweenBone(SpriterBone currentBone, SpriterBone nextBone, int i, long startTime, long endTime) { currentBone.copyValuesTo(this.tempBones[i]); this.tempBones[i].setTimeline((nextBone != null) ? currentBone.getTimeline() : -1 ); SpriterAbstractObject parent = (this.tempBones[i] .hasParent()) ? this.tempBones[this.tempBones[i].getParentId()] : this.tempParent; if (nextBone != null) { if (parent != this.tempParent) { if (!currentBone.getParent().equals(nextBone.getParent())) { nextBone = (SpriterBone)this.getTimelineObject(currentBone , this.tempBones2); SpriterCalculator.reTranslateRelative(parent, nextBone); nextBone.setAngle(nextBone.getAngle() * this.flippedX * this.flippedY); } } else { if (nextBone.hasParent()) { nextBone = (SpriterBone)this.getTimelineObject(currentBone , this.tempBones2); SpriterCalculator.reTranslateRelative(parent, nextBone); nextBone.setAngle(nextBone.getAngle() * this.flippedX * this.flippedY); } } if (this.tempBones[i].active) { this.interpolateAbstractObject(this.tempBones[i], currentBone, nextBone, startTime , endTime); } } this.moddedBones[currentBone.getId()].modSpriterBone(this.tempBones[i]); if (this.transitionFixed) { this.tempBones[i].copyValuesTo(this.lastFrame.getBones()[i]); } else { this.tempBones[i].copyValuesTo(this.lastTempFrame.getBones()[i]); } if (!this.tempBones[i].hasParent() || !this.moddedBones[currentBone.getId()].isActive ()) { this.tempBones[i].setX(this.tempBones[i].getX() + this.pivotX); this.tempBones[i].setY(this.tempBones[i].getY() + this.pivotY); } this.translateRelative(this.tempBones[i], parent); }
/// <summary>Generates data which is necessary to animate all animations as intended. /// </summary> /// <remarks> /// Generates data which is necessary to animate all animations as intended. /// This method has to called inside the specific constructor. /// </remarks> protected internal void generateData() { int maxObjects = 0; int maxBones = 0; int maxBonesFrameIndex = 0; int maxObjectsFrameIndex = 0; int maxBonesAnimationIndex = 0; int maxObjectsAnimationIndex = 0; foreach (SpriterAnimation animation in this.animations) { foreach (SpriterKeyFrame frame in animation.frames) { maxBones = System.Math.Max(frame.getBones().Length, maxBones); if (maxBones == frame.getBones().Length) { maxBonesFrameIndex = frame.getId(); maxBonesAnimationIndex = animation.id; } maxObjects = System.Math.Max(frame.getObjects().Length, maxObjects); if (maxObjects == frame.getObjects().Length) { maxObjectsFrameIndex = frame.getId(); maxObjectsAnimationIndex = animation.id; } foreach (SpriterObject o in frame.getObjects()) { o.setLoader(this.loader); o.setRef(this.loader.findReference(o.getRef())); } } } this.instructions = new DrawInstruction[maxObjects]; this.moddedObjects = new SpriterModObject[this.instructions .Length]; this.tempObjects = new SpriterObject[this.instructions .Length]; this.tempObjects2 = new SpriterObject[this.instructions .Length]; this.nonTransformedTempObjects = new SpriterObject [this.instructions.Length]; for (int i = 0; i < this.instructions.Length; i++) { this.instructions[i] = new DrawInstruction(null, 0, 0, 0, 0, 0, 0, 0, 0); this.tempObjects[i] = new SpriterObject(); this.tempObjects2[i] = new SpriterObject(); this.nonTransformedTempObjects[i] = new SpriterObject (); this.nonTransformedTempObjects[i].setId(i); this.moddedObjects[i] = new SpriterModObject(); this.moddedObjects[i].setId(i); } this.tempBones = new SpriterBone[maxBones]; this.tempBones2 = new SpriterBone[tempBones.Length ]; this.nonTransformedTempBones = new SpriterBone[tempBones .Length]; this.moddedBones = new SpriterModObject[this.tempBones .Length]; for (int i_1 = 0; i_1 < this.tempBones.Length; i_1++) { this.tempBones[i_1] = new SpriterBone(); this.tempBones2[i_1] = new SpriterBone(); this.nonTransformedTempBones[i_1] = new SpriterBone (); this.nonTransformedTempBones[i_1].setId(i_1); this.moddedBones[i_1] = new SpriterModObject(); this.moddedBones[i_1].setId(i_1); } SpriterBone[] tmpBones1 = new SpriterBone [this.tempBones.Length]; SpriterBone[] tmpBones2 = new SpriterBone [this.tempBones.Length]; SpriterObject[] tmpObjs1 = new SpriterObject [this.instructions.Length]; SpriterObject[] tmpObjs2 = new SpriterObject [this.instructions.Length]; for (int i_2 = 0; i_2 < tmpObjs1.Length; i_2++) { tmpObjs1[i_2] = new SpriterObject(); tmpObjs2[i_2] = new SpriterObject(); } for (int i_3 = 0; i_3 < tmpBones1.Length; i_3++) { tmpBones1[i_3] = new SpriterBone(); tmpBones2[i_3] = new SpriterBone(); } this.lastFrame.setBones(tmpBones1); this.lastFrame.setObjects(tmpObjs1); this.lastTempFrame.setBones(tmpBones2); this.lastTempFrame.setObjects(tmpObjs2); foreach (SpriterObject @object in this.animations [maxObjectsAnimationIndex].frames[maxObjectsFrameIndex].getObjects()) { for (int i_4 = 0; i_4 < this.nonTransformedTempObjects.Length; i_4++) { if (this.nonTransformedTempObjects[i_4].getId() == @object.getId()) { @object.copyValuesTo(this.nonTransformedTempObjects[i_4]); } } } foreach (SpriterBone bone in this.animations[maxBonesAnimationIndex ].frames[maxBonesFrameIndex].getBones()) { for (int i_4 = 0; i_4 < this.nonTransformedTempBones.Length; i_4++) { if (this.nonTransformedTempBones[i_4].getId() == bone.getId()) { bone.copyValuesTo(this.nonTransformedTempBones[i_4]); } } } this.generated = true; }
/// <summary>Searches for the right index for a given bone.</summary> /// <remarks>Searches for the right index for a given bone.</remarks> /// <param name="bone">bone to search at.</param> /// <returns>right index for the mod bone you want access to. -1 if not found.</returns> public virtual int getModBoneIndexForBone(SpriterBone bone) { for (int i = 0; i < this.tempObjects.Length; i++) { if (this.tempBones[i].equals(bone)) { return i; } } return -1; }
/// <summary>Searches for the right mod bone for the given bone.</summary> /// <remarks>Searches for the right mod bone for the given bone.</remarks> /// <param name="bone">bone to search at.</param> /// <returns>right mod bone you want access to. Null if not found.</returns> public virtual SpriterModObject getModBoneForBone(SpriterBone bone) { try { return this.moddedBones[this.getModBoneIndexForBone(bone)]; } catch (System.IndexOutOfRangeException) { return null; } }
/// <summary>Calculates the bounding box for the current running animation.</summary> /// <remarks> /// Calculates the bounding box for the current running animation. /// Call this method after updating the spriter player. /// </remarks> /// <param name="bone">root to start at. Set null, to iterate through all objects.</param> public virtual void calcBoundingBox(SpriterBone bone ) { if (bone == null) { this.calcBoundingBoxForAll(); } else { bone.boundingBox.set(this.rect); foreach (SpriterObject @object in bone.getChildObjects()) { SpriterPoint[] points = this.tempObjects[@object.getId()] .getBoundingBox(); bone.boundingBox.left = System.Math.Min(System.Math.Min(System.Math.Min(System.Math .Min(points[0].x, points[1].x), points[2].x), points[3].x), bone.boundingBox.left ); bone.boundingBox.right = System.Math.Max(System.Math.Max(System.Math.Max(System.Math .Max(points[0].x, points[1].x), points[2].x), points[3].x), bone.boundingBox.right ); bone.boundingBox.top = System.Math.Max(System.Math.Max(System.Math.Max(System.Math .Max(points[0].y, points[1].y), points[2].y), points[3].y), bone.boundingBox.top ); bone.boundingBox.bottom = System.Math.Min(System.Math.Min(System.Math.Min(System.Math .Min(points[0].y, points[1].y), points[2].y), points[3].y), bone.boundingBox.bottom ); } this.rect.set(bone.boundingBox); foreach (SpriterBone child in bone.getChildBones( )) { calcBoundingBox(child); bone.boundingBox.set(child.boundingBox); } this.rect.set(bone.boundingBox); } this.rect.calculateSize(); }
/// <summary>Searches for a keyframe in this animation which has a smaller or equal starting time as the given time and contains the given bone. /// </summary> /// <remarks>Searches for a keyframe in this animation which has a smaller or equal starting time as the given time and contains the given bone. /// </remarks> /// <param name="bone"></param> /// <param name="time"></param> /// <returns>A keyframe object which has a smaller or equal starting time than the given time and contains the given bone. /// </returns> public virtual SpriterKeyFrame getPreviousFrameForBone(SpriterBone bone, long time) { SpriterKeyFrame frame = null; foreach (SpriterKeyFrame key in this.frames) { if (!key.containsBone(bone)) { continue; } if (key.getTime() <= time) { frame = key; } else { break; } } return frame; }
/// <summary>Generates all needed keyframes from the given spriter data.</summary> /// <remarks>Generates all needed keyframes from the given spriter data. This method sorts all objects by its z_index value to draw them in a proper way. /// </remarks> /// <param name="spriterData">SpriterData to generate from.</param> /// <returns>generated keyframe list.</returns> public static IList<SpriterAnimation> generateKeyFramePool(com.discobeard.spriter.dom.SpriterData data, com.discobeard.spriter.dom.Entity entity) { IList<SpriterAnimation > spriterAnimations = new List<SpriterAnimation >(); IList<com.discobeard.spriter.dom.Animation> animations = entity.getAnimation(); Com.Brashmonkey.Spriter.mergers.SpriterAnimationBuilder frameBuilder = new Com.Brashmonkey.Spriter.mergers.SpriterAnimationBuilder (); foreach (com.discobeard.spriter.dom.Animation anim in animations) { SpriterAnimation spriterAnimation = frameBuilder .buildAnimation(anim); bool found = false; foreach (SpriterKeyFrame key in spriterAnimation .frames) { if (!found) { found = key.getTime() == anim.getLength(); } sort(key.getObjects()); foreach (SpriterBone bone in key.getBones()) { foreach (SpriterBone bone2 in key.getBones()) { if (bone2.hasParent()) { if (!bone2.equals(bone) && bone2.getParentId() == bone.getId()) { bone.addChildBone(bone2); } } } foreach (SpriterObject @object in key.getObjects( )) { Com.Brashmonkey.Spriter.file.Reference @ref = @object.getRef(); com.discobeard.spriter.dom.File f = data.getFolder()[@ref.folder].getFile()[@ref. file]; @ref.dimensions = new Com.Brashmonkey.Spriter.SpriterRectangle(0, f.getHeight(), f.getWidth(), 0f); if (bone.getId() == @object.getParentId()) { bone.addChildObject(@object); } } } } if (!found) { SpriterKeyFrame firstFrame = spriterAnimation.frames [0]; SpriterKeyFrame lastFrame = new SpriterKeyFrame (); lastFrame.setId(spriterAnimation.keyframes()); lastFrame.setBones(new SpriterBone[firstFrame.getBones ().Length]); lastFrame.setObjects(new SpriterObject[firstFrame .getObjects().Length]); for (int j = 0; j < lastFrame.getBones().Length; j++) { SpriterBone bone = new SpriterBone (); firstFrame.getBones()[j].copyValuesTo(bone); lastFrame.getBones()[j] = bone; } for (int j_1 = 0; j_1 < lastFrame.getObjects().Length; j_1++) { SpriterObject @object = new SpriterObject (); firstFrame.getObjects()[j_1].copyValuesTo(@object); lastFrame.getObjects()[j_1] = @object; } lastFrame.setTime(anim.getLength()); spriterAnimation.frames.Add(lastFrame); } spriterAnimations.Add(spriterAnimation); } return spriterAnimations; }
public virtual void addChildBone(SpriterBone bone ) { bone.setParent(this); childBones.Add(bone); }
/// <summary>Returns whether this frame has information about the given bone.</summary> /// <remarks>Returns whether this frame has information about the given bone.</remarks> /// <param name="bone"></param> /// <returns>True if this frame contains the bone, false otherwise.</returns> public virtual bool containsBone(SpriterBone bone ) { return this.getBoneFor(bone) != null; }
/// <param name="bones">to set to this keyframe.</param> public virtual void setBones(SpriterBone[] bones) { this.bones = bones; }