CCProgressTimer.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /****************************************************************************
  2. Copyright (c) 2010 Lam Pham
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies Inc
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "2d/CCProgressTimer.h"
  24. #include <algorithm>
  25. #include <stddef.h> // offsetof
  26. #include "base/ccTypes.h"
  27. #include "base/ccMacros.h"
  28. #include "base/CCDirector.h"
  29. #include "2d/CCSprite.h"
  30. #include "renderer/CCRenderer.h"
  31. #include "base/ccUtils.h"
  32. #include "renderer/ccShaders.h"
  33. #include "renderer/backend/ProgramState.h"
  34. NS_CC_BEGIN
  35. #define kProgressTextureCoordsCount 4
  36. // kProgressTextureCoords holds points {0,1} {0,0} {1,0} {1,1} we can represent it as bits
  37. const char kProgressTextureCoords = 0x4b;
  38. namespace
  39. {
  40. backend::ProgramState* initPipelineDescriptor(cocos2d::CustomCommand& command, bool ridal, backend::UniformLocation &locMVP, backend::UniformLocation &locTexture)
  41. {
  42. auto& pipelieDescriptor = command.getPipelineDescriptor();
  43. auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
  44. auto programState = new (std::nothrow) backend::ProgramState(program);
  45. CC_SAFE_RELEASE(pipelieDescriptor.programState);
  46. pipelieDescriptor.programState = programState;
  47. //set vertexLayout according to V2F_C4B_T2F structure
  48. auto vertexLayout = programState->getVertexLayout();
  49. const auto& attributeInfo = programState->getProgram()->getActiveAttributes();
  50. auto iter = attributeInfo.find("a_position");
  51. if(iter != attributeInfo.end())
  52. {
  53. vertexLayout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT2, 0, false);
  54. }
  55. iter = attributeInfo.find("a_texCoord");
  56. if(iter != attributeInfo.end())
  57. {
  58. vertexLayout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2, offsetof(V2F_C4B_T2F, texCoords), false);
  59. }
  60. iter = attributeInfo.find("a_color");
  61. if(iter != attributeInfo.end())
  62. {
  63. vertexLayout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4, offsetof(V2F_C4B_T2F, colors), true);
  64. }
  65. vertexLayout->setLayout(sizeof(V2F_C4B_T2F));
  66. if (ridal)
  67. {
  68. command.setDrawType(CustomCommand::DrawType::ELEMENT);
  69. command.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE);
  70. }
  71. else
  72. {
  73. command.setDrawType(CustomCommand::DrawType::ARRAY);
  74. command.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
  75. }
  76. locMVP = programState->getUniformLocation("u_MVPMatrix");
  77. locTexture = programState->getUniformLocation("u_texture");
  78. return programState;
  79. }
  80. }
  81. ProgressTimer* ProgressTimer::create(Sprite* sp)
  82. {
  83. ProgressTimer *progressTimer = new (std::nothrow) ProgressTimer();
  84. if (progressTimer && progressTimer->initWithSprite(sp))
  85. {
  86. progressTimer->autorelease();
  87. return progressTimer;
  88. }
  89. delete progressTimer;
  90. return nullptr;
  91. }
  92. bool ProgressTimer::initWithSprite(Sprite* sp)
  93. {
  94. setAnchorPoint(Vec2(0.5f,0.5f));
  95. setMidpoint(Vec2(0.5f, 0.5f));
  96. setBarChangeRate(Vec2(1,1));
  97. setSprite(sp);
  98. CC_SAFE_RELEASE(_programState);
  99. CC_SAFE_RELEASE(_programState2);
  100. _programState = initPipelineDescriptor(_customCommand, true, _locMVP1, _locTex1);
  101. _programState2 = initPipelineDescriptor(_customCommand2, false, _locMVP2, _locTex2);
  102. return true;
  103. }
  104. ProgressTimer::~ProgressTimer()
  105. {
  106. CC_SAFE_RELEASE(_sprite);
  107. CC_SAFE_RELEASE(_programState);
  108. CC_SAFE_RELEASE(_programState2);
  109. }
  110. void ProgressTimer::setPercentage(float percentage)
  111. {
  112. if (_percentage != percentage)
  113. {
  114. _percentage = clampf(percentage, 0, 100);
  115. updateProgress();
  116. }
  117. }
  118. void ProgressTimer::setSprite(Sprite *sprite)
  119. {
  120. if (_sprite != sprite)
  121. {
  122. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  123. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  124. if (sEngine)
  125. {
  126. if (_sprite)
  127. sEngine->releaseScriptObject(this, _sprite);
  128. if (sprite)
  129. sEngine->retainScriptObject(this, sprite);
  130. }
  131. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  132. CC_SAFE_RETAIN(sprite);
  133. CC_SAFE_RELEASE(_sprite);
  134. _sprite = sprite;
  135. setContentSize(_sprite->getContentSize());
  136. // Every time we set a new sprite, we free the current vertex data
  137. if (!_vertexData.empty())
  138. {
  139. _vertexData.clear();
  140. updateProgress();
  141. }
  142. }
  143. }
  144. void ProgressTimer::setType(Type type)
  145. {
  146. if (type == Type::RADIAL)
  147. {
  148. _customCommand.setDrawType(CustomCommand::DrawType::ELEMENT);
  149. _customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE);
  150. }
  151. else
  152. {
  153. _customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
  154. _customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
  155. }
  156. if (type != _type)
  157. {
  158. // release all previous information
  159. _vertexData.clear();
  160. _type = type;
  161. }
  162. }
  163. void ProgressTimer::setReverseDirection(bool reverse)
  164. {
  165. if( _reverseDirection != reverse ) {
  166. _reverseDirection = reverse;
  167. // release all previous information
  168. _vertexData.clear();
  169. }
  170. }
  171. // Interval
  172. ///
  173. // @returns the vertex position from the texture coordinate
  174. ///
  175. Tex2F ProgressTimer::textureCoordFromAlphaPoint(Vec2 alpha)
  176. {
  177. Tex2F ret(0.0f, 0.0f);
  178. if (!_sprite) {
  179. return ret;
  180. }
  181. V3F_C4B_T2F_Quad quad = _sprite->getQuad();
  182. Vec2 min(quad.bl.texCoords.u,quad.bl.texCoords.v);
  183. Vec2 max(quad.tr.texCoords.u,quad.tr.texCoords.v);
  184. // Fix bug #1303 so that progress timer handles sprite frame texture rotation
  185. if (_sprite->isTextureRectRotated()) {
  186. std::swap(alpha.x, alpha.y);
  187. }
  188. return Tex2F(min.x * (1.f - alpha.x) + max.x * alpha.x, min.y * (1.f - alpha.y) + max.y * alpha.y);
  189. }
  190. Vec2 ProgressTimer::vertexFromAlphaPoint(Vec2 alpha)
  191. {
  192. Vec2 ret(0.0f, 0.0f);
  193. if (!_sprite) {
  194. return ret;
  195. }
  196. V3F_C4B_T2F_Quad quad = _sprite->getQuad();
  197. Vec2 min(quad.bl.vertices.x,quad.bl.vertices.y);
  198. Vec2 max(quad.tr.vertices.x,quad.tr.vertices.y);
  199. ret.x = min.x * (1.f - alpha.x) + max.x * alpha.x;
  200. ret.y = min.y * (1.f - alpha.y) + max.y * alpha.y;
  201. return ret;
  202. }
  203. void ProgressTimer::updateColor()
  204. {
  205. if (!_sprite)
  206. return;
  207. if (!_vertexData.empty())
  208. {
  209. const Color4B& sc = _sprite->getQuad().tl.colors;
  210. for (int i = 0; i < _vertexData.size(); ++i)
  211. {
  212. _vertexData[i].colors = sc;
  213. }
  214. }
  215. }
  216. void ProgressTimer::updateProgress()
  217. {
  218. switch (_type)
  219. {
  220. case Type::RADIAL:
  221. updateRadial();
  222. break;
  223. case Type::BAR:
  224. updateBar();
  225. break;
  226. default:
  227. break;
  228. }
  229. }
  230. void ProgressTimer::setAnchorPoint(const Vec2& anchorPoint)
  231. {
  232. Node::setAnchorPoint(anchorPoint);
  233. }
  234. Vec2 ProgressTimer::getMidpoint() const
  235. {
  236. return _midpoint;
  237. }
  238. void ProgressTimer::setColor(const Color3B &color)
  239. {
  240. _sprite->setColor(color);
  241. updateColor();
  242. }
  243. const Color3B& ProgressTimer::getColor() const
  244. {
  245. return _sprite->getColor();
  246. }
  247. void ProgressTimer::setOpacity(uint8_t opacity)
  248. {
  249. _sprite->setOpacity(opacity);
  250. updateColor();
  251. }
  252. uint8_t ProgressTimer::getOpacity() const
  253. {
  254. return _sprite->getOpacity();
  255. }
  256. void ProgressTimer::setMidpoint(const Vec2& midPoint)
  257. {
  258. _midpoint = midPoint.getClampPoint(Vec2::ZERO, Vec2(1, 1));
  259. }
  260. ///
  261. // Update does the work of mapping the texture onto the triangles
  262. // It now doesn't occur the cost of free/alloc data every update cycle.
  263. // It also only changes the percentage point but no other points if they have not
  264. // been modified.
  265. //
  266. // It now deals with flipped texture. If you run into this problem, just use the
  267. // sprite property and enable the methods flipX, flipY.
  268. ///
  269. void ProgressTimer::updateRadial()
  270. {
  271. if (!_sprite) {
  272. return;
  273. }
  274. float alpha = _percentage / 100.f;
  275. float angle = 2.f*((float)M_PI) * ( _reverseDirection ? alpha : 1.0f - alpha);
  276. // We find the vector to do a hit detection based on the percentage
  277. // We know the first vector is the one @ 12 o'clock (top,mid) so we rotate
  278. // from that by the progress angle around the _midpoint pivot
  279. Vec2 topMid(_midpoint.x, 1.f);
  280. Vec2 percentagePt = topMid.rotateByAngle(_midpoint, angle);
  281. int index = 0;
  282. Vec2 hit;
  283. if (alpha == 0.f) {
  284. // More efficient since we don't always need to check intersection
  285. // If the alpha is zero then the hit point is top mid and the index is 0.
  286. hit = topMid;
  287. index = 0;
  288. } else if (alpha == 1.f) {
  289. // More efficient since we don't always need to check intersection
  290. // If the alpha is one then the hit point is top mid and the index is 4.
  291. hit = topMid;
  292. index = 4;
  293. } else {
  294. // We run a for loop checking the edges of the texture to find the
  295. // intersection point
  296. // We loop through five points since the top is split in half
  297. float min_t = FLT_MAX;
  298. for (int i = 0; i <= kProgressTextureCoordsCount; ++i) {
  299. int pIndex = (i + (kProgressTextureCoordsCount - 1))%kProgressTextureCoordsCount;
  300. Vec2 edgePtA = boundaryTexCoord(i % kProgressTextureCoordsCount);
  301. Vec2 edgePtB = boundaryTexCoord(pIndex);
  302. // Remember that the top edge is split in half for the 12 o'clock position
  303. // Let's deal with that here by finding the correct endpoints
  304. if(i == 0){
  305. edgePtB = edgePtA.lerp(edgePtB, 1-_midpoint.x);
  306. } else if(i == 4){
  307. edgePtA = edgePtA.lerp(edgePtB, 1-_midpoint.x);
  308. }
  309. // s and t are returned by ccpLineIntersect
  310. float s = 0, t = 0;
  311. if(Vec2::isLineIntersect(edgePtA, edgePtB, _midpoint, percentagePt, &s, &t))
  312. {
  313. // Since our hit test is on rays we have to deal with the top edge
  314. // being in split in half so we have to test as a segment
  315. if ((i == 0 || i == 4)) {
  316. // s represents the point between edgePtA--edgePtB
  317. if (!(0.f <= s && s <= 1.f)) {
  318. continue;
  319. }
  320. }
  321. // As long as our t isn't negative we are at least finding a
  322. // correct hitpoint from _midpoint to percentagePt.
  323. if (t >= 0.f) {
  324. // Because the percentage line and all the texture edges are
  325. // rays we should only account for the shortest intersection
  326. if (t < min_t) {
  327. min_t = t;
  328. index = i;
  329. }
  330. }
  331. }
  332. }
  333. // Now that we have the minimum magnitude we can use that to find our intersection
  334. hit = _midpoint+ ((percentagePt - _midpoint) * min_t);
  335. }
  336. // The size of the vertex data is the index from the hitpoint
  337. // the 3 is for the _midpoint, 12 o'clock point and hitpoint position.
  338. bool sameIndexCount = true;
  339. if (_vertexData.size() != index + 3)
  340. {
  341. sameIndexCount = false;
  342. _vertexData.resize(index + 3);
  343. _customCommand.createVertexBuffer(sizeof(_vertexData[0]), (unsigned int)_vertexData.size(), CustomCommand::BufferUsage::DYNAMIC);
  344. }
  345. if (_indexData.size() != 3 + 3 * index)
  346. {
  347. _indexData.resize(3 + 3 * index);
  348. _customCommand.createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, (unsigned int)_indexData.size(), CustomCommand::BufferUsage::STATIC);
  349. }
  350. if (!sameIndexCount)
  351. {
  352. // First we populate the array with the _midpoint, then all
  353. // vertices/texcoords/colors of the 12 'o clock start and edges and the hitpoint
  354. _vertexData[0].texCoords = textureCoordFromAlphaPoint(_midpoint);
  355. _vertexData[0].vertices = vertexFromAlphaPoint(_midpoint);
  356. _vertexData[1].texCoords = textureCoordFromAlphaPoint(topMid);
  357. _vertexData[1].vertices = vertexFromAlphaPoint(topMid);
  358. for(int i = 0; i < index; ++i)
  359. {
  360. Vec2 alphaPoint = boundaryTexCoord(i);
  361. _vertexData[i+2].texCoords = textureCoordFromAlphaPoint(alphaPoint);
  362. _vertexData[i+2].vertices = vertexFromAlphaPoint(alphaPoint);
  363. }
  364. for (int i = 0; i < index + 1; i++)
  365. {
  366. _indexData[i * 3] = 0;
  367. _indexData[i * 3 + 1] = i + 2;
  368. _indexData[i * 3 + 2] = i + 1;
  369. }
  370. _customCommand.updateIndexBuffer(_indexData.data(), (unsigned int)(_indexData.size() * sizeof(_indexData[0])) );
  371. }
  372. // hitpoint will go last
  373. _vertexData[_vertexData.size() - 1].texCoords = textureCoordFromAlphaPoint(hit);
  374. _vertexData[_vertexData.size() - 1].vertices = vertexFromAlphaPoint(hit);
  375. updateColor();
  376. _customCommand.updateVertexBuffer(_vertexData.data(), (unsigned int)(sizeof(_vertexData[0]) * _vertexData.size()) );
  377. }
  378. ///
  379. // Update does the work of mapping the texture onto the triangles for the bar
  380. // It now doesn't occur the cost of free/alloc data every update cycle.
  381. // It also only changes the percentage point but no other points if they have not
  382. // been modified.
  383. //
  384. // It now deals with flipped texture. If you run into this problem, just use the
  385. // sprite property and enable the methods flipX, flipY.
  386. ///
  387. void ProgressTimer::updateBar()
  388. {
  389. if (!_sprite)
  390. return;
  391. float alpha = _percentage / 100.0f;
  392. Vec2 alphaOffset = Vec2(1.0f * (1.0f - _barChangeRate.x) + alpha * _barChangeRate.x, 1.0f * (1.0f - _barChangeRate.y) + alpha * _barChangeRate.y) * 0.5f;
  393. Vec2 min = _midpoint - alphaOffset;
  394. Vec2 max = _midpoint + alphaOffset;
  395. if (min.x < 0.f) {
  396. max.x += -min.x;
  397. min.x = 0.f;
  398. }
  399. if (max.x > 1.f) {
  400. min.x -= max.x - 1.f;
  401. max.x = 1.f;
  402. }
  403. if (min.y < 0.f) {
  404. max.y += -min.y;
  405. min.y = 0.f;
  406. }
  407. if (max.y > 1.f) {
  408. min.y -= max.y - 1.f;
  409. max.y = 1.f;
  410. }
  411. if (!_reverseDirection) {
  412. if (_vertexData.size() != 4)
  413. {
  414. _vertexData.resize(4);
  415. _customCommand.createVertexBuffer(sizeof(_vertexData[0]),(unsigned int) _vertexData.size(), CustomCommand::BufferUsage::DYNAMIC);
  416. }
  417. // TOPLEFT
  418. _vertexData[0].texCoords = textureCoordFromAlphaPoint(Vec2(min.x,max.y));
  419. _vertexData[0].vertices = vertexFromAlphaPoint(Vec2(min.x,max.y));
  420. // BOTLEFT
  421. _vertexData[1].texCoords = textureCoordFromAlphaPoint(Vec2(min.x,min.y));
  422. _vertexData[1].vertices = vertexFromAlphaPoint(Vec2(min.x,min.y));
  423. // TOPRIGHT
  424. _vertexData[2].texCoords = textureCoordFromAlphaPoint(Vec2(max.x,max.y));
  425. _vertexData[2].vertices = vertexFromAlphaPoint(Vec2(max.x,max.y));
  426. // BOTRIGHT
  427. _vertexData[3].texCoords = textureCoordFromAlphaPoint(Vec2(max.x,min.y));
  428. _vertexData[3].vertices = vertexFromAlphaPoint(Vec2(max.x,min.y));
  429. updateColor();
  430. _customCommand.updateVertexBuffer(_vertexData.data(), (unsigned int)(sizeof(_vertexData[0]) * _vertexData.size()));
  431. } else {
  432. if(_vertexData.size() != 8) {
  433. _vertexData.resize(8);
  434. _customCommand.createVertexBuffer(sizeof(_vertexData[0]), (unsigned int)(_vertexData.size() / 2), CustomCommand::BufferUsage::DYNAMIC);
  435. _customCommand2.createVertexBuffer(sizeof(_vertexData[0]), (unsigned int)(_vertexData.size() / 2), CustomCommand::BufferUsage::DYNAMIC);
  436. // TOPLEFT 1
  437. _vertexData[0].texCoords = textureCoordFromAlphaPoint(Vec2(0,1));
  438. _vertexData[0].vertices = vertexFromAlphaPoint(Vec2(0,1));
  439. // BOTLEFT 1
  440. _vertexData[1].texCoords = textureCoordFromAlphaPoint(Vec2(0,0));
  441. _vertexData[1].vertices = vertexFromAlphaPoint(Vec2(0,0));
  442. // TOPRIGHT 2
  443. _vertexData[6].texCoords = textureCoordFromAlphaPoint(Vec2(1,1));
  444. _vertexData[6].vertices = vertexFromAlphaPoint(Vec2(1,1));
  445. // BOTRIGHT 2
  446. _vertexData[7].texCoords = textureCoordFromAlphaPoint(Vec2(1,0));
  447. _vertexData[7].vertices = vertexFromAlphaPoint(Vec2(1,0));
  448. }
  449. // TOPRIGHT 1
  450. _vertexData[2].texCoords = textureCoordFromAlphaPoint(Vec2(min.x,max.y));
  451. _vertexData[2].vertices = vertexFromAlphaPoint(Vec2(min.x,max.y));
  452. // BOTRIGHT 1
  453. _vertexData[3].texCoords = textureCoordFromAlphaPoint(Vec2(min.x,min.y));
  454. _vertexData[3].vertices = vertexFromAlphaPoint(Vec2(min.x,min.y));
  455. // TOPLEFT 2
  456. _vertexData[4].texCoords = textureCoordFromAlphaPoint(Vec2(max.x,max.y));
  457. _vertexData[4].vertices = vertexFromAlphaPoint(Vec2(max.x,max.y));
  458. // BOTLEFT 2
  459. _vertexData[5].texCoords = textureCoordFromAlphaPoint(Vec2(max.x,min.y));
  460. _vertexData[5].vertices = vertexFromAlphaPoint(Vec2(max.x,min.y));
  461. updateColor();
  462. _customCommand.updateVertexBuffer(_vertexData.data(), (unsigned int)(sizeof(_vertexData[0]) * _vertexData.size() / 2));
  463. _customCommand2.updateVertexBuffer((char*)_vertexData.data() + sizeof(_vertexData[0]) * _vertexData.size() / 2,
  464. (unsigned int)(sizeof(_vertexData[0]) * _vertexData.size() / 2));
  465. }
  466. }
  467. Vec2 ProgressTimer::boundaryTexCoord(char index)
  468. {
  469. if (index < kProgressTextureCoordsCount)
  470. {
  471. if (_reverseDirection)
  472. return Vec2((kProgressTextureCoords>>(7-(index<<1)))&1,(kProgressTextureCoords>>(7-((index<<1)+1)))&1);
  473. else
  474. return Vec2((kProgressTextureCoords>>((index<<1)+1))&1,(kProgressTextureCoords>>(index<<1))&1);
  475. }
  476. else
  477. return Vec2::ZERO;
  478. }
  479. void ProgressTimer::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  480. {
  481. if( _vertexData.empty() || ! _sprite)
  482. return;
  483. const cocos2d::Mat4& projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
  484. Mat4 finalMat = projectionMat * transform;
  485. _programState->setUniform(_locMVP1, finalMat.m, sizeof(finalMat.m));
  486. _programState->setTexture(_locTex1, 0, _sprite->getTexture()->getBackendTexture());
  487. if(_type == Type::BAR)
  488. {
  489. if (!_reverseDirection)
  490. {
  491. _customCommand.init(_globalZOrder, _sprite->getBlendFunc());
  492. renderer->addCommand(&_customCommand);
  493. }
  494. else
  495. {
  496. _customCommand.init(_globalZOrder, _sprite->getBlendFunc());
  497. renderer->addCommand(&_customCommand);
  498. _customCommand2.init(_globalZOrder, _sprite->getBlendFunc());
  499. _programState2->setUniform(_locMVP2, finalMat.m, sizeof(finalMat.m));
  500. _programState2->setTexture(_locTex2, 0, _sprite->getTexture()->getBackendTexture());
  501. renderer->addCommand(&_customCommand2);
  502. }
  503. }
  504. else
  505. {
  506. _customCommand.init(_globalZOrder, _sprite->getBlendFunc());
  507. renderer->addCommand(&_customCommand);
  508. }
  509. }
  510. NS_CC_END