private void TransformNode(SpriteTransform sTransform, SpriteTransform parentTransform)
        {
            float r   = 0;
            float sin = 0;
            float cos = 0;

            float sx = 0;
            float sy = 0;

            {
                //TRS
                r   = Mathf.Deg2Rad * (parentTransform.rotation);
                sin = Mathf.Sin(r);
                cos = Mathf.Cos(r);

                sx = sTransform.position.x;
                sy = sTransform.position.y;

                sTransform.position.x = parentTransform.position.x + (sx * cos - sy * sin) * parentTransform.scale.x;
                sTransform.position.y = parentTransform.position.y + (sx * sin + sy * cos) * parentTransform.scale.y;


                sTransform.scale.x *= parentTransform.scale.x;
                sTransform.scale.y *= parentTransform.scale.y;

                sTransform.rotation += parentTransform.rotation;
            }

            for (SpriteTransform iter = sTransform.firstChild; iter != null; iter = iter.next)
            {
                TransformNode(iter, sTransform);
            }
        }
        internal void fini()
        {
            for (int i = 0, e = componentEnable.Length; i < e; i++)
            {
                componentEnable[i] = 0;
            }
            enableList.Clear();


            // remove from SpriteTransform
            tmpBuffer.AddRange(referenceList);
            foreach (SpriteTransform.StateComponentPair pair in tmpBuffer)
            {
                pair.applyTo.DetachState(pair.state, pair.component);
            }
            tmpBuffer.Clear();


            referenceList.Clear();
            subClipState.Clear();

            if (subClipState.Count > 0)
            {
                Debug.LogError("sub state not empty " + _name);
                subClipState.Clear();
            }

            _name           = string.Empty;
            parentTransform = null;
            _clip           = null;
            _animation      = null;
            parentState     = null;

            isRemoving = false;
        }
Beispiel #3
0
        internal void DetachSprite(int idx)
        {
            int lastIdx = activePrimitiveCount - 1;

            SpriteTransform tmpSprTransform = spriteTransforms[idx];

            if (tmpSprTransform.overrideSprite != null)
            {
                OverrideSprite os = new OverrideSprite();

                os.pathHash = spriteTransforms[idx].component._fullPathHash;
                os.sprite   = spriteTransforms[idx].overrideSprite;
                overrideList.Add(os);
            }

            tmpSprTransform.overrideHash   = 0;
            tmpSprTransform.overrideSprite = null;

            if (lastIdx != idx)
            {
                SpritePrimitive tmpPri = primitives[idx];

                spriteTransforms[idx]    = spriteTransforms[lastIdx];
                spriteTransforms[idx].id = idx;
                primitives[idx]          = primitives[lastIdx];

                tmpSprTransform.id = lastIdx;

                spriteTransforms[lastIdx] = tmpSprTransform;
                primitives[lastIdx]       = tmpPri;
            }

            activePrimitiveCount--;
            _activePrimitiveCount.value = activePrimitiveCount;
        }
Beispiel #4
0
        /// <summary>
        /// Attach a <see cref="Sprite"> Sprite</see> to renderer.
        /// </summary>
        /// <param name="sprite">Attach <see cref="Sprite"> Sprite</see> to renderer.</param>
        /// <returns>Attached <see cref="Sprite"> Sprite</see>'s index in renderer.</returns>
        public int AttachSprite(Sprite sprite)
        {
            SpriteTransform ret = AttachSpriteComponent(null);

            ret.sprite = sprite;
            return(ret.id);
        }
Beispiel #5
0
        /// <summary>
        /// Set the max <see cref="Sprite">Sprite</see>'s count can be attach to renderer.
        /// </summary>
        /// <param name="maxSprites">How many sprite the renderer can be attach.</param>
        public void Resize(int maxSprites)
        {
            int layer  = gameObject.layer;
            int oldLen = primitives.Length;


            if (oldLen < maxSprites)
            {
                System.Array.Resize <SpritePrimitive>(ref primitives, maxSprites * 2);
                System.Array.Resize <SpriteTransform>(ref spriteTransforms, maxSprites * 2);

                int ownerID = instanceID;
                for (int i = oldLen; i < primitives.Length; i++)
                {
                    SpritePrimitive pri = new SpritePrimitive(1);
                    pri.ownerID   = ownerID;
                    primitives[i] = pri;

                    spriteTransforms[i] = new SpriteTransform();
                    spriteTransforms[i].ResetTransform();
                }
            }

            primitiveCount = primitives.Length;

            primitiveGroup.primitives = primitives;
        }
        /// <summary>
        /// The rotation of the transform in gameobject local space.\
        /// Only available in PreUpdate hanlder in SpriteAnimation.
        /// </summary>
        public float GetGlobalRotate()
        {
            float           ret  = 0;
            SpriteTransform iter = this;

            while (iter != null)
            {
                ret += iter.rotation;
                iter = iter.parent;
            }
            return(ret);
        }
        /// <summary>
        /// The scale of the transform in gameobject local space.
        /// Only available in PreUpdate hanlder in SpriteAnimation.
        /// </summary>
        /// <returns></returns>
        public Vector2 GetGlobalScale()
        {
            Vector2         ret  = Vector2.one;
            SpriteTransform iter = this;

            while (iter != null)
            {
                ret  = new Vector2(ret.x * iter.scale.x, ret.y * iter.scale.y);
                iter = iter.parent;
            }
            return(ret);
        }
        /// <summary>
        /// Matrix that transforms a point from local space into gameobject local space
        /// Only available in PreUpdate hanlder in SpriteAnimation.
        /// </summary>
        public Matrix4x4 GetLocalToWorldMatrix()
        {
            Matrix4x4       mat  = Matrix4x4.identity;
            SpriteTransform iter = this;

            while (iter != null)
            {
                mat  = Matrix4x4.TRS(iter.position, Quaternion.Euler(0, 0, iter.rotation), new Vector3(iter.scale.x, iter.scale.y, 1f)) * mat;
                iter = iter.parent;
            }
            return(mat);
        }
        void RemoveSprite(SpriteAnimationState state, SpriteAnimationComponent component, SpriteTransform parentTransform, int parentHash)
        {
            SpriteTransform sprTransfrom = spriteRenderer.GetSpriteTransformByFullPathHash(component._fullPathHash + parentHash);


            if (sprTransfrom != null)
            {
                sprTransfrom.DetachState(state, component);

                if (sprTransfrom.attachStateComponent.Count <= 0)
                {
                    if (sprTransfrom.prve != null)
                    {
                        sprTransfrom.prve.next = sprTransfrom.next;
                    }

                    if (sprTransfrom.next != null)
                    {
                        sprTransfrom.next.prve = sprTransfrom.prve;
                    }

                    if (sprTransfrom.parent != null && sprTransfrom.parent.firstChild == sprTransfrom)
                    {
                        sprTransfrom.parent.firstChild = sprTransfrom.next;
                    }

                    for (SpriteTransform child = sprTransfrom.firstChild; child != null; child = child.next)
                    {
                        child.parent = null;
                    }
                    {
                        sprTransfrom.parent     = null;
                        sprTransfrom.next       = null;
                        sprTransfrom.prve       = null;
                        sprTransfrom.firstChild = null;
                        sprTransfrom.lastChild  = null;
                    }

                    if (stateRoot.Contains(sprTransfrom))
                    {
                        stateRoot.Remove(sprTransfrom);
                    }

                    spriteRenderer.DetachSprite(sprTransfrom.id);
                }
            }

            for (int i = 0, e = component.children.Length; i < e; i++)
            {
                RemoveSprite(state, state.clip.subComponents[component.children[i]], sprTransfrom, 0);
            }
        }
Beispiel #10
0
        internal void AddSprite(SpriteAnimationState state)
        {
            AddSprite(state, state.clip.root, state.parentTransform,
                      state.parentTransform == null ? 0 : state.parentTransform.component._fullPathHash);


            SpriteTransform sprTransfrom = spriteRenderer.GetSpriteTransformByFullPathHash(state.clip.root._fullPathHash);

            if (state.parentTransform == null && !stateRoot.Contains(sprTransfrom))
            {
                stateRoot.Add(sprTransfrom);
            }
        }
Beispiel #11
0
 /// <summary>
 /// Copy the field from other to self.
 /// </summary>
 /// <param name="other">Copy source SpriteTransform.</param>
 public void Clone(SpriteTransform other)
 {
     position    = other.position;
     rotation    = other.rotation;
     scale       = other.scale;
     shear       = other.shear;
     color       = other.color;
     sprite      = other.sprite;
     layer       = other.layer;
     parent      = other.parent;
     component   = other.component;
     _override   = other._override;
     _isOverride = other._isOverride;
 }
Beispiel #12
0
        internal SpriteAnimationKeyFrame EvaluateAddtive(float time, SpriteTransform transform, float[] transformProperty)
        {
            int idx = (int)curves[0].curve.Evaluate(time);
            SpriteAnimationKeyFrame kf = keyFrames[idx];

            SpriteAnimationCurve curve = null;

            for (int i = 1, e = curves.Length; i < e; i++)
            {
                curve = curves[i];
                transformProperty[(int)curve.type] = curve.length == 0 ? 0f : (curve.curve.Evaluate(time) - curve.defaultValue);
            }
            return(kf);
        }
Beispiel #13
0
        void AddSprite(SpriteAnimationState state, SpriteAnimationComponent component, SpriteTransform parentTransform, int parentHash)
        {
            int searchHash = component._fullPathHash + parentHash;

            SpriteTransform sprTransform = spriteRenderer.GetSpriteTransformByFullPathHash(searchHash);

            if (sprTransform == null)
            {
                {
                    sprTransform = spriteRenderer.AttachSpriteComponent(component);

                    sprTransform.parent = parentTransform;

                    sprTransform.layer   = (int)component.layer + state.parentComponentLayer;
                    sprTransform.visible = component.visible;

                    sprTransform.searchHash = searchHash;

                    //sprTransform.name = state.parentTransform != null ? state.parentTransform.component._fullPath + component._fullPath : component._fullPath;
                }


                if (parentTransform != null)
                {
                    if (parentTransform.firstChild == null)
                    {
                        parentTransform.firstChild = sprTransform;
                        parentTransform.lastChild  = sprTransform;

                        sprTransform.prve = null;
                        sprTransform.next = null;
                    }
                    else
                    {
                        sprTransform.next = parentTransform.lastChild.next;
                        sprTransform.prve = parentTransform.lastChild;

                        parentTransform.lastChild.next = sprTransform;
                    }
                }
            }

            sprTransform.AttachState(state, component);

            for (int i = 0, e = component.children.Length; i < e; i++)
            {
                AddSprite(state, state.clip.subComponents[component.children[i]], sprTransform, parentHash);
            }
        }
Beispiel #14
0
        private void TransformComponent()
        {
            SpriteTransform parentTransform = new SpriteTransform();

            foreach (SpriteTransform rootTransform in stateRoot)
            {
                parentTransform.ResetTransform();
                TransformNode(rootTransform, parentTransform);
            }

            if (postTransformUpdate != null)
            {
                postTransformUpdate(this);
            }
        }
Beispiel #15
0
        private void EvaluateCompoment()
        {
            for (int i = 0; i < spriteRenderer.GetAttachSpriteCount(); i++)
            {
                SpriteTransform sprTransform = spriteRenderer.GetSpriteTransform(i);

                EvaluateTransform(sprTransform);
            }



            if (preTransformUpdate != null)
            {
                preTransformUpdate(this);
            }
        }
Beispiel #16
0
        void AddSprite(SpriteAnimationState state, SpriteAnimationComponent component, SpriteTransform parentTransform, int parentHash)
        {
            SpriteTransform sprTransfrom = spriteRenderer.GetSpriteTransformByFullPathHash(component._fullPathHash + parentHash);

            if (sprTransfrom == null)
            {
                {
                    sprTransfrom = spriteRenderer.AttachSpriteComponent(component);

                    sprTransfrom.parent = parentTransform;

                    sprTransfrom.layer   = (int)component.layer + state.parentComponentLayer;
                    sprTransfrom.visible = component.visible;
                }


                sprTransfrom.overrideHash = parentHash != 0 ? component._fullPathHash + parentHash : 0;


                if (parentTransform != null)
                {
                    if (parentTransform.firstChild == null)
                    {
                        parentTransform.firstChild = sprTransfrom;
                        parentTransform.lastChild  = sprTransfrom;

                        sprTransfrom.prve = null;
                        sprTransfrom.next = null;
                    }
                    else
                    {
                        sprTransfrom.next = parentTransform.lastChild.next;
                        sprTransfrom.prve = parentTransform.lastChild;

                        parentTransform.lastChild.next = sprTransfrom;
                    }
                }
            }

            sprTransfrom.AttachState(state, component);

            for (int i = 0, e = component.children.Length; i < e; i++)
            {
                AddSprite(state, state.clip.subComponents[component.children[i]], sprTransfrom, 0);
            }
        }
Beispiel #17
0
        void binderHandler(SpriteAnimation ani)
        {
            foreach (BinderItem item in bindObjects)
            {
                SpriteTransform tmp = spriteRenderer.GetSpriteTransform(item.componentName);
                if (tmp != null)
                {
                    Vector3 offset   = item.offset;
                    Vector3 position = tmp.position;
                    Vector3 pos      = position + (Quaternion.Euler(0, 0, tmp.rotation) * offset);

                    pos.z = item.bindObject.transform.position.z;
                    item.bindObject.transform.position = transform.TransformPoint(pos);
                    item.bindObject.transform.rotation = Quaternion.Euler(0, 0, transform.eulerAngles.z + tmp.rotation + item.rotation);
                }
            }
        }
Beispiel #18
0
        internal SpriteAnimationKeyFrame EvaluateBlending(float time, SpriteTransform transform, float[] transformProperty)
        {
            int idx = (int)curves[0].curve.Evaluate(time);
            SpriteAnimationKeyFrame kf = keyFrames[idx];

            if (!transform.isSpriteValid && kf.isSpriteValid)
            {
                transform._sprite       = kf.sprite;
                transform.isSpriteValid = true;
            }

            float v = 0f;

            foreach (SpriteAnimationCurve curve in curves)
            {
                transformProperty[(int)curve.type] = curve.length == 0 ? curve.defaultValue : curve.curve.Evaluate(time);
            }
            return(kf);
        }
 private void ClearState()
 {
     //internal bool _enabled = false;
     _enabled = false;
     //internal SpriteAnimationClip _clip;
     _clip = null;
     //internal string _name;
     _name = string.Empty;
     //internal SpriteAnimation _animation;
     _animation = null;
     //internal float _speed = 1f;
     _speed = 1f;
     //internal float _normalizedSpeed = 0f;
     _normalizedSpeed = 0f;
     //internal float lastTime = 0f;
     lastTime = 0f;
     //internal int lastFrameIndex = 0;
     lastFrameIndex = 0;
     //internal float lastEvaluateTime = 0f;
     lastEvaluateTime = 0f;
     //internal float _startTime = 0;
     _startTime = 0;
     //internal int _layer = 0;
     _layer = 0;
     //internal float _endTime = 0f;
     _endTime = 0f;
     //internal bool useCurveInState = false;
     useCurveInState = false;
     //internal AnimationLinearCurve curve = null;
     //internal bool _isPaused = false;
     _isPaused = false;
     //internal float _weight = 1f;
     _weight = 1f;
     //internal float blendStep = 0f;
     blendStep = 0f;
     //internal bool removeAfterStop = false;
     removeAfterStop = false;
     //internal int parentComponentLayer = 0;
     parentComponentLayer = 0;
     //internal SpriteTransform parentTransform = null;
     parentTransform = null;
 }
Beispiel #20
0
        internal SpriteAnimationKeyFrame EvaluateBlending(float time, SpriteTransform transform, float[] transformProperty)
        {
            int idx = (int)curves[0].curve.Evaluate(time);
            SpriteAnimationKeyFrame kf = keyFrames[idx];

            if (!transform.isSpriteValid)
            {
                transform.sprite = kf.sprite;
            }

            float v = 0f;
            SpriteAnimationCurve curve = null;

            for (int i = 1, e = curves.Length; i < e; i++)
            {
                curve = curves[i];
                transformProperty[(int)curve.type] = curve.length == 0 ? curve.defaultValue : curve.curve.Evaluate(time);
            }
            return(kf);
        }
        internal void fini()
        {
            for (int i = 0, e = componentEnable.Length; i < e; i++)
            {
                componentEnable[i] = 0;
            }
            enableList.Clear();


            // remove from SpriteTransform
            tmpBuffer.AddRange(referenceList);
            foreach (SpriteTransform.StateComponentPair pair in tmpBuffer)
            {
                pair.applyTo.DetachState(pair.state, pair.component);
            }
            tmpBuffer.Clear();


            _name           = string.Empty;
            parentTransform = null;
        }
Beispiel #22
0
        internal SpriteTransform AttachSpriteComponent(SpriteAnimationComponent component)
        {
            int idx = activePrimitiveCount++;

            SpriteTransform ret = spriteTransforms[idx];

            ret.id        = idx;
            ret.primitive = primitives[idx];
            ret.ResetTransform();

            ret.component = component;
            if (component != null)
            {
                ret.name = component.name;
            }

            if (component != null && overrideList.Count > 0)
            {
                for (int i = 0; i < overrideList.Count; i++)
                {
                    if (overrideList[i].pathHash == component._fullPathHash)
                    {
                        ret.overrideSprite = overrideList[i].sprite;
                        overrideList.RemoveAt(i);
                        break;
                    }
                }
            }

            if (activePrimitiveCount == primitiveCount)
            {
                Resize(activePrimitiveCount + 1);
            }

            _activePrimitiveCount.value = activePrimitiveCount;

            return(ret);
        }
Beispiel #23
0
        private void TransformComponent()
        {
            Transform tran = transform;

            foreach (SpriteTransform rootTransform in stateRoot)
            {
                parentTransform.ResetTransform();

                if (applyRootToTransform)
                {
                    for (SpriteTransform iter = rootTransform.firstChild; iter != null; iter = iter.next)
                    {
                        TransformNode(iter, parentTransform);
                    }

                    Vector3 pos = rootTransform.position;
                    pos.z = transform.localPosition.z;
                    transform.localPosition = pos;
                    transform.localRotation = Quaternion.Euler(new Vector3(0, 0, rootTransform.rotation));
                    transform.localScale    = new Vector3(rootTransform.scale.x, rootTransform.scale.y, transform.localScale.z);

                    rootTransform.position = Vector2.zero;
                    rootTransform.rotation = 0;
                    rootTransform.scale    = Vector2.one;
                    rootTransform.shear    = Vector2.zero;
                    rootTransform.color    = Color.white;
                }
                else
                {
                    TransformNode(rootTransform, parentTransform);
                }
            }

            if (postTransformUpdate != null)
            {
                postTransformUpdate(this);
            }
        }
        /// <summary>
        /// Internal function.
        /// </summary>
        /// <param name="node"></param>
        public void updateCollisionTransformsForNode(ColliderNode node)
        {
            //SpriteTransform is transform data of animation node, include postion, rotation, scale, shear
            //SpriteTransform is in local space of SpriteRenderer component gameObject

            if (spriteRenderer == null)
            {
                spriteRenderer = GetComponent <SpriteRenderer>();
            }

            SpriteTransform nodeTransform = spriteRenderer.GetSpriteTransformByFullPathHash(node.nodePathHash);

            if (nodeTransform == null)
            {
                return;
            }

            // Find the sprite object
            Sprite partSprite = null;

            if (nodeTransform.overrideSprite != null)
            {
                partSprite = nodeTransform.overrideSprite;
            }

            else if (nodeTransform.sprite != null)
            {
                partSprite = nodeTransform.sprite;
            }

            if (nodeTransform != null && partSprite)
            {
                //todo:
                // 1. setting collider to correct center and size
                // You can get use Sprite and SpriteTransform to calculate the bounding box of a animation part.
                // And use the bounding box data to set the center and size
                // 2. setting collider in correct plane in world space.
                // EasyMotion2D support sprite rendering to 3 plane: XY, ZY, XZ. Collider should be in correct plane
                // etc

                Vector3    position = Vector3.zero;
                Quaternion rotation = Quaternion.identity;
                Vector3    scale    = Vector3.one;
                Vector3    size     = Vector3.one;
                Vector3    center   = Vector3.zero;

                float   localRotation = spriteRenderer.localRotation;
                Vector2 localScale    = spriteRenderer.localScale;
                bool    rr            = localScale.x * localScale.y < 0f;
                float   sr            = rr  ? -1f : 1f;


                float pr   = Mathf.Deg2Rad * (localRotation);
                float psin = Mathf.Sin(pr);
                float pcos = Mathf.Cos(pr);


                float psx = (nodeTransform.position.x) * localScale.x;
                float psy = (nodeTransform.position.y) * localScale.y;

                float tx = (psx * pcos - psy * psin);
                float ty = (psx * psin + psy * pcos);



                switch (spriteRenderer.plane)
                {
                case SpritePlane.PlaneXY:
                {
                    position = new Vector3(tx, ty, node.depthOffset);

                    rotation = Quaternion.Euler(0, 0, localRotation + nodeTransform.rotation * sr);

                    scale = new Vector3((1.0f + nodeTransform.shear.y) * localScale.x,
                                        (1.0f + nodeTransform.shear.x) * localScale.y,
                                        1);

                    size = new Vector3((partSprite.imageRect.width * nodeTransform.scale.x),
                                       (partSprite.imageRect.height * nodeTransform.scale.y),
                                       node.thickness);

                    center = new Vector3((partSprite.imageRect.width * 0.5f - partSprite.pivot.x),
                                         (partSprite.imageRect.height * 0.5f - partSprite.pivot.y),
                                         0);

                    center.y = -center.y;
                }
                break;

                case SpritePlane.PlaneZY:
                {
                    position = new Vector3(-node.depthOffset, ty, tx);

                    rotation = Quaternion.Euler(-localRotation + nodeTransform.rotation * -sr, 0, 0);

                    scale = new Vector3(1,
                                        (1.0f + nodeTransform.shear.x) * localScale.y,
                                        (1.0f + nodeTransform.shear.y) * localScale.x);

                    size = new Vector3(node.thickness,
                                       partSprite.imageRect.height * nodeTransform.scale.y,
                                       partSprite.imageRect.width * nodeTransform.scale.x);

                    center = new Vector3(0,
                                         (partSprite.imageRect.height * 0.5f - partSprite.pivot.y),
                                         (partSprite.imageRect.width * 0.5f - partSprite.pivot.x)
                                         );

                    center.y = -center.y;
                }
                break;

                case SpritePlane.PlaneXZ:
                {
                    position = new Vector3(tx, -node.depthOffset, ty);

                    rotation = Quaternion.Euler(0, (-localRotation - nodeTransform.rotation * sr) + (rr ? 180f : 0f), 0);

                    scale = new Vector3((1.0f + nodeTransform.shear.y) * localScale.y,
                                        1,
                                        (1.0f + nodeTransform.shear.x) * localScale.x);

                    size = new Vector3(partSprite.imageRect.width * nodeTransform.scale.x,
                                       node.thickness,
                                       partSprite.imageRect.height * nodeTransform.scale.y);

                    center = new Vector3((partSprite.imageRect.width * 0.5f - partSprite.pivot.x),
                                         0,
                                         (partSprite.imageRect.height * 0.5f - partSprite.pivot.y));

                    center.z = -center.z;
                }
                break;
                }



                Transform t = node.colliderObject.transform;

                t.localPosition = position;
                t.localRotation = rotation;
                t.localScale    = scale;

                BoxCollider tmpCollider = (node.colliderObject.collider as BoxCollider);


                tmpCollider.center = center;
                tmpCollider.size   = size;



                if (Application.isPlaying)
                {
                    // Check the collision listener event
                    if (!node.eventListenerSet)
                    {
                        setNodeCollisionListeners(node);
                    }
                }
                else
                {
                    // If the game is not playing, but the collision listener is still set, disable it so we know to reenable
                    //	it when the game is playing.
                    if (node.eventListenerSet)
                    {
                        node.eventListenerSet = false;
                    }
                }
            }
        }
Beispiel #25
0
        void RemoveSprite(SpriteAnimationState state, SpriteAnimationComponent component, SpriteTransform parentTransform, int parentHash)
        {
            int             searchHash   = component._fullPathHash + parentHash;
            SpriteTransform sprTransfrom = spriteRenderer.GetSpriteTransformByFullPathHash(searchHash);


            for (int i = 0, e = component.children.Length; i < e; i++)
            {
                RemoveSprite(state, state.clip.subComponents[component.children[i]], sprTransfrom, parentHash);
            }


            if (sprTransfrom != null)
            {
                //if (sprTransfrom.lastRefClip != null)
                //{
                //    string name = state.name + "_" + sprTransfrom.lastRefClip.name;
                //    SpriteAnimationState _state = this[name];


                //    if (_state != null)
                //    {
                //        if (_state.parentState != null)
                //            _state.parentState.subClipState.Remove(_state);

                //        Stop(_state);
                //    }

                //    sprTransfrom.lastRefClip = null;
                //}


                sprTransfrom.DetachState(state, component);


                if (sprTransfrom.attachStateComponent.Count <= 0)
                {
                    if (sprTransfrom.prve != null)
                    {
                        sprTransfrom.prve.next = sprTransfrom.next;
                    }

                    if (sprTransfrom.next != null)
                    {
                        sprTransfrom.next.prve = sprTransfrom.prve;
                    }

                    if (sprTransfrom.parent != null && sprTransfrom.parent.firstChild == sprTransfrom)
                    {
                        sprTransfrom.parent.firstChild = sprTransfrom.next;
                    }

                    for (SpriteTransform child = sprTransfrom.firstChild; child != null; child = child.next)
                    {
                        child.parent = null;
                    }

                    {
                        sprTransfrom.parent     = null;
                        sprTransfrom.next       = null;
                        sprTransfrom.prve       = null;
                        sprTransfrom.firstChild = null;
                        sprTransfrom.lastChild  = null;
                    }

                    if (stateRoot.Contains(sprTransfrom))
                    {
                        stateRoot.Remove(sprTransfrom);
                    }


                    spriteRenderer.DetachSprite(sprTransfrom.id);
                }
            }
        }
        private void EvaluateCompoment()
        {
            for (int i = 0; i < spriteRenderer.GetAttachSpriteCount(); i++)
            {
                SpriteTransform sprTransform = spriteRenderer.GetSpriteTransform(i);
                sprTransform.ZeroTransform();


                foreach (SpriteTransform.StateComponentPair pair in sprTransform.attachStateComponent)
                {
                    SpriteAnimationKeyFrame kf = null;

                    if (pair.state.blendMode == AnimationBlendMode.Additive)
                    {
                        //reset buffer
                        transformPropertys[1]  = transformPropertys[2] = transformPropertys[3] = 0f;
                        transformPropertys[4]  = transformPropertys[5] = transformPropertys[6] = transformPropertys[7] = transformPropertys[8] = transformPropertys[9] = 0f;
                        transformPropertys[10] = transformPropertys[11] = 0f;

                        kf = pair.component.EvaluateAddtive(pair.state.lastEvaluateTime, sprTransform, transformPropertys);
                    }
                    else
                    {
                        //reset buffer
                        transformPropertys[1]  = transformPropertys[2] = transformPropertys[3] = 0f;
                        transformPropertys[4]  = transformPropertys[5] = transformPropertys[6] = transformPropertys[7] = transformPropertys[8] = transformPropertys[9] = 1f;
                        transformPropertys[10] = transformPropertys[11] = 0f;

                        kf = pair.component.EvaluateBlending(pair.state.lastEvaluateTime, sprTransform, transformPropertys);
                    }

                    if (pair.applyTo.lastRefClip != kf.refClip)
                    {
                        if (pair.applyTo.lastRefClip != null)
                        {
                            string name = pair.state.name + "_" + pair.applyTo.lastRefClip.name;
                            SpriteAnimationState state = this[name];

                            if (state != null)
                            {
                                Stop(state);
                            }
                        }


                        if (kf.refClip != null)
                        {
                            string name = pair.state.name + "_" + kf.refClip.name;

                            if (!animationStates.ContainsKey(name))
                            {
                                AddClip(kf.refClip, name);
                                SpriteAnimationState state = this[name];

                                state.parentTransform      = spriteRenderer.GetSpriteTransformByFullPathHash(pair.component._fullPathHash);
                                state.parentComponentLayer = (int)pair.component.layer;
                                state.removeAfterStop      = true;

                                state.layer   = pair.state.layer;
                                state.weight  = pair.state.weight;
                                state.enabled = true;

                                state.lastTime         = 0f;
                                state.lastFrameIndex   = -1;
                                state.lastEvaluateTime = 0;


                                pair.applyTo.lastRefClip = kf.refClip;
                            }
                        }
                    }


                    float weight = pair.weight;


                    {
                        sprTransform.position.x += transformPropertys[1] * weight;
                        sprTransform.position.y += transformPropertys[2] * weight;
                        sprTransform.rotation   += transformPropertys[3] * weight;
                        sprTransform.scale.x    += transformPropertys[4] * weight;
                        sprTransform.scale.y    += transformPropertys[5] * weight;
                        sprTransform.color.r    += transformPropertys[6] * weight;
                        sprTransform.color.g    += transformPropertys[7] * weight;
                        sprTransform.color.b    += transformPropertys[8] * weight;
                        sprTransform.color.a    += transformPropertys[9] * weight;
                        sprTransform.shear.x    += transformPropertys[10] * weight;
                        sprTransform.shear.y    += transformPropertys[11] * weight;
                    }
                }
            }


            if (preTransformUpdate != null)
            {
                preTransformUpdate(this);
            }
        }
        void updateText(string text)
        {
            if (font == null || !enabled || renderMode == null || !gameObject.active)
            {
                return;
            }

            float offsetX = 0f;
            float offsetY = 0f;

            char lastChar = (char)0;

            base.Clear();

            if (text.Length == 0)
            {
                return;
            }

            __colorLT = _colorLT;
            __colorRT = _colorRT;
            __colorLB = _colorLB;
            __colorRB = _colorRB;


            base.Resize(text.Length * 2);

            textSpriteTransforms.Clear();


            int e = text.Length;

            for (int i = 0; i < e; i++)
            {
                char c = text[i];

                if (textArea && (c == '\n') || (c == '\r'))
                {
                    alignText(offsetX);

                    offsetX  = 0;
                    offsetY -= font.common.lineHeight;
                }


                if (textArea && offsetY < -textAreaSize.y)
                {
                    break;
                }


                FontChar fontChar = font.GetChar(c);
                if (fontChar == null)
                {
                    continue;
                }


                int k = font.GetKerning(lastChar, c);


                if (textArea && (((offsetX + fontChar.xOffset + k) >= textAreaSize.x)))
                {
                    alignText(offsetX);

                    offsetX  = 0;
                    offsetY -= font.common.lineHeight;
                }

                int             idx  = AttachSprite(fontChar.sprite);
                SpriteTransform tran = GetSpriteTransform(idx);

                textSpriteTransforms.Add(tran);

                tran.position = new Vector2(offsetX + fontChar.xOffset + k, offsetY - fontChar.yOffset);
                tran.layer    = (e - i) & 0xf;


                offsetX += fontChar.xAdvance;
                lastChar = c;
            }

            alignText(offsetX);

            if (updateMode == SpriteRendererUpdateMode.None)
            {
                Apply();
            }
        }
Beispiel #28
0
 /// <summary>
 /// Copy constructor of SpriteTransform
 /// </summary>
 /// <param name="other">Copy source SpriteTransform.</param>
 public SpriteTransform(SpriteTransform other)
 {
     Clone(other);
 }
Beispiel #29
0
        internal void EvaluateTransform(SpriteTransform sprTransform)
        {
            sprTransform.ZeroTransform();


            for (int idx = 0; idx < sprTransform.attachStateComponent.Count; idx++)
            {
                SpriteTransform.StateComponentPair pair = sprTransform.attachStateComponent[idx];

                SpriteAnimationKeyFrame kf = null;


                if (pair.state.blendMode == AnimationBlendMode.Additive)
                {
                    //reset buffer
                    transformPropertys[1]  = transformPropertys[2] = transformPropertys[3] = 0f;
                    transformPropertys[4]  = transformPropertys[5] = transformPropertys[6] = transformPropertys[7] = transformPropertys[8] = transformPropertys[9] = 0f;
                    transformPropertys[10] = transformPropertys[11] = 0f;

                    kf = pair.component.EvaluateAddtive(pair.state.lastEvaluateTime, sprTransform, transformPropertys);
                }
                else
                {
                    //reset buffer
                    transformPropertys[1]  = transformPropertys[2] = transformPropertys[3] = 0f;
                    transformPropertys[4]  = transformPropertys[5] = transformPropertys[6] = transformPropertys[7] = transformPropertys[8] = transformPropertys[9] = 1f;
                    transformPropertys[10] = transformPropertys[11] = 0f;

                    kf = pair.component.EvaluateBlending(pair.state.lastEvaluateTime, sprTransform, transformPropertys);
                }


                if (pair.lastRefClip != kf.refClip)
                {
                    if (pair.lastRefClip != null)
                    {
                        string name = pair.state.name + "_" + pair.component.fullPath + "_" + pair.lastRefClip.name;
                        SpriteAnimationState state = this[name];

                        if (state != null)
                        {
                            Stop(state);
                        }

                        pair.lastRefClip = null;
                    }


                    if (kf.refClip != null)
                    {
                        string name = pair.state.name + "_" + pair.component.fullPath + "_" + kf.refClip.name;

                        if (!animationStates.ContainsKey(name))
                        {
                            AddClip(kf.refClip, name);
                            SpriteAnimationState state = this[name];

                            state.parentState          = pair.state;
                            state.parentTransform      = pair.applyTo;
                            state.parentComponentLayer = (int)pair.component.layer;
                            state.removeAfterStop      = true;

                            state.layer  = pair.state.layer;
                            state.weight = pair.state.weight;

                            state.lastTime         = 0f;
                            state.lastFrameIndex   = -1;
                            state.lastEvaluateTime = 0;


                            pair.lastRefClip = kf.refClip;
                            state.parentState.subClipState.Add(state);

                            state.enabled = true;
                        }
                    }
                }


                float weight = pair.weight;


                {
                    sprTransform.position.x += transformPropertys[1] * weight;
                    sprTransform.position.y += transformPropertys[2] * weight;
                    sprTransform.rotation   += transformPropertys[3] * weight;
                    sprTransform.scale.x    += transformPropertys[4] * weight;
                    sprTransform.scale.y    += transformPropertys[5] * weight;
                    sprTransform.color.r    += transformPropertys[6] * weight;
                    sprTransform.color.g    += transformPropertys[7] * weight;
                    sprTransform.color.b    += transformPropertys[8] * weight;
                    sprTransform.color.a    += transformPropertys[9] * weight;
                    sprTransform.shear.x    += transformPropertys[10] * weight;
                    sprTransform.shear.y    += transformPropertys[11] * weight;
                }
            }
        }
Beispiel #30
0
        static void DrawAnimationComponent(Vector2 orig, float frameRate,
                                           SpriteAnimationComponent component, float time, Matrix4x4 parentMat, float parentRotation, Vector2 parentScale, SpriteTransform parentTran, int parentLayer)
        {
            SpriteTransform transform = new SpriteTransform();

            transform.parent    = parentTran;
            transform.component = component;
            transform.layer     = (int)component.layer + parentLayer;

            Sprite spr = null;
            SpriteAnimationClip refClip = null;


            SpriteAnimationKeyFrame kf = component.Evaluate(time,
                                                            ref transform.position,
                                                            ref transform.rotation,
                                                            ref transform.scale,
                                                            ref transform.shear,
                                                            ref transform.color,
                                                            ref spr,
                                                            ref refClip);

            transform.sprite = spr;

            componentTransforms.Add(transform);

            transform.position.y = -transform.position.y;

            Vector2   tranPosition = parentMat.MultiplyPoint3x4(transform.position);
            Matrix4x4 tmpMat       = Matrix4x4.TRS(transform.position, Quaternion.Euler(0, 0, -transform.rotation), transform.scale);



            if (refClip != null)
            {
                float l    = refClip.length;
                float tick = 1f / refClip.frameRate;
                AnimationLinearCurve clipPlayingCurve = new AnimationLinearCurve(0, 0, l, l);

                clipPlayingCurve.wrapMode = refClip.wrapMode;
                float t = clipPlayingCurve.Evaluate(time - kf.frameIndex * tick);

                DrawAnimationComponent(orig, frameRate, refClip.root, t, parentMat * tmpMat,
                                       parentRotation + transform.rotation,
                                       new Vector2(transform.scale.x * parentScale.x, transform.scale.y * parentScale.y),
                                       transform, transform.layer);
            }



            foreach (int comIdx in component.children)
            {
                SpriteAnimationComponent com = component.clip.subComponents[comIdx];
                DrawAnimationComponent(orig, frameRate, com, time, parentMat * tmpMat,
                                       parentRotation + transform.rotation,
                                       new Vector2(transform.scale.x * parentScale.x, transform.scale.y * parentScale.y),
                                       transform, parentLayer);
            }


            transform.position = tranPosition - orig;
            transform.scale    = new Vector2(transform.scale.x * parentScale.x, transform.scale.y * parentScale.y);

            transform.rotation = parentRotation + transform.rotation;
        }