Android Open Source - droidengine2d Animation






From Project

Back to project page droidengine2d.

License

The source code is released under:

Apache License

If you think the Android project droidengine2d listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*   Copyright 2013-2014 Miguel Vicente Linares
 */* w w w  . j a v a2s.c  om*/
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.miviclin.droidengine2d.graphics.animation;

import java.util.ArrayList;

/**
 * Animation.
 * 
 * @author Miguel Vicente Linares
 * 
 */
public class Animation {

  private enum State {
    INITIALIZED,
    RUNNING,
    PAUSED,
    FINISHED;
  }

  private ArrayList<AnimationStateListener> listeners;
  private ArrayList<AnimationFrame> frames;
  private int currentFrameIndex;
  private float elapsedTime;
  private boolean loopModeEnabled;
  private State state;

  /**
   * Creates a new Animation with initial capacity for 10 frames and loop mode enabled by default.
   */
  public Animation() {
    this(10, true);
  }

  /**
   * Creates a new Animation with initial capacity for 10 frames by default.
   * 
   * @param loopMode If enabled, the animation will loop forever, otherwise, it will not loop.
   */
  public Animation(boolean loopMode) {
    this(10, loopMode);
  }

  /**
   * Creates a new Animation.
   * 
   * @param initialCapacity Number of frames that can be added before resizing the array of frames.
   * @param loopMode If true, the animation will loop, otherwise it won't.
   */
  public Animation(int initialCapacity, boolean loopMode) {
    this.listeners = new ArrayList<AnimationStateListener>();
    this.frames = new ArrayList<AnimationFrame>(initialCapacity);
    this.currentFrameIndex = 0;
    this.elapsedTime = 0;
    this.loopModeEnabled = loopMode;
    this.state = State.INITIALIZED;
  }

  /**
   * Adds a frame to this Animation.
   * 
   * @param frame AnimationFrame to be added.
   */
  public void addFrame(AnimationFrame frame) {
    frames.add(frame);
  }

  /**
   * Returns all the AnimationFrames of this Animation.
   * 
   * @return AnimationFrames
   */
  public ArrayList<AnimationFrame> getFrames() {
    return frames;
  }

  /**
   * Returns the current frame of this Animation.
   * 
   * @return AnimationFrame or null if this Animation does not contain any frame.
   */
  public AnimationFrame getCurrentFrame() {
    if (frames.size() > 0) {
      return frames.get(currentFrameIndex);
    }
    return null;
  }

  /**
   * Updates this Animation and returns the current AnimationFrame.
   * 
   * @param delta Elapsed time since the last update.
   * @return AnimationFrame or null if this Animation does not contain any frame.
   */
  public AnimationFrame update(float delta) {
    if (state == State.INITIALIZED) {
      state = State.RUNNING;
      notifyAnimationStarted();
    }
    if ((state == State.RUNNING) && (frames.size() > 0)) {
      elapsedTime += delta;
      if (elapsedTime > frames.get(currentFrameIndex).getDelay()) {
        elapsedTime = 0;
        currentFrameIndex++;
        notifyAnimationFrameChanged();
        if (currentFrameIndex >= frames.size()) {
          currentFrameIndex = 0;
          notifyAnimationLoopFinished();
          if (!loopModeEnabled) {
            state = State.FINISHED;
            currentFrameIndex = frames.size() - 1;
            notifyAnimationFinished();
          }
        }
      }
    }
    if (frames.size() > 0) {
      return frames.get(currentFrameIndex);
    }
    return null;
  }

  /**
   * Resets the state of this Animation. The list of AnimationFrames will not be reset.
   */
  public void reset() {
    currentFrameIndex = 0;
    elapsedTime = 0;
    state = State.INITIALIZED;
  }

  /**
   * Removes all the AnimationFrames of this Animation and resets it.
   */
  public void clear() {
    currentFrameIndex = 0;
    elapsedTime = 0;
    state = State.INITIALIZED;
    frames.clear();
  }

  /**
   * Returns true if the loop mode is enabled.
   * 
   * @return true if the loop mode is enabled, false otherwise
   */
  public boolean isLoopModeEnabled() {
    return loopModeEnabled;
  }

  /**
   * Enables or disables the loop mode.
   * 
   * @param loopMode true to enable the loop mode, false to disable it.
   */
  public void setLoopModeEnabled(boolean loopMode) {
    this.loopModeEnabled = loopMode;
    if (state == State.FINISHED) {
      state = State.RUNNING;
      currentFrameIndex = 0;
    }
  }

  /**
   * Pauses this Animation if it has not finished (if loop mode is enabled, the Animation is never considered
   * finished).
   */
  public void pause() {
    if (state != State.FINISHED) {
      state = State.PAUSED;
      notifyAnimationPaused();
    }
  }

  /**
   * Resumed this Animation if it was paused.
   */
  public void resume() {
    if (state == State.PAUSED) {
      state = State.RUNNING;
      notifyAnimationResumed();
    }
  }

  /**
   * Returns true if this Animation is running, and false if it is paused or finished.
   * 
   * @return true if this Animation is running, false otherwise
   */
  public boolean isRunning() {
    return state == State.RUNNING;
  }

  /**
   * Returns true if this Animation is paused.
   * 
   * @return true if this Animation is paused, false otherwise
   */
  public boolean isPaused() {
    return state == State.PAUSED;
  }

  /**
   * Returns true if this Animation is finished.<br>
   * If loop mode is enabled, an Animation is never considered finished.
   * 
   * @return true if this Animation is finished, false otherwise
   */
  public boolean isFinished() {
    return state == State.FINISHED;
  }

  /**
   * Registers an {@link AnimationStateListener} in this Animation.
   * 
   * @param listener AnimationStateListener
   */
  public void addAnimationStateListener(AnimationStateListener listener) {
    listeners.add(listener);
  }

  /**
   * Removes the specified {@link AnimationStateListener} from the list of listeners of this animation.
   * 
   * @param listener AnimationStateListener
   * @return true if the specified listener was removed, false otherwise
   */
  public boolean removeAnimationStateListener(AnimationStateListener listener) {
    return listeners.remove(listener);
  }

  /**
   * Notifies all AnimationStateListeners that this Animation has started.<br>
   * Calls {@link AnimationStateListener#onAnimationStarted(Animation)} in every registered listener.
   */
  protected void notifyAnimationStarted() {
    for (int i = 0; i < listeners.size(); i++) {
      listeners.get(i).onAnimationStarted(this);
    }
  }

  /**
   * Notifies all AnimationStateListeners that this Animation has been paused.<br>
   * Calls {@link AnimationStateListener#onAnimationPaused(Animation)} in every registered listener.
   */
  protected void notifyAnimationPaused() {
    for (int i = 0; i < listeners.size(); i++) {
      listeners.get(i).onAnimationPaused(this);
    }
  }

  /**
   * Notifies all AnimationStateListeners that this Animation has been resumed.<br>
   * Calls {@link AnimationStateListener#onAnimationResumed(Animation)} in every registered listener.
   */
  protected void notifyAnimationResumed() {
    for (int i = 0; i < listeners.size(); i++) {
      listeners.get(i).onAnimationResumed(this);
    }
  }

  /**
   * Notifies all AnimationStateListeners that the AnimationFrame has changed.<br>
   * Calls {@link AnimationStateListener#onAnimationFrameChanged(Animation)} in every registered listener.
   */
  protected void notifyAnimationFrameChanged() {
    for (int i = 0; i < listeners.size(); i++) {
      listeners.get(i).onAnimationFrameChanged(this);
    }
  }

  /**
   * Notifies all AnimationStateListeners that this Animation has looped.<br>
   * Calls {@link AnimationStateListener#onAnimationLoopFinished(Animation)} in every registered listener.
   */
  protected void notifyAnimationLoopFinished() {
    for (int i = 0; i < listeners.size(); i++) {
      listeners.get(i).onAnimationLoopFinished(this);
    }
  }

  /**
   * Notifies all AnimationStateListeners that this Animation has finished. If the loop mode is enabled, the Animation
   * never finishes, so this method will never be called.<br>
   * Calls {@link AnimationStateListener#onAnimationFinished(Animation)} in every registered listener.
   */
  protected void notifyAnimationFinished() {
    for (int i = 0; i < listeners.size(); i++) {
      listeners.get(i).onAnimationFinished(this);
    }
  }

}




Java Source Code List

com.miviclin.droidengine2d.AbstractGame.java
com.miviclin.droidengine2d.EngineActivity.java
com.miviclin.droidengine2d.EngineLock.java
com.miviclin.droidengine2d.Engine.java
com.miviclin.droidengine2d.GameThread.java
com.miviclin.droidengine2d.Game.java
com.miviclin.droidengine2d.audio.MusicPlayer.java
com.miviclin.droidengine2d.audio.SoundManager.java
com.miviclin.droidengine2d.gamestate.GameStateAdapter.java
com.miviclin.droidengine2d.gamestate.GameStateManager.java
com.miviclin.droidengine2d.gamestate.GameStateNotRegisteredException.java
com.miviclin.droidengine2d.gamestate.GameState.java
com.miviclin.droidengine2d.gamestate.OnGameStateChangeListener.java
com.miviclin.droidengine2d.graphics.Color.java
com.miviclin.droidengine2d.graphics.DefaultRenderer.java
com.miviclin.droidengine2d.graphics.EngineRenderer.java
com.miviclin.droidengine2d.graphics.GLDebugger.java
com.miviclin.droidengine2d.graphics.GLRenderer.java
com.miviclin.droidengine2d.graphics.GLView.java
com.miviclin.droidengine2d.graphics.Graphics.java
com.miviclin.droidengine2d.graphics.animation.AnimationFrame.java
com.miviclin.droidengine2d.graphics.animation.AnimationStateAdapter.java
com.miviclin.droidengine2d.graphics.animation.AnimationStateListener.java
com.miviclin.droidengine2d.graphics.animation.Animation.java
com.miviclin.droidengine2d.graphics.cameras.Camera.java
com.miviclin.droidengine2d.graphics.cameras.OrthographicCamera.java
com.miviclin.droidengine2d.graphics.material.BlendingOptions.java
com.miviclin.droidengine2d.graphics.material.ColorMaterial.java
com.miviclin.droidengine2d.graphics.material.Material.java
com.miviclin.droidengine2d.graphics.material.TextureColorMaterial.java
com.miviclin.droidengine2d.graphics.material.TextureHsvMaterial.java
com.miviclin.droidengine2d.graphics.material.TextureMaterial.java
com.miviclin.droidengine2d.graphics.material.TransparentTextureMaterial.java
com.miviclin.droidengine2d.graphics.material.UnsupportedMaterialException.java
com.miviclin.droidengine2d.graphics.mesh.ColorMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.Geometry.java
com.miviclin.droidengine2d.graphics.mesh.GraphicsBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.RectangleBatchGeometry.java
com.miviclin.droidengine2d.graphics.mesh.RectangleBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.TextureColorMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.TextureHsvMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.TextureMaterialBatchRendererBase.java
com.miviclin.droidengine2d.graphics.mesh.TextureMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.TransparentTextureMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.shader.ShaderProgramException.java
com.miviclin.droidengine2d.graphics.shader.ShaderProgram.java
com.miviclin.droidengine2d.graphics.shader.ShaderVars.java
com.miviclin.droidengine2d.graphics.text.BitmapFont.java
com.miviclin.droidengine2d.graphics.text.FontChar.java
com.miviclin.droidengine2d.graphics.text.Font.java
com.miviclin.droidengine2d.graphics.text.UndefinedCharacterException.java
com.miviclin.droidengine2d.graphics.texture.TextureAtlas.java
com.miviclin.droidengine2d.graphics.texture.TextureManager.java
com.miviclin.droidengine2d.graphics.texture.TexturePackerAtlas.java
com.miviclin.droidengine2d.graphics.texture.TextureRegion.java
com.miviclin.droidengine2d.graphics.texture.Texture.java
com.miviclin.droidengine2d.input.DefaultKeyEventProcessor.java
com.miviclin.droidengine2d.input.GameInputManager.java
com.miviclin.droidengine2d.input.GameStateInputManager.java
com.miviclin.droidengine2d.input.KeyEventInfo.java
com.miviclin.droidengine2d.input.KeyEventProcessor.java
com.miviclin.droidengine2d.input.KeyProcessor.java
com.miviclin.droidengine2d.input.MotionEventProcessor.java
com.miviclin.droidengine2d.input.TouchProcessor.java
com.miviclin.droidengine2d.input.sensor.AccelerometerValuesListener.java
com.miviclin.droidengine2d.input.sensor.Accelerometer.java
com.miviclin.droidengine2d.input.sensor.SensorUtilities.java
com.miviclin.droidengine2d.resources.AssetsLoader.java
com.miviclin.droidengine2d.util.ActivityUtilities.java
com.miviclin.droidengine2d.util.MutexLock.java
com.miviclin.droidengine2d.util.PrimitiveTypeSize.java
com.miviclin.droidengine2d.util.TransformUtilities.java
com.miviclin.droidengine2d.util.Transform.java
com.miviclin.droidengine2d.util.math.Matrix4.java
com.miviclin.droidengine2d.util.math.MatrixFix.java
com.miviclin.droidengine2d.util.math.Vector2.java
com.miviclin.droidengine2d.util.math.Vector3.java
com.miviclin.droidengine2d.util.time.TimeConstants.java
com.miviclin.droidengine2d.util.time.TimeCounter.java