CCTransitionPageTurn.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /****************************************************************************
  2. Copyright (c) 2009 Sindesso Pty Ltd http://www.sindesso.com/
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 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/CCTransitionPageTurn.h"
  24. #include "base/CCDirector.h"
  25. #include "2d/CCActionPageTurn3D.h"
  26. #include "2d/CCNodeGrid.h"
  27. #include "renderer/CCRenderer.h"
  28. NS_CC_BEGIN
  29. TransitionPageTurn::TransitionPageTurn()
  30. {
  31. _inSceneProxy = NodeGrid::create();
  32. _outSceneProxy = NodeGrid::create();
  33. _inSceneProxy->retain();
  34. _outSceneProxy->retain();
  35. }
  36. TransitionPageTurn::~TransitionPageTurn()
  37. {
  38. CC_SAFE_RELEASE(_inSceneProxy);
  39. CC_SAFE_RELEASE(_outSceneProxy);
  40. }
  41. /** creates a base transition with duration and incoming scene */
  42. TransitionPageTurn * TransitionPageTurn::create(float t, Scene *scene, bool backwards)
  43. {
  44. TransitionPageTurn * transition = new (std::nothrow) TransitionPageTurn();
  45. transition->initWithDuration(t,scene,backwards);
  46. transition->autorelease();
  47. return transition;
  48. }
  49. /** initializes a transition with duration and incoming scene */
  50. bool TransitionPageTurn::initWithDuration(float t, Scene *scene, bool backwards)
  51. {
  52. // FIXME:: needed before [super init]
  53. _back = backwards;
  54. if (TransitionScene::initWithDuration(t, scene))
  55. {
  56. // do something
  57. }
  58. return true;
  59. }
  60. void TransitionPageTurn::sceneOrder()
  61. {
  62. _isInSceneOnTop = _back;
  63. }
  64. void TransitionPageTurn::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  65. {
  66. Scene::draw(renderer, transform, flags);
  67. if( _isInSceneOnTop ) {
  68. _outSceneProxy->visit(renderer, transform, flags);
  69. _inSceneProxy->visit(renderer, transform, flags);
  70. } else {
  71. _inSceneProxy->visit(renderer, transform, flags);
  72. _outSceneProxy->visit(renderer, transform, flags);
  73. }
  74. }
  75. void TransitionPageTurn::onEnter()
  76. {
  77. TransitionScene::onEnter();
  78. _inSceneProxy->setTarget(_inScene);
  79. _outSceneProxy->setTarget(_outScene);
  80. _inSceneProxy->onEnter();
  81. _outSceneProxy->onEnter();
  82. Size s = Director::getInstance()->getWinSize();
  83. int x,y;
  84. if (s.width > s.height)
  85. {
  86. x=16;
  87. y=12;
  88. }
  89. else
  90. {
  91. x=12;
  92. y=16;
  93. }
  94. ActionInterval *action = this->actionWithSize(Size(x,y));
  95. if (! _back )
  96. {
  97. _outSceneProxy->runAction
  98. (
  99. Sequence::create
  100. (
  101. action,
  102. CallFunc::create(CC_CALLBACK_0(TransitionScene::finish,this)),
  103. StopGrid::create(),
  104. nullptr
  105. )
  106. );
  107. }
  108. else
  109. {
  110. // to prevent initial flicker
  111. _inSceneProxy->setVisible(false);
  112. _inSceneProxy->runAction
  113. (
  114. Sequence::create
  115. (
  116. Show::create(),
  117. action,
  118. CallFunc::create(CC_CALLBACK_0(TransitionScene::finish,this)),
  119. StopGrid::create(),
  120. nullptr
  121. )
  122. );
  123. }
  124. }
  125. void TransitionPageTurn::onExit()
  126. {
  127. _outSceneProxy->setTarget(nullptr);
  128. _outSceneProxy->setTarget(nullptr);
  129. _outSceneProxy->onExit();
  130. _inSceneProxy->onExit();
  131. TransitionScene::onExit();
  132. }
  133. ActionInterval* TransitionPageTurn:: actionWithSize(const Size& vector)
  134. {
  135. if (_back)
  136. {
  137. // Get hold of the PageTurn3DAction
  138. return ReverseTime::create
  139. (
  140. PageTurn3D::create(_duration, vector)
  141. );
  142. }
  143. else
  144. {
  145. // Get hold of the PageTurn3DAction
  146. return PageTurn3D::create(_duration, vector);
  147. }
  148. }
  149. NS_CC_END