CCTransitionProgress.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "2d/CCTransitionProgress.h"
  25. #include "base/CCDirector.h"
  26. #include "2d/CCRenderTexture.h"
  27. #include "2d/CCProgressTimer.h"
  28. #include "2d/CCActionInstant.h"
  29. #include "2d/CCActionProgressTimer.h"
  30. NS_CC_BEGIN
  31. enum {
  32. kSceneRadial = 0xc001,
  33. };
  34. TransitionProgress::TransitionProgress()
  35. : _to(0.0f)
  36. , _from(0.0f)
  37. , _sceneToBeModified(nullptr)
  38. {
  39. }
  40. TransitionProgress* TransitionProgress::create(float t, Scene* scene)
  41. {
  42. TransitionProgress* newScene = new (std::nothrow) TransitionProgress();
  43. if(newScene && newScene->initWithDuration(t, scene))
  44. {
  45. newScene->autorelease();
  46. return newScene;
  47. }
  48. CC_SAFE_DELETE(newScene);
  49. return nullptr;
  50. }
  51. // TransitionProgress
  52. void TransitionProgress::onEnter()
  53. {
  54. TransitionScene::onEnter();
  55. setupTransition();
  56. // create a transparent color layer
  57. // in which we are going to add our rendertextures
  58. Size size = Director::getInstance()->getWinSize();
  59. // create the second render texture for outScene
  60. RenderTexture *texture = RenderTexture::create((int)size.width, (int)size.height,Texture2D::PixelFormat::RGBA8888,GL_DEPTH24_STENCIL8);
  61. texture->getSprite()->setAnchorPoint(Vec2(0.5f,0.5f));
  62. texture->setPosition(size.width/2, size.height/2);
  63. texture->setAnchorPoint(Vec2(0.5f,0.5f));
  64. // render outScene to its texturebuffer
  65. texture->beginWithClear(0, 0, 0, 1);
  66. _sceneToBeModified->visit();
  67. texture->end();
  68. // Since we've passed the outScene to the texture we don't need it.
  69. if (_sceneToBeModified == _outScene)
  70. {
  71. hideOutShowIn();
  72. }
  73. // We need the texture in RenderTexture.
  74. ProgressTimer *node = progressTimerNodeWithRenderTexture(texture);
  75. // create the blend action
  76. auto layerAction = Sequence::create(
  77. ProgressFromTo::create(_duration, _from, _to),
  78. CallFunc::create(CC_CALLBACK_0(TransitionScene::finish,this)),
  79. nullptr);
  80. // run the blend action
  81. node->runAction(layerAction);
  82. // add the layer (which contains our two rendertextures) to the scene
  83. addChild(node, 2, kSceneRadial);
  84. }
  85. // clean up on exit
  86. void TransitionProgress::onExit()
  87. {
  88. // remove our layer and release all containing objects
  89. removeChildByTag(kSceneRadial, true);
  90. TransitionScene::onExit();
  91. }
  92. void TransitionProgress::sceneOrder()
  93. {
  94. _isInSceneOnTop = false;
  95. }
  96. void TransitionProgress::setupTransition()
  97. {
  98. _sceneToBeModified = _outScene;
  99. _from = 100;
  100. _to = 0;
  101. }
  102. ProgressTimer* TransitionProgress::progressTimerNodeWithRenderTexture(RenderTexture* /*texture*/)
  103. {
  104. CCASSERT(false, "override me - abstract class");
  105. return nullptr;
  106. }
  107. // TransitionProgressRadialCCW
  108. ProgressTimer* TransitionProgressRadialCCW::progressTimerNodeWithRenderTexture(RenderTexture* texture)
  109. {
  110. Size size = Director::getInstance()->getWinSize();
  111. ProgressTimer* node = ProgressTimer::create(texture->getSprite());
  112. // but it is flipped upside down so we flip the sprite
  113. node->getSprite()->setFlippedY(true);
  114. node->setType(ProgressTimer::Type::RADIAL);
  115. // Return the radial type that we want to use
  116. node->setReverseDirection(false);
  117. node->setPercentage(100);
  118. node->setPosition(size.width/2, size.height/2);
  119. node->setAnchorPoint(Vec2(0.5f,0.5f));
  120. return node;
  121. }
  122. TransitionProgressRadialCCW* TransitionProgressRadialCCW::create(float t, Scene* scene)
  123. {
  124. TransitionProgressRadialCCW* newScene = new (std::nothrow) TransitionProgressRadialCCW();
  125. if(newScene && newScene->initWithDuration(t, scene))
  126. {
  127. newScene->autorelease();
  128. return newScene;
  129. }
  130. CC_SAFE_DELETE(newScene);
  131. return nullptr;
  132. }
  133. // TransitionProgressRadialCW
  134. TransitionProgressRadialCW* TransitionProgressRadialCW::create(float t, Scene* scene)
  135. {
  136. TransitionProgressRadialCW* newScene = new (std::nothrow) TransitionProgressRadialCW();
  137. if(newScene && newScene->initWithDuration(t, scene))
  138. {
  139. newScene->autorelease();
  140. return newScene;
  141. }
  142. CC_SAFE_DELETE(newScene);
  143. return nullptr;
  144. }
  145. ProgressTimer* TransitionProgressRadialCW::progressTimerNodeWithRenderTexture(RenderTexture* texture)
  146. {
  147. Size size = Director::getInstance()->getWinSize();
  148. ProgressTimer* node = ProgressTimer::create(texture->getSprite());
  149. // but it is flipped upside down so we flip the sprite
  150. node->getSprite()->setFlippedY(true);
  151. node->setType( ProgressTimer::Type::RADIAL );
  152. // Return the radial type that we want to use
  153. node->setReverseDirection(true);
  154. node->setPercentage(100);
  155. node->setPosition(size.width/2, size.height/2);
  156. node->setAnchorPoint(Vec2(0.5f,0.5f));
  157. return node;
  158. }
  159. // TransitionProgressHorizontal
  160. TransitionProgressHorizontal* TransitionProgressHorizontal::create(float t, Scene* scene)
  161. {
  162. TransitionProgressHorizontal* newScene = new (std::nothrow) TransitionProgressHorizontal();
  163. if(newScene && newScene->initWithDuration(t, scene))
  164. {
  165. newScene->autorelease();
  166. return newScene;
  167. }
  168. CC_SAFE_DELETE(newScene);
  169. return nullptr;
  170. }
  171. ProgressTimer* TransitionProgressHorizontal::progressTimerNodeWithRenderTexture(RenderTexture* texture)
  172. {
  173. Size size = Director::getInstance()->getWinSize();
  174. ProgressTimer* node = ProgressTimer::create(texture->getSprite());
  175. // but it is flipped upside down so we flip the sprite
  176. node->getSprite()->setFlippedY(true);
  177. node->setType( ProgressTimer::Type::BAR);
  178. node->setMidpoint(Vec2(1, 0));
  179. node->setBarChangeRate(Vec2(1,0));
  180. node->setPercentage(100);
  181. node->setPosition(size.width/2, size.height/2);
  182. node->setAnchorPoint(Vec2(0.5f,0.5f));
  183. return node;
  184. }
  185. // TransitionProgressVertical
  186. TransitionProgressVertical* TransitionProgressVertical::create(float t, Scene* scene)
  187. {
  188. TransitionProgressVertical* newScene = new (std::nothrow) TransitionProgressVertical();
  189. if(newScene && newScene->initWithDuration(t, scene))
  190. {
  191. newScene->autorelease();
  192. return newScene;
  193. }
  194. CC_SAFE_DELETE(newScene);
  195. return nullptr;
  196. }
  197. ProgressTimer* TransitionProgressVertical::progressTimerNodeWithRenderTexture(RenderTexture* texture)
  198. {
  199. Size size = Director::getInstance()->getWinSize();
  200. ProgressTimer* node = ProgressTimer::create(texture->getSprite());
  201. // but it is flipped upside down so we flip the sprite
  202. node->getSprite()->setFlippedY(true);
  203. node->setType(ProgressTimer::Type::BAR);
  204. node->setMidpoint(Vec2(0, 0));
  205. node->setBarChangeRate(Vec2(0,1));
  206. node->setPercentage(100);
  207. node->setPosition(size.width/2, size.height/2);
  208. node->setAnchorPoint(Vec2(0.5f,0.5f));
  209. return node;
  210. }
  211. // TransitionProgressInOut
  212. TransitionProgressInOut* TransitionProgressInOut::create(float t, Scene* scene)
  213. {
  214. TransitionProgressInOut* newScene = new (std::nothrow) TransitionProgressInOut();
  215. if(newScene && newScene->initWithDuration(t, scene))
  216. {
  217. newScene->autorelease();
  218. return newScene;
  219. }
  220. CC_SAFE_DELETE(newScene);
  221. return nullptr;
  222. }
  223. void TransitionProgressInOut::sceneOrder()
  224. {
  225. _isInSceneOnTop = false;
  226. }
  227. void TransitionProgressInOut::setupTransition()
  228. {
  229. _sceneToBeModified = _inScene;
  230. _from = 0;
  231. _to = 100;
  232. }
  233. ProgressTimer* TransitionProgressInOut::progressTimerNodeWithRenderTexture(RenderTexture* texture)
  234. {
  235. Size size = Director::getInstance()->getWinSize();
  236. ProgressTimer* node = ProgressTimer::create(texture->getSprite());
  237. // but it is flipped upside down so we flip the sprite
  238. node->getSprite()->setFlippedY(true);
  239. node->setType( ProgressTimer::Type::BAR);
  240. node->setMidpoint(Vec2(0.5f, 0.5f));
  241. node->setBarChangeRate(Vec2(1, 1));
  242. node->setPercentage(0);
  243. node->setPosition(size.width/2, size.height/2);
  244. node->setAnchorPoint(Vec2(0.5f,0.5f));
  245. return node;
  246. }
  247. // TransitionProgressOutIn
  248. TransitionProgressOutIn* TransitionProgressOutIn::create(float t, Scene* scene)
  249. {
  250. TransitionProgressOutIn* newScene = new (std::nothrow) TransitionProgressOutIn();
  251. if(newScene && newScene->initWithDuration(t, scene))
  252. {
  253. newScene->autorelease();
  254. return newScene;
  255. }
  256. CC_SAFE_DELETE(newScene);
  257. return nullptr;
  258. }
  259. ProgressTimer* TransitionProgressOutIn::progressTimerNodeWithRenderTexture(RenderTexture* texture)
  260. {
  261. Size size = Director::getInstance()->getWinSize();
  262. ProgressTimer* node = ProgressTimer::create(texture->getSprite());
  263. // but it is flipped upside down so we flip the sprite
  264. node->getSprite()->setFlippedY(true);
  265. node->setType( ProgressTimer::Type::BAR );
  266. node->setMidpoint(Vec2(0.5f, 0.5f));
  267. node->setBarChangeRate(Vec2(1, 1));
  268. node->setPercentage(100);
  269. node->setPosition(size.width/2, size.height/2);
  270. node->setAnchorPoint(Vec2(0.5f,0.5f));
  271. return node;
  272. }
  273. NS_CC_END