diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index dee30e4fe..997734335 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -731,11 +731,22 @@ public class PApplet extends Applet Object pauseObject = new Object(); Thread thread; +// static final String[] registerNames = { +// "size", +// "pause", "resume", +// "pre", "draw", "post", +// "mouseEvent", "keyEvent", "touchEvent", +// "dispose" +// }; + HashMap registerMap = + new HashMap(); // protected RegisteredMethods sizeMethods; - protected RegisteredMethods pauseMethods, resumeMethods; - protected RegisteredMethods preMethods, drawMethods, postMethods; +// protected RegisteredMethods pauseMethods, resumeMethods; +// protected RegisteredMethods preMethods, drawMethods, postMethods; + /** Old methods with AWT API that should not be used. */ protected RegisteredMethods mouseEventMethods, keyEventMethods; - protected RegisteredMethods disposeMethods; +// protected RegisteredMethods disposeMethods; + // messages to send if attached as an external vm @@ -835,14 +846,14 @@ public class PApplet extends Applet // these need to be inited before setup // sizeMethods = new RegisteredMethods(); - pauseMethods = new RegisteredMethods(); - resumeMethods = new RegisteredMethods(); - preMethods = new RegisteredMethods(); - drawMethods = new RegisteredMethods(); - postMethods = new RegisteredMethods(); - mouseEventMethods = new RegisteredMethods(); - keyEventMethods = new RegisteredMethods(); - disposeMethods = new RegisteredMethods(); +// pauseMethods = new RegisteredMethods(); +// resumeMethods = new RegisteredMethods(); +// preMethods = new RegisteredMethods(); +// drawMethods = new RegisteredMethods(); +// postMethods = new RegisteredMethods(); +// mouseEventMethods = new RegisteredMethods(); +// keyEventMethods = new RegisteredMethods(); +// disposeMethods = new RegisteredMethods(); try { getAppletContext(); @@ -957,7 +968,8 @@ public class PApplet extends Applet paused = false; // unpause the thread resume(); - resumeMethods.handle(); +// resumeMethods.handle(); + handleMethods("resume"); debug("un-pausing thread"); synchronized (pauseObject) { @@ -998,7 +1010,8 @@ public class PApplet extends Applet paused = true; // causes animation thread to sleep pause(); - pauseMethods.handle(); +// pauseMethods.handle(); + handleMethods("pause"); // actual pause will happen in the run() method @@ -1046,82 +1059,67 @@ public class PApplet extends Applet } - /** - * This returns the last width and height specified by the user - * via the size() command. - */ -// public Dimension getPreferredSize() { -// return new Dimension(width, height); -// } - - -// public void addNotify() { -// super.addNotify(); -// println("addNotify()"); -// } - - ////////////////////////////////////////////////////////////// - public class RegisteredMethods { + class RegisteredMethods { int count; - Object objects[]; - Method methods[]; - Object[] convArgs = new Object[] { }; + Object[] objects; + // Because the Method comes from the class being called, + // it will be unique for most, if not all, objects. + Method[] methods; + Object[] emptyArgs = new Object[] { }; - // convenience version for no args - public void handle() { - handle(convArgs); + + void handle() { + handle(emptyArgs); } - public void handle(Object oargs[]) { + + void handle(Object[] args) { for (int i = 0; i < count; i++) { try { - //System.out.println(objects[i] + " " + args); - methods[i].invoke(objects[i], oargs); + methods[i].invoke(objects[i], args); } catch (Exception e) { - //// check for wrapped exception, get root exception + // check for wrapped exception, get root exception Throwable t; if (e instanceof InvocationTargetException) { InvocationTargetException ite = (InvocationTargetException) e; - //ite.getTargetException().printStackTrace(); t = ite.getCause(); } else { - //e.printStackTrace(); t = e; } - //// check for RuntimeException, and allow to bubble up + // check for RuntimeException, and allow to bubble up if (t instanceof RuntimeException) { - //// re-throw exception + // re-throw exception throw (RuntimeException) t; } else { - //// trap and print as usual + // trap and print as usual t.printStackTrace(); } } } } - public void add(Object object, Method method) { - if (objects == null) { - objects = new Object[5]; - methods = new Method[5]; + + void add(Object object, Method method) { + if (findIndex(object) == -1) { + if (objects == null) { + objects = new Object[5]; + methods = new Method[5]; + + } else if (count == objects.length) { + objects = (Object[]) PApplet.expand(objects); + methods = (Method[]) PApplet.expand(methods); + } + objects[count] = object; + methods[count] = method; + count++; + } else { + die(method.getName() + "() already added for this instance of " + + object.getClass().getName()); } - if (count == objects.length) { - objects = (Object[]) PApplet.expand(objects); - methods = (Method[]) PApplet.expand(methods); -// Object otemp[] = new Object[count << 1]; -// System.arraycopy(objects, 0, otemp, 0, count); -// objects = otemp; -// Method mtemp[] = new Method[count << 1]; -// System.arraycopy(methods, 0, mtemp, 0, count); -// methods = mtemp; - } - objects[count] = object; - methods[count] = method; - count++; } @@ -1130,8 +1128,10 @@ public class PApplet extends Applet * must be called multiple times if object is registered multiple times). * Does not shrink array afterwards, silently returns if method not found. */ - public void remove(Object object, Method method) { - int index = findIndex(object, method); +// public void remove(Object object, Method method) { +// int index = findIndex(object, method); + public void remove(Object object) { + int index = findIndex(object); if (index != -1) { // shift remaining methods by one to preserve ordering count--; @@ -1145,9 +1145,12 @@ public class PApplet extends Applet } } - protected int findIndex(Object object, Method method) { + +// protected int findIndex(Object object, Method method) { + protected int findIndex(Object object) { for (int i = 0; i < count; i++) { - if (objects[i] == object && methods[i].equals(method)) { + if (objects[i] == object) { +// if (objects[i] == object && methods[i].equals(method)) { //objects[i].equals() might be overridden, so use == for safety // since here we do care about actual object identity //methods[i]==method is never true even for same method, so must use @@ -1160,127 +1163,260 @@ public class PApplet extends Applet } -// public void registerSize(Object o) { + /** + * Register a built-in event so that it can be fired for libraries, etc. + * Supported events include: + *