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