Merge with -r 22620:23107.
[blender.git] / source / gameengine / Ketsji / KX_IpoActuator.cpp
1 /**
2  * Do Ipo stuff
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #if defined (__sgi)
33 #include <math.h>
34 #else
35 #include <cmath>
36 #endif
37  
38 #include "KX_IpoActuator.h"
39 #include "KX_GameObject.h"
40 #include "FloatValue.h"
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #include "KX_KetsjiEngine.h"
47
48 /* ------------------------------------------------------------------------- */
49 /* Type strings                                                              */
50 /* ------------------------------------------------------------------------- */
51
52 const char *KX_IpoActuator::S_KX_ACT_IPO_PLAY_STRING      = "Play";
53 const char *KX_IpoActuator::S_KX_ACT_IPO_PINGPONG_STRING  = "PingPong";
54 const char *KX_IpoActuator::S_KX_ACT_IPO_FLIPPER_STRING   = "Flipper";
55 const char *KX_IpoActuator::S_KX_ACT_IPO_LOOPSTOP_STRING  = "LoopStop";
56 const char *KX_IpoActuator::S_KX_ACT_IPO_LOOPEND_STRING   = "LoopEnd";
57 const char *KX_IpoActuator::S_KX_ACT_IPO_KEY2KEY_STRING   = "Key2key";
58 const char *KX_IpoActuator::S_KX_ACT_IPO_FROM_PROP_STRING = "FromProp";
59
60 /* ------------------------------------------------------------------------- */
61 /* Native functions                                                          */
62 /* ------------------------------------------------------------------------- */
63
64 KX_IpoActuator::KX_IpoActuator(SCA_IObject* gameobj,
65                                                            const STR_String& propname,
66                                                            const STR_String& framePropname,
67                                                            float starttime,
68                                                            float endtime,
69                                                            bool recurse,
70                                                            int acttype,
71                                                            bool ipo_as_force,
72                                                            bool ipo_add,
73                                                            bool ipo_local)
74         : SCA_IActuator(gameobj),
75         m_bNegativeEvent(false),
76         m_startframe (starttime),
77         m_endframe(endtime),
78         m_recurse(recurse),
79         m_localtime(starttime),
80         m_direction(1),
81         m_propname(propname),
82         m_framepropname(framePropname),
83         m_ipo_as_force(ipo_as_force),
84         m_ipo_add(ipo_add),
85         m_ipo_local(ipo_local),
86         m_type(acttype)
87 {
88         m_starttime = -2.0*fabs(m_endframe - m_startframe) - 1.0;
89         m_bIpoPlaying = false;
90 }
91
92 void KX_IpoActuator::SetStart(float starttime) 
93
94         m_startframe=starttime;
95 }
96
97 void KX_IpoActuator::SetEnd(float endtime) 
98
99         m_endframe=endtime;
100 }
101
102 bool KX_IpoActuator::ClampLocalTime()
103 {
104         if (m_startframe < m_endframe)
105         {
106                 if (m_localtime < m_startframe)
107                 {
108                         m_localtime = m_startframe;
109                         return true;
110                 } 
111                 else if (m_localtime > m_endframe)
112                 {
113                         m_localtime = m_endframe;
114                         return true;
115                 }
116         } else {
117                 if (m_localtime > m_startframe)
118                 {
119                         m_localtime = m_startframe;
120                         return true;
121                 }
122                 else if (m_localtime < m_endframe)
123                 {
124                         m_localtime = m_endframe;
125                         return true;
126                 }
127         }
128         return false;
129 }
130
131 void KX_IpoActuator::SetStartTime(float curtime)
132 {
133         float direction = m_startframe < m_endframe ? 1.0f : -1.0f;
134
135         if (m_direction > 0)
136                 m_starttime = curtime - direction*(m_localtime - m_startframe)/KX_KetsjiEngine::GetAnimFrameRate();
137         else
138                 m_starttime = curtime - direction*(m_endframe - m_localtime)/KX_KetsjiEngine::GetAnimFrameRate();
139 }
140
141 void KX_IpoActuator::SetLocalTime(float curtime)
142 {
143         float delta_time = (curtime - m_starttime)*KX_KetsjiEngine::GetAnimFrameRate();
144         
145         // negative delta_time is caused by floating point inaccuracy
146         // perhaps the inaccuracy could be reduced a bit
147         if ((m_localtime==m_startframe || m_localtime==m_endframe) && delta_time<0.0)
148         {
149                 delta_time = 0.0;
150         }
151         
152         if (m_endframe < m_startframe)
153                 delta_time = -delta_time;
154
155         if (m_direction > 0)
156                 m_localtime = m_startframe + delta_time;
157         else
158                 m_localtime = m_endframe - delta_time;
159 }
160
161 bool KX_IpoActuator::Update(double curtime, bool frame)
162 {
163         // result = true if animation has to be continued, false if animation stops
164         // maybe there are events for us in the queue !
165         bool bNegativeEvent = false;
166         bool numevents = false;
167         bool bIpoStart = false;
168
169         curtime -= KX_KetsjiEngine::GetSuspendedDelta();
170
171         if (frame)
172         {
173                 numevents = m_posevent || m_negevent;
174                 bNegativeEvent = IsNegativeEvent();
175                 RemoveAllEvents();
176         }
177         
178         float  start_smaller_then_end = ( m_startframe < m_endframe ? 1.0f : -1.0f);
179
180         bool result=true;
181         if (!bNegativeEvent)
182         {
183                 if (m_starttime < -2.0f*start_smaller_then_end*(m_endframe - m_startframe))
184                 {
185                         // start for all Ipo, initial start for LOOP_STOP
186                         m_starttime = curtime;
187                         m_bIpoPlaying = true;
188                         bIpoStart = true;
189                 }
190         }       
191
192         switch ((IpoActType)m_type)
193         {
194                 
195         case KX_ACT_IPO_PLAY:
196         {
197                 // Check if playing forwards.  result = ! finished
198                 
199                 if (start_smaller_then_end > 0.f)
200                         result = (m_localtime < m_endframe && m_bIpoPlaying);
201                 else
202                         result = (m_localtime > m_endframe && m_bIpoPlaying);
203                 
204                 if (result)
205                 {
206                         SetLocalTime(curtime);
207                 
208                         /* Perform clamping */
209                         ClampLocalTime();
210         
211                         if (bIpoStart)
212                                 ((KX_GameObject*)GetParent())->InitIPO(m_ipo_as_force, m_ipo_add, m_ipo_local);
213                         ((KX_GameObject*)GetParent())->UpdateIPO(m_localtime,m_recurse);
214                 } else
215                 {
216                         m_localtime=m_startframe;
217                         m_direction=1;
218                 }
219                 break;
220         }
221         case KX_ACT_IPO_PINGPONG:
222         {
223                 result = true;
224                 if (bNegativeEvent && !m_bIpoPlaying)
225                         result = false;
226                 else
227                         SetLocalTime(curtime);
228                         
229                 if (ClampLocalTime())
230                 {
231                         result = false;
232                         m_direction = -m_direction;
233                 }
234                 
235                 if (bIpoStart && m_direction > 0)
236                         ((KX_GameObject*)GetParent())->InitIPO(m_ipo_as_force, m_ipo_add, m_ipo_local);
237                 ((KX_GameObject*)GetParent())->UpdateIPO(m_localtime,m_recurse);
238                 break;
239         }
240         case KX_ACT_IPO_FLIPPER:
241         {
242                 if (bNegativeEvent && !m_bIpoPlaying)
243                         result = false;
244                 if (numevents)
245                 {
246                         float oldDirection = m_direction;
247                         if (bNegativeEvent)
248                                 m_direction = -1;
249                         else
250                                 m_direction = 1;
251                         if (m_direction != oldDirection)
252                                 // changing direction, reset start time
253                                 SetStartTime(curtime);
254                 }
255                 
256                 SetLocalTime(curtime);
257                 
258                 if (ClampLocalTime() && m_localtime == m_startframe)
259                         result = false;
260
261                 if (bIpoStart)
262                         ((KX_GameObject*)GetParent())->InitIPO(m_ipo_as_force, m_ipo_add, m_ipo_local);                 
263                 ((KX_GameObject*)GetParent())->UpdateIPO(m_localtime,m_recurse);
264                 break;
265         }
266
267         case KX_ACT_IPO_LOOPSTOP:
268         {
269                 if (numevents)
270                 {
271                         if (bNegativeEvent)
272                         {
273                                 result = false;
274                                 m_bNegativeEvent = false;
275                                 numevents = false;
276                         }
277                         if (!m_bIpoPlaying)
278                         {
279                                 // Ipo was stopped, make sure we will restart from where it stopped
280                                 SetStartTime(curtime);
281                                 if (!bNegativeEvent)
282                                         // positive signal will restart the Ipo
283                                         m_bIpoPlaying = true;
284                         }
285
286                 } // fall through to loopend, and quit the ipo animation immediatly 
287         }
288         case KX_ACT_IPO_LOOPEND:
289         {
290                 if (numevents){
291                         if (bNegativeEvent && m_bIpoPlaying){
292                                 m_bNegativeEvent = true;
293                         }
294                 }
295                 
296                 if (bNegativeEvent && !m_bIpoPlaying){
297                         result = false;
298                 } 
299                 else
300                 {
301                         if (m_localtime*start_smaller_then_end < m_endframe*start_smaller_then_end)
302                         {
303                                 SetLocalTime(curtime);
304                         }
305                         else{
306                                 if (!m_bNegativeEvent){
307                                         /* Perform wraparound */
308                                         SetLocalTime(curtime);
309                                         if (start_smaller_then_end > 0.f)
310                                                 m_localtime = m_startframe + fmod(m_localtime - m_startframe, m_endframe - m_startframe);
311                                         else
312                                                 m_localtime = m_startframe - fmod(m_startframe - m_localtime, m_startframe - m_endframe);
313                                         SetStartTime(curtime);
314                                         bIpoStart = true;
315                                 }
316                                 else
317                                 {       
318                                         /* Perform clamping */
319                                         m_localtime=m_endframe;
320                                         result = false;
321                                         m_bNegativeEvent = false;
322                                 }
323                         }
324                 }
325                 
326                 if (m_bIpoPlaying && bIpoStart)
327                         ((KX_GameObject*)GetParent())->InitIPO(m_ipo_as_force, m_ipo_add, m_ipo_local);
328                 ((KX_GameObject*)GetParent())->UpdateIPO(m_localtime,m_recurse);
329                 break;
330         }
331         
332         case KX_ACT_IPO_KEY2KEY:
333         {
334                 // not implemented yet
335                 result = false;
336                 break;
337         }
338         
339         case KX_ACT_IPO_FROM_PROP:
340         {
341                 result = !bNegativeEvent;
342
343                 CValue* propval = GetParent()->GetProperty(m_propname);
344                 if (propval)
345                 {
346                         m_localtime = propval->GetNumber(); 
347         
348                         if (bIpoStart)
349                                 ((KX_GameObject*)GetParent())->InitIPO(m_ipo_as_force, m_ipo_add, m_ipo_local);
350                         ((KX_GameObject*)GetParent())->UpdateIPO(m_localtime,m_recurse);
351                 } else
352                 {
353                         result = false;
354                 }
355                 break;
356         }
357                 
358         default:
359                 result = false;
360         }
361
362         /* Set the property if its defined */
363         if (m_framepropname[0] != '\0') {
364                 CValue* propowner = GetParent();
365                 CValue* oldprop = propowner->GetProperty(m_framepropname);
366                 CValue* newval = new CFloatValue(m_localtime);
367                 if (oldprop) {
368                         oldprop->SetValue(newval);
369                 } else {
370                         propowner->SetProperty(m_framepropname, newval);
371                 }
372                 newval->Release();
373         }
374
375         if (!result)
376         {
377                 if (m_type != KX_ACT_IPO_LOOPSTOP)
378                         m_starttime = -2.0*start_smaller_then_end*(m_endframe - m_startframe) - 1.0;
379                 m_bIpoPlaying = false;
380         }
381
382         return result;
383 }
384
385 int KX_IpoActuator::string2mode(char* modename) {
386         IpoActType res = KX_ACT_IPO_NODEF;
387
388         if (strcmp(modename, S_KX_ACT_IPO_PLAY_STRING)==0) { 
389                 res = KX_ACT_IPO_PLAY;
390         } else if (strcmp(modename, S_KX_ACT_IPO_PINGPONG_STRING)==0) {
391                 res = KX_ACT_IPO_PINGPONG;
392         } else if (strcmp(modename, S_KX_ACT_IPO_FLIPPER_STRING)==0) {
393                 res = KX_ACT_IPO_FLIPPER;
394         } else if (strcmp(modename, S_KX_ACT_IPO_LOOPSTOP_STRING)==0) {
395                 res = KX_ACT_IPO_LOOPSTOP;
396         } else if (strcmp(modename, S_KX_ACT_IPO_LOOPEND_STRING)==0) {
397                 res = KX_ACT_IPO_LOOPEND;
398         } else if (strcmp(modename, S_KX_ACT_IPO_KEY2KEY_STRING)==0) {
399                 res = KX_ACT_IPO_KEY2KEY;
400         } else if (strcmp(modename, S_KX_ACT_IPO_FROM_PROP_STRING)==0) {
401                 res = KX_ACT_IPO_FROM_PROP;
402         }
403
404         return res;
405 }
406
407 /* ------------------------------------------------------------------------- */
408 /* Python functions                                                          */
409 /* ------------------------------------------------------------------------- */
410
411
412
413 /* Integration hooks ------------------------------------------------------- */
414 PyTypeObject KX_IpoActuator::Type = {
415         PyVarObject_HEAD_INIT(NULL, 0)
416         "KX_IpoActuator",
417         sizeof(PyObjectPlus_Proxy),
418         0,
419         py_base_dealloc,
420         0,
421         0,
422         0,
423         0,
424         py_base_repr,
425         0,0,0,0,0,0,0,0,0,
426         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
427         0,0,0,0,0,0,0,
428         Methods,
429         0,
430         0,
431         &SCA_IActuator::Type,
432         0,0,0,0,0,0,
433         py_base_new
434 };
435
436 PyMethodDef KX_IpoActuator::Methods[] = {
437         {NULL,NULL} //Sentinel
438 };
439
440 PyAttributeDef KX_IpoActuator::Attributes[] = {
441         KX_PYATTRIBUTE_FLOAT_RW("frameStart", 0, 300000, KX_IpoActuator, m_startframe),
442         KX_PYATTRIBUTE_FLOAT_RW("frameEnd", 0, 300000, KX_IpoActuator, m_endframe),
443         KX_PYATTRIBUTE_STRING_RW("propName", 0, 64, false, KX_IpoActuator, m_propname),
444         KX_PYATTRIBUTE_STRING_RW("framePropName", 0, 64, false, KX_IpoActuator, m_framepropname),
445         KX_PYATTRIBUTE_INT_RW("mode", KX_ACT_IPO_NODEF+1, KX_ACT_IPO_MAX-1, true, KX_IpoActuator, m_type),
446         KX_PYATTRIBUTE_BOOL_RW("useIpoAsForce", KX_IpoActuator, m_ipo_as_force),
447         KX_PYATTRIBUTE_BOOL_RW("useIpoAdd", KX_IpoActuator, m_ipo_add),
448         KX_PYATTRIBUTE_BOOL_RW("useIpoLocal", KX_IpoActuator, m_ipo_local),
449         KX_PYATTRIBUTE_BOOL_RW("useChildren", KX_IpoActuator, m_recurse),
450         
451         { NULL }        //Sentinel
452 };
453
454 /* eof */