/libs/cocos2d/CCTransition.m

http://github.com/kstenerud/ObjectAL-for-iPhone · Objective C · 1046 lines · 730 code · 173 blank · 143 comment · 26 complexity · f0e9f9f5ffca8b8dbdb4fc517ac62d6c MD5 · raw file

  1. /*
  2. * cocos2d for iPhone: http://www.cocos2d-iphone.org
  3. *
  4. * Copyright (c) 2008-2010 Ricardo Quesada
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  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. */
  25. #import "CCTransition.h"
  26. #import "CCNode.h"
  27. #import "CCDirector.h"
  28. #import "CCIntervalAction.h"
  29. #import "CCInstantAction.h"
  30. #import "CCCameraAction.h"
  31. #import "CCLayer.h"
  32. #import "CCCamera.h"
  33. #import "CCTiledGridAction.h"
  34. #import "CCEaseAction.h"
  35. #import "CCTouchDispatcher.h"
  36. #import "CCRenderTexture.h"
  37. #import "Support/CGPointExtension.h"
  38. enum {
  39. kSceneFade = 0xFADEFADE,
  40. };
  41. @interface CCTransitionScene (Private)
  42. -(void) sceneOrder;
  43. - (void)setNewScene:(ccTime)dt;
  44. @end
  45. @implementation CCTransitionScene
  46. +(id) transitionWithDuration:(ccTime) t scene:(CCScene*)s
  47. {
  48. return [[[self alloc] initWithDuration:t scene:s] autorelease];
  49. }
  50. -(id) initWithDuration:(ccTime) t scene:(CCScene*)s
  51. {
  52. NSAssert( s != nil, @"Argument scene must be non-nil");
  53. if( (self=[super init]) ) {
  54. duration = t;
  55. // retain
  56. inScene = [s retain];
  57. outScene = [[CCDirector sharedDirector] runningScene];
  58. [outScene retain];
  59. NSAssert( inScene != outScene, @"Incoming scene must be different from the outgoing scene" );
  60. // disable events while transitions
  61. [[CCTouchDispatcher sharedDispatcher] setDispatchEvents: NO];
  62. [self sceneOrder];
  63. }
  64. return self;
  65. }
  66. -(void) sceneOrder
  67. {
  68. inSceneOnTop = YES;
  69. }
  70. -(void) draw
  71. {
  72. if( inSceneOnTop ) {
  73. [outScene visit];
  74. [inScene visit];
  75. } else {
  76. [inScene visit];
  77. [outScene visit];
  78. }
  79. }
  80. -(void) finish
  81. {
  82. /* clean up */
  83. [inScene setVisible:YES];
  84. [inScene setPosition:ccp(0,0)];
  85. [inScene setScale:1.0f];
  86. [inScene setRotation:0.0f];
  87. [inScene.camera restore];
  88. [outScene setVisible:NO];
  89. [outScene setPosition:ccp(0,0)];
  90. [outScene setScale:1.0f];
  91. [outScene setRotation:0.0f];
  92. [outScene.camera restore];
  93. [self schedule:@selector(setNewScene:) interval:0];
  94. }
  95. -(void) setNewScene: (ccTime) dt
  96. {
  97. [self unschedule:_cmd];
  98. CCDirector *director = [CCDirector sharedDirector];
  99. // Before replacing, save the "send cleanup to scene"
  100. sendCleanupToScene = [director sendCleanupToScene];
  101. [director replaceScene: inScene];
  102. // enable events while transitions
  103. [[CCTouchDispatcher sharedDispatcher] setDispatchEvents: YES];
  104. // issue #267
  105. [outScene setVisible:YES];
  106. }
  107. -(void) hideOutShowIn
  108. {
  109. [inScene setVisible:YES];
  110. [outScene setVisible:NO];
  111. }
  112. // custom onEnter
  113. -(void) onEnter
  114. {
  115. [super onEnter];
  116. [inScene onEnter];
  117. // outScene should not receive the onEnter callback
  118. }
  119. // custom onExit
  120. -(void) onExit
  121. {
  122. [super onExit];
  123. [outScene onExit];
  124. // inScene should not receive the onExit callback
  125. // only the onEnterTransitionDidFinish
  126. [inScene onEnterTransitionDidFinish];
  127. }
  128. // custom cleanup
  129. -(void) cleanup
  130. {
  131. [super cleanup];
  132. if( sendCleanupToScene )
  133. [outScene cleanup];
  134. }
  135. -(void) dealloc
  136. {
  137. [inScene release];
  138. [outScene release];
  139. [super dealloc];
  140. }
  141. @end
  142. //
  143. // Oriented Transition
  144. //
  145. @implementation CCOrientedTransitionScene
  146. +(id) transitionWithDuration:(ccTime) t scene:(CCScene*)s orientation:(tOrientation)o
  147. {
  148. return [[[self alloc] initWithDuration:t scene:s orientation:o] autorelease];
  149. }
  150. -(id) initWithDuration:(ccTime) t scene:(CCScene*)s orientation:(tOrientation)o
  151. {
  152. if( (self=[super initWithDuration:t scene:s]) )
  153. orientation = o;
  154. return self;
  155. }
  156. @end
  157. //
  158. // RotoZoom
  159. //
  160. @implementation CCRotoZoomTransition
  161. -(void) onEnter
  162. {
  163. [super onEnter];
  164. [inScene setScale:0.001f];
  165. [outScene setScale:1.0f];
  166. [inScene setAnchorPoint:ccp(0.5f, 0.5f)];
  167. [outScene setAnchorPoint:ccp(0.5f, 0.5f)];
  168. CCIntervalAction *rotozoom = [CCSequence actions: [CCSpawn actions:
  169. [CCScaleBy actionWithDuration:duration/2 scale:0.001f],
  170. [CCRotateBy actionWithDuration:duration/2 angle:360 *2],
  171. nil],
  172. [CCDelayTime actionWithDuration:duration/2],
  173. nil];
  174. [outScene runAction: rotozoom];
  175. [inScene runAction: [CCSequence actions:
  176. [rotozoom reverse],
  177. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  178. nil]];
  179. }
  180. @end
  181. //
  182. // JumpZoom
  183. //
  184. @implementation CCJumpZoomTransition
  185. -(void) onEnter
  186. {
  187. [super onEnter];
  188. CGSize s = [[CCDirector sharedDirector] winSize];
  189. [inScene setScale:0.5f];
  190. [inScene setPosition:ccp( s.width,0 )];
  191. [inScene setAnchorPoint:ccp(0.5f, 0.5f)];
  192. [outScene setAnchorPoint:ccp(0.5f, 0.5f)];
  193. CCIntervalAction *jump = [CCJumpBy actionWithDuration:duration/4 position:ccp(-s.width,0) height:s.width/4 jumps:2];
  194. CCIntervalAction *scaleIn = [CCScaleTo actionWithDuration:duration/4 scale:1.0f];
  195. CCIntervalAction *scaleOut = [CCScaleTo actionWithDuration:duration/4 scale:0.5f];
  196. CCIntervalAction *jumpZoomOut = [CCSequence actions: scaleOut, jump, nil];
  197. CCIntervalAction *jumpZoomIn = [CCSequence actions: jump, scaleIn, nil];
  198. CCIntervalAction *delay = [CCDelayTime actionWithDuration:duration/2];
  199. [outScene runAction: jumpZoomOut];
  200. [inScene runAction: [CCSequence actions: delay,
  201. jumpZoomIn,
  202. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  203. nil] ];
  204. }
  205. @end
  206. //
  207. // MoveInL
  208. //
  209. @implementation CCMoveInLTransition
  210. -(void) onEnter
  211. {
  212. [super onEnter];
  213. [self initScenes];
  214. CCIntervalAction *a = [self action];
  215. [inScene runAction: [CCSequence actions:
  216. [self easeActionWithAction:a],
  217. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  218. nil]
  219. ];
  220. }
  221. -(CCIntervalAction*) action
  222. {
  223. return [CCMoveTo actionWithDuration:duration position:ccp(0,0)];
  224. }
  225. -(CCIntervalAction*) easeActionWithAction:(CCIntervalAction*)action
  226. {
  227. return [CCEaseOut actionWithAction:action rate:2.0f];
  228. // return [EaseElasticOut actionWithAction:action period:0.4f];
  229. }
  230. -(void) initScenes
  231. {
  232. CGSize s = [[CCDirector sharedDirector] winSize];
  233. [inScene setPosition: ccp( -s.width,0) ];
  234. }
  235. @end
  236. //
  237. // MoveInR
  238. //
  239. @implementation CCMoveInRTransition
  240. -(void) initScenes
  241. {
  242. CGSize s = [[CCDirector sharedDirector] winSize];
  243. [inScene setPosition: ccp( s.width,0) ];
  244. }
  245. @end
  246. //
  247. // MoveInT
  248. //
  249. @implementation CCMoveInTTransition
  250. -(void) initScenes
  251. {
  252. CGSize s = [[CCDirector sharedDirector] winSize];
  253. [inScene setPosition: ccp( 0, s.height) ];
  254. }
  255. @end
  256. //
  257. // MoveInB
  258. //
  259. @implementation CCMoveInBTransition
  260. -(void) initScenes
  261. {
  262. CGSize s = [[CCDirector sharedDirector] winSize];
  263. [inScene setPosition: ccp( 0, -s.height) ];
  264. }
  265. @end
  266. //
  267. // SlideInL
  268. //
  269. // The adjust factor is needed to prevent issue #442
  270. // One solution is to use DONT_RENDER_IN_SUBPIXELS images, but NO
  271. // The other issue is that in some transitions (and I don't know why)
  272. // the order should be reversed (In in top of Out or vice-versa).
  273. #define ADJUST_FACTOR 0.5f
  274. @implementation CCSlideInLTransition
  275. -(void) onEnter
  276. {
  277. [super onEnter];
  278. [self initScenes];
  279. CCIntervalAction *in = [self action];
  280. CCIntervalAction *out = [self action];
  281. id inAction = [self easeActionWithAction:in];
  282. id outAction = [CCSequence actions:
  283. [self easeActionWithAction:out],
  284. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  285. nil];
  286. [inScene runAction: inAction];
  287. [outScene runAction: outAction];
  288. }
  289. -(void) sceneOrder
  290. {
  291. inSceneOnTop = NO;
  292. }
  293. -(void) initScenes
  294. {
  295. CGSize s = [[CCDirector sharedDirector] winSize];
  296. [inScene setPosition: ccp( -(s.width-ADJUST_FACTOR),0) ];
  297. }
  298. -(CCIntervalAction*) action
  299. {
  300. CGSize s = [[CCDirector sharedDirector] winSize];
  301. return [CCMoveBy actionWithDuration:duration position:ccp(s.width-ADJUST_FACTOR,0)];
  302. }
  303. -(CCIntervalAction*) easeActionWithAction:(CCIntervalAction*)action
  304. {
  305. return [CCEaseOut actionWithAction:action rate:2.0f];
  306. // return [EaseElasticOut actionWithAction:action period:0.4f];
  307. }
  308. @end
  309. //
  310. // SlideInR
  311. //
  312. @implementation CCSlideInRTransition
  313. -(void) sceneOrder
  314. {
  315. inSceneOnTop = YES;
  316. }
  317. -(void) initScenes
  318. {
  319. CGSize s = [[CCDirector sharedDirector] winSize];
  320. [inScene setPosition: ccp( s.width-ADJUST_FACTOR,0) ];
  321. }
  322. -(CCIntervalAction*) action
  323. {
  324. CGSize s = [[CCDirector sharedDirector] winSize];
  325. return [CCMoveBy actionWithDuration:duration position:ccp(-(s.width-ADJUST_FACTOR),0)];
  326. }
  327. @end
  328. //
  329. // SlideInT
  330. //
  331. @implementation CCSlideInTTransition
  332. -(void) sceneOrder
  333. {
  334. inSceneOnTop = NO;
  335. }
  336. -(void) initScenes
  337. {
  338. CGSize s = [[CCDirector sharedDirector] winSize];
  339. [inScene setPosition: ccp(0,s.height-ADJUST_FACTOR) ];
  340. }
  341. -(CCIntervalAction*) action
  342. {
  343. CGSize s = [[CCDirector sharedDirector] winSize];
  344. return [CCMoveBy actionWithDuration:duration position:ccp(0,-(s.height-ADJUST_FACTOR))];
  345. }
  346. @end
  347. //
  348. // SlideInB
  349. //
  350. @implementation CCSlideInBTransition
  351. -(void) sceneOrder
  352. {
  353. inSceneOnTop = YES;
  354. }
  355. -(void) initScenes
  356. {
  357. CGSize s = [[CCDirector sharedDirector] winSize];
  358. [inScene setPosition: ccp(0,-(s.height-ADJUST_FACTOR)) ];
  359. }
  360. -(CCIntervalAction*) action
  361. {
  362. CGSize s = [[CCDirector sharedDirector] winSize];
  363. return [CCMoveBy actionWithDuration:duration position:ccp(0,s.height-ADJUST_FACTOR)];
  364. }
  365. @end
  366. //
  367. // ShrinkGrow Transition
  368. //
  369. @implementation CCShrinkGrowTransition
  370. -(void) onEnter
  371. {
  372. [super onEnter];
  373. [inScene setScale:0.001f];
  374. [outScene setScale:1.0f];
  375. [inScene setAnchorPoint:ccp(2/3.0f,0.5f)];
  376. [outScene setAnchorPoint:ccp(1/3.0f,0.5f)];
  377. CCIntervalAction *scaleOut = [CCScaleTo actionWithDuration:duration scale:0.01f];
  378. CCIntervalAction *scaleIn = [CCScaleTo actionWithDuration:duration scale:1.0f];
  379. [inScene runAction: [self easeActionWithAction:scaleIn]];
  380. [outScene runAction: [CCSequence actions:
  381. [self easeActionWithAction:scaleOut],
  382. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  383. nil] ];
  384. }
  385. -(CCIntervalAction*) easeActionWithAction:(CCIntervalAction*)action
  386. {
  387. return [CCEaseOut actionWithAction:action rate:2.0f];
  388. // return [EaseElasticOut actionWithAction:action period:0.3f];
  389. }
  390. @end
  391. //
  392. // FlipX Transition
  393. //
  394. @implementation CCFlipXTransition
  395. -(void) onEnter
  396. {
  397. [super onEnter];
  398. CCIntervalAction *inA, *outA;
  399. [inScene setVisible: NO];
  400. float inDeltaZ, inAngleZ;
  401. float outDeltaZ, outAngleZ;
  402. if( orientation == kOrientationRightOver ) {
  403. inDeltaZ = 90;
  404. inAngleZ = 270;
  405. outDeltaZ = 90;
  406. outAngleZ = 0;
  407. } else {
  408. inDeltaZ = -90;
  409. inAngleZ = 90;
  410. outDeltaZ = -90;
  411. outAngleZ = 0;
  412. }
  413. inA = [CCSequence actions:
  414. [CCDelayTime actionWithDuration:duration/2],
  415. [CCShow action],
  416. [CCOrbitCamera actionWithDuration: duration/2 radius: 1 deltaRadius:0 angleZ:inAngleZ deltaAngleZ:inDeltaZ angleX:0 deltaAngleX:0],
  417. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  418. nil ];
  419. outA = [CCSequence actions:
  420. [CCOrbitCamera actionWithDuration: duration/2 radius: 1 deltaRadius:0 angleZ:outAngleZ deltaAngleZ:outDeltaZ angleX:0 deltaAngleX:0],
  421. [CCHide action],
  422. [CCDelayTime actionWithDuration:duration/2],
  423. nil ];
  424. [inScene runAction: inA];
  425. [outScene runAction: outA];
  426. }
  427. @end
  428. //
  429. // FlipY Transition
  430. //
  431. @implementation CCFlipYTransition
  432. -(void) onEnter
  433. {
  434. [super onEnter];
  435. CCIntervalAction *inA, *outA;
  436. [inScene setVisible: NO];
  437. float inDeltaZ, inAngleZ;
  438. float outDeltaZ, outAngleZ;
  439. if( orientation == kOrientationUpOver ) {
  440. inDeltaZ = 90;
  441. inAngleZ = 270;
  442. outDeltaZ = 90;
  443. outAngleZ = 0;
  444. } else {
  445. inDeltaZ = -90;
  446. inAngleZ = 90;
  447. outDeltaZ = -90;
  448. outAngleZ = 0;
  449. }
  450. inA = [CCSequence actions:
  451. [CCDelayTime actionWithDuration:duration/2],
  452. [CCShow action],
  453. [CCOrbitCamera actionWithDuration: duration/2 radius: 1 deltaRadius:0 angleZ:inAngleZ deltaAngleZ:inDeltaZ angleX:90 deltaAngleX:0],
  454. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  455. nil ];
  456. outA = [CCSequence actions:
  457. [CCOrbitCamera actionWithDuration: duration/2 radius: 1 deltaRadius:0 angleZ:outAngleZ deltaAngleZ:outDeltaZ angleX:90 deltaAngleX:0],
  458. [CCHide action],
  459. [CCDelayTime actionWithDuration:duration/2],
  460. nil ];
  461. [inScene runAction: inA];
  462. [outScene runAction: outA];
  463. }
  464. @end
  465. //
  466. // FlipAngular Transition
  467. //
  468. @implementation CCFlipAngularTransition
  469. -(void) onEnter
  470. {
  471. [super onEnter];
  472. CCIntervalAction *inA, *outA;
  473. [inScene setVisible: NO];
  474. float inDeltaZ, inAngleZ;
  475. float outDeltaZ, outAngleZ;
  476. if( orientation == kOrientationRightOver ) {
  477. inDeltaZ = 90;
  478. inAngleZ = 270;
  479. outDeltaZ = 90;
  480. outAngleZ = 0;
  481. } else {
  482. inDeltaZ = -90;
  483. inAngleZ = 90;
  484. outDeltaZ = -90;
  485. outAngleZ = 0;
  486. }
  487. inA = [CCSequence actions:
  488. [CCDelayTime actionWithDuration:duration/2],
  489. [CCShow action],
  490. [CCOrbitCamera actionWithDuration: duration/2 radius: 1 deltaRadius:0 angleZ:inAngleZ deltaAngleZ:inDeltaZ angleX:-45 deltaAngleX:0],
  491. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  492. nil ];
  493. outA = [CCSequence actions:
  494. [CCOrbitCamera actionWithDuration: duration/2 radius: 1 deltaRadius:0 angleZ:outAngleZ deltaAngleZ:outDeltaZ angleX:45 deltaAngleX:0],
  495. [CCHide action],
  496. [CCDelayTime actionWithDuration:duration/2],
  497. nil ];
  498. [inScene runAction: inA];
  499. [outScene runAction: outA];
  500. }
  501. @end
  502. //
  503. // ZoomFlipX Transition
  504. //
  505. @implementation CCZoomFlipXTransition
  506. -(void) onEnter
  507. {
  508. [super onEnter];
  509. CCIntervalAction *inA, *outA;
  510. [inScene setVisible: NO];
  511. float inDeltaZ, inAngleZ;
  512. float outDeltaZ, outAngleZ;
  513. if( orientation == kOrientationRightOver ) {
  514. inDeltaZ = 90;
  515. inAngleZ = 270;
  516. outDeltaZ = 90;
  517. outAngleZ = 0;
  518. } else {
  519. inDeltaZ = -90;
  520. inAngleZ = 90;
  521. outDeltaZ = -90;
  522. outAngleZ = 0;
  523. }
  524. inA = [CCSequence actions:
  525. [CCDelayTime actionWithDuration:duration/2],
  526. [CCSpawn actions:
  527. [CCOrbitCamera actionWithDuration: duration/2 radius: 1 deltaRadius:0 angleZ:inAngleZ deltaAngleZ:inDeltaZ angleX:0 deltaAngleX:0],
  528. [CCScaleTo actionWithDuration:duration/2 scale:1],
  529. [CCShow action],
  530. nil],
  531. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  532. nil ];
  533. outA = [CCSequence actions:
  534. [CCSpawn actions:
  535. [CCOrbitCamera actionWithDuration: duration/2 radius: 1 deltaRadius:0 angleZ:outAngleZ deltaAngleZ:outDeltaZ angleX:0 deltaAngleX:0],
  536. [CCScaleTo actionWithDuration:duration/2 scale:0.5f],
  537. nil],
  538. [CCHide action],
  539. [CCDelayTime actionWithDuration:duration/2],
  540. nil ];
  541. inScene.scale = 0.5f;
  542. [inScene runAction: inA];
  543. [outScene runAction: outA];
  544. }
  545. @end
  546. //
  547. // ZoomFlipY Transition
  548. //
  549. @implementation CCZoomFlipYTransition
  550. -(void) onEnter
  551. {
  552. [super onEnter];
  553. CCIntervalAction *inA, *outA;
  554. [inScene setVisible: NO];
  555. float inDeltaZ, inAngleZ;
  556. float outDeltaZ, outAngleZ;
  557. if( orientation == kOrientationUpOver ) {
  558. inDeltaZ = 90;
  559. inAngleZ = 270;
  560. outDeltaZ = 90;
  561. outAngleZ = 0;
  562. } else {
  563. inDeltaZ = -90;
  564. inAngleZ = 90;
  565. outDeltaZ = -90;
  566. outAngleZ = 0;
  567. }
  568. inA = [CCSequence actions:
  569. [CCDelayTime actionWithDuration:duration/2],
  570. [CCSpawn actions:
  571. [CCOrbitCamera actionWithDuration: duration/2 radius: 1 deltaRadius:0 angleZ:inAngleZ deltaAngleZ:inDeltaZ angleX:90 deltaAngleX:0],
  572. [CCScaleTo actionWithDuration:duration/2 scale:1],
  573. [CCShow action],
  574. nil],
  575. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  576. nil ];
  577. outA = [CCSequence actions:
  578. [CCSpawn actions:
  579. [CCOrbitCamera actionWithDuration: duration/2 radius: 1 deltaRadius:0 angleZ:outAngleZ deltaAngleZ:outDeltaZ angleX:90 deltaAngleX:0],
  580. [CCScaleTo actionWithDuration:duration/2 scale:0.5f],
  581. nil],
  582. [CCHide action],
  583. [CCDelayTime actionWithDuration:duration/2],
  584. nil ];
  585. inScene.scale = 0.5f;
  586. [inScene runAction: inA];
  587. [outScene runAction: outA];
  588. }
  589. @end
  590. //
  591. // ZoomFlipAngular Transition
  592. //
  593. @implementation CCZoomFlipAngularTransition
  594. -(void) onEnter
  595. {
  596. [super onEnter];
  597. CCIntervalAction *inA, *outA;
  598. [inScene setVisible: NO];
  599. float inDeltaZ, inAngleZ;
  600. float outDeltaZ, outAngleZ;
  601. if( orientation == kOrientationRightOver ) {
  602. inDeltaZ = 90;
  603. inAngleZ = 270;
  604. outDeltaZ = 90;
  605. outAngleZ = 0;
  606. } else {
  607. inDeltaZ = -90;
  608. inAngleZ = 90;
  609. outDeltaZ = -90;
  610. outAngleZ = 0;
  611. }
  612. inA = [CCSequence actions:
  613. [CCDelayTime actionWithDuration:duration/2],
  614. [CCSpawn actions:
  615. [CCOrbitCamera actionWithDuration: duration/2 radius: 1 deltaRadius:0 angleZ:inAngleZ deltaAngleZ:inDeltaZ angleX:-45 deltaAngleX:0],
  616. [CCScaleTo actionWithDuration:duration/2 scale:1],
  617. [CCShow action],
  618. nil],
  619. [CCShow action],
  620. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  621. nil ];
  622. outA = [CCSequence actions:
  623. [CCSpawn actions:
  624. [CCOrbitCamera actionWithDuration: duration/2 radius: 1 deltaRadius:0 angleZ:outAngleZ deltaAngleZ:outDeltaZ angleX:45 deltaAngleX:0],
  625. [CCScaleTo actionWithDuration:duration/2 scale:0.5f],
  626. nil],
  627. [CCHide action],
  628. [CCDelayTime actionWithDuration:duration/2],
  629. nil ];
  630. inScene.scale = 0.5f;
  631. [inScene runAction: inA];
  632. [outScene runAction: outA];
  633. }
  634. @end
  635. //
  636. // Fade Transition
  637. //
  638. @implementation CCFadeTransition
  639. +(id) transitionWithDuration:(ccTime)d scene:(CCScene*)s withColor:(ccColor3B)color
  640. {
  641. return [[[self alloc] initWithDuration:d scene:s withColor:color] autorelease];
  642. }
  643. -(id) initWithDuration:(ccTime)d scene:(CCScene*)s withColor:(ccColor3B)aColor
  644. {
  645. if( (self=[super initWithDuration:d scene:s]) ) {
  646. color.r = aColor.r;
  647. color.g = aColor.g;
  648. color.b = aColor.b;
  649. }
  650. return self;
  651. }
  652. -(id) initWithDuration:(ccTime)d scene:(CCScene*)s
  653. {
  654. return [self initWithDuration:d scene:s withColor:ccBLACK];
  655. }
  656. -(void) onEnter
  657. {
  658. [super onEnter];
  659. CCColorLayer *l = [CCColorLayer layerWithColor:color];
  660. [inScene setVisible: NO];
  661. [self addChild: l z:2 tag:kSceneFade];
  662. CCNode *f = [self getChildByTag:kSceneFade];
  663. CCIntervalAction *a = [CCSequence actions:
  664. [CCFadeIn actionWithDuration:duration/2],
  665. [CCCallFunc actionWithTarget:self selector:@selector(hideOutShowIn)],
  666. [CCFadeOut actionWithDuration:duration/2],
  667. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  668. nil ];
  669. [f runAction: a];
  670. }
  671. -(void) onExit
  672. {
  673. [super onExit];
  674. [self removeChildByTag:kSceneFade cleanup:NO];
  675. }
  676. @end
  677. //
  678. // Cross Fade Transition
  679. //
  680. @implementation CCCrossFadeTransition
  681. -(void) draw
  682. {
  683. // override draw since both scenes (textures) are rendered in 1 scene
  684. }
  685. -(void) onEnter
  686. {
  687. [super onEnter];
  688. // create a transparent color layer
  689. // in which we are going to add our rendertextures
  690. ccColor4B color = {0,0,0,0};
  691. CGSize size = [[CCDirector sharedDirector] winSize];
  692. CCColorLayer * layer = [CCColorLayer layerWithColor:color];
  693. // create the first render texture for inScene
  694. CCRenderTexture *inTexture = [CCRenderTexture renderTextureWithWidth:size.width height:size.height];
  695. inTexture.sprite.anchorPoint= ccp(0.5f,0.5f);
  696. inTexture.position = ccp(size.width/2, size.height/2);
  697. inTexture.anchorPoint = ccp(0.5f,0.5f);
  698. // render inScene to its texturebuffer
  699. [inTexture begin];
  700. [inScene visit];
  701. [inTexture end];
  702. // create the second render texture for outScene
  703. CCRenderTexture *outTexture = [CCRenderTexture renderTextureWithWidth:size.width height:size.height];
  704. outTexture.sprite.anchorPoint= ccp(0.5f,0.5f);
  705. outTexture.position = ccp(size.width/2, size.height/2);
  706. outTexture.anchorPoint = ccp(0.5f,0.5f);
  707. // render outScene to its texturebuffer
  708. [outTexture begin];
  709. [outScene visit];
  710. [outTexture end];
  711. // create blend functions
  712. ccBlendFunc blend1 = {GL_ONE, GL_ONE}; // inScene will lay on background and will not be used with alpha
  713. ccBlendFunc blend2 = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA}; // we are going to blend outScene via alpha
  714. // set blendfunctions
  715. [inTexture.sprite setBlendFunc:blend1];
  716. [outTexture.sprite setBlendFunc:blend2];
  717. // add render textures to the layer
  718. [layer addChild:inTexture];
  719. [layer addChild:outTexture];
  720. // initial opacity:
  721. [inTexture.sprite setOpacity:255];
  722. [outTexture.sprite setOpacity:255];
  723. // create the blend action
  724. CCIntervalAction * layerAction = [CCSequence actions:
  725. [CCFadeTo actionWithDuration:duration opacity:0],
  726. [CCCallFunc actionWithTarget:self selector:@selector(hideOutShowIn)],
  727. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  728. nil ];
  729. // run the blend action
  730. [outTexture.sprite runAction: layerAction];
  731. // add the layer (which contains our two rendertextures) to the scene
  732. [self addChild: layer z:2 tag:kSceneFade];
  733. }
  734. // clean up on exit
  735. -(void) onExit
  736. {
  737. // remove our layer and release all containing objects
  738. [self removeChildByTag:kSceneFade cleanup:NO];
  739. [super onExit];
  740. }
  741. @end
  742. //
  743. // TurnOffTilesTransition
  744. //
  745. @implementation CCTurnOffTilesTransition
  746. // override addScenes, and change the order
  747. -(void) sceneOrder
  748. {
  749. inSceneOnTop = NO;
  750. }
  751. -(void) onEnter
  752. {
  753. [super onEnter];
  754. CGSize s = [[CCDirector sharedDirector] winSize];
  755. float aspect = s.width / s.height;
  756. int x = 12 * aspect;
  757. int y = 12;
  758. id toff = [CCTurnOffTiles actionWithSize: ccg(x,y) duration:duration];
  759. id action = [self easeActionWithAction:toff];
  760. [outScene runAction: [CCSequence actions: action,
  761. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  762. [CCStopGrid action],
  763. nil]
  764. ];
  765. }
  766. -(CCIntervalAction*) easeActionWithAction:(CCIntervalAction*)action
  767. {
  768. return action;
  769. // return [EaseIn actionWithAction:action rate:2.0f];
  770. }
  771. @end
  772. #pragma mark Split Transitions
  773. //
  774. // SplitCols Transition
  775. //
  776. @implementation CCSplitColsTransition
  777. -(void) onEnter
  778. {
  779. [super onEnter];
  780. inScene.visible = NO;
  781. id split = [self action];
  782. id seq = [CCSequence actions:
  783. split,
  784. [CCCallFunc actionWithTarget:self selector:@selector(hideOutShowIn)],
  785. [split reverse],
  786. nil
  787. ];
  788. [self runAction: [CCSequence actions:
  789. [self easeActionWithAction:seq],
  790. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  791. [CCStopGrid action],
  792. nil]
  793. ];
  794. }
  795. -(CCIntervalAction*) action
  796. {
  797. return [CCSplitCols actionWithCols:3 duration:duration/2.0f];
  798. }
  799. -(CCIntervalAction*) easeActionWithAction:(CCIntervalAction*)action
  800. {
  801. return [CCEaseInOut actionWithAction:action rate:3.0f];
  802. }
  803. @end
  804. //
  805. // SplitRows Transition
  806. //
  807. @implementation CCSplitRowsTransition
  808. -(CCIntervalAction*) action
  809. {
  810. return [CCSplitRows actionWithRows:3 duration:duration/2.0f];
  811. }
  812. @end
  813. #pragma mark Fade Grid Transitions
  814. //
  815. // FadeTR Transition
  816. //
  817. @implementation CCFadeTRTransition
  818. -(void) sceneOrder
  819. {
  820. inSceneOnTop = NO;
  821. }
  822. -(void) onEnter
  823. {
  824. [super onEnter];
  825. CGSize s = [[CCDirector sharedDirector] winSize];
  826. float aspect = s.width / s.height;
  827. int x = 12 * aspect;
  828. int y = 12;
  829. id action = [self actionWithSize:ccg(x,y)];
  830. [outScene runAction: [CCSequence actions:
  831. [self easeActionWithAction:action],
  832. [CCCallFunc actionWithTarget:self selector:@selector(finish)],
  833. [CCStopGrid action],
  834. nil]
  835. ];
  836. }
  837. -(CCIntervalAction*) actionWithSize: (ccGridSize) v
  838. {
  839. return [CCFadeOutTRTiles actionWithSize:v duration:duration];
  840. }
  841. -(CCIntervalAction*) easeActionWithAction:(CCIntervalAction*)action
  842. {
  843. return action;
  844. // return [EaseIn actionWithAction:action rate:2.0f];
  845. }
  846. @end
  847. //
  848. // FadeBL Transition
  849. //
  850. @implementation CCFadeBLTransition
  851. -(CCIntervalAction*) actionWithSize: (ccGridSize) v
  852. {
  853. return [CCFadeOutBLTiles actionWithSize:v duration:duration];
  854. }
  855. @end
  856. //
  857. // FadeUp Transition
  858. //
  859. @implementation CCFadeUpTransition
  860. -(CCIntervalAction*) actionWithSize: (ccGridSize) v
  861. {
  862. return [CCFadeOutUpTiles actionWithSize:v duration:duration];
  863. }
  864. @end
  865. //
  866. // FadeDown Transition
  867. //
  868. @implementation CCFadeDownTransition
  869. -(CCIntervalAction*) actionWithSize: (ccGridSize) v
  870. {
  871. return [CCFadeOutDownTiles actionWithSize:v duration:duration];
  872. }
  873. @end