Переглянути джерело

Simplified Timeline interface.

NathanSweet 12 роки тому
батько
коміт
42d072ae8d

+ 0 - 12
spine-cpp/include/spine/Animation.h

@@ -53,10 +53,6 @@ public:
 	virtual ~Timeline () {
 	virtual ~Timeline () {
 	}
 	}
 
 
-	virtual float getDuration () const = 0;
-
-	virtual int getKeyframeCount () const = 0;
-
 	virtual void apply (BaseSkeleton *skeleton, float time, float alpha = 1) const = 0;
 	virtual void apply (BaseSkeleton *skeleton, float time, float alpha = 1) const = 0;
 };
 };
 
 
@@ -92,8 +88,6 @@ public:
 	RotateTimeline (int keyframeCount);
 	RotateTimeline (int keyframeCount);
 	virtual ~RotateTimeline ();
 	virtual ~RotateTimeline ();
 
 
-	virtual float getDuration () const;
-	virtual int getKeyframeCount () const;
 	virtual void apply (BaseSkeleton *skeleton, float time, float alpha = 1) const;
 	virtual void apply (BaseSkeleton *skeleton, float time, float alpha = 1) const;
 
 
 	void setKeyframe (int keyframeIndex, float time, float value);
 	void setKeyframe (int keyframeIndex, float time, float value);
@@ -110,8 +104,6 @@ public:
 	TranslateTimeline (int keyframeCount);
 	TranslateTimeline (int keyframeCount);
 	virtual ~TranslateTimeline ();
 	virtual ~TranslateTimeline ();
 
 
-	virtual float getDuration () const;
-	virtual int getKeyframeCount () const;
 	virtual void apply (BaseSkeleton *skeleton, float time, float alpha = 1) const;
 	virtual void apply (BaseSkeleton *skeleton, float time, float alpha = 1) const;
 
 
 	void setKeyframe (int keyframeIndex, float time, float x, float y);
 	void setKeyframe (int keyframeIndex, float time, float x, float y);
@@ -137,8 +129,6 @@ public:
 	ColorTimeline (int keyframeCount);
 	ColorTimeline (int keyframeCount);
 	virtual ~ColorTimeline ();
 	virtual ~ColorTimeline ();
 
 
-	virtual float getDuration () const;
-	virtual int getKeyframeCount () const;
 	virtual void apply (BaseSkeleton *skeleton, float time, float alpha = 1) const;
 	virtual void apply (BaseSkeleton *skeleton, float time, float alpha = 1) const;
 
 
 	void setKeyframe (int keyframeIndex, float time, float r, float g, float b, float a);
 	void setKeyframe (int keyframeIndex, float time, float r, float g, float b, float a);
@@ -156,8 +146,6 @@ public:
 	AttachmentTimeline (int keyframeCount);
 	AttachmentTimeline (int keyframeCount);
 	virtual ~AttachmentTimeline ();
 	virtual ~AttachmentTimeline ();
 
 
-	virtual float getDuration () const;
-	virtual int getKeyframeCount () const;
 	virtual void apply (BaseSkeleton *skeleton, float time, float alpha = 1) const;
 	virtual void apply (BaseSkeleton *skeleton, float time, float alpha = 1) const;
 
 
 	/** The AttachmentTimeline owns the attachmentName.
 	/** The AttachmentTimeline owns the attachmentName.

+ 0 - 32
spine-cpp/src/spine/Animation.cpp

@@ -180,14 +180,6 @@ RotateTimeline::~RotateTimeline () {
 	delete[] frames;
 	delete[] frames;
 }
 }
 
 
-float RotateTimeline::getDuration () const {
-	return frames[framesLength - 2];
-}
-
-int RotateTimeline::getKeyframeCount () const {
-	return framesLength / 2;
-}
-
 void RotateTimeline::setKeyframe (int keyframeIndex, float time, float value) {
 void RotateTimeline::setKeyframe (int keyframeIndex, float time, float value) {
 	keyframeIndex *= 2;
 	keyframeIndex *= 2;
 	frames[keyframeIndex] = time;
 	frames[keyframeIndex] = time;
@@ -251,14 +243,6 @@ TranslateTimeline::~TranslateTimeline () {
 	delete[] frames;
 	delete[] frames;
 }
 }
 
 
-float TranslateTimeline::getDuration () const {
-	return frames[framesLength - 3];
-}
-
-int TranslateTimeline::getKeyframeCount () const {
-	return framesLength / 3;
-}
-
 void TranslateTimeline::setKeyframe (int keyframeIndex, float time, float x, float y) {
 void TranslateTimeline::setKeyframe (int keyframeIndex, float time, float x, float y) {
 	keyframeIndex *= 3;
 	keyframeIndex *= 3;
 	frames[keyframeIndex] = time;
 	frames[keyframeIndex] = time;
@@ -347,14 +331,6 @@ ColorTimeline::~ColorTimeline () {
 	delete[] frames;
 	delete[] frames;
 }
 }
 
 
-float ColorTimeline::getDuration () const {
-	return frames[framesLength - 5];
-}
-
-int ColorTimeline::getKeyframeCount () const {
-	return framesLength / 5;
-}
-
 void ColorTimeline::setKeyframe (int keyframeIndex, float time, float r, float g, float b, float a) {
 void ColorTimeline::setKeyframe (int keyframeIndex, float time, float r, float g, float b, float a) {
 	keyframeIndex *= 5;
 	keyframeIndex *= 5;
 	frames[keyframeIndex] = time;
 	frames[keyframeIndex] = time;
@@ -428,14 +404,6 @@ AttachmentTimeline::~AttachmentTimeline () {
 	delete[] attachmentNames;
 	delete[] attachmentNames;
 }
 }
 
 
-float AttachmentTimeline::getDuration () const {
-	return frames[framesLength - 1];
-}
-
-int AttachmentTimeline::getKeyframeCount () const {
-	return framesLength;
-}
-
 void AttachmentTimeline::setKeyframe (int keyframeIndex, float time, string *attachmentName) {
 void AttachmentTimeline::setKeyframe (int keyframeIndex, float time, string *attachmentName) {
 	frames[keyframeIndex] = time;
 	frames[keyframeIndex] = time;
 	if (attachmentNames[keyframeIndex]) delete attachmentNames[keyframeIndex];
 	if (attachmentNames[keyframeIndex]) delete attachmentNames[keyframeIndex];

+ 7 - 5
spine-cpp/src/spine/BaseSkeletonJson.cpp

@@ -23,11 +23,12 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  ******************************************************************************/
  ******************************************************************************/
 
 
+#include <spine/BaseSkeletonJson.h>
 #include <cstdlib>
 #include <cstdlib>
 #include <fstream>
 #include <fstream>
 #include <stdexcept>
 #include <stdexcept>
+#include <algorithm>
 #include <json/json.h>
 #include <json/json.h>
-#include <spine/BaseSkeletonJson.h>
 #include <spine/BaseAttachmentLoader.h>
 #include <spine/BaseAttachmentLoader.h>
 #include <spine/BaseRegionAttachment.h>
 #include <spine/BaseRegionAttachment.h>
 #include <spine/SkeletonData.h>
 #include <spine/SkeletonData.h>
@@ -38,6 +39,7 @@
 
 
 using std::string;
 using std::string;
 using std::vector;
 using std::vector;
+using std::max;
 using std::runtime_error;
 using std::runtime_error;
 using std::invalid_argument;
 using std::invalid_argument;
 
 
@@ -271,7 +273,7 @@ Animation* BaseSkeletonJson::readAnimation (const char *begin, const char *end,
 					keyframeIndex++;
 					keyframeIndex++;
 				}
 				}
 				timelines.push_back(timeline);
 				timelines.push_back(timeline);
-				if (timeline->getDuration() > duration) duration = timeline->getDuration();
+				duration = max(duration, timeline->frames[values.size() * 2 - 2]);
 
 
 			} else if (timelineName == TIMELINE_TRANSLATE || timelineName == TIMELINE_SCALE) {
 			} else if (timelineName == TIMELINE_TRANSLATE || timelineName == TIMELINE_SCALE) {
 				TranslateTimeline *timeline;
 				TranslateTimeline *timeline;
@@ -296,7 +298,7 @@ Animation* BaseSkeletonJson::readAnimation (const char *begin, const char *end,
 					keyframeIndex++;
 					keyframeIndex++;
 				}
 				}
 				timelines.push_back(timeline);
 				timelines.push_back(timeline);
-				if (timeline->getDuration() > duration) duration = timeline->getDuration();
+				duration = max(duration, timeline->frames[values.size() * 3 - 3]);
 
 
 			} else {
 			} else {
 				throw runtime_error("Invalid timeline type for a bone: " + timelineName + " (" + boneName + ")");
 				throw runtime_error("Invalid timeline type for a bone: " + timelineName + " (" + boneName + ")");
@@ -333,7 +335,7 @@ Animation* BaseSkeletonJson::readAnimation (const char *begin, const char *end,
 						keyframeIndex++;
 						keyframeIndex++;
 					}
 					}
 					timelines.push_back(timeline);
 					timelines.push_back(timeline);
-					if (timeline->getDuration() > duration) duration = timeline->getDuration();
+					duration = max(duration, timeline->frames[values.size() * 5 - 5]);
 
 
 				} else if (timelineName == TIMELINE_ATTACHMENT) {
 				} else if (timelineName == TIMELINE_ATTACHMENT) {
 					AttachmentTimeline *timeline = new AttachmentTimeline(values.size());
 					AttachmentTimeline *timeline = new AttachmentTimeline(values.size());
@@ -348,7 +350,7 @@ Animation* BaseSkeletonJson::readAnimation (const char *begin, const char *end,
 								nameValue.isNull() ? 0 : new string(nameValue.asString()));
 								nameValue.isNull() ? 0 : new string(nameValue.asString()));
 					}
 					}
 					timelines.push_back(timeline);
 					timelines.push_back(timeline);
-					if (timeline->getDuration() > duration) duration = timeline->getDuration();
+					duration = max(duration, timeline->frames[values.size() - 1]);
 
 
 				} else {
 				} else {
 					throw runtime_error("Invalid timeline type for a slot: " + timelineName + " (" + slotName + ")");
 					throw runtime_error("Invalid timeline type for a slot: " + timelineName + " (" + slotName + ")");

+ 5 - 36
spine-libgdx/src/com/esotericsoftware/spine/Animation.java

@@ -44,8 +44,7 @@ public class Animation {
 		return timelines;
 		return timelines;
 	}
 	}
 
 
-	/** Returns the duration of the animation in seconds. Defaults to the max {@link Timeline#getDuration() duration} of the
-	 * timelines. */
+	/** Returns the duration of the animation in seconds. */
 	public float getDuration () {
 	public float getDuration () {
 		return duration;
 		return duration;
 	}
 	}
@@ -115,13 +114,7 @@ public class Animation {
 		return -1;
 		return -1;
 	}
 	}
 
 
-	/** The keyframes for a single animation timeline. */
 	static public interface Timeline {
 	static public interface Timeline {
-		/** Returns the time in seconds of the last keyframe. */
-		public float getDuration ();
-
-		public int getKeyframeCount ();
-
 		/** Sets the value(s) for the specified time. */
 		/** Sets the value(s) for the specified time. */
 		public void apply (Skeleton skeleton, float time, float alpha);
 		public void apply (Skeleton skeleton, float time, float alpha);
 	}
 	}
@@ -138,6 +131,10 @@ public class Animation {
 			curves = new float[(keyframeCount - 1) * 6];
 			curves = new float[(keyframeCount - 1) * 6];
 		}
 		}
 
 
+		public int getKeyframeCount () {
+			return curves.length / 6 + 1;
+		}
+
 		public void setLinear (int keyframeIndex) {
 		public void setLinear (int keyframeIndex) {
 			curves[keyframeIndex * 6] = LINEAR;
 			curves[keyframeIndex * 6] = LINEAR;
 		}
 		}
@@ -215,14 +212,6 @@ public class Animation {
 			frames = new float[keyframeCount * 2];
 			frames = new float[keyframeCount * 2];
 		}
 		}
 
 
-		public float getDuration () {
-			return frames[frames.length - 2];
-		}
-
-		public int getKeyframeCount () {
-			return frames.length / 2;
-		}
-
 		public void setBoneIndex (int boneIndex) {
 		public void setBoneIndex (int boneIndex) {
 			this.boneIndex = boneIndex;
 			this.boneIndex = boneIndex;
 		}
 		}
@@ -292,14 +281,6 @@ public class Animation {
 			frames = new float[keyframeCount * 3];
 			frames = new float[keyframeCount * 3];
 		}
 		}
 
 
-		public float getDuration () {
-			return frames[frames.length - 3];
-		}
-
-		public int getKeyframeCount () {
-			return frames.length / 3;
-		}
-
 		public void setBoneIndex (int boneIndex) {
 		public void setBoneIndex (int boneIndex) {
 			this.boneIndex = boneIndex;
 			this.boneIndex = boneIndex;
 		}
 		}
@@ -391,14 +372,6 @@ public class Animation {
 			frames = new float[keyframeCount * 5];
 			frames = new float[keyframeCount * 5];
 		}
 		}
 
 
-		public float getDuration () {
-			return frames[frames.length - 5];
-		}
-
-		public int getKeyframeCount () {
-			return frames.length / 5;
-		}
-
 		public void setSlotIndex (int slotIndex) {
 		public void setSlotIndex (int slotIndex) {
 			this.slotIndex = slotIndex;
 			this.slotIndex = slotIndex;
 		}
 		}
@@ -468,10 +441,6 @@ public class Animation {
 			attachmentNames = new String[keyframeCount];
 			attachmentNames = new String[keyframeCount];
 		}
 		}
 
 
-		public float getDuration () {
-			return frames[frames.length - 1];
-		}
-
 		public int getKeyframeCount () {
 		public int getKeyframeCount () {
 			return frames.length;
 			return frames.length;
 		}
 		}

+ 8 - 6
spine-libgdx/src/com/esotericsoftware/spine/SkeletonBinary.java

@@ -57,6 +57,8 @@ public class SkeletonBinary {
 	static public final int CURVE_STEPPED = 1;
 	static public final int CURVE_STEPPED = 1;
 	static public final int CURVE_BEZIER = 2;
 	static public final int CURVE_BEZIER = 2;
 
 
+	static private final Color tempColor = new Color();
+
 	private final AttachmentLoader attachmentLoader;
 	private final AttachmentLoader attachmentLoader;
 	private float scale = 1;
 	private float scale = 1;
 
 
@@ -208,7 +210,7 @@ public class SkeletonBinary {
 							if (keyframeIndex < keyCount - 1) readCurve(input, keyframeIndex, timeline);
 							if (keyframeIndex < keyCount - 1) readCurve(input, keyframeIndex, timeline);
 						}
 						}
 						timelines.add(timeline);
 						timelines.add(timeline);
-						duration = Math.max(duration, timeline.getDuration());
+						duration = Math.max(duration, timeline.getKeyframes()[keyCount * 2 - 2]);
 						break;
 						break;
 					}
 					}
 					case TIMELINE_TRANSLATE:
 					case TIMELINE_TRANSLATE:
@@ -228,7 +230,7 @@ public class SkeletonBinary {
 							if (keyframeIndex < keyCount - 1) readCurve(input, keyframeIndex, timeline);
 							if (keyframeIndex < keyCount - 1) readCurve(input, keyframeIndex, timeline);
 						}
 						}
 						timelines.add(timeline);
 						timelines.add(timeline);
-						duration = Math.max(duration, timeline.getDuration());
+						duration = Math.max(duration, timeline.getKeyframes()[keyCount * 3 - 3]);
 						break;
 						break;
 					default:
 					default:
 						throw new RuntimeException("Invalid timeline type for a bone: " + timelineType + " (" + boneName + ")");
 						throw new RuntimeException("Invalid timeline type for a bone: " + timelineType + " (" + boneName + ")");
@@ -250,12 +252,12 @@ public class SkeletonBinary {
 						timeline.setSlotIndex(slotIndex);
 						timeline.setSlotIndex(slotIndex);
 						for (int keyframeIndex = 0; keyframeIndex < keyCount; keyframeIndex++) {
 						for (int keyframeIndex = 0; keyframeIndex < keyCount; keyframeIndex++) {
 							float time = input.readFloat();
 							float time = input.readFloat();
-							Color.rgba8888ToColor(Color.tmp, input.readInt());
-							timeline.setKeyframe(keyframeIndex, time, Color.tmp.r, Color.tmp.g, Color.tmp.b, Color.tmp.a);
+							Color.rgba8888ToColor(tempColor, input.readInt());
+							timeline.setKeyframe(keyframeIndex, time, tempColor.r, tempColor.g, tempColor.b, tempColor.a);
 							if (keyframeIndex < keyCount - 1) readCurve(input, keyframeIndex, timeline);
 							if (keyframeIndex < keyCount - 1) readCurve(input, keyframeIndex, timeline);
 						}
 						}
 						timelines.add(timeline);
 						timelines.add(timeline);
-						duration = Math.max(duration, timeline.getDuration());
+						duration = Math.max(duration, timeline.getKeyframes()[keyCount * 5 - 5]);
 						break;
 						break;
 					}
 					}
 					case TIMELINE_ATTACHMENT:
 					case TIMELINE_ATTACHMENT:
@@ -264,7 +266,7 @@ public class SkeletonBinary {
 						for (int keyframeIndex = 0; keyframeIndex < keyCount; keyframeIndex++)
 						for (int keyframeIndex = 0; keyframeIndex < keyCount; keyframeIndex++)
 							timeline.setKeyframe(keyframeIndex, input.readFloat(), input.readString());
 							timeline.setKeyframe(keyframeIndex, input.readFloat(), input.readString());
 						timelines.add(timeline);
 						timelines.add(timeline);
-						duration = Math.max(duration, timeline.getDuration());
+						duration = Math.max(duration, timeline.getKeyframes()[keyCount - 1]);
 						break;
 						break;
 					default:
 					default:
 						throw new RuntimeException("Invalid timeline type for a slot: " + timelineType + " (" + slotName + ")");
 						throw new RuntimeException("Invalid timeline type for a slot: " + timelineType + " (" + slotName + ")");

+ 4 - 4
spine-libgdx/src/com/esotericsoftware/spine/SkeletonJson.java

@@ -211,7 +211,7 @@ public class SkeletonJson {
 						keyframeIndex++;
 						keyframeIndex++;
 					}
 					}
 					timelines.add(timeline);
 					timelines.add(timeline);
-					duration = Math.max(duration, timeline.getDuration());
+					duration = Math.max(duration, timeline.getKeyframes()[timeline.getKeyframeCount() * 2 - 2]);
 
 
 				} else if (timelineName.equals(TIMELINE_TRANSLATE) || timelineName.equals(TIMELINE_SCALE)) {
 				} else if (timelineName.equals(TIMELINE_TRANSLATE) || timelineName.equals(TIMELINE_SCALE)) {
 					TranslateTimeline timeline;
 					TranslateTimeline timeline;
@@ -234,7 +234,7 @@ public class SkeletonJson {
 						keyframeIndex++;
 						keyframeIndex++;
 					}
 					}
 					timelines.add(timeline);
 					timelines.add(timeline);
-					duration = Math.max(duration, timeline.getDuration());
+					duration = Math.max(duration, timeline.getKeyframes()[timeline.getKeyframeCount() * 3 - 3]);
 
 
 				} else
 				} else
 					throw new RuntimeException("Invalid timeline type for a bone: " + timelineName + " (" + boneName + ")");
 					throw new RuntimeException("Invalid timeline type for a bone: " + timelineName + " (" + boneName + ")");
@@ -264,7 +264,7 @@ public class SkeletonJson {
 							keyframeIndex++;
 							keyframeIndex++;
 						}
 						}
 						timelines.add(timeline);
 						timelines.add(timeline);
-						duration = Math.max(duration, timeline.getDuration());
+						duration = Math.max(duration, timeline.getKeyframes()[timeline.getKeyframeCount() * 5 - 5]);
 
 
 					} else if (timelineName.equals(TIMELINE_ATTACHMENT)) {
 					} else if (timelineName.equals(TIMELINE_ATTACHMENT)) {
 						AttachmentTimeline timeline = new AttachmentTimeline(values.size);
 						AttachmentTimeline timeline = new AttachmentTimeline(values.size);
@@ -276,7 +276,7 @@ public class SkeletonJson {
 							timeline.setKeyframe(keyframeIndex++, time, (String)valueMap.get("name"));
 							timeline.setKeyframe(keyframeIndex++, time, (String)valueMap.get("name"));
 						}
 						}
 						timelines.add(timeline);
 						timelines.add(timeline);
-						duration = Math.max(duration, timeline.getDuration());
+						duration = Math.max(duration, timeline.getKeyframes()[timeline.getKeyframeCount() - 1]);
 
 
 					} else
 					} else
 						throw new RuntimeException("Invalid timeline type for a slot: " + timelineName + " (" + slotName + ")");
 						throw new RuntimeException("Invalid timeline type for a slot: " + timelineName + " (" + slotName + ")");