Skip to content

Commit

Permalink
[as3][c][cpp][csharp][libgdx][lua][ts] Fixed loading transform constr…
Browse files Browse the repository at this point in the history
…aint timelines from JSON data.

* Fixed SkeletonJson not allocating enough memory for curves (maximum of 6 curves/key, not 4). Fixes email: FMfcgzGkbDfvckWcFxWlsTWzFhMXsTDw
* Removed SkeletonData and Skeleton methods: findBoneIndex, findSlotIndex. Bones and slots have an index field that should be used instead.
* Removed SkeletonData and Skeleton methods: find*ConstraintIndex. Only SkeletonJson needed these, they don't need to be in the public API.
* Used a local for number of frames where it was used many times.
  • Loading branch information
NathanSweet committed Sep 13, 2021
1 parent a8da830 commit dc9a6eb
Show file tree
Hide file tree
Showing 27 changed files with 346 additions and 547 deletions.
26 changes: 1 addition & 25 deletions spine-as3/spine-as3/src/spine/Skeleton.as
Expand Up @@ -411,18 +411,6 @@ package spine {
return null;
}

/** @return -1 if the bone was not found. */
public function findBoneIndex(boneName : String) : int {
if (boneName == null)
throw new ArgumentError("boneName cannot be null.");
var i : int = 0;
for each (var bone : Bone in bones) {
if (bone._data._name == boneName) return i;
i++;
}
return -1;
}

/** @return May be null. */
public function findSlot(slotName : String) : Slot {
if (slotName == null)
Expand All @@ -432,18 +420,6 @@ package spine {
return null;
}

/** @return -1 if the bone was not found. */
public function findSlotIndex(slotName : String) : int {
if (slotName == null)
throw new ArgumentError("slotName cannot be null.");
var i : int = 0;
for each (var slot : Slot in slots) {
if (slot._data._name == slotName) return i;
i++;
}
return -1;
}

public function get skin() : Skin {
return _skin;
}
Expand Down Expand Up @@ -486,7 +462,7 @@ package spine {

/** @return May be null. */
public function getAttachmentForSlotName(slotName : String, attachmentName : String) : Attachment {
return getAttachmentForSlotIndex(data.findSlotIndex(slotName), attachmentName);
return getAttachmentForSlotIndex(data.findSlot(slotName).index, attachmentName);
}

/** @return May be null. */
Expand Down
16 changes: 0 additions & 16 deletions spine-as3/spine-as3/src/spine/SkeletonData.as
Expand Up @@ -62,14 +62,6 @@ package spine {
return null;
}

/** @return -1 if the bone was not found. */
public function findBoneIndex(boneName : String) : int {
if (boneName == null) throw new ArgumentError("boneName cannot be null.");
for (var i : int = 0, n : int = bones.length; i < n; i++)
if (bones[i]._name == boneName) return i;
return -1;
}

// --- Slots.
/** @return May be null. */
public function findSlot(slotName : String) : SlotData {
Expand All @@ -81,14 +73,6 @@ package spine {
return null;
}

/** @return -1 if the bone was not found. */
public function findSlotIndex(slotName : String) : int {
if (slotName == null) throw new ArgumentError("slotName cannot be null.");
for (var i : int = 0, n : int = slots.length; i < n; i++)
if (slots[i]._name == slotName) return i;
return -1;
}

// --- Skins.
/** @return May be null. */
public function findSkin(skinName : String) : Skin {
Expand Down
61 changes: 33 additions & 28 deletions spine-as3/spine-as3/src/spine/SkeletonJson.as
Expand Up @@ -437,27 +437,29 @@ package spine {
var time : Number, time2 : Number;
var curve : Object;
var timelineName : String;
var i : int, n : int;
var i : int, n : int, frames : int;

// Slot timelines.
var slots : Object = map["slots"];
for (slotName in slots) {
slotMap = slots[slotName];
slotIndex = skeletonData.findSlotIndex(slotName);
slotIndex = skeletonData.findSlot(slotName).index;

for (timelineName in slotMap) {
timelineMap = slotMap[timelineName];
if (!timelineMap) continue;

frames = timelineMap.length;
if (timelineName == "attachment") {
var attachmentTimeline : AttachmentTimeline = new AttachmentTimeline(timelineMap.length, slotIndex);
for (frame = 0; frame < timelineMap.length; frame++) {
var attachmentTimeline : AttachmentTimeline = new AttachmentTimeline(frames, slotIndex);
for (frame = 0; frame < frames; frame++) {
keyMap = timelineMap[frame];
attachmentTimeline.setFrame(frame, getNumber(keyMap, "time", 0), keyMap.name);
}
timelines.push(attachmentTimeline);

} else if (timelineName == "rgba") {
var rgbaTimeline : RGBATimeline = new RGBATimeline(timelineMap.length, timelineMap.length << 2, slotIndex);
var rgbaTimeline : RGBATimeline = new RGBATimeline(frames, frames << 2, slotIndex);
keyMap = timelineMap[0];
time = getNumber(keyMap, "time", 0);
var rgba : Color = Color.fromString(keyMap.color);
Expand Down Expand Up @@ -485,7 +487,7 @@ package spine {
timelines.push(rgbaTimeline);

} else if (timelineName == "rgb") {
var rgbTimeline : RGBTimeline = new RGBTimeline(timelineMap.length, timelineMap.length * 3, slotIndex);
var rgbTimeline : RGBTimeline = new RGBTimeline(frames, frames * 3, slotIndex);
keyMap = timelineMap[0];
time = getNumber(keyMap, "time", 0);
var rgb : Color = Color.fromString(keyMap.color);
Expand All @@ -512,9 +514,9 @@ package spine {
timelines.push(rgbTimeline);

} else if (timelineName == "alpha") {
timelines.push(readTimeline(timelineMap, new AlphaTimeline(timelineMap.length, timelineMap.length, slotIndex), 0, 1));
timelines.push(readTimeline(timelineMap, new AlphaTimeline(frames, frames, slotIndex), 0, 1));
} else if (timelineName == "rgba2") {
var rgba2Timeline : RGBA2Timeline = new RGBA2Timeline(timelineMap.length, timelineMap.length * 7, slotIndex);
var rgba2Timeline : RGBA2Timeline = new RGBA2Timeline(frames, frames * 7, slotIndex);

keyMap = timelineMap[0];
time = getNumber(keyMap, "time", 0);
Expand Down Expand Up @@ -549,7 +551,7 @@ package spine {
timelines.push(rgba2Timeline);

} else if (timelineName == "rgb2") {
var rgb2Timeline : RGB2Timeline = new RGB2Timeline(timelineMap.length, timelineMap.length * 6, slotIndex);
var rgb2Timeline : RGB2Timeline = new RGB2Timeline(frames, frames * 6, slotIndex);

keyMap = timelineMap[0];
time = getNumber(keyMap, "time", 0);
Expand Down Expand Up @@ -590,42 +592,44 @@ package spine {
// Bone timelines.
var bones : Object = map["bones"];
for (var boneName : String in bones) {
var boneIndex : int = skeletonData.findBoneIndex(boneName);
if (boneIndex == -1) throw new Error("Bone not found: " + boneName);
var bone : BoneData = skeletonData.findBone(boneName);
if (!bone) throw new Error("Bone not found: " + boneName);
var boneIndex : int = bone.index;
var boneMap : Object = bones[boneName];

for (timelineName in boneMap) {
timelineMap = boneMap[timelineName];
if (timelineMap.length == 0) continue;
frames = timelineMap.length;
if (frames == 0) continue;

if (timelineName === "rotate") {
timelines.push(readTimeline(timelineMap, new RotateTimeline(timelineMap.length, timelineMap.length, boneIndex), 0, 1));
timelines.push(readTimeline(timelineMap, new RotateTimeline(frames, frames, boneIndex), 0, 1));
} else if (timelineName === "translate") {
var translateTimeline : TranslateTimeline = new TranslateTimeline(timelineMap.length, timelineMap.length << 1, boneIndex);
var translateTimeline : TranslateTimeline = new TranslateTimeline(frames, frames << 1, boneIndex);
timelines.push(readTimeline2(timelineMap, translateTimeline, "x", "y", 0, scale));
} else if (timelineName === "translatex") {
var translateXTimeline : TranslateXTimeline = new TranslateXTimeline(timelineMap.length, timelineMap.length, boneIndex);
var translateXTimeline : TranslateXTimeline = new TranslateXTimeline(frames, frames, boneIndex);
timelines.push(readTimeline(timelineMap, translateXTimeline, 0, scale));
} else if (timelineName === "translatey") {
var translateYTimeline : TranslateYTimeline = new TranslateYTimeline(timelineMap.length, timelineMap.length, boneIndex);
var translateYTimeline : TranslateYTimeline = new TranslateYTimeline(frames, frames, boneIndex);
timelines.push(readTimeline(timelineMap, translateYTimeline, 0, scale));
} else if (timelineName === "scale") {
var scaleTimeline : ScaleTimeline = new ScaleTimeline(timelineMap.length, timelineMap.length << 1, boneIndex);
var scaleTimeline : ScaleTimeline = new ScaleTimeline(frames, frames << 1, boneIndex);
timelines.push(readTimeline2(timelineMap, scaleTimeline, "x", "y", 1, 1));
} else if (timelineName === "scalex") {
var scaleXTimeline : ScaleXTimeline = new ScaleXTimeline(timelineMap.length, timelineMap.length, boneIndex);
var scaleXTimeline : ScaleXTimeline = new ScaleXTimeline(frames, frames, boneIndex);
timelines.push(readTimeline(timelineMap, scaleXTimeline, 1, 1));
} else if (timelineName === "scaley") {
var scaleYTimeline : ScaleYTimeline = new ScaleYTimeline(timelineMap.length, timelineMap.length, boneIndex);
var scaleYTimeline : ScaleYTimeline = new ScaleYTimeline(frames, frames, boneIndex);
timelines.push(readTimeline(timelineMap, scaleYTimeline, 1, 1));
} else if (timelineName === "shear") {
var shearTimeline : ShearTimeline = new ShearTimeline(timelineMap.length, timelineMap.length << 1, boneIndex);
var shearTimeline : ShearTimeline = new ShearTimeline(frames, frames << 1, boneIndex);
timelines.push(readTimeline2(timelineMap, shearTimeline, "x", "y", 0, 1));
} else if (timelineName === "shearx") {
var shearXTimeline : ShearXTimeline = new ShearXTimeline(timelineMap.length, timelineMap.length, boneIndex);
var shearXTimeline : ShearXTimeline = new ShearXTimeline(frames, frames, boneIndex);
timelines.push(readTimeline(timelineMap, shearXTimeline, 0, 1));
} else if (timelineName === "sheary") {
var shearYTimeline : ShearYTimeline = new ShearYTimeline(timelineMap.length, timelineMap.length, boneIndex);
var shearYTimeline : ShearYTimeline = new ShearYTimeline(frames, frames, boneIndex);
timelines.push(readTimeline(timelineMap, shearYTimeline, 0, 1));
} else
throw new Error("Invalid timeline type for a bone: " + timelineName + " (" + boneName + ")");
Expand Down Expand Up @@ -682,7 +686,7 @@ package spine {
if (!keyMap) continue;

var transformIndex : int = skeletonData.transformConstraints.indexOf(skeletonData.findTransformConstraint(transformName));
var transformTimeline : TransformConstraintTimeline = new TransformConstraintTimeline(timelineMap.length, timelineMap.length << 2, transformIndex);
var transformTimeline : TransformConstraintTimeline = new TransformConstraintTimeline(timelineMap.length, timelineMap.length * 6, transformIndex);

time = getNumber(keyMap, "time", 0);
mixRotate = getNumber(keyMap, "mixRotate", 1);
Expand Down Expand Up @@ -742,14 +746,15 @@ package spine {
keyMap = timelineMap[0];
if (!keyMap) continue;

frames = timelineMap.length;
if (timelineName === "position") {
var positionTimeline : PathConstraintPositionTimeline = new PathConstraintPositionTimeline(timelineMap.length, timelineMap.length, index);
var positionTimeline : PathConstraintPositionTimeline = new PathConstraintPositionTimeline(frames, frames, index);
timelines.push(readTimeline(timelineMap, positionTimeline, 0, pathData.positionMode == PositionMode.fixed ? scale : 1));
} else if (timelineName === "spacing") {
var spacingTimeline : PathConstraintSpacingTimeline = new PathConstraintSpacingTimeline(timelineMap.length, timelineMap.length, index);
var spacingTimeline : PathConstraintSpacingTimeline = new PathConstraintSpacingTimeline(frames, frames, index);
timelines.push(readTimeline(timelineMap, spacingTimeline, 0, pathData.spacingMode == SpacingMode.length || pathData.spacingMode == SpacingMode.fixed ? scale : 1));
} else if (timelineName === "mix") {
var mixTimeline : PathConstraintMixTimeline = new PathConstraintMixTimeline(timelineMap.size, timelineMap.size * 3, index);
var mixTimeline : PathConstraintMixTimeline = new PathConstraintMixTimeline(frames, frames * 3, index);
time = getNumber(keyMap, "time", 0);
mixRotate = getNumber(keyMap, "mixRotate", 1);
mixX = getNumber(keyMap, "mixX", 1);
Expand Down Expand Up @@ -790,7 +795,7 @@ package spine {
if (skin == null) throw new Error("Skin not found: " + deformName);
for (slotName in deformMap) {
slotMap = deformMap[slotName];
slotIndex = skeletonData.findSlotIndex(slotName);
slotIndex = skeletonData.findSlot(slotName).index;
if (slotIndex == -1) throw new Error("Slot not found: " + slotMap.name);
for (timelineName in slotMap) {
timelineMap = slotMap[timelineName];
Expand Down Expand Up @@ -859,7 +864,7 @@ package spine {
var unchanged : Vector.<int> = new Vector.<int>(slotCount - offsets.length, true);
var originalIndex : int = 0, unchangedIndex : int = 0;
for each (var offsetMap : Object in offsets) {
slotIndex = skeletonData.findSlotIndex(offsetMap["slot"]);
slotIndex = skeletonData.findSlot(offsetMap["slot"]).index;
if (slotIndex == -1) throw new Error("Slot not found: " + offsetMap["slot"]);
// Collect unchanged items.
while (originalIndex != slotIndex)
Expand Down
2 changes: 0 additions & 2 deletions spine-c/spine-c-unit-tests/README.md
Expand Up @@ -27,7 +27,6 @@ Depending on the test agent build environment, you should build the output solut
This build step should not execute if the previous step did not successfully complete.
Again, depending on the test agent build environment, you should have produced an executable. Run this executable.


## Usage
Make sure [CMake](https://cmake.org/download/) is installed.

Expand All @@ -40,7 +39,6 @@ cmake ..
### Win32 build
msbuild spine_unit_test.sln /t:spine_unit_test /p:Configuration="Debug" /p:Platform="Win32"


## Licensing
This Spine Runtime may only be used for personal or internal use, typically to evaluate Spine before purchasing. If you would like to incorporate a Spine Runtime into your applications, distribute software containing a Spine Runtime, or modify a Spine Runtime, then you will need a valid [Spine license](https://esotericsoftware.com/spine-purchase). Please see the [Spine Runtimes Software License](https://github.com/EsotericSoftware/spine-runtimes/blob/master/LICENSE) for detailed information.

Expand Down
4 changes: 0 additions & 4 deletions spine-c/spine-c/include/spine/Skeleton.h
Expand Up @@ -88,13 +88,9 @@ SP_API void spSkeleton_setSlotsToSetupPose(const spSkeleton *self);

/* Returns 0 if the bone was not found. */
SP_API spBone *spSkeleton_findBone(const spSkeleton *self, const char *boneName);
/* Returns -1 if the bone was not found. */
SP_API int spSkeleton_findBoneIndex(const spSkeleton *self, const char *boneName);

/* Returns 0 if the slot was not found. */
SP_API spSlot *spSkeleton_findSlot(const spSkeleton *self, const char *slotName);
/* Returns -1 if the slot was not found. */
SP_API int spSkeleton_findSlotIndex(const spSkeleton *self, const char *slotName);

/* Sets the skin used to look up attachments before looking in the SkeletonData defaultSkin. Attachments from the new skin are
* attached if the corresponding attachment from the old skin was attached. If there was no old skin, each slot's setup mode
Expand Down
10 changes: 0 additions & 10 deletions spine-c/spine-c/include/spine/SkeletonData.h
Expand Up @@ -87,12 +87,8 @@ SP_API void spSkeletonData_dispose(spSkeletonData *self);

SP_API spBoneData *spSkeletonData_findBone(const spSkeletonData *self, const char *boneName);

SP_API int spSkeletonData_findBoneIndex(const spSkeletonData *self, const char *boneName);

SP_API spSlotData *spSkeletonData_findSlot(const spSkeletonData *self, const char *slotName);

SP_API int spSkeletonData_findSlotIndex(const spSkeletonData *self, const char *slotName);

SP_API spSkin *spSkeletonData_findSkin(const spSkeletonData *self, const char *skinName);

SP_API spEventData *spSkeletonData_findEvent(const spSkeletonData *self, const char *eventName);
Expand All @@ -101,17 +97,11 @@ SP_API spAnimation *spSkeletonData_findAnimation(const spSkeletonData *self, con

SP_API spIkConstraintData *spSkeletonData_findIkConstraint(const spSkeletonData *self, const char *constraintName);

SP_API int spSkeletonData_findIkConstraintIndex(const spSkeletonData *self, const char *constraintName);

SP_API spTransformConstraintData *
spSkeletonData_findTransformConstraint(const spSkeletonData *self, const char *constraintName);

SP_API int spSkeletonData_findTransformConstraintIndex(const spSkeletonData *self, const char *constraintName);

SP_API spPathConstraintData *spSkeletonData_findPathConstraint(const spSkeletonData *self, const char *constraintName);

SP_API int spSkeletonData_findPathConstraintIndex(const spSkeletonData *self, const char *constraintName);

#ifdef __cplusplus
}
#endif
Expand Down
16 changes: 1 addition & 15 deletions spine-c/spine-c/src/spine/Skeleton.c
Expand Up @@ -531,27 +531,13 @@ spBone *spSkeleton_findBone(const spSkeleton *self, const char *boneName) {
return 0;
}

int spSkeleton_findBoneIndex(const spSkeleton *self, const char *boneName) {
int i;
for (i = 0; i < self->bonesCount; ++i)
if (strcmp(self->data->bones[i]->name, boneName) == 0) return i;
return -1;
}

spSlot *spSkeleton_findSlot(const spSkeleton *self, const char *slotName) {
int i;
for (i = 0; i < self->slotsCount; ++i)
if (strcmp(self->data->slots[i]->name, slotName) == 0) return self->slots[i];
return 0;
}

int spSkeleton_findSlotIndex(const spSkeleton *self, const char *slotName) {
int i;
for (i = 0; i < self->slotsCount; ++i)
if (strcmp(self->data->slots[i]->name, slotName) == 0) return i;
return -1;
}

int spSkeleton_setSkinByName(spSkeleton *self, const char *skinName) {
spSkin *skin;
if (!skinName) {
Expand Down Expand Up @@ -587,7 +573,7 @@ void spSkeleton_setSkin(spSkeleton *self, spSkin *newSkin) {

spAttachment *
spSkeleton_getAttachmentForSlotName(const spSkeleton *self, const char *slotName, const char *attachmentName) {
int slotIndex = spSkeletonData_findSlotIndex(self->data, slotName);
int slotIndex = spSkeletonData_findSlot(self->data, slotName)->index;
return spSkeleton_getAttachmentForSlotIndex(self, slotIndex, attachmentName);
}

Expand Down

0 comments on commit dc9a6eb

Please sign in to comment.