/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2010 Lam Pham
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.
****************************************************************************/
/**
* Radial Counter-Clockwise
* @type Number
* @constant
*/
cc.PROGRESS_TIMER_TYPE_RADIAL = 0;
/**
* Bar
* @type Number
* @constant
*/
cc.PROGRESS_TIMER_TYPE_BAR = 1;
/**
* @constant
* @type Number
*/
cc.PROGRESS_TEXTURE_COORDS_COUNT = 4;
/**
* @constant
* @type Number
*/
cc.PROGRESS_TEXTURE_COORDS = 0x4b;
/**
* cc.Progresstimer is a subclass of cc.Node.
* It renders the inner sprite according to the percentage.
* The progress can be Radial, Horizontal or vertical.
* @class
* @extends cc.NodeRGBA
*/
cc.ProgressTimer = cc.NodeRGBA.extend(/** @lends cc.ProgressTimer# */{
_type:null,
_percentage:0.0,
_sprite:null,
_midPoint:null,
_barChangeRate:null,
_reverseDirection:false,
/**
* Midpoint is used to modify the progress start position.
* If you're using radials type then the midpoint changes the center point
* If you're using bar type the the midpoint changes the bar growth
* it expands from the center but clamps to the sprites edge so:
* you want a left to right then set the midpoint all the way to cc.p(0,y)
* you want a right to left then set the midpoint all the way to cc.p(1,y)
* you want a bottom to top then set the midpoint all the way to cc.p(x,0)
* you want a top to bottom then set the midpoint all the way to cc.p(x,1)
* @return {cc.Point}
*/
getMidpoint:function () {
return cc.p(this._midPoint.x, this._midPoint);
},
/**
* Midpoint setter
* @param {cc.Point} mpoint
*/
setMidpoint:function (mpoint) {
this._midPoint = cc.pClamp(mpoint, cc.p(0, 0), cc.p(1, 1));
},
/**
* This allows the bar type to move the component at a specific rate
* Set the component to 0 to make sure it stays at 100%.
* For example you want a left to right bar but not have the height stay 100%
* Set the rate to be cc.p(0,1); and set the midpoint to = cc.p(0,.5f);
* @return {cc.Point}
*/
getBarChangeRate:function () {
return cc.p(this._barChangeRate.x, this._barChangeRate.y);
},
/**
* @param {cc.Point} barChangeRate
*/
setBarChangeRate:function (barChangeRate) {
this._barChangeRate = cc.pClamp(barChangeRate, cc.p(0, 0), cc.p(1, 1));
},
/**
* Change the percentage to change progress
* @return {cc.PROGRESS_TIMER_TYPE_RADIAL|cc.PROGRESS_TIMER_TYPE_BAR}
*/
getType:function () {
return this._type;
},
/**
* Percentages are from 0 to 100
* @return {Number}
*/
getPercentage:function () {
return this._percentage;
},
/**
* The image to show the progress percentage, retain
* @return {cc.Sprite}
*/
getSprite:function () {
return this._sprite;
},
/**
* from 0-100
* @param {Number} percentage
*/
setPercentage:function (percentage) {
if (this._percentage != percentage) {
this._percentage = cc.clampf(percentage, 0, 100);
this._updateProgress();
}
},
setOpacityModifyRGB:function (bValue) {
},
isOpacityModifyRGB:function () {
return false;
},
isReverseDirection:function () {
return this._reverseDirection;
},
_boundaryTexCoord:function (index) {
if (index < cc.PROGRESS_TEXTURE_COORDS_COUNT) {
var locProTextCoords = cc.PROGRESS_TEXTURE_COORDS;
if (this._reverseDirection)
return cc.p((locProTextCoords >> (7 - (index << 1))) & 1, (locProTextCoords >> (7 - ((index << 1) + 1))) & 1);
else
return cc.p((locProTextCoords >> ((index << 1) + 1)) & 1, (locProTextCoords >> (index << 1)) & 1);
}
return cc.PointZero();
},
_origin:null,
_startAngle:270,
_endAngle:270,
_radius:0,
_counterClockWise:false,
_barRect:null,
_vertexDataCount:0,
_vertexData:null,
_vertexArrayBuffer:null,
_vertexWebGLBuffer:null,
_vertexDataDirty:false,
ctor: null,
_ctorForCanvas: function () {
cc.NodeRGBA.prototype.ctor.call(this);
this._type = cc.PROGRESS_TIMER_TYPE_RADIAL;
this._percentage = 0.0;
this._midPoint = cc.p(0, 0);
this._barChangeRate = cc.p(0, 0);
this._reverseDirection = false;
this._sprite = null;
this._origin = cc.PointZero();
this._startAngle = 270;
this._endAngle = 270;
this._radius = 0;
this._counterClockWise = false;
this._barRect = cc.RectZero();
},
_ctorForWebGL: function () {
cc.NodeRGBA.prototype.ctor.call(this);
this._type = cc.PROGRESS_TIMER_TYPE_RADIAL;
this._percentage = 0.0;
this._midPoint = cc.p(0, 0);
this._barChangeRate = cc.p(0, 0);
this._reverseDirection = false;
this._sprite = null;
this._vertexWebGLBuffer = cc.renderContext.createBuffer();
this._vertexDataCount = 0;
this._vertexData = null;
this._vertexArrayBuffer = null;
this._vertexDataDirty = false;
},
/**
* set color of sprite
* @param {cc.Color3B} color
*/
setColor:function (color) {
this._sprite.setColor(color);
this._updateColor();
},
/**
* Opacity
* @param {Number} opacity
*/
setOpacity:function (opacity) {
this._sprite.setOpacity(opacity);
this._updateColor();
},
/**
* return color of sprite
* @return {cc.Color3B}
*/
getColor:function () {
return this._sprite.getColor();
},
/**
* return Opacity of sprite
* @return {Number}
*/
getOpacity:function () {
return this._sprite.getOpacity();
},
/**
* @param {Boolean} reverse
*/
setReverseProgress:null,
_setReverseProgressForCanvas:function (reverse) {
if (this._reverseDirection !== reverse)
this._reverseDirection = reverse;
},
_setReverseProgressForWebGL:function (reverse) {
if (this._reverseDirection !== reverse) {
this._reverseDirection = reverse;
// release all previous information
this._vertexData = null;
this._vertexArrayBuffer = null;
this._vertexDataCount = 0;
}
},
/**
* @param {cc.Sprite} sprite
*/
setSprite:null,
_setSpriteForCanvas:function (sprite) {
if (this._sprite != sprite) {
this._sprite = sprite;
this.setContentSize(this._sprite.getContentSize());
}
},
_setSpriteForWebGL:function (sprite) {
if (sprite && this._sprite != sprite) {
this._sprite = sprite;
this.setContentSize(sprite.getContentSize());
// Everytime we set a new sprite, we free the current vertex data
if (this._vertexData) {
this._vertexData = null;
this._vertexArrayBuffer = null;
this._vertexDataCount = 0;
}
}
},
/**
* set Progress type of cc.ProgressTimer
* @param {cc.PROGRESS_TIMER_TYPE_RADIAL|cc.PROGRESS_TIMER_TYPE_BAR} type
*/
setType:null,
_setTypeForCanvas:function (type) {
if (type !== this._type)
this._type = type;
},
_setTypeForWebGL:function (type) {
if (type !== this._type) {
// release all previous information
if (this._vertexData) {
this._vertexData = null;
this._vertexArrayBuffer = null;
this._vertexDataCount = 0;
}
this._type = type;
}
},
/**
* Reverse Progress setter
* @param {Boolean} reverse
*/
setReverseDirection: null,
_setReverseDirectionForCanvas: function (reverse) {
if (this._reverseDirection !== reverse)
this._reverseDirection = reverse;
},
_setReverseDirectionForWebGL: function (reverse) {
if (this._reverseDirection !== reverse) {
this._reverseDirection = reverse;
//release all previous information
this._vertexData = null;
this._vertexArrayBuffer = null;
this._vertexDataCount = 0;
}
},
/**
* @param {cc.Point} alpha
* @return {cc.Vertex2F | Object} the vertex position from the texture coordinate
* @private
*/
_textureCoordFromAlphaPoint:function (alpha) {
var locSprite = this._sprite;
if (!locSprite) {
return {u:0, v:0}; //new cc.Tex2F(0, 0);
}
var quad = locSprite.getQuad();
var min = cc.p(quad.bl.texCoords.u, quad.bl.texCoords.v);
var max = cc.p(quad.tr.texCoords.u, quad.tr.texCoords.v);
// Fix bug #1303 so that progress timer handles sprite frame texture rotation
if (locSprite.isTextureRectRotated()) {
var temp = alpha.x;
alpha.x = alpha.y;
alpha.y = temp;
}
return {u: min.x * (1 - alpha.x) + max.x * alpha.x, v: min.y * (1 - alpha.y) + max.y * alpha.y};
},
_vertexFromAlphaPoint:function (alpha) {
if (!this._sprite) {
return {x: 0, y: 0};
}
var quad = this._sprite.getQuad();
var min = cc.p(quad.bl.vertices.x, quad.bl.vertices.y);
var max = cc.p(quad.tr.vertices.x, quad.tr.vertices.y);
return {x: min.x * (1 - alpha.x) + max.x * alpha.x, y: min.y * (1 - alpha.y) + max.y * alpha.y};
},
/**
* Initializes a progress timer with the sprite as the shape the timer goes through
* @param {cc.Sprite} sprite
* @return {Boolean}
*/
initWithSprite:null,
_initWithSpriteForCanvas:function (sprite) {
this.setPercentage(0);
this.setAnchorPoint(0.5, 0.5);
this._type = cc.PROGRESS_TIMER_TYPE_RADIAL;
this._reverseDirection = false;
this.setMidpoint(cc.p(0.5, 0.5));
this.setBarChangeRate(cc.p(1, 1));
this.setSprite(sprite);
return true;
},
_initWithSpriteForWebGL:function (sprite) {
this.setPercentage(0);
this._vertexData = null;
this._vertexArrayBuffer = null;
this._vertexDataCount = 0;
this.setAnchorPoint(0.5, 0.5);
this._type = cc.PROGRESS_TIMER_TYPE_RADIAL;
this._reverseDirection = false;
this.setMidpoint(cc.p(0.5, 0.5));
this.setBarChangeRate(cc.p(1, 1));
this.setSprite(sprite);
//shader program
this.setShaderProgram(cc.ShaderCache.getInstance().programForKey(cc.SHADER_POSITION_TEXTURECOLOR));
return true;
},
/**
* stuff gets drawn here
* @param {CanvasRenderingContext2D} ctx
*/
draw:null,
_drawForCanvas:function (ctx) {
var context = ctx || cc.renderContext;
var locSprite = this._sprite;
if (locSprite._isLighterMode)
context.globalCompositeOperation = 'lighter';
var locEGL_ScaleX = cc.EGLView.getInstance().getScaleX(), locEGL_ScaleY = cc.EGLView.getInstance().getScaleY();
context.globalAlpha = locSprite._displayedOpacity / 255;
var locRect = locSprite._rect, locContentSize = locSprite._contentSize, locOffsetPosition = locSprite._offsetPosition, locDrawSizeCanvas = locSprite._drawSize_Canvas;
var flipXOffset = 0 | (locOffsetPosition.x), flipYOffset = -locOffsetPosition.y - locRect.height, locTextureCoord = locSprite._textureRect_Canvas;
locDrawSizeCanvas.width = locRect.width * locEGL_ScaleX;
locDrawSizeCanvas.height = locRect.height * locEGL_ScaleY;
context.save();
if (locSprite._flippedX) {
flipXOffset = -locOffsetPosition.x - locRect.width;
context.scale(-1, 1);
}
if (locSprite._flippedY) {
flipYOffset = locOffsetPosition.y;
context.scale(1, -1);
}
flipXOffset *= locEGL_ScaleX;
flipYOffset *= locEGL_ScaleY;
//clip
if (this._type == cc.PROGRESS_TIMER_TYPE_BAR) {
var locBarRect = this._barRect;
context.beginPath();
context.rect(locBarRect.x * locEGL_ScaleX, locBarRect.y * locEGL_ScaleY, locBarRect.width * locEGL_ScaleX, locBarRect.height * locEGL_ScaleY);
context.clip();
context.closePath();
} else if (this._type == cc.PROGRESS_TIMER_TYPE_RADIAL) {
var locOriginX = this._origin.x * locEGL_ScaleX;
var locOriginY = this._origin.y * locEGL_ScaleY;
context.beginPath();
context.arc(locOriginX, locOriginY, this._radius * locEGL_ScaleY, (Math.PI / 180) * this._startAngle, (Math.PI / 180) * this._endAngle, this._counterClockWise);
context.lineTo(locOriginX, locOriginY);
context.clip();
context.closePath();
}
//draw sprite
if (locSprite._texture && locTextureCoord.validRect) {
var image = locSprite._texture.getHtmlElementObj();
if (this._colorized) {
context.drawImage(image,
0, 0, locTextureCoord.width, locTextureCoord.height,
flipXOffset, flipYOffset, locDrawSizeCanvas.width, locDrawSizeCanvas.height);
} else {
context.drawImage(image,
locTextureCoord.x, locTextureCoord.y, locTextureCoord.width, locTextureCoord.height,
flipXOffset, flipYOffset, locDrawSizeCanvas.width , locDrawSizeCanvas.height);
}
} else if (locContentSize.width !== 0) {
var curColor = this.getColor();
context.fillStyle = "rgba(" + curColor.r + "," + curColor.g + "," + curColor.b + ",1)";
context.fillRect(flipXOffset, flipYOffset, locContentSize.width * locEGL_ScaleX, locContentSize.height * locEGL_ScaleY);
}
context.restore();
cc.INCREMENT_GL_DRAWS(1);
},
_drawForWebGL:function (ctx) {
var context = ctx || cc.renderContext;
if (!this._vertexData || !this._sprite)
return;
cc.NODE_DRAW_SETUP(this);
var blendFunc = this._sprite.getBlendFunc();
cc.glBlendFunc(blendFunc.src, blendFunc.dst);
cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
if (this._sprite.getTexture())
cc.glBindTexture2D(this._sprite.getTexture());
else
cc.glBindTexture2D(null);
context.bindBuffer(context.ARRAY_BUFFER, this._vertexWebGLBuffer);
if(this._vertexDataDirty){
context.bufferData(context.ARRAY_BUFFER, this._vertexArrayBuffer, context.DYNAMIC_DRAW);
this._vertexDataDirty = false;
}
var locVertexDataLen = cc.V2F_C4B_T2F.BYTES_PER_ELEMENT;
context.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, context.FLOAT, false, locVertexDataLen, 0);
context.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, context.UNSIGNED_BYTE, true, locVertexDataLen, 8);
context.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, context.FLOAT, false, locVertexDataLen, 12);
if (this._type === cc.PROGRESS_TIMER_TYPE_RADIAL)
context.drawArrays(context.TRIANGLE_FAN, 0, this._vertexDataCount);
else if (this._type == cc.PROGRESS_TIMER_TYPE_BAR) {
if (!this._reverseDirection)
context.drawArrays(context.TRIANGLE_STRIP, 0, this._vertexDataCount);
else {
context.drawArrays(context.TRIANGLE_STRIP, 0, this._vertexDataCount / 2);
context.drawArrays(context.TRIANGLE_STRIP, 4, this._vertexDataCount / 2);
// 2 draw calls
cc.g_NumberOfDraws++;
}
}
cc.g_NumberOfDraws++;
},
/**
*
* 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.
*
* 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.
*