Home | History | Annotate | Download | only in events
      1 /*
      2  * To change this template, choose Tools | Templates
      3  * and open the template in the editor.
      4  */
      5 package com.jme3.cinematic.events;
      6 
      7 import com.jme3.animation.LoopMode;
      8 import com.jme3.app.Application;
      9 import com.jme3.cinematic.Cinematic;
     10 import com.jme3.export.InputCapsule;
     11 import com.jme3.export.JmeExporter;
     12 import com.jme3.export.JmeImporter;
     13 import com.jme3.export.OutputCapsule;
     14 import com.jme3.math.Quaternion;
     15 import com.jme3.scene.Spatial;
     16 import com.jme3.util.TempVars;
     17 import java.io.IOException;
     18 import java.util.logging.Level;
     19 import java.util.logging.Logger;
     20 
     21 /**
     22  *
     23  * @author Nehon
     24  * @deprecated use spatial animation instead.
     25  */
     26 @Deprecated
     27 public class RotationTrack extends AbstractCinematicEvent {
     28 
     29     private static final Logger log = Logger.getLogger(RotationTrack.class.getName());
     30     private Quaternion startRotation = new Quaternion();
     31     private Quaternion endRotation = new Quaternion();
     32     private Spatial spatial;
     33     private String spatialName = "";
     34     private float value = 0;
     35 
     36     @Override
     37     public void initEvent(Application app, Cinematic cinematic) {
     38         super.initEvent(app, cinematic);
     39         if (spatial == null) {
     40             spatial = cinematic.getScene().getChild(spatialName);
     41             if (spatial == null) {
     42             } else {
     43                 log.log(Level.WARNING, "spatial {0} not found in the scene", spatialName);
     44             }
     45         }
     46     }
     47 
     48     public RotationTrack() {
     49     }
     50 
     51     public RotationTrack(Spatial spatial, Quaternion endRotation) {
     52         this.endRotation.set(endRotation);
     53         this.spatial = spatial;
     54         spatialName = spatial.getName();
     55     }
     56 
     57     public RotationTrack(Spatial spatial, Quaternion endRotation, float initialDuration, LoopMode loopMode) {
     58         super(initialDuration, loopMode);
     59         this.endRotation.set(endRotation);
     60         this.spatial = spatial;
     61         spatialName = spatial.getName();
     62     }
     63 
     64     public RotationTrack(Spatial spatial, Quaternion endRotation, LoopMode loopMode) {
     65         super(loopMode);
     66         this.endRotation.set(endRotation);
     67         this.spatial = spatial;
     68         spatialName = spatial.getName();
     69     }
     70 
     71     public RotationTrack(Spatial spatial, Quaternion endRotation, float initialDuration) {
     72         super(initialDuration);
     73         this.endRotation.set(endRotation);
     74         this.spatial = spatial;
     75         spatialName = spatial.getName();
     76     }
     77 
     78     @Override
     79     public void onPlay() {
     80         if (playState != playState.Paused) {
     81             startRotation.set(spatial.getWorldRotation());
     82         }
     83         if (initialDuration == 0 && spatial != null) {
     84             spatial.setLocalRotation(endRotation);
     85             stop();
     86         }
     87     }
     88 
     89     @Override
     90     public void onUpdate(float tpf) {
     91         if (spatial != null) {
     92             value = Math.min(time / initialDuration, 1.0f);
     93             TempVars vars = TempVars.get();
     94             Quaternion q = vars.quat1;
     95             q.set(startRotation).slerp(endRotation, value);
     96 
     97             spatial.setLocalRotation(q);
     98             vars.release();
     99         }
    100     }
    101 
    102     @Override
    103     public void onStop() {
    104         value = 0;
    105     }
    106 
    107     @Override
    108     public void onPause() {
    109     }
    110 
    111     @Override
    112     public void write(JmeExporter ex) throws IOException {
    113         super.write(ex);
    114         OutputCapsule oc = ex.getCapsule(this);
    115         oc.write(spatialName, "spatialName", "");
    116         oc.write(endRotation, "endRotation", null);
    117     }
    118 
    119     @Override
    120     public void read(JmeImporter im) throws IOException {
    121         super.read(im);
    122         InputCapsule ic = im.getCapsule(this);
    123         spatialName = ic.readString("spatialName", "");
    124         endRotation = (Quaternion) ic.readSavable("endRotation", null);
    125     }
    126 }
    127