Пример #1
0
        protected void updateRadial()
        {
            float   single       = Math.Max(this.m_pSprite.quad.br.texCoords.u, this.m_pSprite.quad.bl.texCoords.u);
            float   single1      = Math.Min(this.m_pSprite.quad.br.texCoords.u, this.m_pSprite.quad.bl.texCoords.u);
            float   single2      = Math.Max(this.m_pSprite.quad.tl.texCoords.v, this.m_pSprite.quad.bl.texCoords.v);
            float   single3      = Math.Min(this.m_pSprite.quad.tl.texCoords.v, this.m_pSprite.quad.bl.texCoords.v);
            CCPoint cCPoint      = new CCPoint(single, single2);
            CCPoint cCPoint1     = new CCPoint(single1, single3);
            CCPoint cCPoint2     = CCPointExtension.ccpAdd(cCPoint1, CCPointExtension.ccpCompMult(this.m_tAnchorPoint, CCPointExtension.ccpSub(cCPoint, cCPoint1)));
            float   mFPercentage = this.m_fPercentage / 100f;
            float   single4      = 6.28318548f * (this.m_eType == CCProgressTimerType.kCCProgressTimerTypeRadialCW ? mFPercentage : 1f - mFPercentage);
            CCPoint cCPoint3     = new CCPoint(cCPoint2.x, cCPoint1.y);
            CCPoint cCPoint4     = CCPointExtension.ccpRotateByAngle(cCPoint3, cCPoint2, single4);
            int     num          = 0;
            CCPoint cCPoint5     = new CCPoint();

            if (mFPercentage == 0f)
            {
                cCPoint5 = cCPoint3;
                num      = 0;
            }
            else if (mFPercentage != 1f)
            {
                float single5 = float.MaxValue;
                for (int i = 0; i <= 4; i++)
                {
                    int     num1     = (i + 3) % 4;
                    CCPoint cCPoint6 = CCPointExtension.ccpAdd(cCPoint1, CCPointExtension.ccpCompMult(this.boundaryTexCoord(i % 4), CCPointExtension.ccpSub(cCPoint, cCPoint1)));
                    CCPoint cCPoint7 = CCPointExtension.ccpAdd(cCPoint1, CCPointExtension.ccpCompMult(this.boundaryTexCoord(num1), CCPointExtension.ccpSub(cCPoint, cCPoint1)));
                    if (i == 0)
                    {
                        cCPoint7 = CCPointExtension.ccpLerp(cCPoint6, cCPoint7, 0.5f);
                    }
                    else if (i == 4)
                    {
                        cCPoint6 = CCPointExtension.ccpLerp(cCPoint6, cCPoint7, 0.5f);
                    }
                    float single6 = 0f;
                    float single7 = 0f;
                    if (CCPointExtension.ccpLineIntersect(cCPoint6, cCPoint7, cCPoint2, cCPoint4, ref single6, ref single7) && (i != 0 && i != 4 || 0f <= single6 && single6 <= 1f) && single7 >= 0f && single7 < single5)
                    {
                        single5 = single7;
                        num     = i;
                    }
                }
                cCPoint5 = CCPointExtension.ccpAdd(cCPoint2, CCPointExtension.ccpMult(CCPointExtension.ccpSub(cCPoint4, cCPoint2), single5));
            }
            else
            {
                cCPoint5 = cCPoint3;
                num      = 4;
            }
            bool flag = true;

            if (this.m_nVertexDataCount != num + 3)
            {
                flag = false;
                if (this.m_pVertexData != null)
                {
                    this.m_pVertexData      = null;
                    this.m_nVertexDataCount = 0;
                }
            }
            if (this.m_pVertexData == null)
            {
                this.m_nVertexDataCount = num + 3;
                this.m_pVertexData      = new ccV2F_C4B_T2F[this.m_nVertexDataCount];
                for (int j = 0; j < this.m_nVertexDataCount; j++)
                {
                    this.m_pVertexData[j] = new ccV2F_C4B_T2F();
                }
                this.updateColor();
            }
            if (!flag)
            {
                this.m_pVertexData[0].texCoords = new ccTex2F(cCPoint2.x, cCPoint2.y);
                this.m_pVertexData[0].vertices  = this.vertexFromTexCoord(cCPoint2);
                this.m_pVertexData[1].texCoords = new ccTex2F(cCPoint2.x, cCPoint1.y);
                this.m_pVertexData[1].vertices  = this.vertexFromTexCoord(new CCPoint(cCPoint2.x, cCPoint1.y));
                for (int k = 0; k < num; k++)
                {
                    CCPoint cCPoint8 = CCPointExtension.ccpAdd(cCPoint1, CCPointExtension.ccpCompMult(this.boundaryTexCoord(k), CCPointExtension.ccpSub(cCPoint, cCPoint1)));
                    this.m_pVertexData[k + 2].texCoords = new ccTex2F(cCPoint8.x, cCPoint8.y);
                    this.m_pVertexData[k + 2].vertices  = this.vertexFromTexCoord(cCPoint8);
                }
                if (this.m_pSprite.IsFlipX || this.m_pSprite.IsFlipY)
                {
                    for (int l = 0; l < this.m_nVertexDataCount - 1; l++)
                    {
                        if (this.m_pSprite.IsFlipX)
                        {
                            this.m_pVertexData[l].texCoords.u = cCPoint1.x + cCPoint.x - this.m_pVertexData[l].texCoords.u;
                        }
                        if (this.m_pSprite.IsFlipY)
                        {
                            this.m_pVertexData[l].texCoords.v = cCPoint1.y + cCPoint.y - this.m_pVertexData[l].texCoords.v;
                        }
                    }
                }
            }
            this.m_pVertexData[this.m_nVertexDataCount - 1].texCoords = new ccTex2F(cCPoint5.x, cCPoint5.y);
            this.m_pVertexData[this.m_nVertexDataCount - 1].vertices  = this.vertexFromTexCoord(cCPoint5);
            if (this.m_pSprite.IsFlipX || this.m_pSprite.IsFlipY)
            {
                if (this.m_pSprite.IsFlipX)
                {
                    this.m_pVertexData[this.m_nVertexDataCount - 1].texCoords.u = cCPoint1.x + cCPoint.x - this.m_pVertexData[this.m_nVertexDataCount - 1].texCoords.u;
                }
                if (this.m_pSprite.IsFlipY)
                {
                    this.m_pVertexData[this.m_nVertexDataCount - 1].texCoords.v = cCPoint1.y + cCPoint.y - this.m_pVertexData[this.m_nVertexDataCount - 1].texCoords.v;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Update does the work of mapping the texture onto the triangles
        //	It now doesn't occur the cost of free/alloc data every update cycle.
        //	It also only changes the percentage point but no other points if they have not
        //	been modified.
        //
        //	It now deals with flipped texture. If you run into this problem, just use the
        //	sprite property and enable the methods flipX, flipY.
        /// </summary>
        protected void updateRadial()
        {
            //	Texture Max is the actual max coordinates to deal with non-power of 2 textures
            float   fXMax = Math.Max(m_pSprite.quad.br.texCoords.u, m_pSprite.quad.bl.texCoords.u);
            float   fXMin = Math.Min(m_pSprite.quad.br.texCoords.u, m_pSprite.quad.bl.texCoords.u);
            float   fYMax = Math.Max(m_pSprite.quad.tl.texCoords.v, m_pSprite.quad.bl.texCoords.v);
            float   fYMin = Math.Min(m_pSprite.quad.tl.texCoords.v, m_pSprite.quad.bl.texCoords.v);
            CCPoint tMax  = new CCPoint(fXMax, fYMax);
            CCPoint tMin  = new CCPoint(fXMin, fYMin);

            //	Grab the midpoint
            CCPoint midpoint = CCPointExtension.ccpAdd(tMin, CCPointExtension.ccpCompMult(m_tAnchorPoint, CCPointExtension.ccpSub(tMax, tMin)));

            float alpha = m_fPercentage / 100.0f;

            //	Otherwise we can get the angle from the alpha
            float angle = 2.0f * ((float)Math.PI) * (m_eType == CCProgressTimerType.kCCProgressTimerTypeRadialCW ? alpha : 1.0f - alpha);

            //	We find the vector to do a hit detection based on the percentage
            //	We know the first vector is the one @ 12 o'clock (top,mid) so we rotate
            //	from that by the progress angle around the midpoint pivot
            CCPoint topMid       = new CCPoint(midpoint.x, tMin.y);
            CCPoint percentagePt = CCPointExtension.ccpRotateByAngle(topMid, midpoint, angle);

            int     index = 0;
            CCPoint hit   = new CCPoint();

            if (alpha == 0.0f)
            {
                //	More efficient since we don't always need to check intersection
                //	If the alpha is zero then the hit point is top mid and the index is 0.
                hit   = topMid;
                index = 0;
            }
            else
            if (alpha == 1.0f)
            {
                //	More efficient since we don't always need to check intersection
                //	If the alpha is one then the hit point is top mid and the index is 4.
                hit   = topMid;
                index = 4;
            }
            else
            {
                //	We run a for loop checking the edges of the texture to find the
                //	intersection point
                //	We loop through five points since the top is split in half

                float min_t = float.MaxValue;

                for (int i = 0; i <= kProgressTextureCoordsCount; ++i)
                {
                    int pIndex = (i + (kProgressTextureCoordsCount - 1)) % kProgressTextureCoordsCount;

                    CCPoint edgePtA = CCPointExtension.ccpAdd(tMin, CCPointExtension.ccpCompMult(boundaryTexCoord(i % kProgressTextureCoordsCount), CCPointExtension.ccpSub(tMax, tMin)));
                    CCPoint edgePtB = CCPointExtension.ccpAdd(tMin, CCPointExtension.ccpCompMult(boundaryTexCoord(pIndex), CCPointExtension.ccpSub(tMax, tMin)));

                    //	Remember that the top edge is split in half for the 12 o'clock position
                    //	Let's deal with that here by finding the correct endpoints
                    if (i == 0)
                    {
                        edgePtB = CCPointExtension.ccpLerp(edgePtA, edgePtB, 0.5f);
                    }
                    else
                    if (i == 4)
                    {
                        edgePtA = CCPointExtension.ccpLerp(edgePtA, edgePtB, 0.5f);
                    }

                    //	s and t are returned by ccpLineIntersect
                    float s = 0;
                    float t = 0;
                    if (CCPointExtension.ccpLineIntersect(edgePtA, edgePtB, midpoint, percentagePt, ref s, ref t))
                    {
                        //	Since our hit test is on rays we have to deal with the top edge
                        //	being in split in half so we have to test as a segment
                        if (i == 0 || i == 4)
                        {
                            //	s represents the point between edgePtA--edgePtB
                            if (!(0.0f <= s && s <= 1.0f))
                            {
                                continue;
                            }
                        }

                        //	As long as our t isn't negative we are at least finding a
                        //	correct hitpoint from midpoint to percentagePt.
                        if (t >= 0.0f)
                        {
                            //	Because the percentage line and all the texture edges are
                            //	rays we should only account for the shortest intersection
                            if (t < min_t)
                            {
                                min_t = t;
                                index = i;
                            }
                        }
                    }
                }

                //	Now that we have the minimum magnitude we can use that to find our intersection
                hit = CCPointExtension.ccpAdd(midpoint, CCPointExtension.ccpMult(CCPointExtension.ccpSub(percentagePt, midpoint), min_t));
            }

            //	The size of the vertex data is the index from the hitpoint
            //	the 3 is for the midpoint, 12 o'clock point and hitpoint position.

            bool sameIndexCount = true;

            if (m_nVertexDataCount != index + 3)
            {
                sameIndexCount = false;
                if (m_pVertexData != null)
                {
                    m_pVertexData      = null;
                    m_nVertexDataCount = 0;
                }
            }

            if (m_pVertexData == null)
            {
                m_nVertexDataCount = index + 3;
                m_pVertexData      = new ccV2F_C4B_T2F[m_nVertexDataCount];
                for (int i = 0; i < m_nVertexDataCount; i++)
                {
                    m_pVertexData[i] = new ccV2F_C4B_T2F();
                }


                Debug.Assert(m_pVertexData != null);

                updateColor();
            }

            if (!sameIndexCount)
            {
                //	First we populate the array with the midpoint, then all
                //	vertices/texcoords/colors of the 12 'o clock start and edges and the hitpoint
                m_pVertexData[0].texCoords = new ccTex2F(midpoint.x, midpoint.y);
                m_pVertexData[0].vertices  = vertexFromTexCoord(midpoint);

                m_pVertexData[1].texCoords = new ccTex2F(midpoint.x, tMin.y);
                m_pVertexData[1].vertices  = vertexFromTexCoord(new CCPoint(midpoint.x, tMin.y));

                for (int i = 0; i < index; ++i)
                {
                    CCPoint texCoords = CCPointExtension.ccpAdd(tMin, CCPointExtension.ccpCompMult(boundaryTexCoord(i), CCPointExtension.ccpSub(tMax, tMin)));

                    m_pVertexData[i + 2].texCoords = new ccTex2F(texCoords.x, texCoords.y);
                    m_pVertexData[i + 2].vertices  = vertexFromTexCoord(texCoords);
                }

                //	Flip the texture coordinates if set
                if (m_pSprite.IsFlipX || m_pSprite.IsFlipY)
                {
                    for (int i = 0; i < m_nVertexDataCount - 1; ++i)
                    {
                        if (m_pSprite.IsFlipX)
                        {
                            m_pVertexData[i].texCoords.u = tMin.x + tMax.x - m_pVertexData[i].texCoords.u;
                        }

                        if (m_pSprite.IsFlipY)
                        {
                            m_pVertexData[i].texCoords.v = tMin.y + tMax.y - m_pVertexData[i].texCoords.v;
                        }
                    }
                }
            }

            //	hitpoint will go last
            m_pVertexData[m_nVertexDataCount - 1].texCoords = new ccTex2F(hit.x, hit.y);
            m_pVertexData[m_nVertexDataCount - 1].vertices  = vertexFromTexCoord(hit);

            if (m_pSprite.IsFlipX || m_pSprite.IsFlipY)
            {
                if (m_pSprite.IsFlipX)
                {
                    m_pVertexData[m_nVertexDataCount - 1].texCoords.u = tMin.x + tMax.x - m_pVertexData[m_nVertexDataCount - 1].texCoords.u;
                }

                if (m_pSprite.IsFlipY)
                {
                    m_pVertexData[m_nVertexDataCount - 1].texCoords.v = tMin.y + tMax.y - m_pVertexData[m_nVertexDataCount - 1].texCoords.v;
                }
            }
        }