diff --git a/processing/core/PApplet.java b/processing/core/PApplet.java index ff5de8f20..d48ba7e70 100644 --- a/processing/core/PApplet.java +++ b/processing/core/PApplet.java @@ -146,7 +146,10 @@ public class PApplet extends Applet public int width, height; int libraryCount; - PLibrary libraries[]; + PLibrary libraries[]; + boolean libraryCalls[][]; + //int registeredCount[]; + //PLibrary registered[][]; // this text isn't seen unless PApplet is used on its // own and someone takes advantage of leechErr.. not likely @@ -231,9 +234,9 @@ public class PApplet extends Applet this.pixels = g.pixels; this.width = g.width; this.height = g.height; - //g.applet = this; - //setupComplete = true; + libraries = new PLibrary[10]; + libraryCalls = new boolean[10][PLibrary.CALL_COUNT]; try { getAppletContext(); @@ -252,8 +255,6 @@ public class PApplet extends Applet public void start() { - //System.out.println("PApplet.start"); - thread = new Thread(this); thread.start(); } @@ -266,25 +267,9 @@ public class PApplet extends Applet thread = null; } - // DEPRECATED as of jdk 1.2.. ugh.. will this work? - /* - // kill off any associated threads - Thread threads[] = new Thread[Thread.activeCount()]; - Thread.enumerate(threads); - for (int i = 0; i < threads.length; i++) { - // sometimes these get killed off before i can get 'em - if (threads[i] == null) continue; - - if (threads[i].getName().indexOf("Thread-") == 0) { - //System.out.println("stopping " + threads[i].getName()); - threads[i].stop(); - } - } - */ - for (int i = 0; i < libraryCount; i++) { - if (libraries[i] != null) { - libraries[i].stop(); // endNet/endSerial etc + if (libraryCalls[i][PLibrary.DISPOSE]) { + libraries[i].dispose(); // endNet/endSerial etc } } } @@ -313,6 +298,56 @@ public class PApplet extends Applet // ------------------------------------------------------------ + public void attach(PLibrary library) { + if (libraryCount == libraries.length) { + PLibrary temp[] = new PLibrary[libraryCount << 1]; + System.arraycopy(libraries, 0, temp, 0, libraryCount); + libraries = temp; + boolean ctemp[][] = new boolean[libraryCount << 1][]; + System.arraycopy(libraryCalls, 0, ctemp, 0, libraryCount); + libraryCalls = ctemp; + } + libraries[libraryCount] = library; + libraryCalls[libraryCount] = new boolean[PLibrary.CALL_COUNT]; + libraryCount++; + } + + + public void attach(String libraryName) { + try { + Class c = Class.forName(libraryName); + PLibrary library = (PLibrary) c.newInstance(); + library.setup(this); + + } catch (ClassNotFoundException e) { + System.err.println("Could not find library \"" + libraryName + "\""); + System.err.println("Make sure it's in the libraries folder, "); + System.err.println("or exported somewhere inside the sketchbook,"); + System.err.println("or inside the \"code\" folder of this sketch."); + + } catch (IllegalAccessException e) { + System.err.println("Could not load library \"" + libraryName + "\""); + e.printStackTrace(); + + } catch (InstantiationException e) { + System.err.println("Could not load library \"" + libraryName + "\""); + e.printStackTrace(); + } + } + + + public void registerCall(PLibrary library, int call) { + for (int i = 0; i < libraryCount; i++) { + if (libraries[i] == library) { + libraryCalls[i][call] = true; + } + } + } + + + // ------------------------------------------------------------ + + public void setup() { } @@ -365,6 +400,12 @@ public class PApplet extends Applet this.width = g.width; this.height = g.height; + for (int i = 0; i < libraryCount; i++) { + if (libraryCalls[i][PLibrary.SIZE]) { + libraries[i].size(width, height); // endNet/endSerial etc + } + } + // set this here, and if not inside browser, getDocumentBase() // will fail with a NullPointerException, and cause applet to // be set to null. might be a better way to deal with that, but.. @@ -456,6 +497,11 @@ public class PApplet extends Applet g.beginFrame(); if (THREAD_DEBUG) println(Thread.currentThread().getName() + " 1b draw"); + + for (int i = 0; i < libraryCount; i++) { + if (libraryCalls[PLibrary.PRE][i]) libraries[i].pre(); + } + draw(); // these are called *after* loop so that valid @@ -466,26 +512,35 @@ public class PApplet extends Applet dequeueKeyEvents(); if (THREAD_DEBUG) println(Thread.currentThread().getName() + " 2b endFrame"); - g.endFrame(); - //} // end sync - //update(); - // formerly 'update' - //if (firstFrame) firstFrame = false; - // internal frame counter - frameCount++; - if (THREAD_DEBUG) println(Thread.currentThread().getName() + - " 3a calling repaint() " + frameCount); - repaint(); - if (THREAD_DEBUG) println(Thread.currentThread().getName() + - " 3b calling Toolkit.sync " + frameCount); - getToolkit().sync(); // force repaint now (proper method) - if (THREAD_DEBUG) println(Thread.currentThread().getName() + + for (int i = 0; i < libraryCount; i++) { + if (libraryCalls[PLibrary.DRAW][i]) libraries[i].draw(); + } + + g.endFrame(); + //} // end sync + + //update(); + // formerly 'update' + //if (firstFrame) firstFrame = false; + // internal frame counter + frameCount++; + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 3a calling repaint() " + frameCount); + repaint(); + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 3b calling Toolkit.sync " + frameCount); + getToolkit().sync(); // force repaint now (proper method) + if (THREAD_DEBUG) println(Thread.currentThread().getName() + " 3c done " + frameCount); - //if (THREAD_DEBUG) println(" 3d waiting"); - //wait(); - //if (THREAD_DEBUG) println(" 3d out of wait"); - //frameCount++; + //if (THREAD_DEBUG) println(" 3d waiting"); + //wait(); + //if (THREAD_DEBUG) println(" 3d out of wait"); + //frameCount++; + + for (int i = 0; i < libraryCount; i++) { + if (libraryCalls[PLibrary.POST][i]) libraries[i].post(); + } } } redraw = false; // unset 'redraw' flag in case it was set @@ -584,6 +639,12 @@ public class PApplet extends Applet mouseY = event.getY(); mouseEvent = event; + for (int i = 0; i < libraryCount; i++) { + if (libraryCalls[i][PLibrary.MOUSE]) { + libraries[i].mouse(event); // endNet/endSerial etc + } + } + // this used to only be called on mouseMoved and mouseDragged // change it back if people run into trouble if (firstMouseEvent) { @@ -729,6 +790,12 @@ public class PApplet extends Applet key = event.getKeyChar(); keyCode = event.getKeyCode(); + for (int i = 0; i < libraryCount; i++) { + if (libraryCalls[i][PLibrary.KEY]) { + libraries[i].key(event); // endNet/endSerial etc + } + } + switch (event.getID()) { case KeyEvent.KEY_PRESSED: keyPressed = true; @@ -2005,11 +2072,9 @@ public class PApplet extends Applet * I want to read lines from a file. I have RSI from typing these * eight lines of code so many times. */ - public BufferedReader reader(File file) { + public BufferedReader reader(String filename) { try { - FileInputStream fis = new FileInputStream(file); - InputStreamReader isr = new InputStreamReader(fis); - return new BufferedReader(isr); + return reader(openStream(filename)); } catch (IOException e) { e.printStackTrace(); @@ -2019,14 +2084,11 @@ public class PApplet extends Applet /** - * I want to print lines to a file. I have RSI from typing these - * eight lines of code so many times. + * I want to read lines from a file. And I'm still annoyed. */ - public PrintWriter writer(File file) { + public BufferedReader reader(File file) { try { - FileOutputStream fos = new FileOutputStream(file); - OutputStreamWriter osw = new OutputStreamWriter(fos); - return new PrintWriter(osw); + return reader(new FileInputStream(file)); } catch (IOException e) { e.printStackTrace(); @@ -2035,6 +2097,53 @@ public class PApplet extends Applet } + /** + * I want to read lines from a stream. If I have to type the + * following lines any more I'm gonna send Sun my medical bills. + */ + public BufferedReader reader(InputStream input) throws IOException { + InputStreamReader isr = new InputStreamReader(input); + return new BufferedReader(isr); + } + + + /** + * I want to print lines to a file. Why can't I? + */ + public PrintWriter writer(String filename) { + try { + return writer(new FileOutputStream(save_location(filename, true))); + + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + /** + * I want to print lines to a file. I have RSI from typing these + * eight lines of code so many times. + */ + public PrintWriter writer(File file) { + try { + return writer(new FileOutputStream(file)); + + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + /** + * I want to print lines to a file. Why am I always explaining myself? + * It's the JavaSoft API engineers who need to explain themselves. + */ + public PrintWriter writer(OutputStream output) throws IOException { + OutputStreamWriter osw = new OutputStreamWriter(output); + return new PrintWriter(osw); + } + + public InputStream openStream(String filename) throws IOException { InputStream stream = null; diff --git a/processing/core/PGraphics.java b/processing/core/PGraphics.java index dbfa8d864..3242d49f6 100644 --- a/processing/core/PGraphics.java +++ b/processing/core/PGraphics.java @@ -631,13 +631,6 @@ public class PGraphics extends PImage } } - // note that the zbuffer was messed with for the next frame - //zbufferTainted = (dimensions != 0); - - // BLIT TO IMAGE (SCREEN) - //mis.newPixels(pixels, cm, 0, width); - //frameCount++; - // moving this back here (post-68) because of macosx thread problem mis.newPixels(pixels, cm, 0, width); } diff --git a/processing/core/PLibrary.java b/processing/core/PLibrary.java index 295a02870..7db8bc4fc 100644 --- a/processing/core/PLibrary.java +++ b/processing/core/PLibrary.java @@ -24,41 +24,84 @@ package processing.core; +import java.awt.event.*; + +/** + * A series of messages can be registered via PApplet.registerCall(). + * For instance parent.registerCall(PRE) inside of the setup method + * will make sure that this library is informed each time beginFrame + * has just been called yet no drawing has taken place (would you like + * to take over the camera, eh?) + * + * An assumption for library writers is that they're fairly + * technically savvy and familiar with Java. The primary target + * audience for Procesing is less technical, so libraries are + * designed to be simple to use but only slightly more complex + * to write. + */ public interface PLibrary { - /** - * This sets the parent PApplet in case that's needed for anything. - * It's called on attach(). - */ - public void setParent(PApplet parent); + static final int SIZE = 6; + static final int PRE = 0; + static final int DRAW = 1; + static final int POST = 2; + static final int MOUSE = 3; + static final int KEY = 4; + static final int DISPOSE = 5; + + static final int CALL_COUNT = 7; /** - * Called before (outside of) draw() or loop(). - * Note that this also gets called before beginFrame() - * so no drawing can occur. + * This is called after the constructor on "attach" + */ + public void setup(PApplet parent); + + /** + * Called when the applet is resized. + */ + public void size(int w, int h); + + /** + * Called just after beginFrame() but before anything is + * drawn in the user's draw() method. */ public void pre(); /** - * Called after (outside of) draw() or loop(). + * Called before endFrame() but after all other drawing. */ - public void post(); + public void draw(); + + /** + * Called betwee endFrame() and the next beginFrame() + * so that things can be post-processed based on the final, + * fully rendered, image. + */ + public void post(); + + /** + * If registered, this will be called when a mouse event has occurred. + * Use event.getID() to see whether it's MouseEvent.MOUSE_CLICKED or + * something else exciting. + */ + public void mouse(MouseEvent event); + + /** + * A key event has occurred, use event.getID() to see whether it's + * KeyEvent.KEY_PRESSED or whatever. + */ + public void key(KeyEvent e); /** * Called when the applet or application is stopped. + * Override this method to shut down your threads. + * + * Named dispose() instead of stop() since stop() is often + * a useful method name for library functions (i.e. PMovie.stop() + * which will stop a movie that is playing, but not kill off + * its thread and associated resources. */ - public void stop(); + public void dispose(); + //public void stop(); } - - - -/* -public void libraryEvent(PLibrary who, Object data) { - //if (who instanceof BVideo) { - if (who.signature() == Sonia.SIGNATURE) { - BImage frame = (BImage)data; - // do something with the data - } -} -*/ diff --git a/processing/core/todo.txt b/processing/core/todo.txt index 4dd08f60b..edd37afd4 100644 --- a/processing/core/todo.txt +++ b/processing/core/todo.txt @@ -192,6 +192,9 @@ X http://processing.org/discourse/yabb/YaBB.cgi?board=Syntax;action=display;nu 040921 morning X bug in get() that was removing the high bits (rather than adding) +040921 evening +X lots of work on libraries, figuring out PLibrary api + _ before graphics engine change, attach jogl stuff? _ massive graphics engine changes _ explicitly state depth()/nodepth() @@ -256,10 +259,6 @@ The graphics library was formerly called Bagel, which is an internal name. CORE / PApplet - b _ keypressed hanging on applets with a code folder - b _ http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_software_bugs;action=display;num=1081450102 - - CORE / PImage diff --git a/processing/todo.txt b/processing/todo.txt index fc2d109cc..bc13a2f85 100644 --- a/processing/todo.txt +++ b/processing/todo.txt @@ -70,30 +70,50 @@ X "Processing" folder not properly created on new install X add noLoop() to static mode apps X remove "loop" from special inserts on preproc -040920 afternoon +040921 afternoon o when running externally, build into sketch folder? +X add all imported libs to hash table of jars -_ add all imported libs to hash table of jars - -_ should library be called extension? - -_ libraries -_ attachLibrary(new libsomething()) and attachLibrary("pitaru.Sonia"); +040921 evening +X lots of work on libraries, figuring out PLibrary api +X attachLibrary(new libsomething()) and attachLibrary("pitaru.Sonia"); o final stop() for static shutdown of lib X no static init - make the library designers deal with it -_ stop() for individual items -_ register pre() and post() calls -_ this would avoid having to patch BApplet -_ register for setup() calls +X stop() for individual items +X register pre() and post() calls +X this would avoid having to patch BApplet +X register for setup() calls +X where do libraries for distribution go? +X libraries subfolder of p5 + +_ PLibrary lib = attach(new PClient()); +_ compiling - main file is a .java not a .pde +_ also where no root .pde file with the same name.. (i.e. video) +_ crap.. libraries need to be in packages for this to work +_ but annoying: attach("simong.particlesystem.ParticleSystem") +_ record imports from java files as well +_ ignore imports from java.* or maybe just things pde's classpath + _ be able to link against, but not export, certain parts of lib _ jsyn.jar not needed on export, netscape libs not needed on export -_ where do libraries for distribution go? -_ libraries subfolder of p5 _ netscape.javascript not properly working in 1.4 _ include 'netscape.javascript' as a library to be added _ but don't export it.. ugh.. _ http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_software_bugs;action=display;num=1088034754;start=0 _ after export of library, rebuild "import library" menu +_ should library be called extension? + + +_ keypressed hanging on applets with a code folder +_ http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_software_bugs;action=display;num=1081450102 +_ problems running external vm/vm is hanging +_ seems to be happening because of virus scanning software (norton) +_ may need to launch the applet (using 'start') +_ and talk over a socket instead +_ http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_software_bugs;action=display;num=1067867520;start=0 +_ http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_software_bugs;action=display;num=1067643186 +_ http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_software_bugs;action=display;num=1078714442;start=0 +_ something in that one about mouse position halting or not _ get export working again _ make multiple jar files thing work as an option @@ -444,14 +464,6 @@ BUGS / Windows 1 _ p5's exe prolly has trouble when PATH has quotes (or spaces?) 1 _ http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_software_bugs;action=display;num=1068388889 1 _ CLASSPATH figured out what to do with quotes, but not PATH -1 _ problems running external vm/vm is hanging -1 _ seems to be happening because of virus scanning software (norton) -1 _ may need to launch the applet (using 'start') -1 _ and talk over a socket instead -1 _ http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_software_bugs;action=display;num=1067867520;start=0 -1 _ http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_software_bugs;action=display;num=1067643186 -1 _ http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_software_bugs;action=display;num=1078714442;start=0 -1 _ something in that one about mouse position halting or not DISTRIBUTION / Windows