org.jrabbit.base.graphics.skins.animation
Class AnimatedSkin

java.lang.Object
  extended by org.jrabbit.base.graphics.skins.animation.AnimatedSkin
All Implemented Interfaces:
Renderable, Updateable, Skin, Dimensioned

public class AnimatedSkin
extends java.lang.Object
implements Skin, Updateable

An AnimatedSkin is just that - a Skin that renders changing data over time. Unlike all other default Skins, it is Updateable (and needs to be updated to function), so its usage must reflect this caveat. An AnimatedSkin contains an array of other Skins - these are all the possible frames the animation can display. Alongside this, it maintains a 2-D array of integers; these describe all possible cycles of animation ("actions," if you will) that can be utilized. The format of this index is: {{a1, a2, ... aN}, // Action A {b1, b2, ... bN}, // Action B { ... }, // Continued actions {m1, m2, ... mN}}: // Action M Additionally, the AnimatedSkin has a 2-D array of floats; this represents the "length" of each frame in proportion to the others. This array is structured identically to the animation index; values in the same locations correspond to one another. The AnimatedSkin has an active cycle and frame; it loops through the current cycle (incrementing the frame) based upon the animation rate. The duration of each frame is (base speed * duration index [cycle][frame]). Additionally, the AnimatedSkin has a list of AnimationListeners that it alerts whenever it advances the frame and/or finishes a cycle.

Author:
Chris Molini

Field Summary
private  int counter
          This variable is used to keep track of the total time elapsed on the current frame.
private  int cycle
          The current active cycle.
private static int defaultSpeed
          The default frame duration; this is the number of update "ticks" to delay for each frame.
private  float[][] durationIndex
          The index of frame durations; these correspond to the animation index.
private  int frame
          The current frame in the active cycle.
private  Skin[] frames
          The internal list of all possible Skins the animation can use.
private  int[][] index
          The index of animation cycles; this describes all available animations.
private  java.util.ArrayList<AnimationListener> listeners
          All of the AnimationListeners registered with this object.
private  int speed
          The current base animation speed.
 
Constructor Summary
AnimatedSkin(Skin[] frames, int[][] index)
          Creates an AnimatedSkin that animates the indicated frames with the supplied index.
AnimatedSkin(Skin[] frames, int[][] index, float[][] durationIndex, int speed)
          Creates an AnimatedSkin that animates the indicated frames with the supplied index at the indicated speed, using the indicated index of durations to make some frames longer or shorter than others.
AnimatedSkin(Skin[] frames, int[][] index, int speed)
          Creates an AnimatedSkin that animates the indicated frames with the supplied index at the indicated speed.
 
Method Summary
 void addListener(AnimationListener listener)
          Adds a listener to the list of AnimationListeners.
 void advanceFrame()
          Advances the frame of animation, alerting any listeners as it goes.
 void clearListeners()
          Removes all AnimationListeners from the list.
 Skin currentSkin()
          Accesses the current Skin used to render the frame.
 int cycle()
          Learns the current cycle number.
static int defaultSpeed()
          Learns the current default animation speed.
 float[][] durationIndex()
          Accesses the duration index.
 int frame()
          Learns the current frame number.
 Skin[] frames()
          Accesses the array of Skins used for rendering.
 float height()
          Accesses the dimensions of the AnimatedSkin.
 int[][] index()
          Accesses the index of animations.
private  void init()
          Initializes the default animation settings.
 boolean removeListener(AnimationListener listener)
          Removes the indicated AnimationListener from the list.
 void render()
          Renders the current Skin.
 void resetDurationIndex()
          Resets the durations of all frames to the current animation rate.
 void setCycle(int number)
          Changes the animation cycle.
static void setDefaultSpeed(int speed)
          Changes the default animation speed.
 void setDurationIndex(float[][] durations)
          Redefines the index of frame durations.
 void setFrame(int number)
          Changes the frame of animation.
 void setFrames(Skin[] frames)
          Redefines the list of frames.
 void setIndex(int[][] index)
          Redefines the index of animations.
 void setSpeed(int speed)
          Redefines the current speed of the animation.
 Skin skinAt(int cycle, int frame)
          Accesses a Skin used to render the animation.
 int speed()
          Learns the current base speed of the animation.
 void update(int delta)
          Updates the animation.
 float width()
          Accesses the dimensions of the AnimatedSkin.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultSpeed

private static int defaultSpeed
The default frame duration; this is the number of update "ticks" to delay for each frame. It's set in microseconds resolution; 500 = 1/20th of a second.


frames

private Skin[] frames
The internal list of all possible Skins the animation can use.


index

private int[][] index
The index of animation cycles; this describes all available animations.


durationIndex

private float[][] durationIndex
The index of frame durations; these correspond to the animation index. The duration of each frame when animated is: duration = (speed * durations[cycle][frame]);


cycle

private int cycle
The current active cycle.


frame

private int frame
The current frame in the active cycle.


speed

private int speed
The current base animation speed.


counter

private int counter
This variable is used to keep track of the total time elapsed on the current frame.


listeners

private java.util.ArrayList<AnimationListener> listeners
All of the AnimationListeners registered with this object.

Constructor Detail

AnimatedSkin

public AnimatedSkin(Skin[] frames,
                    int[][] index)
Creates an AnimatedSkin that animates the indicated frames with the supplied index. Every frame has equal duration, and the AnimatedSkin uses the default animation speed.

Parameters:
frames - The frames to use in Animation.
index - The index that will be used to animated the supplied frames.

AnimatedSkin

public AnimatedSkin(Skin[] frames,
                    int[][] index,
                    int speed)
Creates an AnimatedSkin that animates the indicated frames with the supplied index at the indicated speed. Every frame has equal duration.

Parameters:
frames - The frames to use in Animation.
index - The index that will be used to animated the supplied frames.
speed - The base duration (in clock ticks) of each frame.

AnimatedSkin

public AnimatedSkin(Skin[] frames,
                    int[][] index,
                    float[][] durationIndex,
                    int speed)
Creates an AnimatedSkin that animates the indicated frames with the supplied index at the indicated speed, using the indicated index of durations to make some frames longer or shorter than others.

Parameters:
frames - The frames to use in Animation.
index - The index that will be used to animated the supplied frames.
durationIndex - The index that will be used to manage the duration of particular frames.
speed - The base duration (in clock ticks) of each frame.
Method Detail

defaultSpeed

public static int defaultSpeed()
Learns the current default animation speed.

Returns:
The current base default interval between each frame of animation.

setDefaultSpeed

public static void setDefaultSpeed(int speed)
Changes the default animation speed.

Parameters:
speed - The new default frame interval.

init

private void init()
Initializes the default animation settings. The animation speed is set to the default and the list of AnimationListeners is initialized.


addListener

public void addListener(AnimationListener listener)
Adds a listener to the list of AnimationListeners.

Parameters:
listener - The listener to add.

removeListener

public boolean removeListener(AnimationListener listener)
Removes the indicated AnimationListener from the list.

Parameters:
listener - The AnimationListener to remove.
Returns:
True if the removal operation succeeded, false if not.

clearListeners

public void clearListeners()
Removes all AnimationListeners from the list.


frames

public Skin[] frames()
Accesses the array of Skins used for rendering.

Returns:
The list of Skins being used a frames of animation.

setFrames

public void setFrames(Skin[] frames)
Redefines the list of frames.

Parameters:
frames - The new array of Skins to use for rendering each frame of animation.

currentSkin

public Skin currentSkin()
Accesses the current Skin used to render the frame.

Returns:
The active Skin.

skinAt

public Skin skinAt(int cycle,
                   int frame)
Accesses a Skin used to render the animation.

Parameters:
cycle - The cycle of animation.
frame - The frame within the indicated cycle to access.
Returns:
The Skin used to render the indicated frame of animation.

cycle

public int cycle()
Learns the current cycle number.

Returns:
The current frame cycle within the animation index.

setCycle

public void setCycle(int number)
Changes the animation cycle. The change only takes place if the cycle number if possible (greater than 0 and within the limits of the animation index). NOTE: This method does not alert AnimationListeners that a change has taken place.

Parameters:
number - The cycle to use.

frame

public int frame()
Learns the current frame number.

Returns:
The current frame number within the active animation cycle.

setFrame

public void setFrame(int number)
Changes the frame of animation. NOTE: This method does not alert AnimationListeners that a change has taken place.

Parameters:
number - The frame to use.

index

public int[][] index()
Accesses the index of animations.

Returns:
The 2-D integer array that defines all animation cycles usable by the AnimatedSkin.

setIndex

public void setIndex(int[][] index)
Redefines the index of animations. NOTE: This automatically resets the duration index.

Parameters:
index - The new index that defines all animation cycles used by the AnimatedSkin.

speed

public int speed()
Learns the current base speed of the animation.

Returns:
The base number of clock ticks each frame lasts.

setSpeed

public void setSpeed(int speed)
Redefines the current speed of the animation.

Parameters:
speed - The new base speed of the animation. The supplied value should be the base number of clock ticks each frame lasts.

durationIndex

public float[][] durationIndex()
Accesses the duration index.

Returns:
The index of float values that define the proportion of the animation speed each frame lasts.

resetDurationIndex

public void resetDurationIndex()
Resets the durations of all frames to the current animation rate.


setDurationIndex

public void setDurationIndex(float[][] durations)
Redefines the index of frame durations.

Parameters:
durations - The new index of how long each frame of animation should last.

advanceFrame

public void advanceFrame()
Advances the frame of animation, alerting any listeners as it goes.


update

public void update(int delta)
Updates the animation. This simply increments the time counter; if the counter passes the duration of a frame, the frame is advanced.

Specified by:
update in interface Updateable
Parameters:
delta - The number of clock ticks that have passed.

render

public void render()
Renders the current Skin.

Specified by:
render in interface Renderable

width

public float width()
Accesses the dimensions of the AnimatedSkin.

Specified by:
width in interface Dimensioned
Returns:
The width of the current skin being rendered.

height

public float height()
Accesses the dimensions of the AnimatedSkin.

Specified by:
height in interface Dimensioned
Returns:
The height of the current skin being rendered.