PageRenderTime 26ms CodeModel.GetById 11ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/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 ***********************************************************************/
 18package org.mt4j.sceneManagement.transition;
 19
 20import org.mt4j.MTApplication;
 21import org.mt4j.components.visibleComponents.shapes.MTRectangle;
 22import org.mt4j.components.visibleComponents.widgets.MTSceneTexture;
 23import org.mt4j.sceneManagement.Iscene;
 24import org.mt4j.util.MTColor;
 25import org.mt4j.util.animation.AnimationEvent;
 26import org.mt4j.util.animation.IAnimation;
 27import org.mt4j.util.animation.IAnimationListener;
 28import org.mt4j.util.animation.ani.AniAnimation;
 29import org.mt4j.util.math.Vector3D;
 30
 31/**
 32 * The Class SlideTransition.
 33 * 
 34 * @author Christopher Ruff
 35 */
 36public class SlideTransition extends AbstractTransition {
 37	
 38	/** The app. */
 39	private MTApplication app;
 40	
 41	/** The finished. */
 42	private boolean finished;
 43	
 44	/** The last scene. */
 45	private Iscene lastScene;
 46	
 47	/** The next scene. */
 48	private Iscene nextScene;
 49	
 50	/** The last scene window. */
 51	private MTSceneTexture lastSceneWindow;
 52	
 53	/** The next scene window. */
 54	private MTSceneTexture nextSceneWindow;
 55	
 56	/** The anim. */
 57	private IAnimation anim;
 58	
 59	/** The duration. */
 60	private int duration;
 61	
 62	/** The last scene rectangle. */
 63	private MTRectangle lastSceneRectangle;
 64	
 65	/** The next scene rectangle. */
 66	private MTRectangle nextSceneRectangle;
 67	
 68	public boolean slideLeft;
 69	
 70	
 71	/**
 72	 * Instantiates a new slide transition.
 73	 * 
 74	 * @param mtApplication the mt application
 75	 */
 76	public SlideTransition(MTApplication mtApplication) {
 77		this(mtApplication, 2000);
 78	}
 79	
 80	public SlideTransition(MTApplication mtApplication, long duration) {
 81		this(mtApplication, duration, true);
 82	}
 83	
 84	/**
 85	 * Instantiates a new slide transition.
 86	 * 
 87	 * @param mtApplication the mt application
 88	 * @param duration the duration
 89	 */
 90	public SlideTransition(MTApplication mtApplication, long duration, boolean slideLeft) {
 91		super(mtApplication, "Slide Transition");
 92		this.app = mtApplication;
 93		this.duration = (int) duration;
 94		this.finished = true;
 95		this.slideLeft = slideLeft;
 96		
 97//		anim = new Animation("Flip animation 2", new MultiPurposeInterpolator(app.width, 0, this.duration, 0.0f, 0.7f, 1) , this);
 98		anim = new AniAnimation(app.width, 0, this.duration, AniAnimation.CIRC_OUT, this);
 99		if (!slideLeft)
100			((AniAnimation)anim).reverse();
101		anim.addAnimationListener(new IAnimationListener(){
102			public void processAnimationEvent(AnimationEvent ae) {
103				switch (ae.getId()) {
104				case AnimationEvent.ANIMATION_STARTED:
105				case AnimationEvent.ANIMATION_UPDATED:
106					nextSceneRectangle.translateGlobal(new Vector3D(ae.getDelta(),0,0));
107					lastSceneRectangle.translateGlobal(new Vector3D(ae.getDelta(),0,0));
108					break;
109				case AnimationEvent.ANIMATION_ENDED:
110					nextSceneRectangle.translateGlobal(new Vector3D(ae.getDelta(),0,0));
111					lastSceneRectangle.translateGlobal(new Vector3D(ae.getDelta(),0,0));
112					finished = true;
113					break;
114				default:
115					break;
116				}
117			}});
118//		((Animation)anim).setResetOnFinish(true);
119	}
120
121
122	/* (non-Javadoc)
123	 * @see org.mt4j.sceneManagement.transition.ITransition#isFinished()
124	 */
125	public boolean isFinished() {
126		return finished;
127	}
128
129	
130	
131	/* (non-Javadoc)
132	 * @see org.mt4j.sceneManagement.transition.ITransition#setup(org.mt4j.sceneManagement.Iscene, org.mt4j.sceneManagement.Iscene)
133	 */
134	public void setup(Iscene lastScenee, Iscene nextScenee) {
135		this.lastScene = lastScenee;
136		this.nextScene = nextScenee;
137		finished = false;
138		
139		//Disable the scene's global input processors. We will be redirecting the input
140		//from the current scene to the window scene
141		app.getInputManager().disableGlobalInputProcessors(lastScene);
142		app.getInputManager().disableGlobalInputProcessors(nextScene);
143		
144		app.invokeLater(new Runnable() {
145			public void run() {
146				lastSceneWindow = new MTSceneTexture(app,0, 0, lastScene);
147				nextSceneWindow = new MTSceneTexture(app,0, 0, nextScene);
148
149				lastSceneRectangle = new MTRectangle(app,0, 0, app.width, app.height);
150				lastSceneRectangle.setGeometryInfo(lastSceneWindow.getGeometryInfo());
151				lastSceneRectangle.setTexture(lastSceneWindow.getTexture());
152				lastSceneRectangle.setStrokeColor(new MTColor(0,0,0,255));
153
154				nextSceneRectangle = new MTRectangle(app,0, 0, app.width, app.height);
155				nextSceneRectangle.setGeometryInfo(nextSceneWindow.getGeometryInfo());
156				nextSceneRectangle.setTexture(nextSceneWindow.getTexture());
157				nextSceneRectangle.setStrokeColor(new MTColor(0,0,0,255));
158
159				getCanvas().addChild(lastSceneRectangle);
160				getCanvas().addChild(nextSceneRectangle);
161				
162				if (slideLeft)
163					nextSceneRectangle.translateGlobal(new Vector3D(app.width,0,0));
164				else
165					nextSceneRectangle.translateGlobal(new Vector3D(-app.width,0,0));
166				
167				nextSceneRectangle.setVisible(true);
168
169				//Draw scenes into texture once!
170				lastSceneWindow.drawComponent(app.g);
171				nextSceneWindow.drawComponent(app.g);
172				
173				anim.start();
174			}
175		});
176		
177		//TODO wihtout FBO copyPixels
178	}
179	
180	
181	@Override
182	public void onLeave() {
183		finished = true;
184		this.lastScene = null;
185		this.nextScene = null;
186		
187		this.lastSceneWindow.destroy();
188		this.nextSceneWindow.destroy();
189		lastSceneRectangle.destroy();
190		nextSceneRectangle.destroy();
191	}
192	
193
194}