My Project
KeyFrame.h
1 #pragma once
2 
3 #include <vector>
4 #include <string>
5 
6 namespace ParaEngine
7 {
12  template <class T>
14  {
15  private:
16  std::vector<float> times;
17  std::vector<T> data;
18 
20  int m_nPos;
21 
25  int GetValidIndex(int nPos){
26  if(nPos<0)
27  nPos = 0;
28  else if(nPos >= GetSize())
29  nPos = GetSize()-1;
30  return nPos;
31  }
32  public:
33  void Clear(){
34  times.clear();
35  data.clear();
36  m_nPos=0;
37  };
38  MovieKeyFrame():m_nPos(0){};
40  void AppendNewKey(float fTime, const T& key)
41  {
42  times.push_back(fTime);
43  data.push_back(key);
44  }
45 
50  void InsertNewKey(float fTime, const T& key)
51  {
52  int nPos = GetKeyIndexByTime(fTime);
53  if(nPos<0 || nPos >= GetSize() )
54  {
55  AppendNewKey(fTime, key);
56  }
57  else
58  {
59  times.insert(times.begin()+nPos, fTime);
60  data.insert(data.begin()+nPos, key);
61  }
62  }
63 
65  float GetEndingTime()
66  {
67  int nEndIndex = times.size()-1;
68  if(nEndIndex>=0)
69  return times[nEndIndex];
70  else
71  return 0;
72  }
78  void UpdateKeyFrame(int nIndex, float fTime, const T& key)
79  {
80  nIndex = GetValidIndex(nIndex);
81  times[nIndex] = fTime;
82  data[nIndex] = key;
83  }
87  void PopKeyFrame(int nNum)
88  {
89  int nLen = GetSize();
90  {
91  std::vector<float>::iterator iterFrom, iterTo = times.end();
92  if(nLen >nNum)
93  iterFrom = iterTo - nNum;
94  else
95  iterFrom = times.begin();
96  times.erase(iterFrom, iterTo);
97  }
98  {
99  typename std::vector<T>::iterator iterFrom, iterTo = data.end();
100  if(nLen >nNum)
101  iterFrom = iterTo - nNum;
102  else
103  iterFrom = data.begin();
104  data.erase(iterFrom, iterTo);
105  }
106  }
108  T& operator [](int nIndex){
109  return data[GetValidIndex(nIndex)];
110  };
114  float time(int nIndex){
115  return times[GetValidIndex(nIndex)];
116  };
117 
121  int GetPos(){return m_nPos;};
122 
126  void SetPos(int nPos){
127  m_nPos = nPos;
128  };
129 
131  int GetSize(){return (int)data.size();};
132 
136  T& GetRelative(int nShiftPos = 0){
137  return data[GetValidIndex(m_nPos+nShiftPos)];
138  }
139 
144  int GetKeyIndexByTime(float fTime)
145  {
146  int nLen = GetSize();
147  int nPos = GetPos();
148  bool bFound = false;
149  for(int i=0;i<nLen && !bFound;i++) {
150  float fFrom = time(nPos-1);
151  float fTo = time(nPos);
152  if(fFrom<=fTo)
153  {
154  if(fFrom<= fTime && fTime<fTo)
155  bFound = true;
156  else if(fTo<=fTime){
157  if(nPos == (nLen))
158  bFound = true;
159  else
160  nPos++;
161  }
162  else if(fFrom>fTime){
163  if(nPos == 0)
164  bFound = true;
165  else
166  nPos--;
167  }
168  }
169  else
170  {
171  // no rewinding is supported.
172  break;
173  }
174  }
175  if(bFound)
176  return nPos;
177  else
178  return -1;
179  }
188  int UpdateTime(float& fTimeElapsed){
189  int nLen = GetSize();
190  int nPos = GetPos();
191  bool bFound = false;
192  bool bRewind = false;
193  for(int i=0;i<nLen && !bFound;i++) {
194  float fFrom = time(nPos-1);
195  float fTo = time(nPos);
196  if(fFrom<=fTo)
197  {
198  if(fFrom<= fTimeElapsed && fTimeElapsed<fTo)
199  bFound = true;
200  else if(fTo<=fTimeElapsed){
201  if(nPos == (nLen))
202  bFound = true;
203  else
204  nPos++;
205  }
206  else if(fFrom>fTimeElapsed){
207  if(nPos == 0)
208  bFound = true;
209  else
210  nPos--;
211  }
212  }
213  else if(!bRewind)
214  {
215  // a rewind occurs, so to reset the time.
216  fTimeElapsed = fTo;
217  nPos = 0;
218  bRewind = true;
219  }
220  else
221  bFound = true;
222  }
223  SetPos(nPos);
224  return GetPos();
225  }
230  int TrimToTime(float fTimeElapsed)
231  {
232  int nPos = UpdateTime(fTimeElapsed);
233  int nEndPos = GetSize();
234  if(nPos < nEndPos)
235  {
236  PopKeyFrame(nEndPos - nPos);
237  return nEndPos - nPos;
238  }
239  return 0;
240  }
241  };
242  struct PosKey
243  {
244  DVector3 vPos;
245  float fFacing;
246  PosKey(float x,float y,float z,float facing):vPos(x,y,z), fFacing(facing){};
247  PosKey(const DVector3& v,float facing):vPos(v), fFacing(facing){};
248  };
250  struct DialogKey
251  {
257  string sDialog;
258  DialogKey(const string& s):sDialog(s){};
259  DialogKey(){};
260  };
262  struct CameraKey
263  {
264  float fRotY;
265  float fLiftUpAngle;
266  float fDist;
267  CameraKey(float rotY, float LiftUpAngle, float Dist):fRotY(rotY), fLiftUpAngle(LiftUpAngle), fDist(Dist){};
268  };
270  struct EffectKey
271  {
276  string sTarget;
277  public:
278  EffectKey(int effectid, const string& target);
279  EffectKey(int effectid, const char* target);
280  };
289  struct ActionKey
290  {
291  const static int MAX_ACTIONNAME_LEN = 16;
292  string sActionName;
293  enum KeyID
294  {
295  TOGGLE_TO_WALK=0,
296  TOGGLE_TO_RUN,
297  JUMP,
298  MOUNT, // mount on the nearest object
299  NONE
300  };
301  public:
302  ActionKey(const char* name);
303  ActionKey(const std::string& name);
304  ActionKey(KeyID keyID);
305  ActionKey(int nAnimID);
306 
308  const char* ToString()const;
309 
316  int ToAnimID() const;
317 
319  bool IsJump() const;
321  bool IsToggleToWalk() const;
323  bool IsToggleToRun() const;
325  bool IsMount() const;
326  };
327 
328 }
float time(int nIndex)
get the time.
Definition: KeyFrame.h:114
void SetPos(int nPos)
set the current position.
Definition: KeyFrame.h:126
3-dimensional vector with double precision.
Definition: ParaDVector3.h:17
int GetPos()
get the current position.
Definition: KeyFrame.h:121
string sTarget
target name, it can a global biped name or <AttachmentID>.
Definition: KeyFrame.h:276
string sDialog
format: "sentence": speak the sentence in the background "\say sentence": tell the current player to ...
Definition: KeyFrame.h:257
refer to autocamera
Definition: KeyFrame.h:262
key for actions and animations.
Definition: KeyFrame.h:289
int GetSize()
Get the number of key frames.
Definition: KeyFrame.h:131
different physics engine has different winding order.
Definition: EventBinding.h:32
int GetKeyIndexByTime(float fTime)
NOT TESTED: get the index nPos of the key frame, where time[nPos-1]<=fTime<time[nPos] a binary search...
Definition: KeyFrame.h:144
void InsertNewKey(float fTime, const T &key)
NOT TESTED: Insert a new key to the key frame list according to the time.
Definition: KeyFrame.h:50
dialog key for movie
Definition: KeyFrame.h:250
void UpdateKeyFrame(int nIndex, float fTime, const T &key)
update a key frame value is boundary safe.
Definition: KeyFrame.h:78
T & GetRelative(int nShiftPos=0)
get the data at current position + nShiftPos.
Definition: KeyFrame.h:136
int nEffectID
effect ID in the effect database
Definition: KeyFrame.h:273
Definition: KeyFrame.h:242
the missile and magic effect movie key frame.
Definition: KeyFrame.h:270
void PopKeyFrame(int nNum)
pop a specified number of key frames from the end of the key frames
Definition: KeyFrame.h:87
int UpdateTime(float &fTimeElapsed)
update the current time position by a given time in seconds.the current time position may not be the ...
Definition: KeyFrame.h:188
float GetEndingTime()
get the ending time
Definition: KeyFrame.h:65
T & operator[](int nIndex)
indexer
Definition: KeyFrame.h:108
void AppendNewKey(float fTime, const T &key)
append a new key to the end of the key frame list
Definition: KeyFrame.h:40
Generic movie key frame class: T is the data type stored in the key frame.
Definition: KeyFrame.h:13
int TrimToTime(float fTimeElapsed)
trim the key frames to the specified time.
Definition: KeyFrame.h:230