[Committed] Added feature Spatial.getController(Class<T> class )

This is a modification that first announced here:

http://www.jmonkeyengine.com/forum/index.php?topic=12840



To quote:

SomethingNew said:

Would this sort of code be useful to others, I think a method like spatial.getController(MeshAnimationController); would be nice for initialization code, especially for noobs.  That way you don't have to know what order the Controllers were added. :)

Here's the diff for com.jme.scene.Spatial.java:

@@ -369,7 +369,30 @@
         }
         return geometricalControllers.get(i);
     }
+    
+    /**
+     * Returns the first controller hopefully only
+     *    in this list of controllers with class c.
+     *
+     * @param c
+     *            The class to get a controller from.
+     * @return The controller with Class c.
+     * @see com.jme.scene.Controller
+     */
+   @SuppressWarnings("unchecked")
+   public <T extends Controller> T getController(Class<T> c) {
+        if (geometricalControllers == null) {
+            geometricalControllers = new ArrayList<Controller>(1);
+        }
+        for (Controller controller : geometricalControllers) {
+           if (controller.getClassTag() ==  c) {
+                return (T)controller;             
+           }
+      }
+        return null;
 
+    }
+
     /**
      * Returns the ArrayList that contains this spatial's Controllers.
      *





I would think this could be a convenience method for beginning monkeys.  It is slower then getController(int i) because it uses a for loop, but should be useful for many.  Should I commit it?
arielsan said:

what about having two controllers (or more) of the same type? which one should be retrieved?


Ok I made a modification so it can a getController by a class or getControllers which gets all the controllers of that class.  Observe:

@@ -369,7 +369,30 @@
         }
         return geometricalControllers.get(i);
     }
+    
+    /**
+     * Returns the first controller hopefully the only
+     *    in this list of controllers with class c.
+     *
+     * @param c
+     *            The class type to get a controller of.
+     * @return The controller with Class c.
+     * @see com.jme.scene.Controller
+     */
+   @SuppressWarnings("unchecked")
+   public <T extends Controller> T getController(Class<T> c) {
+        if (geometricalControllers == null) {
+            geometricalControllers = new ArrayList<Controller>(1);
+        }
+        for (Controller controller : geometricalControllers) {
+           if (controller.getClassTag() ==  c) {
+                return (T)controller;             
+           }
+      }
+        return null;
 
+    }
+
     /**
      * Returns the ArrayList that contains this spatial's Controllers.
      *
@@ -381,6 +404,29 @@
         }
         return geometricalControllers;
     }
+    
+    /**
+     * Returns the all controllers
+     *    in this list of controllers with class c.
+     *
+     * @param c
+     *            The class type get to controllers of.
+     * @return The controllers with Class c.
+     * @see com.jme.scene.Controller
+     */
+   @SuppressWarnings("unchecked")
+   public <T extends Controller> ArrayList<T> getControllers(Class<T> c) {
+        if (geometricalControllers == null) {
+            geometricalControllers = new ArrayList<Controller>(1);
+        }
+        ArrayList<T> retControllers = new ArrayList<T>();
+        for (Controller controller : geometricalControllers) {
+           if (controller.getClassTag() ==  c) {
+                retControllers.add((T)controller);           
+           }
+      }
+        return retControllers;
+    }



I will commit in a week if no one comments, I should have committed the first post a long time ago, but I didn't so w/e.

Sorry it took awhile (I’m not sure if anybody cares).  It’s committed!  :)



r4892 http://code.google.com/p/jmonkeyengine/source/detail?r=4892