import processing.core.*; 
import processing.xml.*; 

import jmcvideo.*; 

import java.applet.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.regex.*; 

public class moviepainter extends PApplet {

/**
 * MoviePainter
 * Copyright (C) 2009 Eric Nguyen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Contact information:
 *   Eric Nguyen <eric at ericnguyen dot com>
 *
 * @version  0.1
 * @url      http://mindtangle.net/2009/02/23/moviepainter/
 */


JMCMovie brushMovie;
PImage brushMask;
float scale = 1;
float rotation = 0;
float degradingAvX = 0;
float degradingAvY = 0;
float brushsize = 1.5f;
float opacity = 20;
float alpha = .1f; // for IIR Filter
int background = 255;

public void setup() {
  size(1000, 800, P3D);
  background(background);
  brushMovie = new JMCMovie(this, "movie4.mov");
  brushMovie.loop();
  brushMask = loadImage("b-vignette.jpg");
  brushMask.resize(brushMovie.width, brushMovie.height);
  
  // font of choice
  PFont font;
  font = loadFont("FuturaBT-Bold-48.vlw"); 
  textFont(font); 
}

public void mouseDragged() {
  // calculate rotation based on mouse direction
  // average mouse positions, using IIR (Infinite Impulse Response) filter
  if (degradingAvX > 0 || degradingAvY > 0) {
    degradingAvX = (alpha * mouseX) + ((1 - alpha) * degradingAvX);
    degradingAvY = (alpha * mouseY) + ((1 - alpha) * degradingAvY);
  } else {
    degradingAvX = mouseX;
    degradingAvY = mouseY;
  }
  
  rotation = atan2(mouseY-degradingAvY, mouseX-degradingAvX) - HALF_PI;
  scale = brushsize/sqrt(dist(mouseX, mouseY, degradingAvX, degradingAvY));
}

public void draw() {
  if (mousePressed) {
    brushMovie.mask(brushMask);
    translate(mouseX, mouseY);
    rotate(rotation);
    scale(scale, scale);
    tint(255, opacity);
    image(brushMovie, -1*(brushMovie.width/2), -1*(brushMovie.height/2));
  }
}

// Called every time a new frame is available to read
public void movieEvent(JMCMovie m) {
  m.read();
}

public void keyPressed() {
  if (key == ' ') { // blank out screen
    background(background);
  }
}

  static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#ffffff", "moviepainter" });
  }
}

