diff --git a/spine-csharp/src/TransformConstraint.cs b/spine-csharp/src/TransformConstraint.cs index b229e2865..a9be6c990 100644 --- a/spine-csharp/src/TransformConstraint.cs +++ b/spine-csharp/src/TransformConstraint.cs @@ -31,21 +31,19 @@ using System; namespace Spine { + /// + /// + /// Stores the current pose for a transform constraint. A transform constraint adjusts the world transform of the constrained + /// bones to match that of the target bone. + /// + /// See Transform constraints in the Spine User Guide. + /// public class TransformConstraint : IConstraint { internal TransformConstraintData data; internal ExposedList bones; internal Bone target; internal float rotateMix, translateMix, scaleMix, shearMix; - public TransformConstraintData Data { get { return data; } } - public int Order { get { return data.order; } } - public ExposedList Bones { get { return bones; } } - public Bone Target { get { return target; } set { target = value; } } - public float RotateMix { get { return rotateMix; } set { rotateMix = value; } } - public float TranslateMix { get { return translateMix; } set { translateMix = value; } } - public float ScaleMix { get { return scaleMix; } set { scaleMix = value; } } - public float ShearMix { get { return shearMix; } set { shearMix = value; } } - public TransformConstraint (TransformConstraintData data, Skeleton skeleton) { if (data == null) throw new ArgumentNullException("data", "data cannot be null."); if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null."); @@ -57,11 +55,27 @@ namespace Spine { bones = new ExposedList(); foreach (BoneData boneData in data.bones) - bones.Add (skeleton.FindBone (boneData.name)); + bones.Add (skeleton.FindBone(boneData.name)); target = skeleton.FindBone(data.target.name); } + /// Copy constructor. + public TransformConstraint (TransformConstraint constraint, Skeleton skeleton) { + if (constraint == null) throw new ArgumentNullException("constraint cannot be null."); + if (skeleton == null) throw new ArgumentNullException("skeleton cannot be null."); + data = constraint.data; + bones = new ExposedList(constraint.Bones.Count); + foreach (Bone bone in constraint.Bones) + bones.Add(skeleton.Bones.Items[bone.data.index]); + target = skeleton.Bones.Items[constraint.target.data.index]; + rotateMix = constraint.rotateMix; + translateMix = constraint.translateMix; + scaleMix = constraint.scaleMix; + shearMix = constraint.shearMix; + } + + /// Applies the constraint to the constrained bones. public void Apply () { Update(); } @@ -116,13 +130,11 @@ namespace Spine { if (scaleMix > 0) { float s = (float)Math.Sqrt(bone.a * bone.a + bone.c * bone.c); - //float ts = (float)Math.sqrt(ta * ta + tc * tc); - if (s > 0.00001f) s = (s + ((float)Math.Sqrt(ta * ta + tc * tc) - s + data.offsetScaleX) * scaleMix) / s; + if (s != 0) s = (s + ((float)Math.Sqrt(ta * ta + tc * tc) - s + data.offsetScaleX) * scaleMix) / s; bone.a *= s; bone.c *= s; s = (float)Math.Sqrt(bone.b * bone.b + bone.d * bone.d); - //ts = (float)Math.Sqrt(tb * tb + td * td); - if (s > 0.00001f) s = (s + ((float)Math.Sqrt(tb * tb + td * td) - s + data.offsetScaleY) * scaleMix) / s; + if (s != 0) s = (s + ((float)Math.Sqrt(tb * tb + td * td) - s + data.offsetScaleY) * scaleMix) / s; bone.b *= s; bone.d *= s; modified = true; @@ -231,15 +243,15 @@ namespace Spine { float scaleX = bone.ascaleX, scaleY = bone.ascaleY; if (scaleMix != 0) { - if (scaleX > 0.00001f) scaleX = (scaleX + (target.ascaleX - scaleX + data.offsetScaleX) * scaleMix) / scaleX; - if (scaleY > 0.00001f) scaleY = (scaleY + (target.ascaleY - scaleY + data.offsetScaleY) * scaleMix) / scaleY; + if (scaleX != 0) scaleX = (scaleX + (target.ascaleX - scaleX + data.offsetScaleX) * scaleMix) / scaleX; + if (scaleY != 0) scaleY = (scaleY + (target.ascaleY - scaleY + data.offsetScaleY) * scaleMix) / scaleY; } float shearY = bone.ashearY; if (shearMix != 0) { float r = target.ashearY - shearY + data.offsetShearY; r -= (16384 - (int)(16384.499999999996 - r / 360)) * 360; - bone.shearY += r * shearMix; + shearY += r * shearMix; } bone.UpdateWorldTransform(x, y, rotation, scaleX, scaleY, bone.ashearX, shearY); @@ -266,8 +278,8 @@ namespace Spine { float scaleX = bone.ascaleX, scaleY = bone.ascaleY; if (scaleMix != 0) { - if (scaleX > 0.00001f) scaleX *= ((target.ascaleX - 1 + data.offsetScaleX) * scaleMix) + 1; - if (scaleY > 0.00001f) scaleY *= ((target.ascaleY - 1 + data.offsetScaleY) * scaleMix) + 1; + scaleX *= ((target.ascaleX - 1 + data.offsetScaleX) * scaleMix) + 1; + scaleY *= ((target.ascaleY - 1 + data.offsetScaleY) * scaleMix) + 1; } float shearY = bone.ashearY; @@ -277,6 +289,22 @@ namespace Spine { } } + public int Order { get { return data.order; } } + /// The bones that will be modified by this transform constraint. + public ExposedList Bones { get { return bones; } } + /// The target bone whose world transform will be copied to the constrained bones. + public Bone Target { get { return target; } set { target = value; } } + /// A percentage (0-1) that controls the mix between the constrained and unconstrained rotations. + public float RotateMix { get { return rotateMix; } set { rotateMix = value; } } + /// A percentage (0-1) that controls the mix between the constrained and unconstrained translations. + public float TranslateMix { get { return translateMix; } set { translateMix = value; } } + /// A percentage (0-1) that controls the mix between the constrained and unconstrained scales. + public float ScaleMix { get { return scaleMix; } set { scaleMix = value; } } + /// A percentage (0-1) that controls the mix between the constrained and unconstrained scales. + public float ShearMix { get { return shearMix; } set { shearMix = value; } } + /// The transform constraint's setup pose data. + public TransformConstraintData Data { get { return data; } } + override public string ToString () { return data.name; }