/// <summary>Caches information about bones and constraints. Must be called if bones, constraints or weighted path attachments are added
        /// or removed.</summary>
        public void UpdateCache()
        {
            ExposedList <IUpdatable> updateCache = this.updateCache;

            updateCache.Clear();
            this.updateCacheReset.Clear();

            ExposedList <Bone> bones = this.bones;

            for (int i = 0, n = bones.Count; i < n; i++)
            {
                bones.Items[i].sorted = false;
            }

            ExposedList <IkConstraint> ikConstraints = this.ikConstraints;
            var transformConstraints = this.transformConstraints;
            var pathConstraints = this.pathConstraints;
            int ikCount = IkConstraints.Count, transformCount = transformConstraints.Count, pathCount = pathConstraints.Count;
            int constraintCount = ikCount + transformCount + pathCount;

            //outer:
            for (int i = 0; i < constraintCount; i++)
            {
                for (int ii = 0; ii < ikCount; ii++)
                {
                    IkConstraint constraint = ikConstraints.Items[ii];
                    if (constraint.data.order == i)
                    {
                        SortIkConstraint(constraint);
                        goto continue_outer;                         //continue outer;
                    }
                }
                for (int ii = 0; ii < transformCount; ii++)
                {
                    TransformConstraint constraint = transformConstraints.Items[ii];
                    if (constraint.data.order == i)
                    {
                        SortTransformConstraint(constraint);
                        goto continue_outer;                         //continue outer;
                    }
                }
                for (int ii = 0; ii < pathCount; ii++)
                {
                    PathConstraint constraint = pathConstraints.Items[ii];
                    if (constraint.data.order == i)
                    {
                        SortPathConstraint(constraint);
                        goto continue_outer;                         //continue outer;
                    }
                }
                continue_outer : {}
            }

            for (int i = 0, n = bones.Count; i < n; i++)
            {
                SortBone(bones.Items[i]);
            }
        }
        /// <returns>May be null.</returns>
        public TransformConstraint FindTransformConstraint(string constraintName)
        {
            if (constraintName == null)
            {
                throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
            }
            ExposedList <TransformConstraint> transformConstraints = this.transformConstraints;

            for (int i = 0, n = transformConstraints.Count; i < n; i++)
            {
                TransformConstraint transformConstraint = transformConstraints.Items[i];
                if (transformConstraint.data.name == constraintName)
                {
                    return(transformConstraint);
                }
            }
            return(null);
        }
        /// <summary>Sets the bones and constraints to their setup pose values.</summary>
        public void SetBonesToSetupPose()
        {
            var bonesItems = this.bones.Items;

            for (int i = 0, n = bones.Count; i < n; i++)
            {
                bonesItems[i].SetToSetupPose();
            }

            var ikConstraintsItems = this.ikConstraints.Items;

            for (int i = 0, n = ikConstraints.Count; i < n; i++)
            {
                IkConstraint constraint = ikConstraintsItems[i];
                constraint.bendDirection = constraint.data.bendDirection;
                constraint.mix           = constraint.data.mix;
            }

            var transformConstraintsItems = this.transformConstraints.Items;

            for (int i = 0, n = transformConstraints.Count; i < n; i++)
            {
                TransformConstraint     constraint     = transformConstraintsItems[i];
                TransformConstraintData constraintData = constraint.data;
                constraint.rotateMix    = constraintData.rotateMix;
                constraint.translateMix = constraintData.translateMix;
                constraint.scaleMix     = constraintData.scaleMix;
                constraint.shearMix     = constraintData.shearMix;
            }

            var pathConstraintItems = this.pathConstraints.Items;

            for (int i = 0, n = pathConstraints.Count; i < n; i++)
            {
                PathConstraint     constraint     = pathConstraintItems[i];
                PathConstraintData constraintData = constraint.data;
                constraint.position     = constraintData.position;
                constraint.spacing      = constraintData.spacing;
                constraint.rotateMix    = constraintData.rotateMix;
                constraint.translateMix = constraintData.translateMix;
            }
        }
        private void SortTransformConstraint(TransformConstraint constraint)
        {
            SortBone(constraint.target);

            var constrained = constraint.bones;
            int boneCount   = constrained.Count;

            if (constraint.data.local)
            {
                for (int i = 0; i < boneCount; i++)
                {
                    Bone child = constrained.Items[i];
                    SortBone(child.parent);
                    if (!updateCache.Contains(child))
                    {
                        updateCacheReset.Add(child);
                    }
                }
            }
            else
            {
                for (int i = 0; i < boneCount; i++)
                {
                    SortBone(constrained.Items[i]);
                }
            }

            updateCache.Add(constraint);

            for (int i = 0; i < boneCount; i++)
            {
                SortReset(constrained.Items[i].children);
            }
            for (int i = 0; i < boneCount; i++)
            {
                constrained.Items[i].sorted = true;
            }
        }