2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 Lam Pham
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc
|
2012-04-19 14:35: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.
|
|
|
|
****************************************************************************/
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCProgressTimer.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCTextureCache.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "2d/CCDrawingPrimitives.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "renderer/CCGLProgramState.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCCustomCommand.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "math/TransformUtils.h"
|
2013-12-07 09:42:16 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// extern
|
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
#define kProgressTextureCoordsCount 4
|
|
|
|
// kProgressTextureCoords holds points {0,1} {0,0} {1,0} {1,1} we can represent it as bits
|
2013-06-20 14:13:12 +08:00
|
|
|
const char kProgressTextureCoords = 0x4b;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ProgressTimer::ProgressTimer()
|
2013-07-26 17:28:18 +08:00
|
|
|
:_type(Type::RADIAL)
|
2013-07-29 14:07:57 +08:00
|
|
|
,_midpoint(0,0)
|
|
|
|
,_barChangeRate(0,0)
|
2013-06-15 14:03:30 +08:00
|
|
|
,_percentage(0.0f)
|
2013-12-18 17:47:20 +08:00
|
|
|
,_sprite(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
,_vertexDataCount(0)
|
2013-12-18 17:47:20 +08:00
|
|
|
,_vertexData(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
,_reverseDirection(false)
|
2012-04-19 14:35:52 +08:00
|
|
|
{}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ProgressTimer* ProgressTimer::create(Sprite* sp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ProgressTimer *progressTimer = new ProgressTimer();
|
|
|
|
if (progressTimer->initWithSprite(sp))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
progressTimer->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
delete progressTimer;
|
|
|
|
progressTimer = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return progressTimer;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ProgressTimer::initWithSprite(Sprite* sp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
setPercentage(0.0f);
|
2013-12-18 17:47:20 +08:00
|
|
|
_vertexData = nullptr;
|
2013-06-15 14:03:30 +08:00
|
|
|
_vertexDataCount = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
setAnchorPoint(Vector2(0.5f,0.5f));
|
2013-07-26 17:28:18 +08:00
|
|
|
_type = Type::RADIAL;
|
2013-06-15 14:03:30 +08:00
|
|
|
_reverseDirection = false;
|
2014-04-15 18:13:57 +08:00
|
|
|
setMidpoint(Vector2(0.5f, 0.5f));
|
|
|
|
setBarChangeRate(Vector2(1,1));
|
2012-04-19 14:35:52 +08:00
|
|
|
setSprite(sp);
|
2014-05-10 09:39:25 +08:00
|
|
|
|
|
|
|
// shader state
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ProgressTimer::~ProgressTimer(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_vertexData);
|
|
|
|
CC_SAFE_RELEASE(_sprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
void ProgressTimer::setPercentage(float percentage)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
if (_percentage != percentage)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
_percentage = clampf(percentage, 0, 100);
|
2012-04-19 14:35:52 +08:00
|
|
|
updateProgress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
void ProgressTimer::setSprite(Sprite *sprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
if (_sprite != sprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_RETAIN(sprite);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_sprite);
|
2013-12-18 17:47:20 +08:00
|
|
|
_sprite = sprite;
|
2013-06-15 14:03:30 +08:00
|
|
|
setContentSize(_sprite->getContentSize());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// Every time we set a new sprite, we free the current vertex data
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_vertexData)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_vertexData);
|
|
|
|
_vertexDataCount = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 15:11:36 +08:00
|
|
|
void ProgressTimer::setType(Type type)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (type != _type)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// release all previous information
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_vertexData)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_vertexData);
|
2013-12-18 17:47:20 +08:00
|
|
|
_vertexData = nullptr;
|
2013-06-15 14:03:30 +08:00
|
|
|
_vertexDataCount = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_type = type;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProgressTimer::setReverseProgress(bool reverse)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _reverseDirection != reverse ) {
|
|
|
|
_reverseDirection = reverse;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// release all previous information
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_vertexData);
|
|
|
|
_vertexDataCount = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interval
|
|
|
|
|
2010-12-25 15:09:42 +08:00
|
|
|
///
|
2012-04-19 14:35:52 +08:00
|
|
|
// @returns the vertex position from the texture coordinate
|
2010-12-25 15:09:42 +08:00
|
|
|
///
|
2014-04-15 18:13:57 +08:00
|
|
|
Tex2F ProgressTimer::textureCoordFromAlphaPoint(Vector2 alpha)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-07 21:08:14 +08:00
|
|
|
Tex2F ret(0.0f, 0.0f);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_sprite) {
|
2012-04-19 14:35:52 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad quad = _sprite->getQuad();
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 min = Vector2(quad.bl.texCoords.u,quad.bl.texCoords.v);
|
|
|
|
Vector2 max = Vector2(quad.tr.texCoords.u,quad.tr.texCoords.v);
|
2012-04-19 14:35:52 +08:00
|
|
|
// Fix bug #1303 so that progress timer handles sprite frame texture rotation
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_sprite->isTextureRectRotated()) {
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SWAP(alpha.x, alpha.y, float);
|
|
|
|
}
|
2013-07-05 16:49:22 +08:00
|
|
|
return Tex2F(min.x * (1.f - alpha.x) + max.x * alpha.x, min.y * (1.f - alpha.y) + max.y * alpha.y);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 ProgressTimer::vertexFromAlphaPoint(Vector2 alpha)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 ret(0.0f, 0.0f);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_sprite) {
|
2012-04-19 14:35:52 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad quad = _sprite->getQuad();
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 min = Vector2(quad.bl.vertices.x,quad.bl.vertices.y);
|
|
|
|
Vector2 max = Vector2(quad.tr.vertices.x,quad.tr.vertices.y);
|
2012-04-19 14:35:52 +08:00
|
|
|
ret.x = min.x * (1.f - alpha.x) + max.x * alpha.x;
|
|
|
|
ret.y = min.y * (1.f - alpha.y) + max.y * alpha.y;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProgressTimer::updateColor(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_sprite) {
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_vertexData)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
Color4B sc = _sprite->getQuad().tl.colors;
|
2013-06-15 14:03:30 +08:00
|
|
|
for (int i = 0; i < _vertexDataCount; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_vertexData[i].colors = sc;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProgressTimer::updateProgress(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (_type)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 17:28:18 +08:00
|
|
|
case Type::RADIAL:
|
2012-04-19 14:35:52 +08:00
|
|
|
updateRadial();
|
|
|
|
break;
|
2013-07-26 17:28:18 +08:00
|
|
|
case Type::BAR:
|
2012-04-19 14:35:52 +08:00
|
|
|
updateBar();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 16:47:29 +08:00
|
|
|
void ProgressTimer::setAnchorPoint(const Vector2& anchorPoint)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::setAnchorPoint(anchorPoint);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 ProgressTimer::getMidpoint() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _midpoint;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-01-07 16:23:58 +08:00
|
|
|
void ProgressTimer::setColor(const Color3B &color)
|
|
|
|
{
|
|
|
|
_sprite->setColor(color);
|
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Color3B& ProgressTimer::getColor() const
|
|
|
|
{
|
|
|
|
return _sprite->getColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressTimer::setOpacity(GLubyte opacity)
|
|
|
|
{
|
|
|
|
_sprite->setOpacity(opacity);
|
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
GLubyte ProgressTimer::getOpacity() const
|
|
|
|
{
|
|
|
|
return _sprite->getOpacity();
|
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
void ProgressTimer::setMidpoint(const Vector2& midPoint)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
_midpoint = midPoint.getClampPoint(Vector2::ZERO, Vector2(1, 1));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2010-12-25 15:09:42 +08:00
|
|
|
///
|
2012-04-19 14:35: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.
|
|
|
|
///
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProgressTimer::updateRadial(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_sprite) {
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
float alpha = _percentage / 100.f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float angle = 2.f*((float)M_PI) * ( _reverseDirection ? alpha : 1.0f - alpha);
|
2012-04-19 14:35: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
|
2013-06-15 14:03:30 +08:00
|
|
|
// from that by the progress angle around the _midpoint pivot
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 topMid = Vector2(_midpoint.x, 1.f);
|
|
|
|
Vector2 percentagePt = topMid.rotateByAngle(_midpoint, angle);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
int index = 0;
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 hit = Vector2::ZERO;
|
2012-04-19 14:35: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;
|
|
|
|
|
|
|
|
for (int i = 0; i <= kProgressTextureCoordsCount; ++i) {
|
|
|
|
int pIndex = (i + (kProgressTextureCoordsCount - 1))%kProgressTextureCoordsCount;
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 edgePtA = boundaryTexCoord(i % kProgressTextureCoordsCount);
|
|
|
|
Vector2 edgePtB = boundaryTexCoord(pIndex);
|
2012-04-19 14:35: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){
|
2013-07-11 16:38:58 +08:00
|
|
|
edgePtB = edgePtA.lerp(edgePtB, 1-_midpoint.x);
|
2012-04-19 14:35:52 +08:00
|
|
|
} else if(i == 4){
|
2013-07-11 16:38:58 +08:00
|
|
|
edgePtA = edgePtA.lerp(edgePtB, 1-_midpoint.x);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// s and t are returned by ccpLineIntersect
|
|
|
|
float s = 0, t = 0;
|
2014-04-15 18:13:57 +08:00
|
|
|
if(Vector2::isLineIntersect(edgePtA, edgePtB, _midpoint, percentagePt, &s, &t))
|
2012-04-19 14:35: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)) {
|
|
|
|
// s represents the point between edgePtA--edgePtB
|
|
|
|
if (!(0.f <= s && s <= 1.f)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// As long as our t isn't negative we are at least finding a
|
2013-06-15 14:03:30 +08:00
|
|
|
// correct hitpoint from _midpoint to percentagePt.
|
2012-04-19 14:35:52 +08:00
|
|
|
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) {
|
|
|
|
min_t = t;
|
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have the minimum magnitude we can use that to find our intersection
|
2013-07-11 16:38:58 +08:00
|
|
|
hit = _midpoint+ ((percentagePt - _midpoint) * min_t);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// The size of the vertex data is the index from the hitpoint
|
2013-06-15 14:03:30 +08:00
|
|
|
// the 3 is for the _midpoint, 12 o'clock point and hitpoint position.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
bool sameIndexCount = true;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_vertexDataCount != index + 3){
|
2012-04-19 14:35:52 +08:00
|
|
|
sameIndexCount = false;
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_vertexData);
|
|
|
|
_vertexDataCount = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(!_vertexData) {
|
|
|
|
_vertexDataCount = index + 3;
|
2013-07-05 16:49:22 +08:00
|
|
|
_vertexData = (V2F_C4B_T2F*)malloc(_vertexDataCount * sizeof(V2F_C4B_T2F));
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( _vertexData, "CCProgressTimer. Not enough memory");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
updateColor();
|
|
|
|
|
|
|
|
if (!sameIndexCount) {
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// First we populate the array with the _midpoint, then all
|
2012-04-19 14:35:52 +08:00
|
|
|
// vertices/texcoords/colors of the 12 'o clock start and edges and the hitpoint
|
2013-06-15 14:03:30 +08:00
|
|
|
_vertexData[0].texCoords = textureCoordFromAlphaPoint(_midpoint);
|
|
|
|
_vertexData[0].vertices = vertexFromAlphaPoint(_midpoint);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_vertexData[1].texCoords = textureCoordFromAlphaPoint(topMid);
|
|
|
|
_vertexData[1].vertices = vertexFromAlphaPoint(topMid);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
for(int i = 0; i < index; ++i){
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 alphaPoint = boundaryTexCoord(i);
|
2013-06-15 14:03:30 +08:00
|
|
|
_vertexData[i+2].texCoords = textureCoordFromAlphaPoint(alphaPoint);
|
|
|
|
_vertexData[i+2].vertices = vertexFromAlphaPoint(alphaPoint);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// hitpoint will go last
|
2013-06-15 14:03:30 +08:00
|
|
|
_vertexData[_vertexDataCount - 1].texCoords = textureCoordFromAlphaPoint(hit);
|
|
|
|
_vertexData[_vertexDataCount - 1].vertices = vertexFromAlphaPoint(hit);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
// 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.
|
|
|
|
///
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProgressTimer::updateBar(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_sprite) {
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
float alpha = _percentage / 100.0f;
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 alphaOffset = Vector2(1.0f * (1.0f - _barChangeRate.x) + alpha * _barChangeRate.x, 1.0f * (1.0f - _barChangeRate.y) + alpha * _barChangeRate.y) * 0.5f;
|
|
|
|
Vector2 min = _midpoint - alphaOffset;
|
|
|
|
Vector2 max = _midpoint + alphaOffset;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (min.x < 0.f) {
|
|
|
|
max.x += -min.x;
|
|
|
|
min.x = 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max.x > 1.f) {
|
|
|
|
min.x -= max.x - 1.f;
|
|
|
|
max.x = 1.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min.y < 0.f) {
|
|
|
|
max.y += -min.y;
|
|
|
|
min.y = 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max.y > 1.f) {
|
|
|
|
min.y -= max.y - 1.f;
|
|
|
|
max.y = 1.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_reverseDirection) {
|
|
|
|
if(!_vertexData) {
|
|
|
|
_vertexDataCount = 4;
|
2013-07-05 16:49:22 +08:00
|
|
|
_vertexData = (V2F_C4B_T2F*)malloc(_vertexDataCount * sizeof(V2F_C4B_T2F));
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( _vertexData, "CCProgressTimer. Not enough memory");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
// TOPLEFT
|
2014-04-15 18:13:57 +08:00
|
|
|
_vertexData[0].texCoords = textureCoordFromAlphaPoint(Vector2(min.x,max.y));
|
|
|
|
_vertexData[0].vertices = vertexFromAlphaPoint(Vector2(min.x,max.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// BOTLEFT
|
2014-04-15 18:13:57 +08:00
|
|
|
_vertexData[1].texCoords = textureCoordFromAlphaPoint(Vector2(min.x,min.y));
|
|
|
|
_vertexData[1].vertices = vertexFromAlphaPoint(Vector2(min.x,min.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// TOPRIGHT
|
2014-04-15 18:13:57 +08:00
|
|
|
_vertexData[2].texCoords = textureCoordFromAlphaPoint(Vector2(max.x,max.y));
|
|
|
|
_vertexData[2].vertices = vertexFromAlphaPoint(Vector2(max.x,max.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// BOTRIGHT
|
2014-04-15 18:13:57 +08:00
|
|
|
_vertexData[3].texCoords = textureCoordFromAlphaPoint(Vector2(max.x,min.y));
|
|
|
|
_vertexData[3].vertices = vertexFromAlphaPoint(Vector2(max.x,min.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
} else {
|
2013-06-15 14:03:30 +08:00
|
|
|
if(!_vertexData) {
|
|
|
|
_vertexDataCount = 8;
|
2013-07-05 16:49:22 +08:00
|
|
|
_vertexData = (V2F_C4B_T2F*)malloc(_vertexDataCount * sizeof(V2F_C4B_T2F));
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( _vertexData, "CCProgressTimer. Not enough memory");
|
2012-04-19 14:35:52 +08:00
|
|
|
// TOPLEFT 1
|
2014-04-15 18:13:57 +08:00
|
|
|
_vertexData[0].texCoords = textureCoordFromAlphaPoint(Vector2(0,1));
|
|
|
|
_vertexData[0].vertices = vertexFromAlphaPoint(Vector2(0,1));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// BOTLEFT 1
|
2014-04-15 18:13:57 +08:00
|
|
|
_vertexData[1].texCoords = textureCoordFromAlphaPoint(Vector2(0,0));
|
|
|
|
_vertexData[1].vertices = vertexFromAlphaPoint(Vector2(0,0));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// TOPRIGHT 2
|
2014-04-15 18:13:57 +08:00
|
|
|
_vertexData[6].texCoords = textureCoordFromAlphaPoint(Vector2(1,1));
|
|
|
|
_vertexData[6].vertices = vertexFromAlphaPoint(Vector2(1,1));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// BOTRIGHT 2
|
2014-04-15 18:13:57 +08:00
|
|
|
_vertexData[7].texCoords = textureCoordFromAlphaPoint(Vector2(1,0));
|
|
|
|
_vertexData[7].vertices = vertexFromAlphaPoint(Vector2(1,0));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TOPRIGHT 1
|
2014-04-15 18:13:57 +08:00
|
|
|
_vertexData[2].texCoords = textureCoordFromAlphaPoint(Vector2(min.x,max.y));
|
|
|
|
_vertexData[2].vertices = vertexFromAlphaPoint(Vector2(min.x,max.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// BOTRIGHT 1
|
2014-04-15 18:13:57 +08:00
|
|
|
_vertexData[3].texCoords = textureCoordFromAlphaPoint(Vector2(min.x,min.y));
|
|
|
|
_vertexData[3].vertices = vertexFromAlphaPoint(Vector2(min.x,min.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// TOPLEFT 2
|
2014-04-15 18:13:57 +08:00
|
|
|
_vertexData[4].texCoords = textureCoordFromAlphaPoint(Vector2(max.x,max.y));
|
|
|
|
_vertexData[4].vertices = vertexFromAlphaPoint(Vector2(max.x,max.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// BOTLEFT 2
|
2014-04-15 18:13:57 +08:00
|
|
|
_vertexData[5].texCoords = textureCoordFromAlphaPoint(Vector2(max.x,min.y));
|
|
|
|
_vertexData[5].vertices = vertexFromAlphaPoint(Vector2(max.x,min.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 ProgressTimer::boundaryTexCoord(char index)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (index < kProgressTextureCoordsCount) {
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_reverseDirection) {
|
2014-04-15 18:13:57 +08:00
|
|
|
return Vector2((kProgressTextureCoords>>(7-(index<<1)))&1,(kProgressTextureCoords>>(7-((index<<1)+1)))&1);
|
2012-04-19 14:35:52 +08:00
|
|
|
} else {
|
2014-04-15 18:13:57 +08:00
|
|
|
return Vector2((kProgressTextureCoords>>((index<<1)+1))&1,(kProgressTextureCoords>>(index<<1))&1);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2014-04-15 18:13:57 +08:00
|
|
|
return Vector2::ZERO;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-09 11:21:05 +08:00
|
|
|
void ProgressTimer::onDraw(const Matrix &transform, bool transformUpdated)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
2014-05-09 07:42:36 +08:00
|
|
|
getGLProgram()->use();
|
|
|
|
getGLProgram()->setUniformsForBuiltins(transform);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::blendFunc( _sprite->getBlendFunc().src, _sprite->getBlendFunc().dst );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2D( _sprite->getTexture()->getName() );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-04-09 12:07:49 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2013-07-05 16:49:22 +08:00
|
|
|
setGLBufferData((void*) _vertexData, (_vertexDataCount * sizeof(V2F_C4B_T2F)), 0);
|
2013-04-09 12:07:49 +08:00
|
|
|
|
|
|
|
int offset = 0;
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid*)offset);
|
2013-04-09 12:07:49 +08:00
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
offset += sizeof(Vector2);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid*)offset);
|
2013-04-09 12:07:49 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
offset += sizeof(Color4B);
|
2014-05-09 03:34:26 +08:00
|
|
|
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid*)offset);
|
2013-04-09 12:07:49 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(_vertexData[0]) , &_vertexData[0].vertices);
|
2014-05-09 03:34:26 +08:00
|
|
|
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(_vertexData[0]), &_vertexData[0].texCoords);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(_vertexData[0]), &_vertexData[0].colors);
|
2013-04-09 12:07:49 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 17:28:18 +08:00
|
|
|
if(_type == Type::RADIAL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, _vertexDataCount);
|
2014-03-01 14:03:36 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_vertexDataCount);
|
|
|
|
}
|
2013-07-26 17:28:18 +08:00
|
|
|
else if (_type == Type::BAR)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_reverseDirection)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, _vertexDataCount);
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_vertexDataCount);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, _vertexDataCount/2);
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 4, _vertexDataCount/2);
|
2012-04-19 14:35:52 +08:00
|
|
|
// 2 draw calls
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(2,_vertexDataCount);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-08 22:07:35 +08:00
|
|
|
void ProgressTimer::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
|
2013-12-07 09:42:16 +08:00
|
|
|
{
|
|
|
|
if( ! _vertexData || ! _sprite)
|
|
|
|
return;
|
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
_customCommand.init(_globalZOrder);
|
2014-03-06 07:49:08 +08:00
|
|
|
_customCommand.func = CC_CALLBACK_0(ProgressTimer::onDraw, this, transform, transformUpdated);
|
2014-03-01 08:10:48 +08:00
|
|
|
renderer->addCommand(&_customCommand);
|
2013-12-07 09:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|