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

http://mt4j.googlecode.com/ · Java · 177 lines · 92 code · 31 blank · 54 comment · 2 complexity · a581dc4ec30a1f419874ba7917db45e4 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.sceneManagement.Iscene;
  22. import org.mt4j.util.MTColor;
  23. import org.mt4j.util.animation.Animation;
  24. import org.mt4j.util.animation.AnimationEvent;
  25. import org.mt4j.util.animation.IAnimationListener;
  26. import org.mt4j.util.animation.MultiPurposeInterpolator;
  27. import processing.core.PGraphics;
  28. /**
  29. * The Class FadeTransition.
  30. *
  31. * @author Christopher Ruff
  32. */
  33. public class FadeTransition extends AbstractTransition {
  34. /** The app. */
  35. private MTApplication app;
  36. /** The finished. */
  37. private boolean finished;
  38. /** The anim. */
  39. private Animation anim;
  40. /** The anim2. */
  41. private Animation anim2;
  42. /** The full screen quad. */
  43. private MTRectangle fullScreenQuad;
  44. /** The scene to draw. */
  45. private Iscene sceneToDraw;
  46. /** The last scene. */
  47. private Iscene lastScene;
  48. /** The next scene. */
  49. private Iscene nextScene;
  50. /** The duration. */
  51. private long duration;
  52. /**
  53. * Instantiates a new fade transition.
  54. *
  55. * @param mtApplication the mt application
  56. */
  57. public FadeTransition(MTApplication mtApplication) {
  58. this(mtApplication, 2000);
  59. }
  60. /**
  61. * Instantiates a new fade transition.
  62. *
  63. * @param mtApplication the mt application
  64. * @param duration the duration
  65. */
  66. public FadeTransition(MTApplication mtApplication, long duration) {
  67. super(mtApplication, "Fade Transition");
  68. this.app = mtApplication;
  69. this.duration = duration;
  70. this.setClear(true);
  71. finished = false;
  72. anim2 = new Animation("Fade animation 2", new MultiPurposeInterpolator(255,0, this.duration/2f, 0, 0.8f, 1) , this);
  73. anim2.addAnimationListener(new IAnimationListener(){
  74. //@Override
  75. public void processAnimationEvent(AnimationEvent ae) {
  76. switch (ae.getId()) {
  77. case AnimationEvent.ANIMATION_STARTED:
  78. case AnimationEvent.ANIMATION_UPDATED:
  79. fullScreenQuad.setFillColor(new MTColor(0,0,0, ae.getValue()));
  80. break;
  81. case AnimationEvent.ANIMATION_ENDED:
  82. fullScreenQuad.setFillColor(new MTColor(0,0,0, ae.getValue()));
  83. finished = true;
  84. break;
  85. default:
  86. break;
  87. }
  88. }});
  89. anim2.setResetOnFinish(true);
  90. anim = new Animation("Fade animation 1", new MultiPurposeInterpolator(0,255, this.duration/2f, 0, 1, 1) , this);
  91. anim.addAnimationListener(new IAnimationListener(){
  92. //@Override
  93. public void processAnimationEvent(AnimationEvent ae) {
  94. switch (ae.getId()) {
  95. case AnimationEvent.ANIMATION_STARTED:
  96. case AnimationEvent.ANIMATION_UPDATED:
  97. fullScreenQuad.setFillColor(new MTColor(0,0,0, ae.getValue()));
  98. break;
  99. case AnimationEvent.ANIMATION_ENDED:
  100. sceneToDraw = nextScene;
  101. anim2.start();
  102. break;
  103. default:
  104. break;
  105. }
  106. }});
  107. anim.setResetOnFinish(true);
  108. fullScreenQuad = new MTRectangle(app,0, 0, app.width, app.height);
  109. fullScreenQuad.setFillColor(new MTColor(0,0,0,0));
  110. fullScreenQuad.setNoStroke(true);
  111. }
  112. /* (non-Javadoc)
  113. * @see org.mt4j.sceneManagement.transition.ITransition#setup(org.mt4j.sceneManagement.Iscene, org.mt4j.sceneManagement.Iscene)
  114. */
  115. public void setup(Iscene lastScene, Iscene nextScene) {
  116. this.lastScene = lastScene;
  117. this.nextScene = nextScene;
  118. // sceneToDraw = this.getPreviousScene();
  119. sceneToDraw = this.lastScene;
  120. finished = false;
  121. anim.start();
  122. }
  123. /* (non-Javadoc)
  124. * @see org.mt4j.sceneManagement.AbstractScene#drawAndUpdate(processing.core.PGraphics, long)
  125. */
  126. @Override
  127. public void drawAndUpdate(PGraphics graphics, long timeDelta) {
  128. super.drawAndUpdate(graphics, timeDelta);
  129. sceneToDraw.drawAndUpdate(graphics, timeDelta);
  130. fullScreenQuad.drawComponent(graphics);
  131. }
  132. @Override
  133. public void onLeave() {
  134. anim.stop();
  135. anim2.stop();
  136. finished = true;
  137. this.lastScene = null;
  138. this.nextScene = null;
  139. }
  140. /* (non-Javadoc)
  141. * @see org.mt4j.sceneManagement.transition.ITransition#isFinished()
  142. */
  143. public boolean isFinished() {
  144. return finished;
  145. }
  146. }