Add x and y to skeleton for positioning instead of using root bone.

This commit is contained in:
NathanSweet 2013-05-28 18:17:55 +02:00
parent 1f8cad35eb
commit e6d7cf8b26
21 changed files with 63 additions and 41 deletions

View File

@ -14,6 +14,8 @@ public class Skeleton {
public var time:Number; public var time:Number;
public var flipX:Boolean; public var flipX:Boolean;
public var flipY:Boolean; public var flipY:Boolean;
public var x:Number = 0;
public var y:Number = 0;
public function Skeleton (data:SkeletonData) { public function Skeleton (data:SkeletonData) {
if (data == null) if (data == null)

View File

@ -86,9 +86,9 @@ public dynamic class RegionAttachment extends Attachment {
offset[Y4] = localYCos + localX2Sin; offset[Y4] = localYCos + localX2Sin;
} }
public function computeVertices (bone:Bone, vertices:Vector.<Number>) : void { public function computeVertices (x:Number, y:Number, bone:Bone, vertices:Vector.<Number>) : void {
var x:Number = bone.worldX; x += bone.worldX;
var y:Number = bone.worldY; y += bone.worldY;
var m00:Number = bone.m00; var m00:Number = bone.m00;
var m01:Number = bone.m01; var m01:Number = bone.m01;
var m10:Number = bone.m10; var m10:Number = bone.m10;

View File

@ -55,7 +55,7 @@ struct RegionAttachment {
RegionAttachment* RegionAttachment_create (const char* name); RegionAttachment* RegionAttachment_create (const char* name);
void RegionAttachment_setUVs (RegionAttachment* self, float u, float v, float u2, float v2, int/*bool*/rotate); void RegionAttachment_setUVs (RegionAttachment* self, float u, float v, float u2, float v2, int/*bool*/rotate);
void RegionAttachment_updateOffset (RegionAttachment* self); void RegionAttachment_updateOffset (RegionAttachment* self);
void RegionAttachment_computeVertices (RegionAttachment* self, Slot* slot, float* vertices); void RegionAttachment_computeVertices (RegionAttachment* self, float x, float y, Bone* bone, float* vertices);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -50,6 +50,7 @@ struct Skeleton {
float r, g, b, a; float r, g, b, a;
float time; float time;
int/*bool*/flipX, flipY; int/*bool*/flipX, flipY;
float x, y;
}; };
Skeleton* Skeleton_create (SkeletonData* data); Skeleton* Skeleton_create (SkeletonData* data);

View File

@ -90,15 +90,16 @@ void RegionAttachment_updateOffset (RegionAttachment* self) {
self->offset[VERTEX_Y4] = localYCos + localX2Sin; self->offset[VERTEX_Y4] = localYCos + localX2Sin;
} }
void RegionAttachment_computeVertices (RegionAttachment* self, Slot* slot, float* vertices) { void RegionAttachment_computeVertices (RegionAttachment* self, float x, float y, Bone* bone, float* vertices) {
float* offset = self->offset; float* offset = self->offset;
Bone* bone = slot->bone; x += bone->worldX;
vertices[VERTEX_X1] = offset[VERTEX_X1] * bone->m00 + offset[VERTEX_Y1] * bone->m01 + bone->worldX; y += bone->worldY;
vertices[VERTEX_Y1] = offset[VERTEX_X1] * bone->m10 + offset[VERTEX_Y1] * bone->m11 + bone->worldY; vertices[VERTEX_X1] = offset[VERTEX_X1] * bone->m00 + offset[VERTEX_Y1] * bone->m01 + x;
vertices[VERTEX_X2] = offset[VERTEX_X2] * bone->m00 + offset[VERTEX_Y2] * bone->m01 + bone->worldX; vertices[VERTEX_Y1] = offset[VERTEX_X1] * bone->m10 + offset[VERTEX_Y1] * bone->m11 + y;
vertices[VERTEX_Y2] = offset[VERTEX_X2] * bone->m10 + offset[VERTEX_Y2] * bone->m11 + bone->worldY; vertices[VERTEX_X2] = offset[VERTEX_X2] * bone->m00 + offset[VERTEX_Y2] * bone->m01 + x;
vertices[VERTEX_X3] = offset[VERTEX_X3] * bone->m00 + offset[VERTEX_Y3] * bone->m01 + bone->worldX; vertices[VERTEX_Y2] = offset[VERTEX_X2] * bone->m10 + offset[VERTEX_Y2] * bone->m11 + y;
vertices[VERTEX_Y3] = offset[VERTEX_X3] * bone->m10 + offset[VERTEX_Y3] * bone->m11 + bone->worldY; vertices[VERTEX_X3] = offset[VERTEX_X3] * bone->m00 + offset[VERTEX_Y3] * bone->m01 + x;
vertices[VERTEX_X4] = offset[VERTEX_X4] * bone->m00 + offset[VERTEX_Y4] * bone->m01 + bone->worldX; vertices[VERTEX_Y3] = offset[VERTEX_X3] * bone->m10 + offset[VERTEX_Y3] * bone->m11 + y;
vertices[VERTEX_Y4] = offset[VERTEX_X4] * bone->m10 + offset[VERTEX_Y4] * bone->m11 + bone->worldY; vertices[VERTEX_X4] = offset[VERTEX_X4] * bone->m00 + offset[VERTEX_Y4] * bone->m01 + x;
vertices[VERTEX_Y4] = offset[VERTEX_X4] * bone->m10 + offset[VERTEX_Y4] * bone->m11 + y;
} }

View File

@ -219,7 +219,7 @@
Slot* slot = _skeleton->slots[i]; Slot* slot = _skeleton->slots[i];
if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue; if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
RegionAttachment* attachment = (RegionAttachment*)slot->attachment; RegionAttachment* attachment = (RegionAttachment*)slot->attachment;
RegionAttachment_computeVertices(attachment, slot, vertices); RegionAttachment_computeVertices(attachment, slot->skeleton->x, slot->skeleton->y, slot->bone, vertices);
minX = fmin(minX, vertices[VERTEX_X1] * scaleX); minX = fmin(minX, vertices[VERTEX_X1] * scaleX);
minY = fmin(minY, vertices[VERTEX_Y1] * scaleY); minY = fmin(minY, vertices[VERTEX_Y1] * scaleY);
maxX = fmax(maxX, vertices[VERTEX_X1] * scaleX); maxX = fmax(maxX, vertices[VERTEX_X1] * scaleX);

View File

@ -48,7 +48,7 @@ char* _Util_readFile (const char* path, int* length) {
void RegionAttachment_updateQuad (RegionAttachment* self, Slot* slot, ccV3F_C4B_T2F_Quad* quad, bool premultipliedAlpha) { void RegionAttachment_updateQuad (RegionAttachment* self, Slot* slot, ccV3F_C4B_T2F_Quad* quad, bool premultipliedAlpha) {
float vertices[8]; float vertices[8];
RegionAttachment_computeVertices(self, slot, vertices); RegionAttachment_computeVertices(self, slot->skeleton->x, slot->skeleton->y, slot->bone, vertices);
GLubyte r = slot->skeleton->r * slot->r * 255; GLubyte r = slot->skeleton->r * slot->r * 255;
GLubyte g = slot->skeleton->g * slot->g * 255; GLubyte g = slot->skeleton->g * slot->g * 255;

View File

@ -212,7 +212,7 @@ CCRect CCSkeleton::boundingBox () {
Slot* slot = skeleton->slots[i]; Slot* slot = skeleton->slots[i];
if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue; if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
RegionAttachment* attachment = (RegionAttachment*)slot->attachment; RegionAttachment* attachment = (RegionAttachment*)slot->attachment;
RegionAttachment_computeVertices(attachment, slot, vertices); RegionAttachment_computeVertices(attachment, slot->skeleton->x, slot->skeleton->y, slot->bone, vertices);
minX = min(minX, vertices[VERTEX_X1] * scaleX); minX = min(minX, vertices[VERTEX_X1] * scaleX);
minY = min(minY, vertices[VERTEX_Y1] * scaleY); minY = min(minY, vertices[VERTEX_Y1] * scaleY);
maxX = max(maxX, vertices[VERTEX_X1] * scaleX); maxX = max(maxX, vertices[VERTEX_X1] * scaleX);

View File

@ -53,7 +53,7 @@ char* _Util_readFile (const char* path, int* length) {
void RegionAttachment_updateQuad (RegionAttachment* self, Slot* slot, ccV3F_C4B_T2F_Quad* quad, bool premultipliedAlpha) { void RegionAttachment_updateQuad (RegionAttachment* self, Slot* slot, ccV3F_C4B_T2F_Quad* quad, bool premultipliedAlpha) {
float vertices[8]; float vertices[8];
RegionAttachment_computeVertices(self, slot, vertices); RegionAttachment_computeVertices(self, slot->skeleton->x, slot->skeleton->y, slot->bone, vertices);
GLubyte r = slot->skeleton->r * slot->r * 255; GLubyte r = slot->skeleton->r * slot->r * 255;
GLubyte g = slot->skeleton->g * slot->g * 255; GLubyte g = slot->skeleton->g * slot->g * 255;

View File

@ -122,9 +122,9 @@ namespace Spine {
offset[Y4] = localYCos + localX2Sin; offset[Y4] = localYCos + localX2Sin;
} }
public void ComputeVertices (Bone bone, float[] vertices) { public void ComputeVertices (float x, float y, Bone bone, float[] vertices) {
float x = bone.WorldX; x += bone.WorldX;
float y = bone.WorldY; y += bone.WorldY;
float m00 = bone.M00; float m00 = bone.M00;
float m01 = bone.M01; float m01 = bone.M01;
float m10 = bone.M10; float m10 = bone.M10;

View File

@ -61,7 +61,6 @@ namespace Spine {
if (parent != null) { if (parent != null) {
WorldX = X * parent.M00 + Y * parent.M01 + parent.WorldX; WorldX = X * parent.M00 + Y * parent.M01 + parent.WorldX;
WorldY = X * parent.M10 + Y * parent.M11 + parent.WorldY; WorldY = X * parent.M10 + Y * parent.M11 + parent.WorldY;
if (Data.InheritScale) { if (Data.InheritScale) {
WorldScaleX = parent.WorldScaleX * ScaleX; WorldScaleX = parent.WorldScaleX * ScaleX;
WorldScaleY = parent.WorldScaleY * ScaleY; WorldScaleY = parent.WorldScaleY * ScaleY;
@ -69,13 +68,10 @@ namespace Spine {
WorldScaleX = ScaleX; WorldScaleX = ScaleX;
WorldScaleY = ScaleY; WorldScaleY = ScaleY;
} }
WorldRotation = Data.InheritRotation ? parent.WorldRotation + Rotation : Rotation; WorldRotation = Data.InheritRotation ? parent.WorldRotation + Rotation : Rotation;
} else { } else {
WorldX = flipX ? -X : X;
WorldX = X; WorldY = flipY ? -Y : Y;
WorldY = Y;
WorldScaleX = ScaleX; WorldScaleX = ScaleX;
WorldScaleY = ScaleY; WorldScaleY = ScaleY;
WorldRotation = Rotation; WorldRotation = Rotation;

View File

@ -45,6 +45,8 @@ namespace Spine {
return Bones.Count == 0 ? null : Bones[0]; return Bones.Count == 0 ? null : Bones[0];
} }
} }
public float X { get; set; }
public float Y { get; set; }
public Skeleton (SkeletonData data) { public Skeleton (SkeletonData data) {
if (data == null) throw new ArgumentNullException("data cannot be null."); if (data == null) throw new ArgumentNullException("data cannot be null.");

View File

@ -550,6 +550,7 @@ spine.Skeleton = function (skeletonData) {
} }
}; };
spine.Skeleton.prototype = { spine.Skeleton.prototype = {
x: 0, y: 0,
skin: null, skin: null,
r: 1, g: 1, b: 1, a: 1, r: 1, g: 1, b: 1, a: 1,
time: 0, time: 0,
@ -725,9 +726,9 @@ spine.RegionAttachment.prototype = {
offset[6/*X4*/] = localX2Cos - localYSin; offset[6/*X4*/] = localX2Cos - localYSin;
offset[7/*Y4*/] = localYCos + localX2Sin; offset[7/*Y4*/] = localYCos + localX2Sin;
}, },
computeVertices: function (bone, vertices) { computeVertices: function (x, y, bone, vertices) {
var x = bone.worldX; x += bone.worldX;
var y = bone.worldY; y += bone.worldY;
var m00 = bone.m00; var m00 = bone.m00;
var m01 = bone.m01; var m01 = bone.m01;
var m10 = bone.m10; var m10 = bone.m10;

View File

@ -131,7 +131,7 @@ function drawSkeleton (batch, skeleton) {
var slot = drawOrder[i]; var slot = drawOrder[i];
var attachment = slot.attachment; var attachment = slot.attachment;
if (!(attachment instanceof spine.RegionAttachment)) continue; if (!(attachment instanceof spine.RegionAttachment)) continue;
attachment.computeVertices(slot.bone, vertices); attachment.computeVertices(skeleton.x, skeleton.y, slot.bone, vertices);
batch.add( batch.add(
attachment.rendererObject.page.rendererObject, attachment.rendererObject.page.rendererObject,
vertices[0], vertices[1], vertices[0], vertices[1],

View File

@ -78,8 +78,8 @@ public class Bone {
} }
worldRotation = data.inheritRotation ? parent.worldRotation + rotation : rotation; worldRotation = data.inheritRotation ? parent.worldRotation + rotation : rotation;
} else { } else {
worldX = x; worldX = flipX ? -x : x;
worldY = y; worldY = flipY ? -y : y;
worldScaleX = scaleX; worldScaleX = scaleX;
worldScaleY = scaleY; worldScaleY = scaleY;
worldRotation = rotation; worldRotation = rotation;

View File

@ -39,6 +39,7 @@ public class Skeleton {
final Color color; final Color color;
float time; float time;
boolean flipX, flipY; boolean flipX, flipY;
float x, y;
public Skeleton (SkeletonData data) { public Skeleton (SkeletonData data) {
if (data == null) throw new IllegalArgumentException("data cannot be null."); if (data == null) throw new IllegalArgumentException("data cannot be null.");
@ -256,6 +257,22 @@ public class Skeleton {
this.flipY = flipY; this.flipY = flipY;
} }
public float getX () {
return x;
}
public void setX (float x) {
this.x = x;
}
public float getY () {
return y;
}
public void setY (float y) {
this.y = y;
}
public float getTime () { public float getTime () {
return time; return time;
} }

View File

@ -26,6 +26,7 @@
package com.esotericsoftware.spine.attachments; package com.esotericsoftware.spine.attachments;
import com.esotericsoftware.spine.Bone; import com.esotericsoftware.spine.Bone;
import com.esotericsoftware.spine.Skeleton;
import com.esotericsoftware.spine.Slot; import com.esotericsoftware.spine.Slot;
import static com.badlogic.gdx.graphics.g2d.SpriteBatch.*; import static com.badlogic.gdx.graphics.g2d.SpriteBatch.*;
@ -130,7 +131,8 @@ public class RegionAttachment extends Attachment {
} }
public void updateVertices (Slot slot) { public void updateVertices (Slot slot) {
Color skeletonColor = slot.getSkeleton().getColor(); Skeleton skeleton = slot.getSkeleton();
Color skeletonColor = skeleton.getColor();
Color slotColor = slot.getColor(); Color slotColor = slot.getColor();
float color = NumberUtils.intToFloatColor( // float color = NumberUtils.intToFloatColor( //
((int)(255 * skeletonColor.a * slotColor.a) << 24) // ((int)(255 * skeletonColor.a * slotColor.a) << 24) //
@ -145,8 +147,8 @@ public class RegionAttachment extends Attachment {
float[] offset = this.offset; float[] offset = this.offset;
Bone bone = slot.getBone(); Bone bone = slot.getBone();
float x = bone.getWorldX(); float x = bone.getWorldX() + skeleton.getX();
float y = bone.getWorldY(); float y = bone.getWorldY() + skeleton.getY();
float m00 = bone.getM00(); float m00 = bone.getM00();
float m01 = bone.getM01(); float m01 = bone.getM01();
float m10 = bone.getM10(); float m10 = bone.getM10();

View File

@ -84,7 +84,7 @@ void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const {
Attachment* attachment = slot->attachment; Attachment* attachment = slot->attachment;
if (!attachment || attachment->type != ATTACHMENT_REGION) continue; if (!attachment || attachment->type != ATTACHMENT_REGION) continue;
RegionAttachment* regionAttachment = (RegionAttachment*)attachment; RegionAttachment* regionAttachment = (RegionAttachment*)attachment;
RegionAttachment_computeVertices(regionAttachment, slot, vertexPositions); RegionAttachment_computeVertices(regionAttachment, slot->skeleton->x, slot->skeleton->y, slot->bone, vertexPositions);
Uint8 r = skeleton->r * slot->r * 255; Uint8 r = skeleton->r * slot->r * 255;
Uint8 g = skeleton->g * slot->g * 255; Uint8 g = skeleton->g * slot->g * 255;

View File

@ -41,7 +41,7 @@ public class SkeletonSprite extends DisplayObject implements IAnimatable {
var regionAttachment:RegionAttachment = slot.attachment as RegionAttachment; var regionAttachment:RegionAttachment = slot.attachment as RegionAttachment;
if (regionAttachment != null) { if (regionAttachment != null) {
var vertices:Vector.<Number> = this.vertices; var vertices:Vector.<Number> = this.vertices;
regionAttachment.computeVertices(slot.bone, vertices); regionAttachment.computeVertices(skeleton.x, skeleton.y, slot.bone, vertices);
var r:Number = skeleton.r * slot.r; var r:Number = skeleton.r * slot.r;
var g:Number = skeleton.g * slot.g; var g:Number = skeleton.g * slot.g;
var b:Number = skeleton.b * slot.b; var b:Number = skeleton.b * slot.b;
@ -99,7 +99,7 @@ public class SkeletonSprite extends DisplayObject implements IAnimatable {
continue; continue;
var vertices:Vector.<Number> = this.vertices; var vertices:Vector.<Number> = this.vertices;
regionAttachment.computeVertices(slot.bone, vertices); regionAttachment.computeVertices(skeleton.x, skeleton.y, slot.bone, vertices);
value = vertices[0]; value = vertices[0];
if (value < minX) if (value < minX)

View File

@ -78,7 +78,7 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
if (attachment is RegionAttachment) { if (attachment is RegionAttachment) {
RegionAttachment regionAttachment = attachment as RegionAttachment; RegionAttachment regionAttachment = attachment as RegionAttachment;
regionAttachment.ComputeVertices(slot.Bone,vertexPositions); regionAttachment.ComputeVertices(skeleton.X, skeleton.Y, slot.Bone, vertexPositions);
int vertexIndex = quadIndex * 4; int vertexIndex = quadIndex * 4;
vertices[vertexIndex + 0] = new Vector3(vertexPositions[RegionAttachment.X1],vertexPositions[RegionAttachment.Y1],0); vertices[vertexIndex + 0] = new Vector3(vertexPositions[RegionAttachment.X1],vertexPositions[RegionAttachment.Y1],0);

View File

@ -101,7 +101,7 @@ namespace Spine {
item.vertexTR.Color.A = a; item.vertexTR.Color.A = a;
float[] vertices = this.vertices; float[] vertices = this.vertices;
regionAttachment.ComputeVertices(slot.Bone, vertices); regionAttachment.ComputeVertices(skeleton.X, skeleton.Y, slot.Bone, vertices);
item.vertexTL.Position.X = vertices[RegionAttachment.X1]; item.vertexTL.Position.X = vertices[RegionAttachment.X1];
item.vertexTL.Position.Y = vertices[RegionAttachment.Y1]; item.vertexTL.Position.Y = vertices[RegionAttachment.Y1];
item.vertexTL.Position.Z = 0; item.vertexTL.Position.Z = 0;