Home | History | Annotate | Download | only in constraints
      1 package com.jme3.scene.plugins.blender.constraints;
      2 
      3 import java.io.IOException;
      4 
      5 import com.jme3.animation.AnimChannel;
      6 import com.jme3.animation.AnimControl;
      7 import com.jme3.animation.BoneTrack;
      8 import com.jme3.animation.SpatialTrack;
      9 import com.jme3.animation.Track;
     10 import com.jme3.export.JmeExporter;
     11 import com.jme3.export.JmeImporter;
     12 import com.jme3.math.Quaternion;
     13 import com.jme3.math.Vector3f;
     14 import com.jme3.util.TempVars;
     15 
     16 /**
     17  * This class holds either the bone track or spatial track. Is made to improve
     18  * code readability.
     19  *
     20  * @author Marcin Roguski (Kaelthas)
     21  */
     22 /* package */final class BlenderTrack implements Track {
     23 	/** The spatial track. */
     24 	private SpatialTrack spatialTrack;
     25 	/** The bone track. */
     26 	private BoneTrack boneTrack;
     27 
     28 	/**
     29 	 * Constructs the object using spatial track (bone track is null).
     30 	 *
     31 	 * @param spatialTrack
     32 	 *            the spatial track
     33 	 */
     34 	public BlenderTrack(SpatialTrack spatialTrack) {
     35 		this.spatialTrack = spatialTrack;
     36 	}
     37 
     38 	/**
     39 	 * Constructs the object using bone track (spatial track is null).
     40 	 *
     41 	 * @param spatialTrack
     42 	 *            the spatial track
     43 	 */
     44 	public BlenderTrack(BoneTrack boneTrack) {
     45 		this.boneTrack = boneTrack;
     46 	}
     47 
     48 	/**
     49 	 * @return the stored track (either bone or spatial)
     50 	 */
     51 	public Track getTrack() {
     52 		return boneTrack != null ? boneTrack : spatialTrack;
     53 	}
     54 
     55 	/**
     56 	 * @return the array of rotations of this track
     57 	 */
     58 	public Quaternion[] getRotations() {
     59 		if (boneTrack != null) {
     60 			return boneTrack.getRotations();
     61 		}
     62 		return spatialTrack.getRotations();
     63 	}
     64 
     65 	/**
     66 	 * @return the array of scales for this track
     67 	 */
     68 	public Vector3f[] getScales() {
     69 		if (boneTrack != null) {
     70 			return boneTrack.getScales();
     71 		}
     72 		return spatialTrack.getScales();
     73 	}
     74 
     75 	/**
     76 	 * @return the arrays of time for this track
     77 	 */
     78 	public float[] getTimes() {
     79 		if (boneTrack != null) {
     80 			return boneTrack.getTimes();
     81 		}
     82 		return spatialTrack.getTimes();
     83 	}
     84 
     85 	/**
     86 	 * @return the array of translations of this track
     87 	 */
     88 	public Vector3f[] getTranslations() {
     89 		if (boneTrack != null) {
     90 			return boneTrack.getTranslations();
     91 		}
     92 		return spatialTrack.getTranslations();
     93 	}
     94 
     95 	/**
     96 	 * Set the translations, rotations and scales for this bone track
     97 	 *
     98 	 * @param times
     99 	 *            a float array with the time of each frame
    100 	 * @param translations
    101 	 *            the translation of the bone for each frame
    102 	 * @param rotations
    103 	 *            the rotation of the bone for each frame
    104 	 * @param scales
    105 	 *            the scale of the bone for each frame
    106 	 */
    107 	public void setKeyframes(float[] times, Vector3f[] translations,
    108 			Quaternion[] rotations, Vector3f[] scales) {
    109 		if (boneTrack != null) {
    110 			boneTrack.setKeyframes(times, translations, rotations, scales);
    111 		} else {
    112 			spatialTrack.setKeyframes(times, translations, rotations, scales);
    113 		}
    114 	}
    115 
    116 	@Override
    117 	public void write(JmeExporter ex) throws IOException {
    118 	}
    119 
    120 	@Override
    121 	public void read(JmeImporter im) throws IOException {
    122 	}
    123 
    124 	@Override
    125 	public void setTime(float time, float weight, AnimControl control,
    126 			AnimChannel channel, TempVars vars) {
    127 		if (boneTrack != null) {
    128 			boneTrack.setTime(time, weight, control, channel, vars);
    129 		} else {
    130 			spatialTrack.setTime(time, weight, control, channel, vars);
    131 		}
    132 	}
    133 
    134 	@Override
    135 	public float getLength() {
    136 		return spatialTrack == null ? boneTrack.getLength() : spatialTrack
    137 				.getLength();
    138 	}
    139 
    140 	@Override
    141 	public BlenderTrack clone() {
    142 		if (boneTrack != null) {
    143 			return new BlenderTrack(boneTrack.clone());
    144 		}
    145 		return new BlenderTrack(spatialTrack.clone());
    146 	}
    147 }
    148