1 /*
2  * Copyright (c) 2009-2010 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package com.jme3.cinematic;
33 
34 import com.jme3.export.*;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 
41 /**
42  *
43  * @author Nehon
44  */
45 public class TimeLine extends HashMap<Integer, KeyFrame> implements Savable {
46 
47     protected int keyFramesPerSeconds = 30;
48     protected int lastKeyFrameIndex = 0;
49 
TimeLine()50     public TimeLine() {
51         super();
52     }
53 
getKeyFrameAtTime(float time)54     public KeyFrame getKeyFrameAtTime(float time) {
55         return get(getKeyFrameIndexFromTime(time));
56     }
57 
getKeyFrameAtIndex(int keyFrameIndex)58     public KeyFrame getKeyFrameAtIndex(int keyFrameIndex) {
59         return get(keyFrameIndex);
60     }
61 
addKeyFrameAtTime(float time, KeyFrame keyFrame)62     public void addKeyFrameAtTime(float time, KeyFrame keyFrame) {
63         addKeyFrameAtIndex(getKeyFrameIndexFromTime(time), keyFrame);
64     }
65 
addKeyFrameAtIndex(int keyFrameIndex, KeyFrame keyFrame)66     public void addKeyFrameAtIndex(int keyFrameIndex, KeyFrame keyFrame) {
67         put(keyFrameIndex, keyFrame);
68         keyFrame.setIndex(keyFrameIndex);
69         if (lastKeyFrameIndex < keyFrameIndex) {
70             lastKeyFrameIndex = keyFrameIndex;
71         }
72     }
73 
removeKeyFrame(int keyFrameIndex)74     public void removeKeyFrame(int keyFrameIndex) {
75         remove(keyFrameIndex);
76         if (lastKeyFrameIndex == keyFrameIndex) {
77             KeyFrame kf = null;
78             for (int i = keyFrameIndex; kf == null && i >= 0; i--) {
79                 kf = getKeyFrameAtIndex(i);
80                 lastKeyFrameIndex = i;
81             }
82         }
83     }
84 
removeKeyFrame(float time)85     public void removeKeyFrame(float time) {
86         removeKeyFrame(getKeyFrameIndexFromTime(time));
87     }
88 
getKeyFrameIndexFromTime(float time)89     public int getKeyFrameIndexFromTime(float time) {
90         return Math.round(time * keyFramesPerSeconds);
91     }
92 
getKeyFrameTime(KeyFrame keyFrame)93     public float getKeyFrameTime(KeyFrame keyFrame) {
94         return (float)keyFrame.getIndex()/(float)keyFramesPerSeconds;
95     }
96 
getAllKeyFrames()97     public Collection<KeyFrame> getAllKeyFrames() {
98         return values();
99     }
100 
getLastKeyFrameIndex()101     public int getLastKeyFrameIndex() {
102         return lastKeyFrameIndex;
103     }
104 
write(JmeExporter ex)105     public void write(JmeExporter ex) throws IOException {
106         OutputCapsule oc = ex.getCapsule(this);
107         ArrayList list = new ArrayList();
108         list.addAll(values());
109         oc.writeSavableArrayList(list, "keyFrames", null);
110     }
111 
read(JmeImporter im)112     public void read(JmeImporter im) throws IOException {
113         InputCapsule ic = im.getCapsule(this);
114         ArrayList list = ic.readSavableArrayList("keyFrames", null);
115         for (Iterator it = list.iterator(); it.hasNext();) {
116             KeyFrame keyFrame = (KeyFrame) it.next();
117             addKeyFrameAtIndex(keyFrame.getIndex(), keyFrame);
118         }
119     }
120 }
121