Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

KX_IpoActuator.cpp

Go to the documentation of this file.
00001 
00006 
00007 #include "KX_IpoActuator.h"
00008 #include "KX_GameObject.h"
00009 
00010 
00011 /* ------------------------------------------------------------------------- */
00012 /* Type strings                                                              */
00013 /* ------------------------------------------------------------------------- */
00014 
00015 CCString KX_IpoActuator::S_KX_ACT_IPO_PLAY_STRING      = "Play";
00016 CCString KX_IpoActuator::S_KX_ACT_IPO_PINGPONG_STRING  = "PingPong";
00017 CCString KX_IpoActuator::S_KX_ACT_IPO_FLIPPER_STRING   = "Flipper";
00018 CCString KX_IpoActuator::S_KX_ACT_IPO_LOOPSTOP_STRING  = "LoopStop";
00019 CCString KX_IpoActuator::S_KX_ACT_IPO_LOOPEND_STRING   = "LoopEnd";
00020 CCString KX_IpoActuator::S_KX_ACT_IPO_KEY2KEY_STRING   = "Key2key";
00021 CCString KX_IpoActuator::S_KX_ACT_IPO_FROM_PROP_STRING = "FromProp";
00022 
00023 /* ------------------------------------------------------------------------- */
00024 /* Native functions                                                          */
00025 /* ------------------------------------------------------------------------- */
00026 
00027 class CIpoAction : public CAction
00028 {
00029         float           m_curtime;
00030         KX_GameObject* m_gameobj;
00031 
00032 public:
00033         CIpoAction(KX_GameObject* gameobj,float curtime) :
00034           m_curtime(curtime) ,
00035           m_gameobj(gameobj){
00036           };
00037 
00038         virtual void Execute() const

00039         {
00040                 m_gameobj->UpdateIPO(m_curtime);
00041         };
00042 
00043 };
00044 
00045 bool KX_IpoActuator::Update(double curtime,double delta_time)

00046 {
00047         SCA_IActuator::Update(curtime,delta_time);
00048         // result = true if animation has to be continued, false if animation stops
00049         // maybe there are events for us in the queue !
00050         
00051 
00052         bool bNegativeEvent = false;
00053         int numevents = m_events.size();
00054 
00055         for (vector<CValue*>::iterator i=m_events.end(); !(i==m_events.begin());)
00056         {
00057                 i--;
00058                 if ((*i)->GetNumber() == 0.0f)
00059                 {
00060                         int ka=0;
00061                         bNegativeEvent = true;
00062                 }
00063                 m_events.pop_back();
00064         }
00065 
00066         if (bNegativeEvent)
00067         {
00068                 RemoveAllEvents();
00069         }
00070         
00071 
00072         double  start_smaller_then_end = ( m_starttime < m_endtime ? 1.0 : -1.0);
00073 
00074         double deltaframetime = start_smaller_then_end  * delta_time * KX_FIXED_FRAME_PER_SEC;
00075 
00076         bool result=true;
00077         
00078         switch (m_type)
00079         {
00080                 
00081         case KX_ACT_IPO_PLAY:
00082                 {
00083                         
00084                         if (start_smaller_then_end > 0.0)
00085                                 result = (m_localtime < m_endtime && !(m_localtime == m_starttime && bNegativeEvent));
00086                         else
00087                                 result = (m_localtime > m_endtime && !(m_localtime == m_starttime && bNegativeEvent));
00088                         if (result)
00089                         {
00090                                 m_localtime += m_direction * deltaframetime;
00091                                 CIpoAction ipoaction((KX_GameObject*)GetParent(),m_localtime);
00092                                 GetParent()->Execute(ipoaction);
00093                         } else
00094                         {
00095                                 m_localtime=m_starttime;
00096                                 m_direction=1;
00097                         }
00098                         break;
00099                 }
00100         case KX_ACT_IPO_PINGPONG:
00101                 {
00102                         result = true;
00103                         if (bNegativeEvent && ((m_localtime == m_starttime )|| (m_localtime == m_endtime)))
00104                         {
00105                                 result = false;
00106                         } else
00107                         {
00108                                 m_localtime += m_direction * deltaframetime;
00109                         }
00110                         
00111                         if (m_localtime*start_smaller_then_end < m_starttime*start_smaller_then_end)
00112                         {
00113                                 m_localtime = m_starttime;
00114                                 result = false;
00115                                 m_direction = 1;
00116                         }else
00117                         if (m_localtime*start_smaller_then_end > m_endtime*start_smaller_then_end)
00118                         {
00119                                 m_localtime = m_endtime;
00120                                 result = false;
00121                                 m_direction = -1;
00122                         } 
00123                         
00124                         CIpoAction ipoaction((KX_GameObject*)   GetParent(),m_localtime);
00125                         GetParent()->Execute(ipoaction);
00126                         break;
00127                 }
00128 
00129         case KX_ACT_IPO_FLIPPER:
00130                 {
00131                         result = true;
00132                         if (numevents)
00133                         {
00134                                 if (bNegativeEvent)
00135                                         m_direction = -1;
00136                                 else
00137                                         m_direction = 1;
00138                         }
00139 
00140                         if (m_localtime*start_smaller_then_end > m_endtime*start_smaller_then_end)
00141                         {
00142                                 m_localtime = m_endtime;
00143                         } else
00144                          if (m_localtime*start_smaller_then_end < m_starttime*start_smaller_then_end)
00145                          {
00146                                 m_localtime = m_starttime;
00147                                 result = false;
00148                          }
00149                         else
00150                         {
00151                                 m_localtime += m_direction * deltaframetime;
00152                         }
00153                         
00154                         CIpoAction ipoaction((KX_GameObject*)   GetParent(),m_localtime);
00155                         GetParent()->Execute(ipoaction);
00156                         break;
00157                 }
00158 
00159                 case KX_ACT_IPO_LOOPSTOP:
00160                 {
00161                         if (numevents)
00162                         {
00163                                 if (bNegativeEvent)
00164                                 {
00165                                         result = false;
00166                                         m_bNegativeEvent = false;
00167                                         numevents = 0;
00168                                 }
00169                         } // fall through to loopend, and quit the ipo animation immediatly 
00170                 }
00171         
00172                 case KX_ACT_IPO_LOOPEND:
00173                 {
00174                         if (numevents)
00175                         {
00176                                 if (bNegativeEvent)
00177                                 {
00178                                         m_bNegativeEvent = true;
00179                                 }
00180                         }
00181 
00182                         if (bNegativeEvent && m_localtime == m_starttime)
00183                         {
00184                                 result = false;
00185                         } else
00186                         {
00187                                 if (m_localtime*start_smaller_then_end < m_endtime*start_smaller_then_end)                              
00188                                 {
00189                                         m_localtime += m_direction * deltaframetime;
00190                                 } else
00191                                 {
00192                                         if (!m_bNegativeEvent)
00193                                         {
00194                                                 m_localtime = m_starttime;
00195                                         }
00196                                         else
00197                                         {
00198                                                 result = false;
00199                                                 m_bNegativeEvent = false;
00200                                         }
00201                                 }
00202                         }       
00203                         CIpoAction ipoaction((KX_GameObject*)   GetParent(),m_localtime);
00204                         GetParent()->Execute(ipoaction);
00205                         break;
00206                 }
00207         case KX_ACT_IPO_KEY2KEY:
00208                 {
00209                         result = false;
00210                         break;
00211                 }
00212         case KX_ACT_IPO_FROM_PROP:
00213                 {
00214                         result = !bNegativeEvent;
00215 
00216                          CValue* propval = GetParent()->GetProperty(m_propname);
00217                          if (propval)
00218                          {
00219                                 m_localtime = propval->GetNumber();
00220                                 CIpoAction ipoaction((KX_GameObject*)   GetParent(),m_localtime);
00221                                 GetParent()->Execute(ipoaction);
00222 
00223                          } else
00224                          {
00225                                  result = false;
00226                          }
00227                         break;
00228                 }
00229                 
00230         default:
00231                 {
00232                         result = false;
00233                 }
00234         }
00235 
00236         return result;
00237 }
00238 
00239 KX_IpoActuator::IpoActType KX_IpoActuator::string2mode(char* modename) {
00240         IpoActType res = KX_ACT_IPO_NODEF;
00241 
00242         if (modename == S_KX_ACT_IPO_PLAY_STRING) { 
00243                 res = KX_ACT_IPO_PLAY;
00244         } else if (modename == S_KX_ACT_IPO_PINGPONG_STRING) {
00245                 res = KX_ACT_IPO_PINGPONG;
00246         } else if (modename == S_KX_ACT_IPO_FLIPPER_STRING) {
00247                 res = KX_ACT_IPO_FLIPPER;
00248         } else if (modename == S_KX_ACT_IPO_LOOPSTOP_STRING) {
00249                 res = KX_ACT_IPO_LOOPSTOP;
00250         } else if (modename == S_KX_ACT_IPO_LOOPEND_STRING) {
00251                 res = KX_ACT_IPO_LOOPEND;
00252         } else if (modename == S_KX_ACT_IPO_KEY2KEY_STRING) {
00253                 res = KX_ACT_IPO_KEY2KEY;
00254         } else if (modename == S_KX_ACT_IPO_FROM_PROP_STRING) {
00255                 res = KX_ACT_IPO_FROM_PROP;
00256         }
00257 
00258         return res;
00259 }
00260 
00261 /* ------------------------------------------------------------------------- */
00262 /* Python functions                                                          */
00263 /* ------------------------------------------------------------------------- */
00264 
00265 /* Integration hooks ------------------------------------------------------- */
00266 PyTypeObject KX_IpoActuator::Type = {
00267         PyObject_HEAD_INIT(&PyType_Type)
00268         0,
00269         "KX_IpoActuator",
00270         sizeof(KX_IpoActuator),
00271         0,
00272         PyDestructor,
00273         0,
00274         __getattr,
00275         __setattr,
00276         0, //&MyPyCompare,
00277         __repr,
00278         0, //&cvalue_as_number,
00279         0,
00280         0,
00281         0,
00282         0
00283 };
00284 
00285 PyParentObject KX_IpoActuator::Parents[] = {
00286         &KX_IpoActuator::Type,
00287         &SCA_IActuator::Type,
00288         &SCA_ILogicBrick::Type,
00289         &CValue::Type,
00290         NULL
00291 };
00292 
00293 PyMethodDef KX_IpoActuator::Methods[] = {
00294 //      {"set", (PyCFunction) KX_IpoActuator::sPySet, METH_VARARGS},
00295         {"setKey2Key", (PyCFunction) KX_IpoActuator::sPySetKey2Key, METH_VARARGS},
00296         {"setProperty", (PyCFunction) KX_IpoActuator::sPySetProperty, METH_VARARGS},
00297         {"setStart", (PyCFunction) KX_IpoActuator::sPySetStart, METH_VARARGS},
00298         {"getStart", (PyCFunction) KX_IpoActuator::sPyGetStart, METH_VARARGS},
00299         {"setEnd", (PyCFunction) KX_IpoActuator::sPySetEnd, METH_VARARGS},
00300         {"getEnd", (PyCFunction) KX_IpoActuator::sPySetEnd, METH_VARARGS},
00301         {"setUseForce", (PyCFunction) KX_IpoActuator::sPySetUseForce, METH_VARARGS},
00302         {"getUseForce", (PyCFunction) KX_IpoActuator::sPyGetUseForce, METH_VARARGS},
00303         {"setType", (PyCFunction) KX_IpoActuator::sPySetType, METH_VARARGS},
00304         {"getType", (PyCFunction) KX_IpoActuator::sPyGetType, METH_VARARGS},
00305         
00306         {NULL,NULL} //Sentinel
00307 };
00308 
00309 PyObject* KX_IpoActuator::_getattr(char* attr) {
00310         _getattr_up(SCA_IActuator);
00311 }
00312 
00313 
00314 
00315 /* set --------------------------------------------------------------------- */
00316 //
00317 PyObject* KX_IpoActuator::PySet(PyObject* self, 
00318                                                                 PyObject* args, 
00319                                                                 PyObject* kwds) {
00320         /* sets modes PLAY, PINGPONG, FLIPPER, LOOPSTOP, LOOPEND                 */
00321         /* arg 1 = mode string, arg 2 = startframe, arg3 = stopframe,            */
00322         /* arg4 = force toggle                                                   */
00323         char* mode, forceToggle;
00324         IpoActType modenum;
00325         int startFrame, stopFrame;
00326         if(!PyArg_ParseTuple(args, "siis", &mode, &startFrame, 
00327                                                  &stopFrame, &forceToggle)) {
00328                 return NULL;
00329         }
00330         modenum = string2mode(mode);
00331         
00332         /* the 'force' toggle cannot be used yet... */
00333 
00334         switch (modenum) {
00335         case KX_ACT_IPO_PLAY:
00336         case KX_ACT_IPO_PINGPONG:
00337         case KX_ACT_IPO_FLIPPER:
00338         case KX_ACT_IPO_LOOPSTOP:
00339         case KX_ACT_IPO_LOOPEND:
00340                 m_type      = modenum;
00341                 m_starttime = startFrame;
00342                 m_endtime   = stopFrame;
00343                 break;
00344         default:
00345                 ; /* error */
00346         }
00347 
00348         Py_Return;
00349 }
00350 
00351 /* set key 2 key ----------------------------------------------------------- */
00352 
00353 PyObject* KX_IpoActuator::PySetKey2Key(PyObject* self, 
00354                                                                            PyObject* args, 
00355                                                                            PyObject* kwds) {
00356         /* mode is implicit here, but not supported yet... */
00357         /* args: prev.(bool) cycle (bool), hold (bool), property.*/
00358         char* previous, cycle, hold, propertyName;
00359         if (!PyArg_ParseTuple(args, "ssss", &previous, &cycle, 
00360                                                   &hold, &propertyName)) {
00361                 return NULL;
00362         }
00363 
00364         Py_Return;
00365 }
00366 
00367 /* set property  ----------------------------------------------------------- */
00368 
00369 PyObject* KX_IpoActuator::PySetProperty(PyObject* self, 
00370                                                                                 PyObject* args, 
00371                                                                                 PyObject* kwds) {
00372         /* mode is implicit here, but not supported yet... */
00373         /* args: property */
00374         char *propertyName;
00375         if(!PyArg_ParseTuple(args, "s", &propertyName)) {
00376                 return NULL;
00377         }
00378 
00379         Py_Return;
00380 }
00381 
00382 /* 4. setStart:                                                              */
00383 PyObject* KX_IpoActuator::PySetStart(PyObject* self, 
00384                                                                          PyObject* args, 
00385                                                                          PyObject* kwds) {
00386         float startArg;
00387         if(!PyArg_ParseTuple(args, "f", &startArg)) {
00388                 return NULL;            
00389         }
00390         
00391         m_starttime = startArg;
00392 
00393         Py_Return;
00394 }
00395 /* 5. getStart:                                                              */
00396 PyObject* KX_IpoActuator::PyGetStart(PyObject* self, 
00397                                                                          PyObject* args, 
00398                                                                          PyObject* kwds) {
00399         return PyFloat_FromDouble(m_starttime);
00400 }
00401 
00402 /* 6. setEnd:                                                                */
00403 PyObject* KX_IpoActuator::PySetEnd(PyObject* self, 
00404                                                                    PyObject* args, 
00405                                                                    PyObject* kwds) {
00406         float endArg;
00407         if(!PyArg_ParseTuple(args, "f", &endArg)) {
00408                 return NULL;            
00409         }
00410         
00411         m_endtime = endArg;
00412 
00413         Py_Return;
00414 }
00415 /* 7. getEnd:                                                                */
00416 PyObject* KX_IpoActuator::PyGetEnd(PyObject* self, 
00417                                                                    PyObject* args, 
00418                                                                    PyObject* kwds) {
00419         return PyFloat_FromDouble(m_endtime);
00420 }
00421 
00422 /* 6. setUseForce:                                                           */
00423 PyObject* KX_IpoActuator::PySetUseForce(PyObject* self, 
00424                                                                                 PyObject* args, 
00425                                                                                 PyObject* kwds) { 
00426         int boolArg;
00427         
00428         if (!PyArg_ParseTuple(args, "i", &boolArg)) {
00429                 return NULL;
00430         }
00431 
00432         if (boolArg == KX_TRUE) {
00433                 m_useForce = true;
00434         } else if (boolArg == KX_FALSE){
00435                 m_useForce = false;
00436         } else {
00437                 ; /* internal error */
00438         }
00439         
00440         Py_Return;      
00441 }
00442 /* 7. getUseForce:                                                           */
00443 PyObject* KX_IpoActuator::PyGetUseForce(PyObject* self, 
00444                                                                                 PyObject* args, 
00445                                                                                 PyObject* kwds) {
00446         return PyInt_FromLong(!(m_useForce == 0));
00447 }
00448 
00449 
00450 /* 8. setType:                                                               */
00451 PyObject* KX_IpoActuator::PySetType(PyObject* self, 
00452                                                                         PyObject* args, 
00453                                                                         PyObject* kwds) {
00454         int typeArg;
00455         
00456         if (!PyArg_ParseTuple(args, "i", &typeArg)) {
00457                 return NULL;
00458         }
00459         
00460         if ( (typeArg > KX_ACT_IPO_NODEF) 
00461                  && (typeArg < KX_ACT_IPO_KEY2KEY) ) {
00462                 m_type = (IpoActType) typeArg;
00463         }
00464         
00465         Py_Return;
00466 }
00467 /* 9. getType:                                                               */
00468 PyObject* KX_IpoActuator::PyGetType(PyObject* self, 
00469                                                                         PyObject* args, 
00470                                                                         PyObject* kwds) {
00471         return PyInt_FromLong(m_type);
00472 }
00473 
00474 /* eof */

Generated at Thu Feb 1 13:03:05 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000