From d6267527e8c7fcb1df4ed9f5522379cf03cb13a7 Mon Sep 17 00:00:00 2001 From: "PSp..om" Date: Sun, 7 Oct 2012 19:58:28 +0000 Subject: [PATCH] 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 --- .../jme3/scene/control/AbstractControl.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/engine/src/core/com/jme3/scene/control/AbstractControl.java b/engine/src/core/com/jme3/scene/control/AbstractControl.java index abfa2a85d..39ebc14e8 100644 --- a/engine/src/core/com/jme3/scene/control/AbstractControl.java +++ b/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. + *
+     *  AbstractControl c = clone();
+     *  c.spatial = null;
+     *  c.setSpatial(spatial);
+     *  
+ * + * 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;