/*
 * @(#) Cine.java 1.0 2000/7/18
 * Copyright (c) 1997 Lawrence Rodrigues
 */

package com.vistech.imageviewer;
import java.io.*;
import java.beans.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;

 /** Cine Class
   * @version 1.0  18 July 2000
   * @author Lawrence Rodrigues
   **/

public class Movie implements CineController, Runnable, PropertyChangeListener {
   protected ImageDisplay vp;
   protected transient Thread cineThread;
   protected boolean cineOn = false;
   protected int frameRate = 60;
   private long delay = 500;
   protected Image[] cineImageSet, imageSet;
   protected int[] cineIndices;

   public Movie() { }

   public Movie(ImageDisplay imageCanvas){
      vp = imageCanvas;
   }
   public void setImageDisplay(ImageDisplay imageCanvas){
      vp = imageCanvas;
   }

   /** The cine speed is controlled by this method. A delay is
      * introduced between two successive image paints.
      * This delay depends on the cine rate property.
      * The run() method calls the setImage() method in the ImageCanvas class
      * for drawing images.
      */
   public void run(){
      int curIndex =0;
       try{
         while(true){
           if((cineOn) && (cineImageSet != null)){
               vp.setAWTImage(cineImageSet[curIndex++]);
               curIndex %= cineImageSet.length;
               cineThread.sleep(delay);
           }else cineThread.sleep(500);
         }
       }catch(Exception e){}
   }

   public void propertyChange(PropertyChangeEvent pe) {
  /*    String pname = pe.getPropertyName();
      if(pname.equals("cineRate")) {
         Integer val = (Integer)pe.getNewValue();
         setCineRate(val.intValue());
      }
      if(pname.equals("cineOn")) {
         Boolean val1 = (Boolean)pe.getNewValue();
         if(val1.booleanValue()){
           startCine();
           System.out.println("prop change ");
         }
         else stopCine();
      } */

   }

   /**  @param rate the rate at which the cine loop runs.
     */
   public void setCineRate(int rate){
      if(rate==0) return;
      frameRate = rate;
      delay = (int)((60.0/(double)frameRate)*1000);
      if(delay <10) delay =10;
   }

   public int getCineRate(){ return frameRate;}

   public void setCineOn(boolean onOrOff) {cineOn = onOrOff;
      if(cineOn) startCine(); else stopCine();
   }
   public boolean getCineOn() { return cineOn; }
 
   public void startCine(){
      cineOn = true;
      cineThread = new Thread(this);
      cineThread.start();
   }
  
   public void stopCine(){ cineOn = false;}

   /** @param indices an array integers that holds the indices of the imageSet.
     */
   public void setCineIndices(int[] indices){
      cineIndices = indices;
      setCineImageSet(cineIndices);
   }

   /**
     * Sets the cine image set. The input here is a list of indices.
     * This method uses these indices to get the image names from the fileList
     * and then creates a new cineImageSet array for those images.
     * @param indices the fileList indices.
     */

   public void setCineImageSet(int[] indices){
      if(indices == null) return;
      int len = indices.length;
      cineImageSet = new Image[len];
      for(int i=0;i <len;i++){
          cineImageSet[i] = imageSet[indices[i]];
      }
   }
    /**
     * Sets the cine image set array. 
     * @param scnImage an array of screen images.
     */
   public void setCineImageSet(Image[] scnImage){
      if(scnImage == null) return;
      imageSet = scnImage;
      cineImageSet = scnImage;
   }
}
