PageRenderTime 66ms CodeModel.GetById 54ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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