CCTransitionProgress.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /****************************************************************************
  2. Copyright (c) 2009 Lam Pham
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2012 Ricardo Quesada
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  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/CCTransitionProgress.h"
  24. #include "base/CCDirector.h"
  25. #include "2d/CCRenderTexture.h"
  26. #include "2d/CCProgressTimer.h"
  27. #include "2d/CCActionInstant.h"
  28. #include "2d/CCActionProgressTimer.h"
  29. NS_CC_BEGIN
  30. enum {
  31. kSceneRadial = 0xc001,
  32. };
  33. TransitionProgress::TransitionProgress()
  34. : _to(0.0f)
  35. , _from(0.0f)
  36. , _sceneToBeModified(nullptr)
  37. {
  38. }
  39. TransitionProgress* TransitionProgress::create(float t, Scene* scene)
  40. {
  41. TransitionProgress* newScene = new (std::nothrow) TransitionProgress();
  42. if(newScene && newScene->initWithDuration(t, scene))
  43. {
  44. newScene->autorelease();
  45. return newScene;
  46. }
  47. CC_SAFE_DELETE(newScene);
  48. return nullptr;
  49. }
  50. // TransitionProgress
  51. void TransitionProgress::onEnter()
  52. {
  53. TransitionScene::onEnter();
  54. setupTransition();
  55. // create a transparent color layer
  56. // in which we are going to add our rendertextures
  57. Size size = Director::getInstance()->getWinSize();
  58. // create the second render texture for outScene
  59. RenderTexture *texture = RenderTexture::create((int)size.width, (int)size.height,Texture2D::PixelFormat::RGBA8888,GL_DEPTH24_STENCIL8);
  60. texture->getSprite()->setAnchorPoint(Vec2(0.5f,0.5f));
  61. texture->setPosition(size.width/2, size.height/2);
  62. texture->setAnchorPoint(Vec2(0.5f,0.5f));
  63. // render outScene to its texturebuffer
  64. texture->beginWithClear(0, 0, 0, 1);
  65. _sceneToBeModified->visit();
  66. texture->end();
  67. // Since we've passed the outScene to the texture we don't need it.
  68. if (_sceneToBeModified == _outScene)
  69. {
  70. hideOutShowIn();
  71. }
  72. // We need the texture in RenderTexture.
  73. ProgressTimer *node = progressTimerNodeWithRenderTexture(texture);
  74. // create the blend action
  75. auto layerAction = Sequence::create(
  76. ProgressFromTo::create(_duration, _from, _to),
  77. CallFunc::create(CC_CALLBACK_0(TransitionScene::finish,this)),
  78. nullptr);
  79. // run the blend action
  80. node->runAction(layerAction);
  81. // add the layer (which contains our two rendertextures) to the scene
  82. addChild(node, 2, kSceneRadial);
  83. }
  84. // clean up on exit
  85. void TransitionProgress::onExit()
  86. {
  87. // remove our layer and release all containing objects
  88. removeChildByTag(kSceneRadial, true);
  89. TransitionScene::onExit();
  90. }
  91. void TransitionProgress::sceneOrder()
  92. {
  93. _isInSceneOnTop = false;
  94. }
  95. void TransitionProgress::setupTransition()
  96. {
  97. _sceneToBeModified = _outScene;
  98. _from = 100;
  99. _to = 0;
  100. }
  101. ProgressTimer* TransitionProgress::progressTimerNodeWithRenderTexture(RenderTexture* /*texture*/)
  102. {
  103. CCASSERT(false, "override me - abstract class");
  104. return nullptr;
  105. }
  106. // TransitionProgressRadialCCW
  107. ProgressTimer* TransitionProgressRadialCCW::progressTimerNodeWithRenderTexture(RenderTexture* texture)
  108. {
  109. Size size = Director::getInstance()->getWinSize();
  110. ProgressTimer* node = ProgressTimer::create(texture->getSprite());
  111. // but it is flipped upside down so we flip the sprite
  112. node->getSprite()->setFlippedY(true);
  113. node->setType(ProgressTimer::Type::RADIAL);
  114. // Return the radial type that we want to use
  115. node->setReverseDirection(false);
  116. node->setPercentage(100);
  117. node->setPosition(size.width/2, size.height/2);
  118. node->setAnchorPoint(Vec2(0.5f,0.5f));
  119. return node;
  120. }
  121. TransitionProgressRadialCCW* TransitionProgressRadialCCW::create(float t, Scene* scene)
  122. {
  123. TransitionProgressRadialCCW* newScene = new (std::nothrow) TransitionProgressRadialCCW();
  124. if(newScene && newScene->initWithDuration(t, scene))
  125. {
  126. newScene->autorelease();
  127. return newScene;
  128. }
  129. CC_SAFE_DELETE(newScene);
  130. return nullptr;
  131. }
  132. // TransitionProgressRadialCW
  133. TransitionProgressRadialCW* TransitionProgressRadialCW::create(float t, Scene* scene)
  134. {
  135. TransitionProgressRadialCW* newScene = new (std::nothrow) TransitionProgressRadialCW();
  136. if(newScene && newScene->initWithDuration(t, scene))
  137. {
  138. newScene->autorelease();
  139. return newScene;
  140. }
  141. CC_SAFE_DELETE(newScene);
  142. return nullptr;
  143. }
  144. ProgressTimer* TransitionProgressRadialCW::progressTimerNodeWithRenderTexture(RenderTexture* texture)
  145. {
  146. Size size = Director::getInstance()->getWinSize();
  147. ProgressTimer* node = ProgressTimer::create(texture->getSprite());
  148. // but it is flipped upside down so we flip the sprite
  149. node->getSprite()->setFlippedY(true);
  150. node->setType( ProgressTimer::Type::RADIAL );
  151. // Return the radial type that we want to use
  152. node->setReverseDirection(true);
  153. node->setPercentage(100);
  154. node->setPosition(size.width/2, size.height/2);
  155. node->setAnchorPoint(Vec2(0.5f,0.5f));
  156. return node;
  157. }
  158. // TransitionProgressHorizontal
  159. TransitionProgressHorizontal* TransitionProgressHorizontal::create(float t, Scene* scene)
  160. {
  161. TransitionProgressHorizontal* newScene = new (std::nothrow) TransitionProgressHorizontal();
  162. if(newScene && newScene->initWithDuration(t, scene))
  163. {
  164. newScene->autorelease();
  165. return newScene;
  166. }
  167. CC_SAFE_DELETE(newScene);
  168. return nullptr;
  169. }
  170. ProgressTimer* TransitionProgressHorizontal::progressTimerNodeWithRenderTexture(RenderTexture* texture)
  171. {
  172. Size size = Director::getInstance()->getWinSize();
  173. ProgressTimer* node = ProgressTimer::create(texture->getSprite());
  174. // but it is flipped upside down so we flip the sprite
  175. node->getSprite()->setFlippedY(true);
  176. node->setType( ProgressTimer::Type::BAR);
  177. node->setMidpoint(Vec2(1, 0));
  178. node->setBarChangeRate(Vec2(1,0));
  179. node->setPercentage(100);
  180. node->setPosition(size.width/2, size.height/2);
  181. node->setAnchorPoint(Vec2(0.5f,0.5f));
  182. return node;
  183. }
  184. // TransitionProgressVertical
  185. TransitionProgressVertical* TransitionProgressVertical::create(float t, Scene* scene)
  186. {
  187. TransitionProgressVertical* newScene = new (std::nothrow) TransitionProgressVertical();
  188. if(newScene && newScene->initWithDuration(t, scene))
  189. {
  190. newScene->autorelease();
  191. return newScene;
  192. }
  193. CC_SAFE_DELETE(newScene);
  194. return nullptr;
  195. }
  196. ProgressTimer* TransitionProgressVertical::progressTimerNodeWithRenderTexture(RenderTexture* texture)
  197. {
  198. Size size = Director::getInstance()->getWinSize();
  199. ProgressTimer* node = ProgressTimer::create(texture->getSprite());
  200. // but it is flipped upside down so we flip the sprite
  201. node->getSprite()->setFlippedY(true);
  202. node->setType(ProgressTimer::Type::BAR);
  203. node->setMidpoint(Vec2(0, 0));
  204. node->setBarChangeRate(Vec2(0,1));
  205. node->setPercentage(100);
  206. node->setPosition(size.width/2, size.height/2);
  207. node->setAnchorPoint(Vec2(0.5f,0.5f));
  208. return node;
  209. }
  210. // TransitionProgressInOut
  211. TransitionProgressInOut* TransitionProgressInOut::create(float t, Scene* scene)
  212. {
  213. TransitionProgressInOut* newScene = new (std::nothrow) TransitionProgressInOut();
  214. if(newScene && newScene->initWithDuration(t, scene))
  215. {
  216. newScene->autorelease();
  217. return newScene;
  218. }
  219. CC_SAFE_DELETE(newScene);
  220. return nullptr;
  221. }
  222. void TransitionProgressInOut::sceneOrder()
  223. {
  224. _isInSceneOnTop = false;
  225. }
  226. void TransitionProgressInOut::setupTransition()
  227. {
  228. _sceneToBeModified = _inScene;
  229. _from = 0;
  230. _to = 100;
  231. }
  232. ProgressTimer* TransitionProgressInOut::progressTimerNodeWithRenderTexture(RenderTexture* texture)
  233. {
  234. Size size = Director::getInstance()->getWinSize();
  235. ProgressTimer* node = ProgressTimer::create(texture->getSprite());
  236. // but it is flipped upside down so we flip the sprite
  237. node->getSprite()->setFlippedY(true);
  238. node->setType( ProgressTimer::Type::BAR);
  239. node->setMidpoint(Vec2(0.5f, 0.5f));
  240. node->setBarChangeRate(Vec2(1, 1));
  241. node->setPercentage(0);
  242. node->setPosition(size.width/2, size.height/2);
  243. node->setAnchorPoint(Vec2(0.5f,0.5f));
  244. return node;
  245. }
  246. // TransitionProgressOutIn
  247. TransitionProgressOutIn* TransitionProgressOutIn::create(float t, Scene* scene)
  248. {
  249. TransitionProgressOutIn* newScene = new (std::nothrow) TransitionProgressOutIn();
  250. if(newScene && newScene->initWithDuration(t, scene))
  251. {
  252. newScene->autorelease();
  253. return newScene;
  254. }
  255. CC_SAFE_DELETE(newScene);
  256. return nullptr;
  257. }
  258. ProgressTimer* TransitionProgressOutIn::progressTimerNodeWithRenderTexture(RenderTexture* texture)
  259. {
  260. Size size = Director::getInstance()->getWinSize();
  261. ProgressTimer* node = ProgressTimer::create(texture->getSprite());
  262. // but it is flipped upside down so we flip the sprite
  263. node->getSprite()->setFlippedY(true);
  264. node->setType( ProgressTimer::Type::BAR );
  265. node->setMidpoint(Vec2(0.5f, 0.5f));
  266. node->setBarChangeRate(Vec2(1, 1));
  267. node->setPercentage(100);
  268. node->setPosition(size.width/2, size.height/2);
  269. node->setAnchorPoint(Vec2(0.5f,0.5f));
  270. return node;
  271. }
  272. NS_CC_END