mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-06 23:34:53 +08:00
Refactored AttachmentLoader, allowing parameters per attachment type. Added attachment path. Added per attachment color.
This commit is contained in:
parent
e5aca584a2
commit
d8a3f2231c
@ -48,8 +48,6 @@ import com.esotericsoftware.spine.attachments.AttachmentLoader;
|
||||
import com.esotericsoftware.spine.attachments.AttachmentType;
|
||||
import com.esotericsoftware.spine.attachments.BoundingBoxAttachment;
|
||||
import com.esotericsoftware.spine.attachments.RegionAttachment;
|
||||
import com.esotericsoftware.spine.attachments.RegionSequenceAttachment;
|
||||
import com.esotericsoftware.spine.attachments.RegionSequenceAttachment.Mode;
|
||||
|
||||
import com.badlogic.gdx.files.FileHandle;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
@ -189,35 +187,33 @@ public class SkeletonBinary {
|
||||
String name = input.readString();
|
||||
if (name == null) name = attachmentName;
|
||||
|
||||
AttachmentType type = AttachmentType.values()[input.readByte()];
|
||||
Attachment attachment = attachmentLoader.newAttachment(skin, type, name);
|
||||
|
||||
if (attachment instanceof RegionSequenceAttachment) {
|
||||
RegionSequenceAttachment regionSequenceAttachment = (RegionSequenceAttachment)attachment;
|
||||
regionSequenceAttachment.setFrameTime(1 / input.readFloat());
|
||||
regionSequenceAttachment.setMode(Mode.values()[input.readInt(true)]);
|
||||
|
||||
} else if (attachment instanceof RegionAttachment) {
|
||||
RegionAttachment regionAttachment = (RegionAttachment)attachment;
|
||||
regionAttachment.setX(input.readFloat() * scale);
|
||||
regionAttachment.setY(input.readFloat() * scale);
|
||||
regionAttachment.setScaleX(input.readFloat());
|
||||
regionAttachment.setScaleY(input.readFloat());
|
||||
regionAttachment.setRotation(input.readFloat());
|
||||
regionAttachment.setWidth(input.readFloat() * scale);
|
||||
regionAttachment.setHeight(input.readFloat() * scale);
|
||||
regionAttachment.updateOffset();
|
||||
|
||||
} else if (attachment instanceof BoundingBoxAttachment) {
|
||||
BoundingBoxAttachment box = (BoundingBoxAttachment)attachment;
|
||||
switch (AttachmentType.values()[input.readByte()]) {
|
||||
case region:
|
||||
String path = input.readString();
|
||||
if (path.length() == 0) path = name;
|
||||
RegionAttachment region = attachmentLoader.newRegionAttachment(skin, name, path);
|
||||
if (region == null) return null;
|
||||
region.setX(input.readFloat() * scale);
|
||||
region.setY(input.readFloat() * scale);
|
||||
region.setScaleX(input.readFloat());
|
||||
region.setScaleY(input.readFloat());
|
||||
region.setRotation(input.readFloat());
|
||||
region.setWidth(input.readFloat() * scale);
|
||||
region.setHeight(input.readFloat() * scale);
|
||||
Color.rgba8888ToColor(region.getColor(), input.readInt());
|
||||
region.updateOffset();
|
||||
return region;
|
||||
case boundingbox:
|
||||
BoundingBoxAttachment box = attachmentLoader.newBoundingBoxAttachment(skin, name);
|
||||
if (box == null) return null;
|
||||
int n = input.readInt(true);
|
||||
float[] points = new float[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
points[i] = input.readFloat();
|
||||
box.setVertices(points);
|
||||
return box;
|
||||
}
|
||||
|
||||
return attachment;
|
||||
return null;
|
||||
}
|
||||
|
||||
private void readAnimation (String name, DataInput input, SkeletonData skeletonData) {
|
||||
|
||||
@ -48,8 +48,6 @@ import com.esotericsoftware.spine.attachments.AttachmentLoader;
|
||||
import com.esotericsoftware.spine.attachments.AttachmentType;
|
||||
import com.esotericsoftware.spine.attachments.BoundingBoxAttachment;
|
||||
import com.esotericsoftware.spine.attachments.RegionAttachment;
|
||||
import com.esotericsoftware.spine.attachments.RegionSequenceAttachment;
|
||||
import com.esotericsoftware.spine.attachments.RegionSequenceAttachment.Mode;
|
||||
|
||||
import com.badlogic.gdx.files.FileHandle;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
@ -170,40 +168,42 @@ public class SkeletonJson {
|
||||
private Attachment readAttachment (Skin skin, String name, JsonValue map) {
|
||||
name = map.getString("name", name);
|
||||
|
||||
AttachmentType type = AttachmentType.valueOf(map.getString("type", AttachmentType.region.name()));
|
||||
Attachment attachment = attachmentLoader.newAttachment(skin, type, name);
|
||||
switch (AttachmentType.valueOf(map.getString("type", AttachmentType.region.name()))) {
|
||||
case region:
|
||||
RegionAttachment region = attachmentLoader.newRegionAttachment(skin, name, map.getString("path", name));
|
||||
region.setX(map.getFloat("x", 0) * scale);
|
||||
region.setY(map.getFloat("y", 0) * scale);
|
||||
region.setScaleX(map.getFloat("scaleX", 1));
|
||||
region.setScaleY(map.getFloat("scaleY", 1));
|
||||
region.setRotation(map.getFloat("rotation", 0));
|
||||
region.setWidth(map.getFloat("width") * scale);
|
||||
region.setHeight(map.getFloat("height") * scale);
|
||||
|
||||
if (attachment instanceof RegionSequenceAttachment) {
|
||||
RegionSequenceAttachment regionSequenceAttachment = (RegionSequenceAttachment)attachment;
|
||||
String color = map.getString("color", null);
|
||||
if (color != null) region.getColor().set(Color.valueOf(color));
|
||||
|
||||
float fps = map.getFloat("fps");
|
||||
regionSequenceAttachment.setFrameTime(fps);
|
||||
|
||||
String modeString = map.getString("mode");
|
||||
regionSequenceAttachment.setMode(modeString == null ? Mode.forward : Mode.valueOf(modeString));
|
||||
|
||||
} else if (attachment instanceof RegionAttachment) {
|
||||
RegionAttachment regionAttachment = (RegionAttachment)attachment;
|
||||
regionAttachment.setX(map.getFloat("x", 0) * scale);
|
||||
regionAttachment.setY(map.getFloat("y", 0) * scale);
|
||||
regionAttachment.setScaleX(map.getFloat("scaleX", 1));
|
||||
regionAttachment.setScaleY(map.getFloat("scaleY", 1));
|
||||
regionAttachment.setRotation(map.getFloat("rotation", 0));
|
||||
regionAttachment.setWidth(map.getFloat("width", 32) * scale);
|
||||
regionAttachment.setHeight(map.getFloat("height", 32) * scale);
|
||||
regionAttachment.updateOffset();
|
||||
|
||||
} else if (attachment instanceof BoundingBoxAttachment) {
|
||||
BoundingBoxAttachment box = (BoundingBoxAttachment)attachment;
|
||||
region.updateOffset();
|
||||
return region;
|
||||
case boundingbox:
|
||||
BoundingBoxAttachment box = attachmentLoader.newBoundingBoxAttachment(skin, name);
|
||||
JsonValue verticesArray = map.require("vertices");
|
||||
float[] vertices = new float[verticesArray.size];
|
||||
int i = 0;
|
||||
for (JsonValue point = verticesArray.child; point != null; point = point.next())
|
||||
vertices[i++] = point.asFloat() * scale;
|
||||
box.setVertices(vertices);
|
||||
return box;
|
||||
}
|
||||
|
||||
return attachment;
|
||||
// RegionSequenceAttachment regionSequenceAttachment = (RegionSequenceAttachment)attachment;
|
||||
//
|
||||
// float fps = map.getFloat("fps");
|
||||
// regionSequenceAttachment.setFrameTime(fps);
|
||||
//
|
||||
// String modeString = map.getString("mode");
|
||||
// regionSequenceAttachment.setMode(modeString == null ? Mode.forward : Mode.valueOf(modeString));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void readAnimation (String name, JsonValue map, SkeletonData skeletonData) {
|
||||
|
||||
@ -46,28 +46,16 @@ public class AtlasAttachmentLoader implements AttachmentLoader {
|
||||
this.atlas = atlas;
|
||||
}
|
||||
|
||||
public Attachment newAttachment (Skin skin, AttachmentType type, String name) {
|
||||
Attachment attachment = null;
|
||||
switch (type) {
|
||||
case region:
|
||||
attachment = new RegionAttachment(name);
|
||||
break;
|
||||
case regionsequence:
|
||||
attachment = new RegionSequenceAttachment(name);
|
||||
break;
|
||||
case boundingbox:
|
||||
return new BoundingBoxAttachment(name);
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown attachment type: " + type);
|
||||
}
|
||||
|
||||
if (attachment instanceof RegionAttachment) {
|
||||
AtlasRegion region = atlas.findRegion(attachment.getName());
|
||||
if (region == null)
|
||||
throw new RuntimeException("Region not found in atlas: " + attachment + " (" + type + " attachment: " + name + ")");
|
||||
((RegionAttachment)attachment).setRegion(region);
|
||||
}
|
||||
|
||||
public RegionAttachment newRegionAttachment (Skin skin, String name, String path) {
|
||||
RegionAttachment attachment = new RegionAttachment(name);
|
||||
AtlasRegion region = atlas.findRegion(path);
|
||||
if (region == null)
|
||||
throw new RuntimeException("Region not found in atlas: " + attachment + " (region attachment: " + name + ")");
|
||||
attachment.setRegion(region);
|
||||
return attachment;
|
||||
}
|
||||
|
||||
public BoundingBoxAttachment newBoundingBoxAttachment (Skin skin, String name) {
|
||||
return new BoundingBoxAttachment(name);
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,5 +37,8 @@ import com.esotericsoftware.spine.Skin;
|
||||
|
||||
public interface AttachmentLoader {
|
||||
/** @return May be null to not load any attachment. */
|
||||
public Attachment newAttachment (Skin skin, AttachmentType type, String name);
|
||||
public RegionAttachment newRegionAttachment (Skin skin, String name, String path);
|
||||
|
||||
/** @return May be null to not load any attachment. */
|
||||
public BoundingBoxAttachment newBoundingBoxAttachment (Skin skin, String name);
|
||||
}
|
||||
|
||||
@ -34,5 +34,5 @@
|
||||
package com.esotericsoftware.spine.attachments;
|
||||
|
||||
public enum AttachmentType {
|
||||
region, regionsequence, boundingbox
|
||||
region, boundingbox
|
||||
}
|
||||
|
||||
@ -60,6 +60,7 @@ public class RegionAttachment extends Attachment {
|
||||
private float x, y, scaleX = 1, scaleY = 1, rotation, width, height;
|
||||
private final float[] vertices = new float[20];
|
||||
private final float[] offset = new float[8];
|
||||
private final Color color = new Color(1, 1, 1, 1);
|
||||
|
||||
public RegionAttachment (String name) {
|
||||
super(name);
|
||||
@ -151,21 +152,26 @@ public class RegionAttachment extends Attachment {
|
||||
Skeleton skeleton = slot.getSkeleton();
|
||||
Color skeletonColor = skeleton.getColor();
|
||||
Color slotColor = slot.getColor();
|
||||
Color regionColor = color;
|
||||
float r = skeletonColor.r * slotColor.r * regionColor.r;
|
||||
float g = skeletonColor.g * slotColor.g * regionColor.g;
|
||||
float b = skeletonColor.b * slotColor.b * regionColor.b;
|
||||
float a = skeletonColor.a * slotColor.a * regionColor.a * 255;
|
||||
float color;
|
||||
if (premultipliedAlpha) {
|
||||
float a = 255 * skeletonColor.a * slotColor.a;
|
||||
color = NumberUtils.intToFloatColor( //
|
||||
((int)a << 24) //
|
||||
| ((int)(a * skeletonColor.b * slotColor.b) << 16) //
|
||||
| ((int)(a * skeletonColor.g * slotColor.g) << 8) //
|
||||
| ((int)(a * skeletonColor.r * slotColor.r)));
|
||||
r *= a;
|
||||
g *= a;
|
||||
b *= a;
|
||||
} else {
|
||||
color = NumberUtils.intToFloatColor( //
|
||||
((int)(255 * skeletonColor.a * slotColor.a) << 24) //
|
||||
| ((int)(255 * skeletonColor.b * slotColor.b) << 16) //
|
||||
| ((int)(255 * skeletonColor.g * slotColor.g) << 8) //
|
||||
| ((int)(255 * skeletonColor.r * slotColor.r)));
|
||||
r *= 255;
|
||||
g *= 255;
|
||||
b *= 255;
|
||||
}
|
||||
color = NumberUtils.intToFloatColor( //
|
||||
((int)(a) << 24) //
|
||||
| ((int)(b) << 16) //
|
||||
| ((int)(g) << 8) //
|
||||
| ((int)(r)));
|
||||
|
||||
float[] vertices = this.vertices;
|
||||
float[] offset = this.offset;
|
||||
@ -265,4 +271,8 @@ public class RegionAttachment extends Attachment {
|
||||
public void setHeight (float height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public Color getColor () {
|
||||
return color;
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,8 +34,6 @@
|
||||
package com.esotericsoftware.spine;
|
||||
|
||||
import com.esotericsoftware.spine.attachments.AtlasAttachmentLoader;
|
||||
import com.esotericsoftware.spine.attachments.Attachment;
|
||||
import com.esotericsoftware.spine.attachments.AttachmentType;
|
||||
import com.esotericsoftware.spine.attachments.RegionAttachment;
|
||||
|
||||
import com.badlogic.gdx.ApplicationAdapter;
|
||||
@ -87,7 +85,7 @@ public class Box2DExample extends ApplicationAdapter {
|
||||
// This loader creates Box2dAttachments instead of RegionAttachments for an easy way to keep
|
||||
// track of the Box2D body for each attachment.
|
||||
AtlasAttachmentLoader atlasLoader = new AtlasAttachmentLoader(atlas) {
|
||||
public Attachment newAttachment (Skin skin, AttachmentType type, String name) {
|
||||
public RegionAttachment newRegionAttachment (Skin skin, String name, String path) {
|
||||
Box2dAttachment attachment = new Box2dAttachment(name);
|
||||
AtlasRegion region = atlas.findRegion(attachment.getName());
|
||||
if (region == null) throw new RuntimeException("Region not found in atlas: " + attachment);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user