1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.jme3.cinematic.events;
6
7import com.jme3.animation.LoopMode;
8import com.jme3.app.Application;
9import com.jme3.cinematic.Cinematic;
10import com.jme3.export.InputCapsule;
11import com.jme3.export.JmeExporter;
12import com.jme3.export.JmeImporter;
13import com.jme3.export.OutputCapsule;
14import com.jme3.math.FastMath;
15import com.jme3.math.Vector3f;
16import com.jme3.scene.Spatial;
17import java.io.IOException;
18import java.util.logging.Level;
19import java.util.logging.Logger;
20
21/**
22 *
23 * @author Nehon
24 * @deprecated use spatial animation instead.
25 */
26@Deprecated
27public class ScaleTrack extends AbstractCinematicEvent {
28
29    private static final Logger log = Logger.getLogger(RotationTrack.class.getName());
30    private Vector3f startScale;
31    private Vector3f endScale;
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 ScaleTrack() {
49    }
50
51    public ScaleTrack(Spatial spatial, Vector3f endScale) {
52        this.endScale = endScale;
53        this.spatial = spatial;
54        spatialName = spatial.getName();
55    }
56
57    public ScaleTrack(Spatial spatial, Vector3f endScale, float initialDuration, LoopMode loopMode) {
58        super(initialDuration, loopMode);
59        this.endScale = endScale;
60        this.spatial = spatial;
61        spatialName = spatial.getName();
62    }
63
64    public ScaleTrack(Spatial spatial, Vector3f endScale, LoopMode loopMode) {
65        super(loopMode);
66        this.endScale = endScale;
67        this.spatial = spatial;
68        spatialName = spatial.getName();
69    }
70
71    public ScaleTrack(Spatial spatial, Vector3f endScale, float initialDuration) {
72        super(initialDuration);
73        this.endScale = endScale;
74        this.spatial = spatial;
75        spatialName = spatial.getName();
76    }
77
78    @Override
79    public void onPlay() {
80        if (playState != playState.Paused) {
81            startScale = spatial.getWorldScale().clone();
82        }
83        if (initialDuration == 0 && spatial != null) {
84            spatial.setLocalScale(endScale);
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            spatial.setLocalScale(FastMath.interpolateLinear(value, startScale, endScale));
94        }
95    }
96
97    @Override
98    public void onStop() {
99        value = 0;
100    }
101
102    @Override
103    public void onPause() {
104    }
105
106    @Override
107    public void write(JmeExporter ex) throws IOException {
108        super.write(ex);
109        OutputCapsule oc = ex.getCapsule(this);
110        oc.write(spatialName, "spatialName", "");
111        oc.write(endScale, "endScale", null);
112    }
113
114    @Override
115    public void read(JmeImporter im) throws IOException {
116        super.read(im);
117        InputCapsule ic = im.getCapsule(this);
118        spatialName = ic.readString("spatialName", "");
119        endScale = (Vector3f) ic.readSavable("endScale", null);
120    }
121}
122