mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-28 12:41:25 +08:00
Minor refactoring.
This commit is contained in:
parent
e56050e3a7
commit
8cae181c37
@ -103,10 +103,8 @@ public class Animation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int linearSearch (float[] values, float target, int step) {
|
static int linearSearch (float[] values, float target, int step) {
|
||||||
for (int i = 0, last = values.length - step; i <= last; i += step) {
|
for (int i = 0, last = values.length - step; i <= last; i += step)
|
||||||
if (values[i] <= target) continue;
|
if (values[i] > target) return i;
|
||||||
return i;
|
|
||||||
}
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,7 +114,7 @@ public class Animation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Base class for frames that use an interpolation bezier curve. */
|
/** Base class for frames that use an interpolation bezier curve. */
|
||||||
static public abstract class CurveTimeline implements Timeline {
|
abstract static public class CurveTimeline implements Timeline {
|
||||||
static private final float LINEAR = 0;
|
static private final float LINEAR = 0;
|
||||||
static private final float STEPPED = -1;
|
static private final float STEPPED = -1;
|
||||||
static private final int BEZIER_SEGMENTS = 10;
|
static private final int BEZIER_SEGMENTS = 10;
|
||||||
@ -220,11 +218,11 @@ public class Animation {
|
|||||||
return frames;
|
return frames;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the time and value of the specified keyframe. */
|
/** Sets the time and angle of the specified keyframe. */
|
||||||
public void setFrame (int frameIndex, float time, float value) {
|
public void setFrame (int frameIndex, float time, float angle) {
|
||||||
frameIndex *= 2;
|
frameIndex *= 2;
|
||||||
frames[frameIndex] = time;
|
frames[frameIndex] = time;
|
||||||
frames[frameIndex + 1] = value;
|
frames[frameIndex + 1] = angle;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void apply (Skeleton skeleton, float time, float alpha) {
|
public void apply (Skeleton skeleton, float time, float alpha) {
|
||||||
|
|||||||
@ -35,7 +35,7 @@ public class Bone {
|
|||||||
final Bone parent;
|
final Bone parent;
|
||||||
float x, y;
|
float x, y;
|
||||||
float rotation;
|
float rotation;
|
||||||
float scaleX = 1, scaleY = 1;
|
float scaleX, scaleY;
|
||||||
|
|
||||||
float m00, m01, worldX; // a b x
|
float m00, m01, worldX; // a b x
|
||||||
float m10, m11, worldY; // c d y
|
float m10, m11, worldY; // c d y
|
||||||
|
|||||||
@ -251,6 +251,7 @@ public class Skeleton {
|
|||||||
/** @param attachmentName May be null. */
|
/** @param attachmentName May be null. */
|
||||||
public void setAttachment (String slotName, String attachmentName) {
|
public void setAttachment (String slotName, String attachmentName) {
|
||||||
if (slotName == null) throw new IllegalArgumentException("slotName cannot be null.");
|
if (slotName == null) throw new IllegalArgumentException("slotName cannot be null.");
|
||||||
|
Array<Slot> slots = this.slots;
|
||||||
for (int i = 0, n = slots.size; i < n; i++) {
|
for (int i = 0, n = slots.size; i < n; i++) {
|
||||||
Slot slot = slots.get(i);
|
Slot slot = slots.get(i);
|
||||||
if (slot.data.name.equals(slotName)) {
|
if (slot.data.name.equals(slotName)) {
|
||||||
|
|||||||
@ -39,6 +39,7 @@ public class SkeletonData {
|
|||||||
bones.clear();
|
bones.clear();
|
||||||
slots.clear();
|
slots.clear();
|
||||||
skins.clear();
|
skins.clear();
|
||||||
|
animations.clear();
|
||||||
defaultSkin = null;
|
defaultSkin = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -122,9 +122,9 @@ public class SkeletonJson {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Skins.
|
// Skins.
|
||||||
OrderedMap<String, OrderedMap> slotMap = (OrderedMap)root.get("skins");
|
OrderedMap<String, OrderedMap> skinsMap = (OrderedMap)root.get("skins");
|
||||||
if (slotMap != null) {
|
if (skinsMap != null) {
|
||||||
for (Entry<String, OrderedMap> entry : slotMap.entries()) {
|
for (Entry<String, OrderedMap> entry : skinsMap.entries()) {
|
||||||
Skin skin = new Skin(entry.key);
|
Skin skin = new Skin(entry.key);
|
||||||
for (Entry<String, OrderedMap> slotEntry : ((OrderedMap<String, OrderedMap>)entry.value).entries()) {
|
for (Entry<String, OrderedMap> slotEntry : ((OrderedMap<String, OrderedMap>)entry.value).entries()) {
|
||||||
int slotIndex = skeletonData.findSlotIndex(slotEntry.key);
|
int slotIndex = skeletonData.findSlotIndex(slotEntry.key);
|
||||||
@ -209,12 +209,12 @@ public class SkeletonJson {
|
|||||||
RotateTimeline timeline = new RotateTimeline(values.size);
|
RotateTimeline timeline = new RotateTimeline(values.size);
|
||||||
timeline.setBoneIndex(boneIndex);
|
timeline.setBoneIndex(boneIndex);
|
||||||
|
|
||||||
int keyframeIndex = 0;
|
int frameIndex = 0;
|
||||||
for (OrderedMap valueMap : values) {
|
for (OrderedMap valueMap : values) {
|
||||||
float time = (Float)valueMap.get("time");
|
float time = (Float)valueMap.get("time");
|
||||||
timeline.setFrame(keyframeIndex, time, (Float)valueMap.get("angle"));
|
timeline.setFrame(frameIndex, time, (Float)valueMap.get("angle"));
|
||||||
readCurve(timeline, keyframeIndex, valueMap);
|
readCurve(timeline, frameIndex, valueMap);
|
||||||
keyframeIndex++;
|
frameIndex++;
|
||||||
}
|
}
|
||||||
timelines.add(timeline);
|
timelines.add(timeline);
|
||||||
duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]);
|
duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]);
|
||||||
@ -230,14 +230,14 @@ public class SkeletonJson {
|
|||||||
}
|
}
|
||||||
timeline.setBoneIndex(boneIndex);
|
timeline.setBoneIndex(boneIndex);
|
||||||
|
|
||||||
int keyframeIndex = 0;
|
int frameIndex = 0;
|
||||||
for (OrderedMap valueMap : values) {
|
for (OrderedMap valueMap : values) {
|
||||||
float time = (Float)valueMap.get("time");
|
float time = (Float)valueMap.get("time");
|
||||||
Float x = (Float)valueMap.get("x"), y = (Float)valueMap.get("y");
|
Float x = (Float)valueMap.get("x"), y = (Float)valueMap.get("y");
|
||||||
timeline.setFrame(keyframeIndex, time, x == null ? 0 : (x * timelineScale), y == null ? 0
|
timeline
|
||||||
: (y * timelineScale));
|
.setFrame(frameIndex, time, x == null ? 0 : (x * timelineScale), y == null ? 0 : (y * timelineScale));
|
||||||
readCurve(timeline, keyframeIndex, valueMap);
|
readCurve(timeline, frameIndex, valueMap);
|
||||||
keyframeIndex++;
|
frameIndex++;
|
||||||
}
|
}
|
||||||
timelines.add(timeline);
|
timelines.add(timeline);
|
||||||
duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]);
|
duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]);
|
||||||
@ -262,13 +262,13 @@ public class SkeletonJson {
|
|||||||
ColorTimeline timeline = new ColorTimeline(values.size);
|
ColorTimeline timeline = new ColorTimeline(values.size);
|
||||||
timeline.setSlotIndex(slotIndex);
|
timeline.setSlotIndex(slotIndex);
|
||||||
|
|
||||||
int keyframeIndex = 0;
|
int frameIndex = 0;
|
||||||
for (OrderedMap valueMap : values) {
|
for (OrderedMap valueMap : values) {
|
||||||
float time = (Float)valueMap.get("time");
|
float time = (Float)valueMap.get("time");
|
||||||
Color color = Color.valueOf((String)valueMap.get("color"));
|
Color color = Color.valueOf((String)valueMap.get("color"));
|
||||||
timeline.setFrame(keyframeIndex, time, color.r, color.g, color.b, color.a);
|
timeline.setFrame(frameIndex, time, color.r, color.g, color.b, color.a);
|
||||||
readCurve(timeline, keyframeIndex, valueMap);
|
readCurve(timeline, frameIndex, valueMap);
|
||||||
keyframeIndex++;
|
frameIndex++;
|
||||||
}
|
}
|
||||||
timelines.add(timeline);
|
timelines.add(timeline);
|
||||||
duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 5 - 5]);
|
duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 5 - 5]);
|
||||||
@ -277,10 +277,10 @@ public class SkeletonJson {
|
|||||||
AttachmentTimeline timeline = new AttachmentTimeline(values.size);
|
AttachmentTimeline timeline = new AttachmentTimeline(values.size);
|
||||||
timeline.setSlotIndex(slotIndex);
|
timeline.setSlotIndex(slotIndex);
|
||||||
|
|
||||||
int keyframeIndex = 0;
|
int frameIndex = 0;
|
||||||
for (OrderedMap valueMap : values) {
|
for (OrderedMap valueMap : values) {
|
||||||
float time = (Float)valueMap.get("time");
|
float time = (Float)valueMap.get("time");
|
||||||
timeline.setFrame(keyframeIndex++, time, (String)valueMap.get("name"));
|
timeline.setFrame(frameIndex++, time, (String)valueMap.get("name"));
|
||||||
}
|
}
|
||||||
timelines.add(timeline);
|
timelines.add(timeline);
|
||||||
duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
|
duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
|
||||||
@ -295,14 +295,14 @@ public class SkeletonJson {
|
|||||||
skeletonData.addAnimation(new Animation(name, timelines, duration));
|
skeletonData.addAnimation(new Animation(name, timelines, duration));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readCurve (CurveTimeline timeline, int keyframeIndex, OrderedMap valueMap) {
|
private void readCurve (CurveTimeline timeline, int frameIndex, OrderedMap valueMap) {
|
||||||
Object curveObject = valueMap.get("curve");
|
Object curveObject = valueMap.get("curve");
|
||||||
if (curveObject == null) return;
|
if (curveObject == null) return;
|
||||||
if (curveObject.equals("stepped"))
|
if (curveObject.equals("stepped"))
|
||||||
timeline.setStepped(keyframeIndex);
|
timeline.setStepped(frameIndex);
|
||||||
else if (curveObject instanceof Array) {
|
else if (curveObject instanceof Array) {
|
||||||
Array curve = (Array)curveObject;
|
Array curve = (Array)curveObject;
|
||||||
timeline.setCurve(keyframeIndex, (Float)curve.get(0), (Float)curve.get(1), (Float)curve.get(2), (Float)curve.get(3));
|
timeline.setCurve(frameIndex, (Float)curve.get(0), (Float)curve.get(1), (Float)curve.get(2), (Float)curve.get(3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -78,13 +78,25 @@ public class Skin {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Attach each attachment in this skin if the corresponding attachment in the old skin is currently attached. */
|
||||||
|
void attachAll (Skeleton skeleton, Skin oldSkin) {
|
||||||
|
for (Entry<Key, Attachment> entry : oldSkin.attachments.entries()) {
|
||||||
|
int slotIndex = entry.key.slotIndex;
|
||||||
|
Slot slot = skeleton.slots.get(slotIndex);
|
||||||
|
if (slot.attachment == entry.value) {
|
||||||
|
Attachment attachment = getAttachment(slotIndex, entry.key.name);
|
||||||
|
if (attachment != null) slot.setAttachment(attachment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static class Key {
|
static class Key {
|
||||||
int slotIndex;
|
int slotIndex;
|
||||||
String name;
|
String name;
|
||||||
int hashCode;
|
int hashCode;
|
||||||
|
|
||||||
public void set (int slotName, String name) {
|
public void set (int slotName, String name) {
|
||||||
if (name == null) throw new IllegalArgumentException("attachmentName cannot be null.");
|
if (name == null) throw new IllegalArgumentException("name cannot be null.");
|
||||||
this.slotIndex = slotName;
|
this.slotIndex = slotName;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
hashCode = 31 * (31 + name.hashCode()) + slotIndex;
|
hashCode = 31 * (31 + name.hashCode()) + slotIndex;
|
||||||
@ -106,16 +118,4 @@ public class Skin {
|
|||||||
return slotIndex + ":" + name;
|
return slotIndex + ":" + name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Attach each attachment in this skin if the corresponding attachment in the old skin is currently attached. */
|
|
||||||
void attachAll (Skeleton skeleton, Skin oldSkin) {
|
|
||||||
for (Entry<Key, Attachment> entry : oldSkin.attachments.entries()) {
|
|
||||||
int slotIndex = entry.key.slotIndex;
|
|
||||||
Slot slot = skeleton.slots.get(slotIndex);
|
|
||||||
if (slot.attachment == entry.value) {
|
|
||||||
Attachment attachment = getAttachment(slotIndex, entry.key.name);
|
|
||||||
if (attachment != null) slot.setAttachment(attachment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -49,7 +49,7 @@ public class Slot {
|
|||||||
this.data = data;
|
this.data = data;
|
||||||
this.skeleton = skeleton;
|
this.skeleton = skeleton;
|
||||||
this.bone = bone;
|
this.bone = bone;
|
||||||
color = new Color(1, 1, 1, 1);
|
color = new Color();
|
||||||
setToBindPose();
|
setToBindPose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user