mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-06 23:34:53 +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) {
|
||||
for (int i = 0, last = values.length - step; i <= last; i += step) {
|
||||
if (values[i] <= target) continue;
|
||||
return i;
|
||||
}
|
||||
for (int i = 0, last = values.length - step; i <= last; i += step)
|
||||
if (values[i] > target) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -116,7 +114,7 @@ public class Animation {
|
||||
}
|
||||
|
||||
/** 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 STEPPED = -1;
|
||||
static private final int BEZIER_SEGMENTS = 10;
|
||||
@ -220,11 +218,11 @@ public class Animation {
|
||||
return frames;
|
||||
}
|
||||
|
||||
/** Sets the time and value of the specified keyframe. */
|
||||
public void setFrame (int frameIndex, float time, float value) {
|
||||
/** Sets the time and angle of the specified keyframe. */
|
||||
public void setFrame (int frameIndex, float time, float angle) {
|
||||
frameIndex *= 2;
|
||||
frames[frameIndex] = time;
|
||||
frames[frameIndex + 1] = value;
|
||||
frames[frameIndex + 1] = angle;
|
||||
}
|
||||
|
||||
public void apply (Skeleton skeleton, float time, float alpha) {
|
||||
|
||||
@ -35,7 +35,7 @@ public class Bone {
|
||||
final Bone parent;
|
||||
float x, y;
|
||||
float rotation;
|
||||
float scaleX = 1, scaleY = 1;
|
||||
float scaleX, scaleY;
|
||||
|
||||
float m00, m01, worldX; // a b x
|
||||
float m10, m11, worldY; // c d y
|
||||
|
||||
@ -251,6 +251,7 @@ public class Skeleton {
|
||||
/** @param attachmentName May be null. */
|
||||
public void setAttachment (String slotName, String attachmentName) {
|
||||
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++) {
|
||||
Slot slot = slots.get(i);
|
||||
if (slot.data.name.equals(slotName)) {
|
||||
|
||||
@ -39,6 +39,7 @@ public class SkeletonData {
|
||||
bones.clear();
|
||||
slots.clear();
|
||||
skins.clear();
|
||||
animations.clear();
|
||||
defaultSkin = null;
|
||||
}
|
||||
|
||||
|
||||
@ -122,9 +122,9 @@ public class SkeletonJson {
|
||||
}
|
||||
|
||||
// Skins.
|
||||
OrderedMap<String, OrderedMap> slotMap = (OrderedMap)root.get("skins");
|
||||
if (slotMap != null) {
|
||||
for (Entry<String, OrderedMap> entry : slotMap.entries()) {
|
||||
OrderedMap<String, OrderedMap> skinsMap = (OrderedMap)root.get("skins");
|
||||
if (skinsMap != null) {
|
||||
for (Entry<String, OrderedMap> entry : skinsMap.entries()) {
|
||||
Skin skin = new Skin(entry.key);
|
||||
for (Entry<String, OrderedMap> slotEntry : ((OrderedMap<String, OrderedMap>)entry.value).entries()) {
|
||||
int slotIndex = skeletonData.findSlotIndex(slotEntry.key);
|
||||
@ -209,12 +209,12 @@ public class SkeletonJson {
|
||||
RotateTimeline timeline = new RotateTimeline(values.size);
|
||||
timeline.setBoneIndex(boneIndex);
|
||||
|
||||
int keyframeIndex = 0;
|
||||
int frameIndex = 0;
|
||||
for (OrderedMap valueMap : values) {
|
||||
float time = (Float)valueMap.get("time");
|
||||
timeline.setFrame(keyframeIndex, time, (Float)valueMap.get("angle"));
|
||||
readCurve(timeline, keyframeIndex, valueMap);
|
||||
keyframeIndex++;
|
||||
timeline.setFrame(frameIndex, time, (Float)valueMap.get("angle"));
|
||||
readCurve(timeline, frameIndex, valueMap);
|
||||
frameIndex++;
|
||||
}
|
||||
timelines.add(timeline);
|
||||
duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]);
|
||||
@ -230,14 +230,14 @@ public class SkeletonJson {
|
||||
}
|
||||
timeline.setBoneIndex(boneIndex);
|
||||
|
||||
int keyframeIndex = 0;
|
||||
int frameIndex = 0;
|
||||
for (OrderedMap valueMap : values) {
|
||||
float time = (Float)valueMap.get("time");
|
||||
Float x = (Float)valueMap.get("x"), y = (Float)valueMap.get("y");
|
||||
timeline.setFrame(keyframeIndex, time, x == null ? 0 : (x * timelineScale), y == null ? 0
|
||||
: (y * timelineScale));
|
||||
readCurve(timeline, keyframeIndex, valueMap);
|
||||
keyframeIndex++;
|
||||
timeline
|
||||
.setFrame(frameIndex, time, x == null ? 0 : (x * timelineScale), y == null ? 0 : (y * timelineScale));
|
||||
readCurve(timeline, frameIndex, valueMap);
|
||||
frameIndex++;
|
||||
}
|
||||
timelines.add(timeline);
|
||||
duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]);
|
||||
@ -262,13 +262,13 @@ public class SkeletonJson {
|
||||
ColorTimeline timeline = new ColorTimeline(values.size);
|
||||
timeline.setSlotIndex(slotIndex);
|
||||
|
||||
int keyframeIndex = 0;
|
||||
int frameIndex = 0;
|
||||
for (OrderedMap valueMap : values) {
|
||||
float time = (Float)valueMap.get("time");
|
||||
Color color = Color.valueOf((String)valueMap.get("color"));
|
||||
timeline.setFrame(keyframeIndex, time, color.r, color.g, color.b, color.a);
|
||||
readCurve(timeline, keyframeIndex, valueMap);
|
||||
keyframeIndex++;
|
||||
timeline.setFrame(frameIndex, time, color.r, color.g, color.b, color.a);
|
||||
readCurve(timeline, frameIndex, valueMap);
|
||||
frameIndex++;
|
||||
}
|
||||
timelines.add(timeline);
|
||||
duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 5 - 5]);
|
||||
@ -277,10 +277,10 @@ public class SkeletonJson {
|
||||
AttachmentTimeline timeline = new AttachmentTimeline(values.size);
|
||||
timeline.setSlotIndex(slotIndex);
|
||||
|
||||
int keyframeIndex = 0;
|
||||
int frameIndex = 0;
|
||||
for (OrderedMap valueMap : values) {
|
||||
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);
|
||||
duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
|
||||
@ -295,14 +295,14 @@ public class SkeletonJson {
|
||||
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");
|
||||
if (curveObject == null) return;
|
||||
if (curveObject.equals("stepped"))
|
||||
timeline.setStepped(keyframeIndex);
|
||||
timeline.setStepped(frameIndex);
|
||||
else if (curveObject instanceof Array) {
|
||||
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;
|
||||
}
|
||||
|
||||
/** 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 {
|
||||
int slotIndex;
|
||||
String name;
|
||||
int hashCode;
|
||||
|
||||
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.name = name;
|
||||
hashCode = 31 * (31 + name.hashCode()) + slotIndex;
|
||||
@ -106,16 +118,4 @@ public class Skin {
|
||||
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.skeleton = skeleton;
|
||||
this.bone = bone;
|
||||
color = new Color(1, 1, 1, 1);
|
||||
color = new Color();
|
||||
setToBindPose();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user