package visual.dynamic.described;

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.Vector;

import visual.statik.sampled.AggregateContent;
import visual.statik.sampled.Content;
import visual.statik.sampled.TransformableContent;


/**
 * A TweeningSprite that uses sampled visual content
 *
 * Note: This class uses a Vector of Content
 * objects rather than a CompositeContent object for
 * efficiency reasons.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class SampledSprite extends TweeningSprite
{
    private   AggregateContent   tweened;
    private   Vector<Content>    content;
    


    /**
     * Default Constructor
     */
    public SampledSprite()
    {
       super();       

       content = new Vector<Content>();
    }
    


    /**
     * Add a key time
     *
     * Note: This method does not ensure that the first key
     * frame actually contains an image.
     *
     * @param keyTime     The time
     * @param location    The location of the sprite at this time
     * @param rotation    The rotation of the sprite (null to align with path)
     * @param scaling     The scaling of the sprite at this time
     * @param c           The Content to use (or null to use previous)
     */
    public void addKeyTime(int keyTime, Point2D location,
                           Double rotation, Double scaling, 
                           Content c)
    {
       int         index;
       
       index = super.addKeyTime(keyTime, location, rotation, scaling);

       if (index >= 0)
       {
          // If c is null then re-use the last Content
          if (c==null) c = content.get(index-1); 

          content.insertElementAt(c, index);
       }
    }


    /**
     * Get the visual content
     * (based on this Sprite's current state)
     */
    protected visual.statik.TransformableContent getContent()
    {
       AggregateContent      aggregate;       
       Content               currentContent, nextContent;       
       float                 alpha;       
       int                   current, next;
       visual.statik.TransformableContent   result;
       


       result  = null;       
       current = getKeyTimeIndex();
       next    = getNextKeyTimeIndex();

       if (visible && (current >= 0))
       {
          currentContent = content.get(current);
          nextContent    = content.get(next);

          if ((nextContent != null) && 
              (currentContent != nextContent))
          {
             aggregate = new AggregateContent();             
             aggregate.add(currentContent);
             aggregate.add(nextContent);

             // Setup alpha blending
             alpha = (float)getInterpolationFraction();
             
             aggregate.setComposite(
                        AlphaComposite.getInstance(
                                 AlphaComposite.SRC_OVER,
                                 alpha));

             result = aggregate;
          }
          else
          {
             result = currentContent;             
          }
       }

       return result;       
    }
}
