Update SpineView & SpineView.Builder API

This commit is contained in:
Denis Andrasec 2024-07-10 11:34:11 +02:00
parent 01a6952ff5
commit fc4716f749
8 changed files with 91 additions and 73 deletions

View File

@ -133,14 +133,13 @@ fun AnimationState(nav: NavHostController) {
) {
Text("See output in console!")
AndroidView(
factory = { ctx ->
SpineView(ctx).apply {
loadFromAsset(
"spineboy.atlas",
"spineboy-pro.json",
controller
)
}
factory = { context ->
SpineView.loadFromAssets(
"spineboy.atlas",
"spineboy-pro.json",
context,
controller
)
}
)
}

View File

@ -42,21 +42,20 @@ fun DebugRendering(nav: NavHostController) {
}
) { paddingValues ->
AndroidView(
factory = { ctx ->
SpineView(ctx).apply {
loadFromAsset(
"spineboy.atlas",
"spineboy-pro.json",
SpineController.Builder()
.setOnInitialized {
it.animationState.setAnimation(0, "walk", true)
}
.setOnAfterPaint { controller, canvas, commands ->
debugRenderer.render(controller.drawable, canvas, commands)
}
.build()
)
}
factory = { context ->
SpineView.loadFromAssets(
"spineboy.atlas",
"spineboy-pro.json",
context,
SpineController.Builder()
.setOnInitialized {
it.animationState.setAnimation(0, "walk", true)
}
.setOnAfterPaint { controller, canvas, commands ->
debugRenderer.render(controller.drawable, canvas, commands)
}
.build()
)
},
modifier = Modifier.padding(paddingValues)
)

View File

@ -163,10 +163,8 @@ fun DressUp(nav: NavHostController) {
}
}
AndroidView(
factory = { ctx ->
SpineView(ctx).apply {
loadFromDrawable(drawable, controller)
}
factory = { context ->
SpineView.loadFromDrawable(drawable, context, controller)
},
modifier = Modifier.padding(paddingValues)
)

View File

@ -95,14 +95,13 @@ fun IKFollowing(nav: NavHostController) {
}
) {
AndroidView(
factory = { ctx ->
SpineView(ctx).apply {
loadFromAsset(
"spineboy.atlas",
"spineboy-pro.json",
controller
)
}
factory = { context ->
SpineView.loadFromAssets(
"spineboy.atlas",
"spineboy-pro.json",
context,
controller
)
}
)
}

View File

@ -110,14 +110,13 @@ fun Physics(nav: NavHostController) {
}
) {
AndroidView(
factory = { ctx ->
SpineView(ctx).apply {
loadFromAsset(
"celestial-circus.atlas",
"celestial-circus-pro.skel",
controller
)
}
factory = { context ->
SpineView.loadFromAssets(
"celestial-circus.atlas",
"celestial-circus-pro.skel",
context,
controller
)
},
modifier = Modifier.padding(paddingValues)
)

View File

@ -62,16 +62,10 @@ fun PlayPause(
AndroidView(
factory = { ctx ->
SpineView.Builder(ctx)
SpineView.Builder(ctx, controller)
.setLoadFromAssets("dragon.atlas", "dragon-ess.skel")
.setBoundsProvider(SkinAndAnimationBounds("flying"))
.build()
.apply {
loadFromAsset(
"dragon.atlas",
"dragon-ess.skel",
controller
)
}
},
modifier = Modifier.padding(paddingValues)
)

View File

@ -35,18 +35,17 @@ fun SimpleAnimation(nav: NavHostController) {
}
) { paddingValues ->
AndroidView(
factory = { ctx ->
SpineView(ctx).apply {
loadFromAsset(
"spineboy.atlas",
"spineboy-pro.json",
SpineController.Builder()
.setOnInitialized {
it.animationState.setAnimation(0, "walk", true)
}
.build()
)
}
factory = { context ->
SpineView.loadFromAssets(
"spineboy.atlas",
"spineboy-pro.json",
context,
SpineController.Builder()
.setOnInitialized {
it.animationState.setAnimation(0, "walk", true)
}
.build()
)
},
modifier = Modifier.padding(paddingValues)
)

View File

@ -54,11 +54,24 @@ public class SpineView extends View implements Choreographer.FrameCallback {
public static class Builder {
private final Context context;
private final SpineController controller;
private String atlasFileName;
private String skeletonFileName;
private BoundsProvider boundsProvider = new SetupPoseBounds();
private Alignment alignment = Alignment.CENTER;
public Builder(Context context) {
public Builder(Context context, SpineController controller) {
this.context = context;
this.controller = controller;
}
public Builder setLoadFromAssets(String atlasFileName, String skeletonFileName) {
this.atlasFileName = atlasFileName;
this.skeletonFileName = skeletonFileName;
return this;
}
public Builder setBoundsProvider(BoundsProvider boundsProvider) {
@ -72,9 +85,12 @@ public class SpineView extends View implements Choreographer.FrameCallback {
}
public SpineView build() {
SpineView spineView = new SpineView(context);
SpineView spineView = new SpineView(context, controller);
spineView.boundsProvider = boundsProvider;
spineView.alignment = alignment;
if (atlasFileName != null && skeletonFileName != null) {
spineView.loadFromAsset(atlasFileName, skeletonFileName);
}
return spineView;
}
}
@ -97,35 +113,50 @@ public class SpineView extends View implements Choreographer.FrameCallback {
Alignment alignment = Alignment.CENTER;
public SpineView (Context context) {
public SpineView (Context context, SpineController controller) {
super(context);
this.controller = controller;
}
public SpineView (Context context, AttributeSet attrs) {
super(context, attrs);
this.controller = new SpineController();
}
public SpineView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.controller = new SpineController();
}
public void loadFromAsset(String atlasFileName, String skeletonFileName, SpineController controller) {
public static SpineView loadFromAssets(String atlasFileName, String skeletonFileName, Context context, SpineController controller) {
SpineView spineView = new SpineView(context, controller);
spineView.loadFromAsset(atlasFileName, skeletonFileName);
return spineView;
}
public static SpineView loadFromDrawable(AndroidSkeletonDrawable drawable, Context context, SpineController controller) {
SpineView spineView = new SpineView(context, controller);
spineView.loadFromDrawable(drawable);
return spineView;
}
public void setController(SpineController controller) {
this.controller = controller;
}
public void loadFromAsset(String atlasFileName, String skeletonFileName) {
loadFrom(() -> AndroidSkeletonDrawable.fromAsset(atlasFileName, skeletonFileName, getContext()));
}
public void loadFromFile(File atlasFile, File skeletonFile, SpineController controller) {
this.controller = controller;
public void loadFromFile(File atlasFile, File skeletonFile) {
loadFrom(() -> AndroidSkeletonDrawable.fromFile(atlasFile, skeletonFile));
}
public void loadFromHttp(URL atlasUrl, URL skeletonUrl, SpineController controller) {
this.controller = controller;
public void loadFromHttp(URL atlasUrl, URL skeletonUrl) {
loadFrom(() -> AndroidSkeletonDrawable.fromHttp(atlasUrl, skeletonUrl));
}
public void loadFromDrawable(AndroidSkeletonDrawable drawable, SpineController controller) {
this.controller = controller;
public void loadFromDrawable(AndroidSkeletonDrawable drawable) {
loadFrom(() -> drawable);
}