浏览代码

Added a default implementation of cloneForSpatial()
to AbstractControl. It should work in 99% of regular
use cases and not require controls to implement
cloning if they don't need it. And if they didn't
implement it then the error message will be pretty
clear when they try to reload a saved control that
doesn't implement Cloneable.


git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9849 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

PSp..om 13 年之前
父节点
当前提交
d6267527e8
共有 1 个文件被更改,包括 23 次插入0 次删除
  1. 23 0
      engine/src/core/com/jme3/scene/control/AbstractControl.java

+ 23 - 0
engine/src/core/com/jme3/scene/control/AbstractControl.java

@@ -82,6 +82,29 @@ public abstract class AbstractControl implements Control {
      */
     protected abstract void controlRender(RenderManager rm, ViewPort vp);
 
+    /**
+     *  Default implementation of cloneForSpatial() that
+     *  simply clones the control and sets the spatial.
+     *  <pre>
+     *  AbstractControl c = clone();
+     *  c.spatial = null;
+     *  c.setSpatial(spatial);
+     *  </pre>
+     *
+     *  Controls that wish to be persisted must be Cloneable.
+     */
+    @Override
+    public Control cloneForSpatial(Spatial spatial) {
+        try {
+            AbstractControl c = (AbstractControl)clone();
+            c.spatial = null; // to keep setSpatial() from throwing an exception
+            c.setSpatial(spatial);
+            return c;
+        } catch(CloneNotSupportedException e) {
+            throw new RuntimeException( "Can't clone control for spatial", e );
+        } 
+    }
+
     public void update(float tpf) {
         if (!enabled)
             return;