/src/org/mt4j/sceneManagement/transition/SlideTransition.java

http://mt4j.googlecode.com/ · Java · 194 lines · 101 code · 38 blank · 55 comment · 3 complexity · 38fef1894f549443546b2cacae93eab1 MD5 · raw file

  1. /***********************************************************************
  2. * mt4j Copyright (c) 2008 - 2010 Christopher Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. ***********************************************************************/
  18. package org.mt4j.sceneManagement.transition;
  19. import org.mt4j.MTApplication;
  20. import org.mt4j.components.visibleComponents.shapes.MTRectangle;
  21. import org.mt4j.components.visibleComponents.widgets.MTSceneTexture;
  22. import org.mt4j.sceneManagement.Iscene;
  23. import org.mt4j.util.MTColor;
  24. import org.mt4j.util.animation.AnimationEvent;
  25. import org.mt4j.util.animation.IAnimation;
  26. import org.mt4j.util.animation.IAnimationListener;
  27. import org.mt4j.util.animation.ani.AniAnimation;
  28. import org.mt4j.util.math.Vector3D;
  29. /**
  30. * The Class SlideTransition.
  31. *
  32. * @author Christopher Ruff
  33. */
  34. public class SlideTransition extends AbstractTransition {
  35. /** The app. */
  36. private MTApplication app;
  37. /** The finished. */
  38. private boolean finished;
  39. /** The last scene. */
  40. private Iscene lastScene;
  41. /** The next scene. */
  42. private Iscene nextScene;
  43. /** The last scene window. */
  44. private MTSceneTexture lastSceneWindow;
  45. /** The next scene window. */
  46. private MTSceneTexture nextSceneWindow;
  47. /** The anim. */
  48. private IAnimation anim;
  49. /** The duration. */
  50. private int duration;
  51. /** The last scene rectangle. */
  52. private MTRectangle lastSceneRectangle;
  53. /** The next scene rectangle. */
  54. private MTRectangle nextSceneRectangle;
  55. public boolean slideLeft;
  56. /**
  57. * Instantiates a new slide transition.
  58. *
  59. * @param mtApplication the mt application
  60. */
  61. public SlideTransition(MTApplication mtApplication) {
  62. this(mtApplication, 2000);
  63. }
  64. public SlideTransition(MTApplication mtApplication, long duration) {
  65. this(mtApplication, duration, true);
  66. }
  67. /**
  68. * Instantiates a new slide transition.
  69. *
  70. * @param mtApplication the mt application
  71. * @param duration the duration
  72. */
  73. public SlideTransition(MTApplication mtApplication, long duration, boolean slideLeft) {
  74. super(mtApplication, "Slide Transition");
  75. this.app = mtApplication;
  76. this.duration = (int) duration;
  77. this.finished = true;
  78. this.slideLeft = slideLeft;
  79. // anim = new Animation("Flip animation 2", new MultiPurposeInterpolator(app.width, 0, this.duration, 0.0f, 0.7f, 1) , this);
  80. anim = new AniAnimation(app.width, 0, this.duration, AniAnimation.CIRC_OUT, this);
  81. if (!slideLeft)
  82. ((AniAnimation)anim).reverse();
  83. anim.addAnimationListener(new IAnimationListener(){
  84. public void processAnimationEvent(AnimationEvent ae) {
  85. switch (ae.getId()) {
  86. case AnimationEvent.ANIMATION_STARTED:
  87. case AnimationEvent.ANIMATION_UPDATED:
  88. nextSceneRectangle.translateGlobal(new Vector3D(ae.getDelta(),0,0));
  89. lastSceneRectangle.translateGlobal(new Vector3D(ae.getDelta(),0,0));
  90. break;
  91. case AnimationEvent.ANIMATION_ENDED:
  92. nextSceneRectangle.translateGlobal(new Vector3D(ae.getDelta(),0,0));
  93. lastSceneRectangle.translateGlobal(new Vector3D(ae.getDelta(),0,0));
  94. finished = true;
  95. break;
  96. default:
  97. break;
  98. }
  99. }});
  100. // ((Animation)anim).setResetOnFinish(true);
  101. }
  102. /* (non-Javadoc)
  103. * @see org.mt4j.sceneManagement.transition.ITransition#isFinished()
  104. */
  105. public boolean isFinished() {
  106. return finished;
  107. }
  108. /* (non-Javadoc)
  109. * @see org.mt4j.sceneManagement.transition.ITransition#setup(org.mt4j.sceneManagement.Iscene, org.mt4j.sceneManagement.Iscene)
  110. */
  111. public void setup(Iscene lastScenee, Iscene nextScenee) {
  112. this.lastScene = lastScenee;
  113. this.nextScene = nextScenee;
  114. finished = false;
  115. //Disable the scene's global input processors. We will be redirecting the input
  116. //from the current scene to the window scene
  117. app.getInputManager().disableGlobalInputProcessors(lastScene);
  118. app.getInputManager().disableGlobalInputProcessors(nextScene);
  119. app.invokeLater(new Runnable() {
  120. public void run() {
  121. lastSceneWindow = new MTSceneTexture(app,0, 0, lastScene);
  122. nextSceneWindow = new MTSceneTexture(app,0, 0, nextScene);
  123. lastSceneRectangle = new MTRectangle(app,0, 0, app.width, app.height);
  124. lastSceneRectangle.setGeometryInfo(lastSceneWindow.getGeometryInfo());
  125. lastSceneRectangle.setTexture(lastSceneWindow.getTexture());
  126. lastSceneRectangle.setStrokeColor(new MTColor(0,0,0,255));
  127. nextSceneRectangle = new MTRectangle(app,0, 0, app.width, app.height);
  128. nextSceneRectangle.setGeometryInfo(nextSceneWindow.getGeometryInfo());
  129. nextSceneRectangle.setTexture(nextSceneWindow.getTexture());
  130. nextSceneRectangle.setStrokeColor(new MTColor(0,0,0,255));
  131. getCanvas().addChild(lastSceneRectangle);
  132. getCanvas().addChild(nextSceneRectangle);
  133. if (slideLeft)
  134. nextSceneRectangle.translateGlobal(new Vector3D(app.width,0,0));
  135. else
  136. nextSceneRectangle.translateGlobal(new Vector3D(-app.width,0,0));
  137. nextSceneRectangle.setVisible(true);
  138. //Draw scenes into texture once!
  139. lastSceneWindow.drawComponent(app.g);
  140. nextSceneWindow.drawComponent(app.g);
  141. anim.start();
  142. }
  143. });
  144. //TODO wihtout FBO copyPixels
  145. }
  146. @Override
  147. public void onLeave() {
  148. finished = true;
  149. this.lastScene = null;
  150. this.nextScene = null;
  151. this.lastSceneWindow.destroy();
  152. this.nextSceneWindow.destroy();
  153. lastSceneRectangle.destroy();
  154. nextSceneRectangle.destroy();
  155. }
  156. }