PageRenderTime 28ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/src/org/mt4j/util/animation/Animation.java

http://mt4j.googlecode.com/
Java | 284 lines | 116 code | 52 blank | 116 comment | 10 complexity | 63461fb75a3aa5c93f3e79ba5cdc9acc MD5 | raw file
  1/***********************************************************************
  2 * mt4j Copyright (c) 2008 - 2009 C.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.util.animation;
 19
 20
 21
 22/**
 23 * The Class Animation.
 24 * @author Christopher Ruff
 25 */
 26public class Animation extends AbstractAnimation implements IAnimationManagerListener, IAnimation{
 27	
 28	/** The name. */
 29	private String name;
 30	
 31	/** The interpolator. */
 32	private Iinterpolator interpolator;
 33	
 34	/** The reset on finish. */
 35	private boolean resetOnFinish;
 36	
 37	/** The trigger time. */
 38	private long triggerTime;
 39	
 40	/** The trigger count down. */
 41	private long triggerCountDown;
 42	
 43	/** The has started. */
 44	private boolean hasStarted;
 45	
 46	
 47	/**
 48	 * Instantiates a new animation.
 49	 * 
 50	 * @param name the name
 51	 * @param interpolator the interpolator
 52	 * @param targetObject the target object
 53	 */
 54	public Animation(String name, Iinterpolator interpolator, Object targetObject) {
 55		this(name, interpolator, targetObject, 0);
 56	}
 57	
 58	/**
 59	 * creates a new Animation object with the given interpolator.
 60	 * <br> if animating a concrete object, the targetObject should
 61	 * be passed as a parameter. If the Animation has no concrete target
 62	 * "null" can be passed
 63	 * 
 64	 * @param name the name
 65	 * @param interpolator the interpolator
 66	 * @param targetObject the target object
 67	 * @param triggerTime the trigger time
 68	 */
 69	public Animation(String name, Iinterpolator interpolator, Object targetObject, int triggerTime) {
 70		super(targetObject);
 71		this.resetOnFinish = true; //Default
 72		this.name = name;
 73		
 74		this.interpolator = interpolator;
 75		
 76		this.triggerTime = triggerTime;
 77		this.triggerCountDown = triggerTime;
 78		this.hasStarted = false;
 79	}
 80
 81	
 82	/* (non-Javadoc)
 83	 * @see org.mt4j.util.animation.IAnimation#start()
 84	 */
 85	public void start(){
 86		if (this.getInterpolator().isFinished()){
 87			System.err.println("Animation: " + this.getName() + " has finished! To start it again, call restart() or set Animation.setResetOnFinish(true)");
 88			return;
 89		}
 90
 91		AnimationManager.getInstance().registerAnimation(this);
 92		AnimationManager.getInstance().addAnimationManagerListener(this);
 93
 94		//		System.out.println("Animation STARTED: " + this.getName());
 95		//		fireAnimationEvent(new AnimationEvent(this, AnimationEvent.ANIMATION_STARTED, this, this.getTargetObject()));
 96	}
 97
 98	/**
 99	 * Restart.
100	 */
101	public void restart(){
102		this.getInterpolator().resetInterpolator();
103		
104		this.triggerCountDown = this.getTriggerTime();
105		this.hasStarted = false;
106		AnimationManager.getInstance().registerAnimation(this);
107		AnimationManager.getInstance().addAnimationManagerListener(this);
108		
109//		System.out.println("Animation RESTARTED: " + this.getName());
110//		fireAnimationEvent(new AnimationEvent(this, AnimationEvent.ANIMATION_STARTED, this, this.getTargetObject()));
111	}
112	
113	
114	/* (non-Javadoc)
115	 * @see org.mt4j.util.animation.IAnimation#stop()
116	 */
117	public void stop(){
118		AnimationManager.getInstance().unregisterAnimation(this);
119		AnimationManager.getInstance().removeAnimationManagerListener(this);
120		
121//		System.out.println("Animation FINISHED: " + this.getName());
122		//TODO fire?
123//		fireAnimationEvent(new AnimationEvent(this, AnimationEvent.ANIMATION_ENDED, this, this.getTargetObject()));
124	}
125	
126	/**
127	 * Interface method of IAnimationManagerListener
128	 * <br>used to update the anmation (interpolate) with a given timedelta.
129	 * 
130	 * @param ev the ev
131	 */
132	public void updateAnimation(AnimationUpdateEvent ev) {
133		//System.out.println("animating " + a.getName());
134		
135			if (triggerTime != 0){//If trigger is set
136				triggerCountDown -= ev.getDeltaTime(); //if !<0?
137				
138				if (triggerCountDown <= 0){ //if trigger abgelaufen
139					//Interpoliere mit neuerm zeitdelta auf neuen wert
140					interpolator.interpolate(ev.getDeltaTime());
141					
142					if (!interpolator.isFinished()){
143						if (!hasStarted){
144							hasStarted = true;
145							this.fireAnimationEvent(new AnimationEvent(this, AnimationEvent.ANIMATION_STARTED, this, this.getTarget()));
146						}else{
147							this.fireAnimationEvent(new AnimationEvent(this, AnimationEvent.ANIMATION_UPDATED, this, this.getTarget()));
148						}
149					}else{
150						//FIXME wenn gefinished, sollte der interpolator bei lastStepdelta und 0 zur???ckgeben, oder??
151						this.fireAnimationEvent(new AnimationEvent(this, AnimationEvent.ANIMATION_ENDED, this, this.getTarget()));
152						AnimationManager.getInstance().unregisterAnimation(this);
153						AnimationManager.getInstance().removeAnimationManagerListener(this);
154						this.triggerCountDown = this.getTriggerTime();
155						
156						if (this.isResetOnFinish()){
157							this.getInterpolator().resetInterpolator();
158							this.triggerCountDown = this.triggerTime;
159							this.hasStarted = false;
160						}
161					} 
162				}//if triggetcount not up, do nothing
163			}else{//If no trigger is set
164				interpolator.interpolate(ev.getDeltaTime());
165				
166				if (!this.interpolator.isFinished()){
167					if (!this.hasStarted){ //Animation hasnt begun yet
168						this.hasStarted = true;
169						this.fireAnimationEvent(new AnimationEvent(this, AnimationEvent.ANIMATION_STARTED, this, this.getTarget()));
170					}else{
171						this.fireAnimationEvent(new AnimationEvent(this, AnimationEvent.ANIMATION_UPDATED, this, this.getTarget()));
172					}
173				}else{
174					this.fireAnimationEvent(new AnimationEvent(this, AnimationEvent.ANIMATION_ENDED, this, this.getTarget()));
175					AnimationManager.getInstance().unregisterAnimation(this);
176					AnimationManager.getInstance().removeAnimationManagerListener(this);
177					
178					if (this.isResetOnFinish()){
179						this.getInterpolator().resetInterpolator();
180						this.triggerCountDown = this.triggerTime; //Reset triggercountdown
181						this.hasStarted = false;
182					}
183				}//end else interpol !finished
184			}//end else trigger not set
185	}
186	
187
188	/**
189	 * Gets the trigger time.
190	 * 
191	 * @return the trigger time
192	 */
193	public long getTriggerTime() {
194		return triggerTime;
195	}
196
197	/**
198	 * Sets the trigger time.
199	 * 
200	 * @param triggerTime the new trigger time
201	 */
202	public void setTriggerTime(long triggerTime) {
203		this.triggerTime = triggerTime;
204		this.triggerCountDown = triggerTime;
205	}
206	
207	
208	/**
209	 * Checks if is reset on finish.
210	 * 
211	 * @return true, if is reset on finish
212	 */
213	public boolean isResetOnFinish() {
214		return resetOnFinish;
215	}
216
217	/**
218	 * Sets the reset on finish.
219	 * 
220	 * @param resetOnFinish the new reset on finish
221	 */
222	public void setResetOnFinish(boolean resetOnFinish) {
223		this.resetOnFinish = resetOnFinish;
224	}
225
226
227	/**
228	 * Gets the interpolator.
229	 * 
230	 * @return the interpolator
231	 */
232	public Iinterpolator getInterpolator() {
233		return interpolator;
234	}
235
236
237	/**
238	 * Sets the interpolator.
239	 * 
240	 * @param interpolator the new interpolator
241	 */
242	public void setInterpolator(Iinterpolator interpolator) {
243		this.interpolator = interpolator;
244	}
245
246
247	/**
248	 * Gets the name.
249	 * 
250	 * @return the name
251	 */
252	public String getName() {
253		return name;
254	}
255
256
257	/**
258	 * Sets the name.
259	 * 
260	 * @param name the new name
261	 */
262	public void setName(String name) {
263		this.name = name;
264	}
265
266	/* (non-Javadoc)
267	 * @see org.mt4j.util.animation.IAnimation#getCurrentStepDelta()
268	 */
269	public float getDelta() {
270		return this.getInterpolator().getCurrentStepDelta();
271	}
272
273	/* (non-Javadoc)
274	 * @see org.mt4j.util.animation.IAnimation#getCurrentValue()
275	 */
276	public float getValue() {
277		return this.getInterpolator().getCurrentValue();
278	}
279
280		
281	
282	
283
284}