import king.tool.export.ImageExport;
String _movieFilePrefix = null;
int _movieFrameCounter = 0;
LinkedList _movieUndoStack = null;

movieHelp() {
    print("==== PREDEFINED FUNCTIONS ====");
    print("movieHelp()              this help text");
    print("");
    print("movieMarkUndo()          save the current frame number for later undos");
    print("movieRock(frames,deg)    create movie of rocking around Y");
    print("movieSetSize(w,h)        set canvas size for movie output");
    print("movieSpin(frames,deg?)   create movie of spinning around Y");
    print("movieStart(file)         set the filename prefix for a new movie");
    print("movieUndo()              undoes the last set of frames written");
    print("movieWriteFrame(num?)    write the current view as 1+ frame(s)");
    print("");
    print("90 frames and 30 degrees @ 15fps is a nice slow rock");
    print("rock <-> roll: use same # frames and 60 deg, or half and 30 deg;");
    print("500x500 and 600x400 are good sizes; movies read best w/ perspective ON.");
    print("QuickTime: Export QT Movie/LAN, data rate = 400 at above natural sizes.");
    print("");
    //FIXED (?) 28 April 2005
    //print("*** DO NOT use these functions with OpenGL rendering enabled! ***");
    //print("");
}
movieHelp(); // display this message on startup!

void movieMarkUndo()
{
    _movieUndoStack.addLast(_movieFrameCounter);
}

void movieRock(int nFrames, double nDegrees)
{
    // This version is Swing-friendly, so you can see what's happening.
    movieMarkUndo();
    KView startView = kMain.getView();
    int i = 0;
    
    void run()
    {
        KView nextView = (KView) startView.clone();
        nextView.rotateY((float)(Math.toRadians(nDegrees) * Math.sin(2*Math.PI*i / nFrames)));
        kMain.setView(nextView);
        movieWriteFrame();
        if(++i < nFrames) SwingUtilities.invokeLater(super);
        else // cleanup code here:
        {
            kMain.setView(startView);
            print("movieRock() finished!");
        }
    }
    
    SwingUtilities.invokeLater(this);
}

void movieSetSize(int width, int height)
{
    kMain.getCanvas().setPreferredSize(new Dimension(width, height));
    kMain.getTopWindow().pack();
}

void movieSpin(int nFrames)
{ movieSpin(nFrames, 360.0); }

void movieSpin(int nFrames, double nDegrees)
{
    // This version is Swing-friendly, so you can see what's happening.
    movieMarkUndo();
    int i = 0;
    
    void run()
    {
        movieWriteFrame();
        KView v = kMain.getView();
        v.rotateY((float)(Math.toRadians(nDegrees) / nFrames));
        if(++i < nFrames) SwingUtilities.invokeLater(super);
        else print("movieSpin() finished!");
    }
    
    SwingUtilities.invokeLater(this);
}

void movieStart(String fileName)
{
    _movieFilePrefix = fileName;
    _movieFrameCounter = 1;
    _movieUndoStack = new LinkedList();
}

boolean movieUndo()
{
    if(_movieUndoStack.size() == 0)
    {
        print("No movie frames to undo!");
        return false;
    }
    else
    {
        int currFr = _movieFrameCounter;
        _movieFrameCounter = _movieUndoStack.removeLast();
        print("Undid movie frames "+_movieFrameCounter+" - "+(currFr-1));
        return true;
    }
}

void movieWriteFrame()
{ movieWriteFrame(1); }

void movieWriteFrame(int numCopies)
{ movieWriteFrame(numCopies, true); }

void movieWriteFrame(int numCopies, boolean undoable)
{
    if(undoable) movieMarkUndo();
    
    File file = movieGetFileForFrame(_movieFrameCounter++);
    ImageExport.exportImage(kMain.getCanvas(), "png", file);
    print("Wrote frame "+file);
    for(int i = 1; i < numCopies; i++)
    {
        File file2 = movieGetFileForFrame(_movieFrameCounter++);
        InputStream src = new BufferedInputStream(new FileInputStream(file));
        OutputStream dst = new BufferedOutputStream(new FileOutputStream(file2));
        byte[] buffer = new byte[2048];
        int len;
        while((len = src.read(buffer)) != -1) dst.write(buffer, 0, len);
        dst.close();
        src.close();
        print("Wrote frame "+file2);
    }
}

File movieGetFileForFrame(int frameNo)
{
    String filenum = ""+(frameNo);
    while(filenum.length() < 4) filenum = "0"+filenum;
    File file = new File(_movieFilePrefix+"_"+filenum+".png");
    return file;
}
