/* PTMovies	-	Display Panorama Movies
   Copyright (C) 2000 - Helmut Dersch  der@fh-furtwangen.de
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/*------------------------------------------------------------*/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.net.URLConnection;
import java.util.*;
import java.text.*;
import java.io.*;
import java.lang.reflect.*;
import java.lang.*;


public class ptmovies extends Applet implements Runnable
{
	Thread sendMovie = null;
	String fname = "frame";
	String PTViewer = "ptviewer";
	int px = 0, py = 0;
	ptviewer pv = null;
	double fps = 20.0;
	int loop = 1;
	boolean warp = true;
	Image view = null;
	long TimeOfLastDraw = 0;
	boolean dirty = false;

	public ptmovies(){}


	public ptmovies( ptviewer p, String properties ){
		pv = p;
		setStub(new ptstub(pv, properties ));
	}	
	
   	public void init(){
   		String s;
   		// Get parameters
		s = getParameter( "frame"); 		if( s!=null )	fname = s;
		s = getParameter( "PTViewer"); 		if( s!=null )	PTViewer = s;
		s = getParameter( "px");			if( s!=null )	px = Integer.parseInt(s);
		s = getParameter( "py");			if( s!=null )	py = Integer.parseInt(s);
		s = getParameter( "fps");			if( s!=null )	fps = Double.valueOf(s).doubleValue();
		s = getParameter( "loop");			if( s!=null )	loop = Integer.parseInt(s);
		s = getParameter( "warp");			if( s!=null )	{ if( s.equalsIgnoreCase("false") ) warp = false; }
		// Find Applet ptviewer
  
   		while( pv == null ){		
			try{
				pv = (ptviewer) getAppletContext().getApplet(PTViewer);
			}catch(Exception e){
   				try{ Thread.sleep( 2000 );}				
					catch (InterruptedException ie) {return;}
			}
		}
	}
	
	public void start(){
		if( pv == null ) return;
		if( sendMovie == null ){
			sendMovie = new Thread(this);
      		sendMovie.start();
      	}
      	
      	if( !warp )
      		pv.startCommunicating(this);
   	}


   	public synchronized void stop(){
		if( pv == null ) return;

		if( sendMovie != null ){
			sendMovie.stop();
      		sendMovie= null;
      	}
      	
     	if( !warp )
      		pv.stopCommunicating(this);
   	}

	public void run(){
		
		if(pv == null) return;
		
		int i = 0;
		boolean done = false;
		long TIME_PER_FRAME = (long) (1000.0 / fps);
		Image im = null;
		int inc = 1;
		
		while( !done ){
			im = null;
			String filename;

			int k = fname.indexOf('#');
			if( k >= 0 ){
				filename = fname.substring( 0, k ) + i + fname.substring( k+1 );
			}else{
				filename = fname + i;
			}
			im = pv.loadImage( filename );
			if( im != null ){
				if( warp ){ // wait for last draw
					while( pv.dirty ){ // Wait some milliseconds
  						try{ Thread.sleep( 20 );}				
						catch (InterruptedException ie) {return;}
					}
				} else {
					while( dirty ){ // Wait some milliseconds
  						try{ Thread.sleep( 20 );}				
						catch (InterruptedException ie) {return;}
					}
				}
					
					
      			long delay = System.currentTimeMillis() - TimeOfLastDraw;
      			if( delay < TIME_PER_FRAME ){
      				try{ Thread.sleep( TIME_PER_FRAME - delay );}				
					catch (InterruptedException e) {return;}
				}
				
				if( warp ){
					pv.DrawWarpedImage( im, px, py );
             		TimeOfLastDraw = System.currentTimeMillis() ;
				}else{
					view = im;
					pv.repaint();
					dirty = true;
				}
             	i+=inc;
			}else{
				if( i == 0 || loop == 0 )
					done = true;
				else{
					if( loop == 1 ) // start again at beginning
						i = 0;
					else if( loop == 2 ){ // reverse
						inc = -inc;
						i += inc;
					}
				}
			}
		}
		if( !warp )
      		pv.stopCommunicating(this);
	}
	
	public  void paint(Graphics g){
		if( pv == null || view == null || warp ) return;
 		
		g.drawImage(view, px, py, pv);
     	TimeOfLastDraw = System.currentTimeMillis() ;
     	dirty = false;
	}
	

 	

}

