diff --git a/android/core/src/processing/core/PApplet.java b/android/core/src/processing/core/PApplet.java index 033ad8c44..5a92b48d0 100644 --- a/android/core/src/processing/core/PApplet.java +++ b/android/core/src/processing/core/PApplet.java @@ -560,15 +560,6 @@ public class PApplet extends Activity implements PConstants, Runnable { redraw = true; // draw this guy once firstMotion = true; - // these need to be inited before setup - sizeMethods = new RegisteredMethods(); - preMethods = new RegisteredMethods(); - drawMethods = new RegisteredMethods(); - postMethods = new RegisteredMethods(); - mouseEventMethods = new RegisteredMethods(); - keyEventMethods = new RegisteredMethods(); - disposeMethods = new RegisteredMethods(); - Context context = getApplicationContext(); sketchPath = context.getFilesDir().getAbsolutePath(); @@ -584,7 +575,7 @@ public class PApplet extends Activity implements PConstants, Runnable { @Override public void onConfigurationChanged(Configuration newConfig) { - System.out.println("configuration changed: " + newConfig); + if (DEBUG) System.out.println("configuration changed: " + newConfig); super.onConfigurationChanged(newConfig); } @@ -597,6 +588,7 @@ public class PApplet extends Activity implements PConstants, Runnable { // surfaceView.onResume(); if (DEBUG) System.out.println("PApplet.onResume() called"); paused = false; + handleMethods("resume"); //start(); // kick the thread back on resume(); // surfaceView.onResume(); @@ -607,10 +599,10 @@ public class PApplet extends Activity implements PConstants, Runnable { protected void onPause() { super.onPause(); - // TODO need to save all application state here! // System.out.println("PApplet.onPause() called"); paused = true; + handleMethods("pause"); pause(); // handler for others to write // synchronized (this) { // paused = true; @@ -1094,51 +1086,69 @@ public class PApplet extends Activity implements PConstants, Runnable { ////////////////////////////////////////////////////////////// - protected RegisteredMethods sizeMethods; - protected RegisteredMethods preMethods, drawMethods, postMethods; - protected RegisteredMethods mouseEventMethods, keyEventMethods; - protected RegisteredMethods disposeMethods; - public class RegisteredMethods { + /** Map of registered methods, stored by name. */ + HashMap registerMap = + new HashMap(); + + + class RegisteredMethods { int count; - Object objects[]; - Method methods[]; + 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(new Object[] { }); + 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) { - e.printStackTrace(); + // check for wrapped exception, get root exception + Throwable t; + if (e instanceof InvocationTargetException) { + InvocationTargetException ite = (InvocationTargetException) e; + t = ite.getCause(); + } else { + t = e; + } + // check for RuntimeException, and allow to bubble up + if (t instanceof RuntimeException) { + // re-throw exception + throw (RuntimeException) t; + } else { + // 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++; } @@ -1147,8 +1157,10 @@ public class PApplet extends Activity implements PConstants, Runnable { * 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--; @@ -1162,9 +1174,12 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - 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 @@ -1177,126 +1192,177 @@ public class PApplet extends Activity implements PConstants, Runnable { } + /** + * Register a built-in event so that it can be fired for libraries, etc. + * Supported events include: + *