axmol/cocos2dx/misc_nodes/CCProgressTimer.cpp

584 lines
17 KiB
C++
Raw Normal View History

2010-08-11 18:15:52 +08:00
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2010 Lam Pham
2010-08-11 18:15:52 +08:00
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCProgressTimer.h"
#include "ccMacros.h"
#include "CCTextureCache.h"
#include "CCPointExtension.h"
2010-08-11 18:15:52 +08:00
#include <float.h>
namespace cocos2d {
#define kProgressTextureCoordsCount 4
const char kProgressTextureCoords = 0x1e;
2010-08-11 18:15:52 +08:00
CCProgressTimer* CCProgressTimer::progressWithFile(const char *pszFileName)
{
CCProgressTimer *pProgressTimer = new CCProgressTimer();
if (pProgressTimer->initWithFile(pszFileName))
2010-08-11 18:15:52 +08:00
{
pProgressTimer->autorelease();
}
else
{
delete pProgressTimer;
pProgressTimer = NULL;
}
return pProgressTimer;
}
bool CCProgressTimer::initWithFile(const char *pszFileName)
{
return this->initWithTexture(CCTextureCache::sharedTextureCache()->addImage(pszFileName));
}
2010-08-11 18:15:52 +08:00
CCProgressTimer* CCProgressTimer::progressWithTexture(cocos2d::CCTexture2D *pTexture)
{
CCProgressTimer *pProgressTimer = new CCProgressTimer();
if (pProgressTimer->initWithTexture(pTexture))
{
pProgressTimer->autorelease();
2010-08-11 18:15:52 +08:00
}
else
{
delete pProgressTimer;
pProgressTimer = NULL;
}
2010-08-11 18:15:52 +08:00
return pProgressTimer;
}
bool CCProgressTimer::initWithTexture(cocos2d::CCTexture2D *pTexture)
{
m_pSprite = CCSprite::spriteWithTexture(pTexture);
CC_SAFE_RETAIN(m_pSprite);
m_fPercentage = 0.f;
m_pVertexData = NULL;
m_nVertexDataCount = 0;
m_tAnchorPoint = ccp(0.5f, 0.5f);
setContentSize(m_pSprite->getContentSize());
m_eType = kCCProgressTimerTypeRadialCCW;
return true;
}
CCProgressTimer::~CCProgressTimer(void)
{
CC_SAFE_DELETE_ARRAY(m_pVertexData);
CC_SAFE_RELEASE(m_pSprite);
}
void CCProgressTimer::setPercentage(float fPercentage)
{
if (m_fPercentage != fPercentage)
2010-08-11 18:15:52 +08:00
{
m_fPercentage = clampf(fPercentage, 0, 100);
updateProgress();
2010-08-11 18:15:52 +08:00
}
}
2010-08-11 18:15:52 +08:00
void CCProgressTimer::setSprite(cocos2d::CCSprite *pSprite)
{
if (m_pSprite != pSprite)
2010-08-11 18:15:52 +08:00
{
CC_SAFE_RETAIN(pSprite);
CC_SAFE_RELEASE(m_pSprite);
m_pSprite = pSprite;
2010-08-11 18:15:52 +08:00
// Everytime we set a new sprite, we free the current vertex data
2010-08-11 18:15:52 +08:00
if (m_pVertexData)
{
delete[] m_pVertexData;
m_pVertexData = NULL;
m_nVertexDataCount = 0;
2010-08-11 18:15:52 +08:00
}
}
}
2010-08-11 18:15:52 +08:00
void CCProgressTimer::setType(cocos2d::CCProgressTimerType type)
{
if (type != m_eType)
2010-08-11 18:15:52 +08:00
{
// release all previous information
if (m_pVertexData)
2010-08-11 18:15:52 +08:00
{
delete[] m_pVertexData;
m_pVertexData = NULL;
m_nVertexDataCount = 0;
2010-08-11 18:15:52 +08:00
}
m_eType = type;
2010-08-11 18:15:52 +08:00
}
}
2010-08-11 18:15:52 +08:00
// Interval
2010-09-01 15:36:25 +08:00
///
// @returns the vertex position from the texture coordinate
///
ccVertex2F CCProgressTimer::vertexFromTexCoord(cocos2d::CCPoint texCoord)
{
CCPoint tmp;
ccVertex2F ret;
CCTexture2D *pTexture = m_pSprite->getTexture();
if (pTexture)
{
CCSize texSize = pTexture->getContentSizeInPixels();
tmp = ccp(texSize.width * texCoord.x / pTexture->getMaxS(),
texSize.height * (1 - (texCoord.y / pTexture->getMaxT())));
2010-08-11 18:15:52 +08:00
}
else
2010-08-11 18:15:52 +08:00
{
tmp = CCPointZero;
2010-08-11 18:15:52 +08:00
}
ret.x = tmp.x;
ret.y = tmp.y;
return ret;
}
2010-08-11 18:15:52 +08:00
void CCProgressTimer::updateColor(void)
{
GLubyte op = m_pSprite->getOpacity();
ccColor3B c3b = m_pSprite->getColor();
ccColor4B color = {c3b.r, c3b.g, c3b.b, op};
if (m_pSprite->getTexture()->getHasPremultipliedAlpha())
2010-08-11 18:15:52 +08:00
{
color.r *= op / 255;
color.g *= op / 255;
color.b *= op / 255;
2010-08-11 18:15:52 +08:00
}
if (m_pVertexData)
2010-08-11 18:15:52 +08:00
{
for (int i = 0; i < m_nVertexDataCount; ++i)
2010-08-11 18:15:52 +08:00
{
m_pVertexData[i].colors = color;
}
2010-08-11 18:15:52 +08:00
}
}
2010-08-11 18:15:52 +08:00
void CCProgressTimer::updateProgress(void)
{
switch (m_eType)
2010-08-11 18:15:52 +08:00
{
case kCCProgressTimerTypeRadialCW:
case kCCProgressTimerTypeRadialCCW:
updateRadial();
break;
case kCCProgressTimerTypeHorizontalBarLR:
case kCCProgressTimerTypeHorizontalBarRL:
case kCCProgressTimerTypeVerticalBarBT:
case kCCProgressTimerTypeVerticalBarTB:
updateBar();
break;
default:
break;
}
}
2010-08-11 18:15:52 +08:00
///
// 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.
///
void CCProgressTimer::updateRadial(void)
{
// Texture Max is the actual max coordinates to deal with non-power of 2 textures
CCPoint tMax = ccp(m_pSprite->getTexture()->getMaxS(),
m_pSprite->getTexture()->getMaxT());
2010-08-11 18:15:52 +08:00
// Grab the midpoint
CCPoint midpoint = ccpCompMult(m_tAnchorPoint, tMax);
2010-08-11 18:15:52 +08:00
float alpha = m_fPercentage / 100.f;
2010-08-11 18:15:52 +08:00
// Otherwise we can get the angle from the alpha
float angle = 2.f * ((float)M_PI) * (m_eType == kCCProgressTimerTypeRadialCW ? alpha : 1.f - alpha);
2010-08-11 18:15:52 +08:00
// 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 = ccp(midpoint.x, 0.f);
CCPoint percentagePt = ccpRotateByAngle(topMid, midpoint, angle);
2010-08-11 18:15:52 +08:00
int index = 0;
CCPoint hit = CCPointZero;
2010-08-11 18:15:52 +08:00
if (alpha == 0.f)
{
// 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.f)
{
// 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 = FLT_MAX;
2010-08-11 18:15:52 +08:00
for (int i = 0; i <= kProgressTextureCoordsCount; ++i)
2010-08-11 18:15:52 +08:00
{
int pIndex = (i + (kProgressTextureCoordsCount - 1)) % kProgressTextureCoordsCount;
2010-08-11 18:15:52 +08:00
CCPoint edgePtA = ccpCompMult(boundaryTexCoord(i % kProgressTextureCoordsCount), tMax);
CCPoint edgePtB = ccpCompMult(boundaryTexCoord(pIndex), tMax);
2010-08-11 18:15:52 +08:00
// 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)
2010-08-11 18:15:52 +08:00
{
edgePtB = ccpLerp(edgePtA, edgePtB, 0.5f);
} else
if (i == 4)
{
edgePtA = ccpLerp(edgePtA, edgePtB, 0.5f);
2010-08-11 18:15:52 +08:00
}
// s and t are returned by ccpLineIntersect
float s = 0;
float t = 0;
if (ccpLineIntersect(edgePtA, edgePtB, midpoint, percentagePt, &s, &t))
2010-08-11 18:15:52 +08:00
{
// 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)
2010-08-11 18:15:52 +08:00
{
// s represents the point between edgePtA--edgePtB
if (!(0.f <= s && s <= 1.f))
2010-08-11 18:15:52 +08:00
{
continue;
2010-08-11 18:15:52 +08:00
}
}
2010-08-11 18:15:52 +08:00
// As long as our t isn't negative we are at least finding a
// correct hitpoint from midpoint to percentagePt.
if (t >= 0.f)
{
// Because the percentage line and all the texture edges are
// rays we should only account for the shortest intersection
if (t < min_t)
2010-08-11 18:15:52 +08:00
{
min_t = t;
index = i;
2010-08-11 18:15:52 +08:00
}
}
}
2010-08-11 18:15:52 +08:00
}
// Now that we have the minimum magnitude we can use that to find our intersection
hit = ccpAdd(midpoint, ccpMult(ccpSub(percentagePt, midpoint),min_t));
}
2010-08-11 18:15:52 +08:00
// 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.
2010-08-11 18:15:52 +08:00
bool sameIndexCount = true;
if (m_nVertexDataCount != index + 3)
{
sameIndexCount = false;
if (m_pVertexData)
{
delete[] m_pVertexData;
m_pVertexData = NULL;
m_nVertexDataCount = 0;
2010-08-11 18:15:52 +08:00
}
}
if (! m_pVertexData)
2010-08-11 18:15:52 +08:00
{
m_nVertexDataCount = index + 3;
m_pVertexData = new ccV2F_C4B_T2F[m_nVertexDataCount];
assert(m_pVertexData);
2010-08-11 18:15:52 +08:00
updateColor();
}
2010-08-11 18:15:52 +08:00
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 = tex2(midpoint.x, midpoint.y);
m_pVertexData[0].vertices = vertexFromTexCoord(midpoint);
2010-08-11 18:15:52 +08:00
m_pVertexData[1].texCoords = tex2(midpoint.x, 0.f);
m_pVertexData[1].vertices = vertexFromTexCoord(ccp(midpoint.x, 0.f));
2010-08-11 18:15:52 +08:00
for (int i = 0; i < index; ++i)
{
CCPoint texCoords = ccpCompMult(boundaryTexCoord(i), tMax);
2010-08-11 18:15:52 +08:00
m_pVertexData[i+2].texCoords = tex2(texCoords.x, texCoords.y);
m_pVertexData[i+2].vertices = vertexFromTexCoord(texCoords);
}
2010-08-11 18:15:52 +08:00
// Flip the texture coordinates if set
if (m_pSprite->isFlipX() || m_pSprite->isFlipY())
{
for (int i = 0; i < m_nVertexDataCount - 1; ++i)
2010-08-11 18:15:52 +08:00
{
if (m_pSprite->isFlipX())
{
m_pVertexData[i].texCoords.u = tMax.x - m_pVertexData[i].texCoords.u;
2010-08-11 18:15:52 +08:00
}
if (m_pSprite->isFlipY())
{
m_pVertexData[i].texCoords.v = tMax.y - m_pVertexData[i].texCoords.v;
2010-08-11 18:15:52 +08:00
}
}
}
}
2010-08-11 18:15:52 +08:00
// hitpoint will go last
m_pVertexData[m_nVertexDataCount - 1].texCoords = tex2(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 = tMax.x - m_pVertexData[m_nVertexDataCount - 1].texCoords.u;
2010-08-11 18:15:52 +08:00
}
if (m_pSprite->isFlipY())
{
m_pVertexData[m_nVertexDataCount - 1].texCoords.v = tMax.y - m_pVertexData[m_nVertexDataCount - 1].texCoords.v;
}
}
}
///
// Update does the work of mapping the texture onto the triangles for the bar
// 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.
///
void CCProgressTimer::updateBar(void)
{
float alpha = m_fPercentage / 100.f;
CCPoint tMax = ccp(m_pSprite->getTexture()->getMaxS(), m_pSprite->getTexture()->getMaxT());
unsigned char vIndexes[2] = {0, 0};
unsigned char index = 0;
// We know vertex data is always equal to the 4 corners
// If we don't have vertex data then we create it here and populate
// the side of the bar vertices that won't ever change.
if (! m_pVertexData)
{
m_nVertexDataCount = kProgressTextureCoordsCount;
m_pVertexData = new ccV2F_C4B_T2F[m_nVertexDataCount];
assert(m_pVertexData);
if (m_eType == kCCProgressTimerTypeHorizontalBarLR)
{
m_pVertexData[vIndexes[0] = 0].texCoords = tex2(0, 0);
m_pVertexData[vIndexes[1] = 1].texCoords = tex2(0, tMax.y);
} else
if (m_eType == kCCProgressTimerTypeHorizontalBarRL)
{
m_pVertexData[vIndexes[0] = 2].texCoords = tex2(tMax.x, tMax.y);
m_pVertexData[vIndexes[1] = 3].texCoords = tex2(tMax.x, 0.f);
} else
if (m_eType == kCCProgressTimerTypeVerticalBarBT)
{
m_pVertexData[vIndexes[0] = 1].texCoords = tex2(0, tMax.y);
m_pVertexData[vIndexes[1] = 3].texCoords = tex2(tMax.x, tMax.y);
} else
if (m_eType == kCCProgressTimerTypeVerticalBarTB)
{
m_pVertexData[vIndexes[0] = 0].texCoords = tex2(0, 0);
m_pVertexData[vIndexes[1] = 2].texCoords = tex2(tMax.x, 0);
2010-08-11 18:15:52 +08:00
}
index = vIndexes[0];
2010-08-21 13:53:24 +08:00
m_pVertexData[index].vertices = vertexFromTexCoord(ccp(m_pVertexData[index].texCoords.u,
m_pVertexData[index].texCoords.v));
2010-08-21 13:53:24 +08:00
index = vIndexes[1];
m_pVertexData[index].vertices = vertexFromTexCoord(ccp(m_pVertexData[index].texCoords.u,
m_pVertexData[index].texCoords.v));
if (m_pSprite->isFlipY() || m_pSprite->isFlipX())
{
if (m_pSprite->isFlipX())
{
index = vIndexes[0];
2010-08-11 18:15:52 +08:00
m_pVertexData[index].texCoords.u = tMax.x - m_pVertexData[index].texCoords.u;
index = vIndexes[1];
m_pVertexData[index].texCoords.u = tMax.x - m_pVertexData[index].texCoords.u;
}
if (m_pSprite->isFlipY())
{
index = vIndexes[0];
2010-08-11 18:15:52 +08:00
m_pVertexData[index].texCoords.v = tMax.y - m_pVertexData[index].texCoords.v;
index = vIndexes[1];
m_pVertexData[index].texCoords.v = tMax.y - m_pVertexData[index].texCoords.v;
}
2010-08-11 18:15:52 +08:00
}
updateColor();
2010-08-11 18:15:52 +08:00
}
if(m_eType == kCCProgressTimerTypeHorizontalBarLR)
{
m_pVertexData[vIndexes[0] = 3].texCoords = tex2(tMax.x*alpha, tMax.y);
m_pVertexData[vIndexes[1] = 2].texCoords = tex2(tMax.x*alpha, 0);
} else
if (m_eType == kCCProgressTimerTypeHorizontalBarRL)
{
m_pVertexData[vIndexes[0] = 1].texCoords = tex2(tMax.x*(1.f - alpha), 0);
m_pVertexData[vIndexes[1] = 0].texCoords = tex2(tMax.x*(1.f - alpha), tMax.y);
} else
if (m_eType == kCCProgressTimerTypeVerticalBarBT)
{
m_pVertexData[vIndexes[0] = 0].texCoords = tex2(0, tMax.y*(1.f - alpha));
m_pVertexData[vIndexes[1] = 2].texCoords = tex2(tMax.x, tMax.y*(1.f - alpha));
} else
if (m_eType == kCCProgressTimerTypeVerticalBarTB)
{
m_pVertexData[vIndexes[0] = 1].texCoords = tex2(0, tMax.y*alpha);
m_pVertexData[vIndexes[1] = 3].texCoords = tex2(tMax.x, tMax.y*alpha);
}
index = vIndexes[0];
m_pVertexData[index].vertices = vertexFromTexCoord(ccp(m_pVertexData[index].texCoords.u,
m_pVertexData[index].texCoords.v));
index = vIndexes[1];
m_pVertexData[index].vertices = vertexFromTexCoord(ccp(m_pVertexData[index].texCoords.u,
m_pVertexData[index].texCoords.v));
if (m_pSprite->isFlipY() || m_pSprite->isFlipX())
{
if (m_pSprite->isFlipX())
2010-08-11 18:15:52 +08:00
{
index = vIndexes[0];
m_pVertexData[index].texCoords.u = tMax.x - m_pVertexData[index].texCoords.u;
index = vIndexes[1];
m_pVertexData[index].texCoords.u = tMax.x - m_pVertexData[index].texCoords.u;
2010-08-11 18:15:52 +08:00
}
if (m_pSprite->isFlipY())
{
index = vIndexes[0];
m_pVertexData[index].texCoords.v = tMax.y - m_pVertexData[index].texCoords.v;
index = vIndexes[1];
m_pVertexData[index].texCoords.v = tMax.y - m_pVertexData[index].texCoords.v;
}
2010-08-11 18:15:52 +08:00
}
}
2010-08-11 18:15:52 +08:00
CCPoint CCProgressTimer::boundaryTexCoord(char index)
{
if (index < kProgressTextureCoordsCount)
{
switch (m_eType)
2010-08-11 18:15:52 +08:00
{
case kCCProgressTimerTypeRadialCW:
return ccp( (float)((kProgressTextureCoords>>((index<<1)+1))&1), (float)((kProgressTextureCoords>>(index<<1))&1) );
case kCCProgressTimerTypeRadialCCW:
return ccp( (float)((kProgressTextureCoords>>(7-(index<<1)))&1), (float)((kProgressTextureCoords>>(7-((index<<1)+1)))&1));
default:
break;
2010-08-11 18:15:52 +08:00
}
}
2010-08-11 18:15:52 +08:00
return CCPointZero;
}
void CCProgressTimer::draw(void)
{
if(! m_pVertexData)
{
return;
}
2010-08-11 18:15:52 +08:00
if(! m_pSprite)
{
return;
}
2010-08-11 18:15:52 +08:00
ccBlendFunc bf = m_pSprite->getBlendFunc();
bool newBlend = (bf.src != CC_BLEND_SRC || bf.dst != CC_BLEND_DST) ? true : false;
if (newBlend)
{
glBlendFunc(bf.src, bf.dst);
}
/// ========================================================================
// Replaced [texture_ drawAtPoint:CCPointZero] with my own vertexData
// Everything above me and below me is copied from CCTextureNode's draw
glBindTexture(GL_TEXTURE_2D, m_pSprite->getTexture()->getName());
glVertexPointer(2, GL_FLOAT, sizeof(ccV2F_C4B_T2F), &m_pVertexData[0].vertices);
glTexCoordPointer(2, GL_FLOAT, sizeof(ccV2F_C4B_T2F), &m_pVertexData[0].texCoords);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ccV2F_C4B_T2F), &m_pVertexData[0].colors);
if(m_eType == kCCProgressTimerTypeRadialCCW || m_eType == kCCProgressTimerTypeRadialCW)
{
glDrawArrays(GL_TRIANGLE_FAN, 0, m_nVertexDataCount);
} else
if ( m_eType == kCCProgressTimerTypeHorizontalBarLR ||
m_eType == kCCProgressTimerTypeHorizontalBarRL ||
m_eType == kCCProgressTimerTypeVerticalBarBT ||
m_eType == kCCProgressTimerTypeVerticalBarTB)
{
glDrawArrays(GL_TRIANGLE_STRIP, 0, m_nVertexDataCount);
}
//glDrawElements(GL_TRIANGLES, indicesCount_, GL_UNSIGNED_BYTE, indices_);
/// ========================================================================
if( newBlend )
{
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
}
2010-08-11 18:15:52 +08:00
}
} // namespace cocos2d