implement JmeCloneable to simplify BoneTrack cloning, set a good example

v3.2
Stephen Gold 7 years ago
parent 8a3d628263
commit 4b68a4c9dc
  1. 53
      jme3-core/src/main/java/com/jme3/animation/BoneTrack.java

@ -35,6 +35,8 @@ import com.jme3.export.*;
import com.jme3.math.Quaternion; import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
import com.jme3.util.TempVars; import com.jme3.util.TempVars;
import com.jme3.util.clone.Cloner;
import com.jme3.util.clone.JmeCloneable;
import java.io.IOException; import java.io.IOException;
import java.util.BitSet; import java.util.BitSet;
@ -43,7 +45,7 @@ import java.util.BitSet;
* *
* @author Kirill Vainer * @author Kirill Vainer
*/ */
public final class BoneTrack implements Track { public final class BoneTrack implements JmeCloneable, Track {
/** /**
* Bone index in the skeleton which this track affects. * Bone index in the skeleton which this track affects.
@ -274,29 +276,44 @@ public final class BoneTrack implements Track {
} }
/** /**
* This method creates a clone of the current object. * Create a deep clone of this track.
* @return a clone of the current object *
* @return a new track
*/ */
@Override @Override
public BoneTrack clone() { public BoneTrack clone() {
int tablesLength = times.length; return Cloner.deepClone(this);
}
float[] times = this.times.clone();
Vector3f[] sourceTranslations = this.getTranslations();
Quaternion[] sourceRotations = this.getRotations();
Vector3f[] sourceScales = this.getScales();
Vector3f[] translations = new Vector3f[tablesLength]; /**
Quaternion[] rotations = new Quaternion[tablesLength]; * Create a shallow clone for the JME cloner.
Vector3f[] scales = new Vector3f[tablesLength]; *
for (int i = 0; i < tablesLength; ++i) { * @return a new track
translations[i] = sourceTranslations[i].clone(); */
rotations[i] = sourceRotations[i].clone(); @Override
scales[i] = sourceScales != null ? sourceScales[i].clone() : new Vector3f(1.0f, 1.0f, 1.0f); public BoneTrack jmeClone() {
try {
return (BoneTrack) super.clone();
} catch (CloneNotSupportedException exception) {
throw new RuntimeException("Can't clone track", exception);
}
} }
// Need to use the constructor here because of the final fields used in this class /**
return new BoneTrack(targetBoneIndex, times, translations, rotations, scales); * 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) {
translations = cloner.clone(translations);
rotations = cloner.clone(rotations);
scales = cloner.clone(scales);
times = cloner.clone(times);
} }
@Override @Override

Loading…
Cancel
Save