Bladeren bron

follow JME's conventional approach to cloning a SpatialTrack

Stephen Gold 7 jaren geleden
bovenliggende
commit
f5e11d23b2
1 gewijzigde bestanden met toevoegingen van 36 en 19 verwijderingen
  1. 36 19
      jme3-core/src/main/java/com/jme3/animation/SpatialTrack.java

+ 36 - 19
jme3-core/src/main/java/com/jme3/animation/SpatialTrack.java

@@ -41,16 +41,14 @@ import com.jme3.scene.Spatial;
 import com.jme3.util.TempVars;
 import com.jme3.util.clone.Cloner;
 import com.jme3.util.clone.JmeCloneable;
-
 import java.io.IOException;
-import java.util.Arrays;
 
 /**
  * This class represents the track for spatial animation.
  * 
  * @author Marcin Roguski (Kaelthas)
  */
-public class SpatialTrack implements Track, JmeCloneable {
+public class SpatialTrack implements JmeCloneable, Track {
     
     /** 
      * Translations of the track. 
@@ -250,11 +248,16 @@ public class SpatialTrack implements Track, JmeCloneable {
             return times == null ? 0 : times[times.length - 1] - times[0];
     }
 
+    /**
+     * Create a clone with the same track spatial.
+     *
+     * @return a new track
+     */
     @Override
-    public Track clone() {
-        SpatialTrack copy = (SpatialTrack) jmeClone();
-        copy.setTrackSpatial(trackSpatial);
-        return (Track) copy;
+    public SpatialTrack clone() {
+        Cloner cloner = new Cloner();
+        cloner.setClonedValue(trackSpatial, trackSpatial);
+        return cloner.clone(this);
     }
 
     @Override
@@ -270,24 +273,38 @@ public class SpatialTrack implements Track, JmeCloneable {
         return trackSpatial;
     }
 
+    /**
+     * Create a shallow clone for the JME cloner.
+     *
+     * @return a new track
+     */
     @Override
-    public Object jmeClone() {
-        int tablesLength = times.length;
-
-        float[] timesCopy = this.times.clone();
-        Vector3f[] translationsCopy = this.getTranslations() == null ? null : Arrays.copyOf(this.getTranslations(), tablesLength);
-        Quaternion[] rotationsCopy = this.getRotations() == null ? null : Arrays.copyOf(this.getRotations(), tablesLength);
-        Vector3f[] scalesCopy = this.getScales() == null ? null : Arrays.copyOf(this.getScales(), tablesLength);
-
-        //need to use the constructor here because of the final fields used in this class
-        return new SpatialTrack(timesCopy, translationsCopy, rotationsCopy, scalesCopy);
+    public SpatialTrack jmeClone() {
+        try {
+            return (SpatialTrack) super.clone();
+        } catch (CloneNotSupportedException exception) {
+            throw new RuntimeException("Can't clone track", exception);
+        }
     }
 
+    /**
+     * Callback from {@link com.jme3.util.clone.Cloner} to convert this
+     * shallow-cloned track into a deep-cloned one, using the specified cloner
+     * to resolve copied fields.
+     *
+     * @param cloner the cloner currently cloning this control (not null)
+     * @param original the track from which this track was shallow-cloned
+     * (unused)
+     */
     @Override
     public void cloneFields(Cloner cloner, Object original) {
-        this.trackSpatial = cloner.clone(((SpatialTrack) original).trackSpatial);
+        translations = cloner.clone(translations);
+        rotations = cloner.clone(rotations);
+        scales = cloner.clone(scales);
+        trackSpatial = cloner.clone(trackSpatial);
+        times = cloner.clone(times);
     }
-	
+
     @Override
     public void write(JmeExporter ex) throws IOException {
         OutputCapsule oc = ex.getCapsule(this);