|
@@ -7,7 +7,9 @@ import android.graphics.Paint;
|
|
|
import android.graphics.RectF;
|
|
|
|
|
|
import com.badlogic.gdx.math.Vector2;
|
|
|
+import com.badlogic.gdx.utils.Array;
|
|
|
import com.badlogic.gdx.utils.FloatArray;
|
|
|
+import com.esotericsoftware.spine.Animation;
|
|
|
import com.esotericsoftware.spine.AnimationState;
|
|
|
import com.esotericsoftware.spine.AnimationStateData;
|
|
|
import com.esotericsoftware.spine.Skeleton;
|
|
@@ -17,6 +19,25 @@ import com.esotericsoftware.spine.android.utils.SkeletonDataUtils;
|
|
|
import java.io.File;
|
|
|
import java.net.URL;
|
|
|
|
|
|
+/**
|
|
|
+ * A {@link AndroidSkeletonDrawable} bundles loading updating updating an {@link AndroidTextureAtlas}, {@link Skeleton}, and {@link AnimationState}
|
|
|
+ * into a single easy-to-use class.
|
|
|
+ *
|
|
|
+ * Use the {@link AndroidSkeletonDrawable#fromAsset(String, String, Context)}, {@link AndroidSkeletonDrawable#fromFile(File, File)},
|
|
|
+ * or {@link AndroidSkeletonDrawable#fromHttp(URL, URL, File)} methods to construct a {@link AndroidSkeletonDrawable}. To have
|
|
|
+ * multiple skeleton drawable instances share the same {@link AndroidTextureAtlas} and {@link SkeletonData}, use the constructor.
|
|
|
+ *
|
|
|
+ * You can then directly access the {@link AndroidSkeletonDrawable#getAtlas()}, {@link AndroidSkeletonDrawable#getSkeletonData()},
|
|
|
+ * {@link AndroidSkeletonDrawable#getSkeleton()}, {@link AndroidSkeletonDrawable#getAnimationStateData()}, and {@link AndroidSkeletonDrawable#getAnimationState()}
|
|
|
+ * to query and animate the skeleton. Use the {@link AnimationState} to queue animations on one or more tracks
|
|
|
+ * via {@link AnimationState#setAnimation(int, Animation, boolean)} or {@link AnimationState#addAnimation(int, Animation, boolean, float)}.
|
|
|
+ *
|
|
|
+ * To update the {@link AnimationState} and apply it to the {@link Skeleton}, call the {@link AndroidSkeletonDrawable#update(float)} function, providing it
|
|
|
+ * a delta time in seconds to advance the animations.
|
|
|
+ *
|
|
|
+ * To render the current pose of the {@link Skeleton}, use {@link SkeletonRenderer#render(Skeleton)}, {@link SkeletonRenderer#renderToCanvas(Canvas, Array)},
|
|
|
+ * {@link SkeletonRenderer#renderToBitmap(float, float, int, Skeleton)}, depending on your needs.
|
|
|
+ */
|
|
|
public class AndroidSkeletonDrawable {
|
|
|
|
|
|
private final AndroidTextureAtlas atlas;
|
|
@@ -29,6 +50,9 @@ public class AndroidSkeletonDrawable {
|
|
|
|
|
|
private final AnimationState animationState;
|
|
|
|
|
|
+ /**
|
|
|
+ * Constructs a new skeleton drawable from the given (possibly shared) {@link AndroidTextureAtlas} and {@link SkeletonData}.
|
|
|
+ */
|
|
|
public AndroidSkeletonDrawable(AndroidTextureAtlas atlas, SkeletonData skeletonData) {
|
|
|
this.atlas = atlas;
|
|
|
this.skeletonData = skeletonData;
|
|
@@ -40,6 +64,11 @@ public class AndroidSkeletonDrawable {
|
|
|
skeleton.updateWorldTransform(Skeleton.Physics.none);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Updates the {@link AnimationState} using the {@code delta} time given in seconds, applies the
|
|
|
+ * animation state to the {@link Skeleton} and updates the world transforms of the skeleton
|
|
|
+ * to calculate its current pose.
|
|
|
+ */
|
|
|
public void update(float delta) {
|
|
|
animationState.update(delta);
|
|
|
animationState.apply(skeleton);
|
|
@@ -48,71 +77,71 @@ public class AndroidSkeletonDrawable {
|
|
|
skeleton.updateWorldTransform(Skeleton.Physics.update);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Get the {@link AndroidTextureAtlas}
|
|
|
+ */
|
|
|
public AndroidTextureAtlas getAtlas() {
|
|
|
return atlas;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Get the {@link Skeleton}
|
|
|
+ */
|
|
|
public Skeleton getSkeleton() {
|
|
|
return skeleton;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Get the {@link SkeletonData}
|
|
|
+ */
|
|
|
public SkeletonData getSkeletonData() {
|
|
|
return skeletonData;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Get the {@link AnimationStateData}
|
|
|
+ */
|
|
|
public AnimationStateData getAnimationStateData() {
|
|
|
return animationStateData;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Get the {@link AnimationState}
|
|
|
+ */
|
|
|
public AnimationState getAnimationState() {
|
|
|
return animationState;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Constructs a new skeleton drawable from the {@code atlasFileName} and {@code skeletonFileName} from the the apps resources using {@link Context}.
|
|
|
+ *
|
|
|
+ * Throws an exception in case the data could not be loaded.
|
|
|
+ */
|
|
|
public static AndroidSkeletonDrawable fromAsset (String atlasFileName, String skeletonFileName, Context context) {
|
|
|
AndroidTextureAtlas atlas = AndroidTextureAtlas.fromAsset(atlasFileName, context);
|
|
|
SkeletonData skeletonData = SkeletonDataUtils.fromAsset(atlas, skeletonFileName, context);
|
|
|
return new AndroidSkeletonDrawable(atlas, skeletonData);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Constructs a new skeleton drawable from the {@code atlasFile} and {@code skeletonFile}.
|
|
|
+ *
|
|
|
+ * Throws an exception in case the data could not be loaded.
|
|
|
+ */
|
|
|
public static AndroidSkeletonDrawable fromFile (File atlasFile, File skeletonFile) {
|
|
|
AndroidTextureAtlas atlas = AndroidTextureAtlas.fromFile(atlasFile);
|
|
|
SkeletonData skeletonData = SkeletonDataUtils.fromFile(atlas, skeletonFile);
|
|
|
return new AndroidSkeletonDrawable(atlas, skeletonData);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Constructs a new skeleton drawable from the {@code atlasUrl} and {@code skeletonUrl}.
|
|
|
+ *
|
|
|
+ * Throws an exception in case the data could not be loaded.
|
|
|
+ */
|
|
|
public static AndroidSkeletonDrawable fromHttp (URL atlasUrl, URL skeletonUrl, File targetDirectory) {
|
|
|
AndroidTextureAtlas atlas = AndroidTextureAtlas.fromHttp(atlasUrl, targetDirectory);
|
|
|
SkeletonData skeletonData = SkeletonDataUtils.fromHttp(atlas, skeletonUrl, targetDirectory);
|
|
|
return new AndroidSkeletonDrawable(atlas, skeletonData);
|
|
|
}
|
|
|
-
|
|
|
- public Bitmap renderToBitmap(SkeletonRenderer renderer, float width, float height, int bgColor) {
|
|
|
- Vector2 offset = new Vector2(0, 0);
|
|
|
- Vector2 size = new Vector2(0, 0);
|
|
|
- FloatArray floatArray = new FloatArray();
|
|
|
-
|
|
|
- getSkeleton().getBounds(offset, size, floatArray);
|
|
|
-
|
|
|
- RectF bounds = new RectF(offset.x, offset.y, offset.x + size.x, offset.y + size.y);
|
|
|
- float scale = (1 / (bounds.width() > bounds.height() ? bounds.width() / width : bounds.height() / height));
|
|
|
-
|
|
|
- Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
|
|
|
- Canvas canvas = new Canvas(bitmap);
|
|
|
-
|
|
|
- Paint paint = new Paint();
|
|
|
- paint.setColor(bgColor);
|
|
|
- paint.setStyle(Paint.Style.FILL);
|
|
|
-
|
|
|
- // Draw background
|
|
|
- canvas.drawRect(0, 0, width, height, paint);
|
|
|
-
|
|
|
- // Transform canvas
|
|
|
- canvas.translate(width / 2, height / 2);
|
|
|
- canvas.scale(scale, -scale);
|
|
|
- canvas.translate(-(bounds.left + bounds.width() / 2), -(bounds.top + bounds.height() / 2));
|
|
|
-
|
|
|
- renderer.render(canvas, renderer.render(skeleton));
|
|
|
-
|
|
|
- return bitmap;
|
|
|
- }
|
|
|
}
|