/**************************************************************************** Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2011-2012 cocos2d-x.org Copyright (c) 2013-2014 Chukong Technologies Inc. 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. ****************************************************************************/ /** * 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.Node * * @property {cc.Point} midPoint

- 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)

* @property {cc.Point} barChangeRate - This allows the bar type to move the component at a specific rate. * @property {enum} type - Type of the progress timer: cc.ProgressTimer.TYPE_RADIAL|cc.ProgressTimer.TYPE_BAR. * @property {Number} percentage - Percentage to change progress, from 0 to 100. * @property {cc.Sprite} sprite - The sprite to show the progress percentage. * @property {Boolean} reverseDir - Indicate whether the direction is reversed. * */ cc.ProgressTimer = cc.Node.extend(/** @lends cc.ProgressTimer# */{ _type:null, _percentage:0.0, _sprite:null, _midPoint:null, _barChangeRate:null, _reverseDirection:false, _className:"ProgressTimer", /** * 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.y); }, /** * 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.ProgressTimer.TYPE_RADIAL|cc.ProgressTimer.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(); } }, /** * only use for jsbinding * @param bValue */ setOpacityModifyRGB:function (bValue) { }, /** * only use for jsbinding * @returns {boolean} */ isOpacityModifyRGB:function () { return false; }, /** * return if reverse direction * @returns {boolean} */ isReverseDirection:function () { return this._reverseDirection; }, _boundaryTexCoord:function (index) { if (index < cc.ProgressTimer.TEXTURE_COORDS_COUNT) { var locProTextCoords = cc.ProgressTimer.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.p(0,0); }, _origin:null, _startAngle:270, _endAngle:270, _radius:0, _counterClockWise:false, _barRect:null, _vertexDataCount:0, _vertexData:null, _vertexArrayBuffer:null, _vertexWebGLBuffer:null, _vertexDataDirty:false, /** * constructor of cc.cc.ProgressTimer * @function * @param {cc.Sprite} sprite */ ctor: null, _ctorForCanvas: function (sprite) { cc.Node.prototype.ctor.call(this); this._type = cc.ProgressTimer.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.p(0,0); this._startAngle = 270; this._endAngle = 270; this._radius = 0; this._counterClockWise = false; this._barRect = cc.rect(0, 0, 0, 0); sprite && this._initWithSpriteForCanvas(sprite); }, _ctorForWebGL: function (sprite) { cc.Node.prototype.ctor.call(this); this._type = cc.ProgressTimer.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; sprite && this._initWithSpriteForWebGL(sprite); }, /** * set color of sprite * @param {cc.Color} color */ setColor:function (color) { this._sprite.color = color; this._updateColor(); }, /** * set opacity of sprite * @param {Number} opacity */ setOpacity:function (opacity) { this._sprite.opacity = opacity; this._updateColor(); }, /** * return color of sprite * @return {cc.Color} */ getColor:function () { return this._sprite.color; }, /** * return Opacity of sprite * @return {Number} */ getOpacity:function () { return this._sprite.opacity; }, /** * set reverse cc.ProgressTimer * @function * @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; } }, /** * set sprite for cc.ProgressTimer * @function * @param {cc.Sprite} sprite */ setSprite:null, _setSpriteForCanvas:function (sprite) { if (this._sprite != sprite) { this._sprite = sprite; this.width = this._sprite.width; this.height = this._sprite.height; } }, _setSpriteForWebGL:function (sprite) { if (sprite && this._sprite != sprite) { this._sprite = sprite; this.width = sprite.width; this.height = sprite.height; // 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 * @function * @param {cc.ProgressTimer.TYPE_RADIAL|cc.ProgressTimer.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 * @function * @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.quad; 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.textureRectRotated) { 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.quad; 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 * @function * @param {cc.Sprite} sprite * @return {Boolean} */ initWithSprite:null, _initWithSpriteForCanvas:function (sprite) { this.percentage = 0; this.anchorX = 0.5; this.anchorY = 0.5; this._type = cc.ProgressTimer.TYPE_RADIAL; this._reverseDirection = false; this.midPoint = cc.p(0.5, 0.5); this.barChangeRate = cc.p(1, 1); this.sprite = sprite; return true; }, _initWithSpriteForWebGL:function (sprite) { this.percentage = 0; this._vertexData = null; this._vertexArrayBuffer = null; this._vertexDataCount = 0; this.anchorX = 0.5; this.anchorY = 0.5; this._type = cc.ProgressTimer.TYPE_RADIAL; this._reverseDirection = false; this.midPoint = cc.p(0.5, 0.5); this.barChangeRate = cc.p(1, 1); this.sprite = sprite; //shader program this.shaderProgram = cc.shaderCache.programForKey(cc.SHADER_POSITION_TEXTURECOLOR); return true; }, /** * Stuff gets drawn here * @function * @param {CanvasRenderingContext2D} ctx */ draw:null, _drawForCanvas:function (ctx) { var context = ctx || cc._renderContext; var locSprite = this._sprite; if (locSprite._blendFuncStr != "source") context.globalCompositeOperation = locSprite._blendFuncStr; var locEGL_ScaleX = cc.view.getScaleX(), locEGL_ScaleY = cc.view.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.ProgressTimer.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.ProgressTimer.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 (locSprite._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.color; 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.incrementGLDraws(1); }, _drawForWebGL:function (ctx) { var context = ctx || cc._renderContext; if (!this._vertexData || !this._sprite) return; cc.nodeDrawSetup(this); var blendFunc = this._sprite.getBlendFunc(); cc.glBlendFunc(blendFunc.src, blendFunc.dst); cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX); cc.glBindTexture2D(this._sprite.texture); 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.ProgressTimer.TYPE_RADIAL) context.drawArrays(context.TRIANGLE_FAN, 0, this._vertexDataCount); else if (this._type == cc.ProgressTimer.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.
*

* @private */ _updateRadial:function () { if (!this._sprite) return; var i, locMidPoint = this._midPoint; var alpha = this._percentage / 100; var angle = 2 * (cc.PI) * ( this._reverseDirection ? alpha : 1.0 - 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 m_tMidpoint pivot var topMid = cc.p(locMidPoint.x, 1); var percentagePt = cc.pRotateByAngle(topMid, locMidPoint, angle); var index = 0; var hit; if (alpha == 0) { // 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) { // 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 var min_t = cc.FLT_MAX; var locProTextCoordsCount = cc.ProgressTimer.TEXTURE_COORDS_COUNT; for (i = 0; i <= locProTextCoordsCount; ++i) { var pIndex = (i + (locProTextCoordsCount - 1)) % locProTextCoordsCount; var edgePtA = this._boundaryTexCoord(i % locProTextCoordsCount); var edgePtB = this._boundaryTexCoord(pIndex); // 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 = cc.pLerp(edgePtA, edgePtB, 1 - locMidPoint.x); else if (i == 4) edgePtA = cc.pLerp(edgePtA, edgePtB, 1 - locMidPoint.x); // retPoint are returned by ccpLineIntersect var retPoint = cc.p(0, 0); if (cc.pLineIntersect(edgePtA, edgePtB, locMidPoint, percentagePt, retPoint)) { // 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 <= retPoint.x && retPoint.x <= 1)) continue; } // As long as our t isn't negative we are at least finding a // correct hitpoint from m_tMidpoint to percentagePt. if (retPoint.y >= 0) { // Because the percentage line and all the texture edges are // rays we should only account for the shortest intersection if (retPoint.y < min_t) { min_t = retPoint.y; index = i; } } } } // Now that we have the minimum magnitude we can use that to find our intersection hit = cc.pAdd(locMidPoint, cc.pMult(cc.pSub(percentagePt, locMidPoint), min_t)); } // The size of the vertex data is the index from the hitpoint // the 3 is for the m_tMidpoint, 12 o'clock point and hitpoint position. var sameIndexCount = true; if (this._vertexDataCount != index + 3) { sameIndexCount = false; this._vertexData = null; this._vertexArrayBuffer = null; this._vertexDataCount = 0; } if (!this._vertexData) { this._vertexDataCount = index + 3; var locCount = this._vertexDataCount, vertexDataLen = cc.V2F_C4B_T2F.BYTES_PER_ELEMENT; this._vertexArrayBuffer = new ArrayBuffer(locCount * vertexDataLen); var locData = []; for (i = 0; i < locCount; i++) locData[i] = new cc.V2F_C4B_T2F(null, null, null, this._vertexArrayBuffer, i * vertexDataLen); this._vertexData = locData; if(!this._vertexData){ cc.log( "cc.ProgressTimer._updateRadial() : Not enough memory"); return; } } this._updateColor(); var locVertexData = this._vertexData; if (!sameIndexCount) { // First we populate the array with the m_tMidpoint, then all // vertices/texcoords/colors of the 12 'o clock start and edges and the hitpoint locVertexData[0].texCoords = this._textureCoordFromAlphaPoint(locMidPoint); locVertexData[0].vertices = this._vertexFromAlphaPoint(locMidPoint); locVertexData[1].texCoords = this._textureCoordFromAlphaPoint(topMid); locVertexData[1].vertices = this._vertexFromAlphaPoint(topMid); for (i = 0; i < index; i++) { var alphaPoint = this._boundaryTexCoord(i); locVertexData[i + 2].texCoords = this._textureCoordFromAlphaPoint(alphaPoint); locVertexData[i + 2].vertices = this._vertexFromAlphaPoint(alphaPoint); } } // hitpoint will go last locVertexData[this._vertexDataCount - 1].texCoords = this._textureCoordFromAlphaPoint(hit); locVertexData[this._vertexDataCount - 1].vertices = this._vertexFromAlphaPoint(hit); }, /** *

* 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.
*

* @private */ _updateBar:function () { if (!this._sprite) return; var i; var alpha = this._percentage / 100.0; var locBarChangeRate = this._barChangeRate; var alphaOffset = cc.pMult(cc.p((1.0 - locBarChangeRate.x) + alpha * locBarChangeRate.x, (1.0 - locBarChangeRate.y) + alpha * locBarChangeRate.y), 0.5); var min = cc.pSub(this._midPoint, alphaOffset); var max = cc.pAdd(this._midPoint, alphaOffset); if (min.x < 0) { max.x += -min.x; min.x = 0; } if (max.x > 1) { min.x -= max.x - 1; max.x = 1; } if (min.y < 0) { max.y += -min.y; min.y = 0; } if (max.y > 1) { min.y -= max.y - 1; max.y = 1; } var locVertexData; if (!this._reverseDirection) { if (!this._vertexData) { this._vertexDataCount = 4; var vertexDataLen = cc.V2F_C4B_T2F.BYTES_PER_ELEMENT, locCount = 4; this._vertexArrayBuffer = new ArrayBuffer(locCount * vertexDataLen); this._vertexData = []; for (i = 0; i < locCount; i++) this._vertexData[i] = new cc.V2F_C4B_T2F(null, null, null, this._vertexArrayBuffer, i * vertexDataLen); } locVertexData = this._vertexData; // TOPLEFT locVertexData[0].texCoords = this._textureCoordFromAlphaPoint(cc.p(min.x, max.y)); locVertexData[0].vertices = this._vertexFromAlphaPoint(cc.p(min.x, max.y)); // BOTLEFT locVertexData[1].texCoords = this._textureCoordFromAlphaPoint(cc.p(min.x, min.y)); locVertexData[1].vertices = this._vertexFromAlphaPoint(cc.p(min.x, min.y)); // TOPRIGHT locVertexData[2].texCoords = this._textureCoordFromAlphaPoint(cc.p(max.x, max.y)); locVertexData[2].vertices = this._vertexFromAlphaPoint(cc.p(max.x, max.y)); // BOTRIGHT locVertexData[3].texCoords = this._textureCoordFromAlphaPoint(cc.p(max.x, min.y)); locVertexData[3].vertices = this._vertexFromAlphaPoint(cc.p(max.x, min.y)); } else { if (!this._vertexData) { this._vertexDataCount = 8; var rVertexDataLen = cc.V2F_C4B_T2F.BYTES_PER_ELEMENT, rLocCount = 8; this._vertexArrayBuffer = new ArrayBuffer(rLocCount * rVertexDataLen); var rTempData = []; for (i = 0; i < rLocCount; i++) rTempData[i] = new cc.V2F_C4B_T2F(null, null, null, this._vertexArrayBuffer, i * rVertexDataLen); // TOPLEFT 1 rTempData[0].texCoords = this._textureCoordFromAlphaPoint(cc.p(0, 1)); rTempData[0].vertices = this._vertexFromAlphaPoint(cc.p(0, 1)); // BOTLEFT 1 rTempData[1].texCoords = this._textureCoordFromAlphaPoint(cc.p(0, 0)); rTempData[1].vertices = this._vertexFromAlphaPoint(cc.p(0, 0)); // TOPRIGHT 2 rTempData[6].texCoords = this._textureCoordFromAlphaPoint(cc.p(1, 1)); rTempData[6].vertices = this._vertexFromAlphaPoint(cc.p(1, 1)); // BOTRIGHT 2 rTempData[7].texCoords = this._textureCoordFromAlphaPoint(cc.p(1, 0)); rTempData[7].vertices = this._vertexFromAlphaPoint(cc.p(1, 0)); this._vertexData = rTempData; } locVertexData = this._vertexData; // TOPRIGHT 1 locVertexData[2].texCoords = this._textureCoordFromAlphaPoint(cc.p(min.x, max.y)); locVertexData[2].vertices = this._vertexFromAlphaPoint(cc.p(min.x, max.y)); // BOTRIGHT 1 locVertexData[3].texCoords = this._textureCoordFromAlphaPoint(cc.p(min.x, min.y)); locVertexData[3].vertices = this._vertexFromAlphaPoint(cc.p(min.x, min.y)); // TOPLEFT 2 locVertexData[4].texCoords = this._textureCoordFromAlphaPoint(cc.p(max.x, max.y)); locVertexData[4].vertices = this._vertexFromAlphaPoint(cc.p(max.x, max.y)); // BOTLEFT 2 locVertexData[5].texCoords = this._textureCoordFromAlphaPoint(cc.p(max.x, min.y)); locVertexData[5].vertices = this._vertexFromAlphaPoint(cc.p(max.x, min.y)); } this._updateColor(); }, _updateColor:function () { if (!this._sprite || !this._vertexData) return; var sc = this._sprite.quad.tl.colors; var locVertexData = this._vertexData; for (var i = 0, len = this._vertexDataCount; i < len; ++i) locVertexData[i].colors = sc; this._vertexDataDirty = true; }, _updateProgress:null, _updateProgressForCanvas:function () { var locSprite = this._sprite; var sw = locSprite.width, sh = locSprite.height; var locMidPoint = this._midPoint; if (this._type == cc.ProgressTimer.TYPE_RADIAL) { this._radius = Math.round(Math.sqrt(sw * sw + sh * sh)); var locStartAngle, locEndAngle, locCounterClockWise = false, locOrigin = this._origin; locOrigin.x = sw * locMidPoint.x; locOrigin.y = -sh * locMidPoint.y; if (this._reverseDirection) { locEndAngle = 270; locStartAngle = 270 - 3.6 * this._percentage; } else { locStartAngle = -90; locEndAngle = -90 + 3.6 * this._percentage; } if (locSprite._flippedX) { locOrigin.x -= sw * (this._midPoint.x * 2); locStartAngle= -locStartAngle; locEndAngle= -locEndAngle; locStartAngle -= 180; locEndAngle -= 180; locCounterClockWise = !locCounterClockWise; } if (locSprite._flippedY) { locOrigin.y+=sh*(this._midPoint.y*2); locCounterClockWise = !locCounterClockWise; locStartAngle= -locStartAngle; locEndAngle= -locEndAngle; } this._startAngle = locStartAngle; this._endAngle = locEndAngle; this._counterClockWise = locCounterClockWise; } else { var locBarChangeRate = this._barChangeRate; var percentageF = this._percentage / 100; var locBarRect = this._barRect; var drawedSize = cc.size((sw * (1 - locBarChangeRate.x)), (sh * (1 - locBarChangeRate.y))); var drawingSize = cc.size((sw - drawedSize.width) * percentageF, (sh - drawedSize.height) * percentageF); var currentDrawSize = cc.size(drawedSize.width + drawingSize.width, drawedSize.height + drawingSize.height); var startPoint = cc.p(sw * locMidPoint.x, sh * locMidPoint.y); var needToLeft = startPoint.x - currentDrawSize.width / 2; if (locMidPoint.x > 0.5) { if (currentDrawSize.width / 2 >= sw - startPoint.x) { needToLeft = sw - currentDrawSize.width; } } var needToTop = startPoint.y - currentDrawSize.height / 2; if (locMidPoint.y > 0.5) { if (currentDrawSize.height / 2 >= sh - startPoint.y) { needToTop = sh - currentDrawSize.height; } } //left pos locBarRect.x = 0; var flipXNeed = 1; if (locSprite._flippedX) { locBarRect.x -= currentDrawSize.width; flipXNeed = -1; } if (needToLeft > 0) locBarRect.x += needToLeft * flipXNeed; //right pos locBarRect.y = 0; var flipYNeed = 1; if (locSprite._flippedY) { locBarRect.y += currentDrawSize.height; flipYNeed = -1; } if (needToTop > 0) locBarRect.y -= needToTop * flipYNeed; //clip width and clip height locBarRect.width = currentDrawSize.width; locBarRect.height = -currentDrawSize.height; } }, _updateProgressForWebGL:function () { var locType = this._type; if(locType === cc.ProgressTimer.TYPE_RADIAL) this._updateRadial(); else if(locType === cc.ProgressTimer.TYPE_BAR) this._updateBar(); this._vertexDataDirty = true; } }); var _p = cc.ProgressTimer.prototype; if(cc._renderType == cc._RENDER_TYPE_WEBGL){ _p.ctor = _p._ctorForWebGL; _p.setReverseProgress = _p._setReverseProgressForWebGL; _p.setSprite = _p._setSpriteForWebGL; _p.setType = _p._setTypeForWebGL; _p.setReverseDirection = _p._setReverseDirectionForWebGL; _p.initWithSprite = _p._initWithSpriteForWebGL; _p.draw = _p._drawForWebGL; _p._updateProgress = _p._updateProgressForWebGL; } else { _p.ctor = _p._ctorForCanvas; _p.setReverseProgress = _p._setReverseProgressForCanvas; _p.setSprite = _p._setSpriteForCanvas; _p.setType = _p._setTypeForCanvas; _p.setReverseDirection = _p._setReverseDirectionForCanvas; _p.initWithSprite = _p._initWithSpriteForCanvas; _p.draw = _p._drawForCanvas; _p._updateProgress = cc.ProgressTimer.prototype._updateProgressForCanvas; } // Extended properties /** @expose */ _p.midPoint; cc.defineGetterSetter(_p, "midPoint", _p.getMidpoint, _p.setMidpoint); /** @expose */ _p.barChangeRate; cc.defineGetterSetter(_p, "barChangeRate", _p.getBarChangeRate, _p.setBarChangeRate); /** @expose */ _p.type; cc.defineGetterSetter(_p, "type", _p.getType, _p.setType); /** @expose */ _p.percentage; cc.defineGetterSetter(_p, "percentage", _p.getPercentage, _p.setPercentage); /** @expose */ _p.sprite; cc.defineGetterSetter(_p, "sprite", _p.getSprite, _p.setSprite); /** @expose */ _p.reverseDir; cc.defineGetterSetter(_p, "reverseDir", _p.isReverseDirection, _p.setReverseDirection); /** * create a progress timer object with image file name that renders the inner sprite according to the percentage * @deprecated since v3.0,please use new cc.ProgressTimer(sprite) instead. * @param {cc.Sprite} sprite * @return {cc.ProgressTimer} * @example * // Example * var progress = cc.ProgressTimer.create('progress.png') */ cc.ProgressTimer.create = function (sprite) { return new cc.ProgressTimer(sprite); }; /** * @constant * @type Number */ cc.ProgressTimer.TEXTURE_COORDS_COUNT = 4; /** * @constant * @type Number */ cc.ProgressTimer.TEXTURE_COORDS = 0x4b; /** * Radial Counter-Clockwise * @type Number * @constant */ cc.ProgressTimer.TYPE_RADIAL = 0; /** * Bar * @type Number * @constant */ cc.ProgressTimer.TYPE_BAR = 1;