svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r20855:20928
[blender.git] / source / gameengine / Converter / BL_ActionActuator.cpp
1 /**
2 * $Id$
3 *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28 */
29
30 #if defined (__sgi)
31 #include <math.h>
32 #else
33 #include <cmath>
34 #endif
35
36 #include "SCA_LogicManager.h"
37 #include "BL_ActionActuator.h"
38 #include "BL_ArmatureObject.h"
39 #include "BL_SkinDeformer.h"
40 #include "KX_GameObject.h"
41 #include "STR_HashedString.h"
42 #include "DNA_nla_types.h"
43 #include "BKE_action.h"
44 #include "DNA_action_types.h"
45 #include "DNA_armature_types.h"
46 #include "DNA_scene_types.h"
47 #include "MEM_guardedalloc.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_arithb.h"
50 #include "MT_Matrix4x4.h"
51 #include "BKE_utildefines.h"
52 #include "FloatValue.h"
53 #include "PyObjectPlus.h"
54
55 #ifdef HAVE_CONFIG_H
56 #include <config.h>
57 #endif
58
59 BL_ActionActuator::~BL_ActionActuator()
60 {
61         if (m_pose)
62                 game_free_pose(m_pose);
63         if (m_userpose)
64                 game_free_pose(m_userpose);
65         if (m_blendpose)
66                 game_free_pose(m_blendpose);
67 }
68
69 void BL_ActionActuator::ProcessReplica()
70 {
71         SCA_IActuator::ProcessReplica();
72         
73         m_pose = NULL;
74         m_blendpose = NULL;
75         m_localtime=m_startframe;
76         m_lastUpdate=-1;
77         
78 }
79
80 void BL_ActionActuator::SetBlendTime (float newtime){
81         m_blendframe = newtime;
82 }
83
84 CValue* BL_ActionActuator::GetReplica() {
85         BL_ActionActuator* replica = new BL_ActionActuator(*this);//m_float,GetName());
86         replica->ProcessReplica();
87         return replica;
88 }
89
90 bool BL_ActionActuator::ClampLocalTime()
91 {
92         if (m_startframe < m_endframe)
93         {
94                 if (m_localtime < m_startframe)
95                 {
96                         m_localtime = m_startframe;
97                         return true;
98                 } 
99                 else if (m_localtime > m_endframe)
100                 {
101                         m_localtime = m_endframe;
102                         return true;
103                 }
104         } else {
105                 if (m_localtime > m_startframe)
106                 {
107                         m_localtime = m_startframe;
108                         return true;
109                 }
110                 else if (m_localtime < m_endframe)
111                 {
112                         m_localtime = m_endframe;
113                         return true;
114                 }
115         }
116         return false;
117 }
118
119 void BL_ActionActuator::SetStartTime(float curtime)
120 {
121         float direction = m_startframe < m_endframe ? 1.0 : -1.0;
122         
123         if (!(m_flag & ACT_FLAG_REVERSE))
124                 m_starttime = curtime - direction*(m_localtime - m_startframe)/KX_KetsjiEngine::GetAnimFrameRate();
125         else
126                 m_starttime = curtime - direction*(m_endframe - m_localtime)/KX_KetsjiEngine::GetAnimFrameRate();
127 }
128
129 void BL_ActionActuator::SetLocalTime(float curtime)
130 {
131         float delta_time = (curtime - m_starttime)*KX_KetsjiEngine::GetAnimFrameRate();
132         
133         if (m_endframe < m_startframe)
134                 delta_time = -delta_time;
135
136         if (!(m_flag & ACT_FLAG_REVERSE))
137                 m_localtime = m_startframe + delta_time;
138         else
139                 m_localtime = m_endframe - delta_time;
140 }
141
142
143 bool BL_ActionActuator::Update(double curtime, bool frame)
144 {
145         bool bNegativeEvent = false;
146         bool bPositiveEvent = false;
147         bool keepgoing = true;
148         bool wrap = false;
149         bool apply=true;
150         int     priority;
151         float newweight;
152
153         curtime -= KX_KetsjiEngine::GetSuspendedDelta();
154         
155         // result = true if animation has to be continued, false if animation stops
156         // maybe there are events for us in the queue !
157         if (frame)
158         {
159                 bNegativeEvent = m_negevent;
160                 bPositiveEvent = m_posevent;
161                 RemoveAllEvents();
162                 
163                 if (bPositiveEvent)
164                         m_flag |= ACT_FLAG_ACTIVE;
165                 
166                 if (bNegativeEvent)
167                 {
168                         // dont continue where we left off when restarting
169                         if (m_end_reset) {
170                                 m_flag &= ~ACT_FLAG_LOCKINPUT;
171                         }
172                         
173                         if (!(m_flag & ACT_FLAG_ACTIVE))
174                                 return false;
175                         m_flag &= ~ACT_FLAG_ACTIVE;
176                 }
177         }
178         
179         /*      We know that action actuators have been discarded from all non armature objects:
180         if we're being called, we're attached to a BL_ArmatureObject */
181         BL_ArmatureObject *obj = (BL_ArmatureObject*)GetParent();
182         float length = m_endframe - m_startframe;
183         
184         priority = m_priority;
185         
186         /* Determine pre-incrementation behaviour and set appropriate flags */
187         switch (m_playtype){
188         case ACT_ACTION_MOTION:
189                 if (bNegativeEvent){
190                         keepgoing=false;
191                         apply=false;
192                 };
193                 break;
194         case ACT_ACTION_FROM_PROP:
195                 if (bNegativeEvent){
196                         apply=false;
197                         keepgoing=false;
198                 }
199                 break;
200         case ACT_ACTION_LOOP_END:
201                 if (bPositiveEvent){
202                         if (!(m_flag & ACT_FLAG_LOCKINPUT)){
203                                 m_flag &= ~ACT_FLAG_KEYUP;
204                                 m_flag &= ~ACT_FLAG_REVERSE;
205                                 m_flag |= ACT_FLAG_LOCKINPUT;
206                                 m_localtime = m_startframe;
207                                 m_starttime = curtime;
208                         }
209                 }
210                 if (bNegativeEvent){
211                         m_flag |= ACT_FLAG_KEYUP;
212                 }
213                 break;
214         case ACT_ACTION_LOOP_STOP:
215                 if (bPositiveEvent){
216                         if (!(m_flag & ACT_FLAG_LOCKINPUT)){
217                                 m_flag &= ~ACT_FLAG_REVERSE;
218                                 m_flag &= ~ACT_FLAG_KEYUP;
219                                 m_flag |= ACT_FLAG_LOCKINPUT;
220                                 SetStartTime(curtime);
221                         }
222                 }
223                 if (bNegativeEvent){
224                         m_flag |= ACT_FLAG_KEYUP;
225                         m_flag &= ~ACT_FLAG_LOCKINPUT;
226                         keepgoing=false;
227                         apply=false;
228                 }
229                 break;
230         case ACT_ACTION_FLIPPER:
231                 if (bPositiveEvent){
232                         if (!(m_flag & ACT_FLAG_LOCKINPUT)){
233                                 m_flag &= ~ACT_FLAG_REVERSE;
234                                 m_flag |= ACT_FLAG_LOCKINPUT;
235                                 SetStartTime(curtime);
236                         }
237                 }
238                 else if (bNegativeEvent){
239                         m_flag |= ACT_FLAG_REVERSE;
240                         m_flag &= ~ACT_FLAG_LOCKINPUT;
241                         SetStartTime(curtime);
242                 }
243                 break;
244         case ACT_ACTION_PLAY:
245                 if (bPositiveEvent){
246                         if (!(m_flag & ACT_FLAG_LOCKINPUT)){
247                                 m_flag &= ~ACT_FLAG_REVERSE;
248                                 m_localtime = m_starttime;
249                                 m_starttime = curtime;
250                                 m_flag |= ACT_FLAG_LOCKINPUT;
251                         }
252                 }
253                 break;
254         default:
255                 break;
256         }
257         
258         /* Perform increment */
259         if (keepgoing){
260                 if (m_playtype == ACT_ACTION_MOTION){
261                         MT_Point3       newpos;
262                         MT_Point3       deltapos;
263                         
264                         newpos = obj->NodeGetWorldPosition();
265                         
266                         /* Find displacement */
267                         deltapos = newpos-m_lastpos;
268                         m_localtime += (length/m_stridelength) * deltapos.length();
269                         m_lastpos = newpos;
270                 }
271                 else{
272                         SetLocalTime(curtime);
273                 }
274         }
275         
276         /* Check if a wrapping response is needed */
277         if (length){
278                 if (m_localtime < m_startframe || m_localtime > m_endframe)
279                 {
280                         m_localtime = m_startframe + fmod(m_localtime, length);
281                         wrap = true;
282                 }
283         }
284         else
285                 m_localtime = m_startframe;
286         
287         /* Perform post-increment tasks */
288         switch (m_playtype){
289         case ACT_ACTION_FROM_PROP:
290                 {
291                         CValue* propval = GetParent()->GetProperty(m_propname);
292                         if (propval)
293                                 m_localtime = propval->GetNumber();
294                         
295                         if (bNegativeEvent){
296                                 keepgoing=false;
297                         }
298                 }
299                 break;
300         case ACT_ACTION_MOTION:
301                 break;
302         case ACT_ACTION_LOOP_STOP:
303                 break;
304         case ACT_ACTION_FLIPPER:
305                 if (wrap){
306                         if (!(m_flag & ACT_FLAG_REVERSE)){
307                                 m_localtime=m_endframe;
308                                 //keepgoing = false;
309                         }
310                         else {
311                                 m_localtime=m_startframe;
312                                 keepgoing = false;
313                         }
314                 }
315                 break;
316         case ACT_ACTION_LOOP_END:
317                 if (wrap){
318                         if (m_flag & ACT_FLAG_KEYUP){
319                                 keepgoing = false;
320                                 m_localtime = m_endframe;
321                                 m_flag &= ~ACT_FLAG_LOCKINPUT;
322                         }
323                         SetStartTime(curtime);
324                 }
325                 break;
326         case ACT_ACTION_PLAY:
327                 if (wrap){
328                         m_localtime = m_endframe;
329                         keepgoing = false;
330                         m_flag &= ~ACT_FLAG_LOCKINPUT;
331                 }
332                 break;
333         default:
334                 keepgoing = false;
335                 break;
336         }
337         
338         /* Set the property if its defined */
339         if (m_framepropname[0] != '\0') {
340                 CValue* propowner = GetParent();
341                 CValue* oldprop = propowner->GetProperty(m_framepropname);
342                 CValue* newval = new CFloatValue(m_localtime);
343                 if (oldprop) {
344                         oldprop->SetValue(newval);
345                 } else {
346                         propowner->SetProperty(m_framepropname, newval);
347                 }
348                 newval->Release();
349         }
350         
351         if (bNegativeEvent)
352                 m_blendframe=0.0;
353         
354         /* Apply the pose if necessary*/
355         if (apply){
356
357                 /* Priority test */
358                 if (obj->SetActiveAction(this, priority, curtime)){
359                         
360                         /* Get the underlying pose from the armature */
361                         obj->GetPose(&m_pose);
362                         
363                         /* Override the necessary channels with ones from the action */
364                         // XXX extract_pose_from_action(m_pose, m_action, m_localtime);
365
366                         /* Perform the user override (if any) */
367                         if (m_userpose){
368                                 extract_pose_from_pose(m_pose, m_userpose);
369 //                              clear_pose(m_userpose);
370                                 MEM_freeN(m_userpose);
371                                 m_userpose = NULL;
372                         }
373 #if 1
374                         /* Handle blending */
375                         if (m_blendin && (m_blendframe<m_blendin)){
376                                 /* If this is the start of a blending sequence... */
377                                 if ((m_blendframe==0.0) || (!m_blendpose)){
378                                         obj->GetMRDPose(&m_blendpose);
379                                         m_blendstart = curtime;
380                                 }
381                                 
382                                 /* Find percentages */
383                                 newweight = (m_blendframe/(float)m_blendin);
384                                 // XXX blend_poses(m_pose, m_blendpose, 1.0 - newweight, ACTSTRIPMODE_BLEND);
385
386                                 /* Increment current blending percentage */
387                                 m_blendframe = (curtime - m_blendstart)*KX_KetsjiEngine::GetAnimFrameRate();
388                                 if (m_blendframe>m_blendin)
389                                         m_blendframe = m_blendin;
390                                 
391                         }
392 #endif
393                         m_lastUpdate = m_localtime;
394                         obj->SetPose (m_pose);
395                 }
396                 else{
397                         m_blendframe = 0.0;
398                 }
399         }
400         
401         if (!keepgoing){
402                 m_blendframe = 0.0;
403         }
404         return keepgoing;
405 };
406
407 /* ------------------------------------------------------------------------- */
408 /* Python functions                                                          */
409 /* ------------------------------------------------------------------------- */
410
411 /*     setStart                                                              */
412 const char BL_ActionActuator::GetAction_doc[] = 
413 "getAction()\n"
414 "\tReturns a string containing the name of the current action.\n";
415
416 PyObject* BL_ActionActuator::PyGetAction(PyObject* args, 
417                                                                                  PyObject* kwds) {
418         ShowDeprecationWarning("getAction()", "the action property");
419
420         if (m_action){
421                 return PyString_FromString(m_action->id.name+2);
422         }
423         Py_RETURN_NONE;
424 }
425
426 /*     getProperty                                                             */
427 const char BL_ActionActuator::GetProperty_doc[] = 
428 "getProperty()\n"
429 "\tReturns the name of the property to be used in FromProp mode.\n";
430
431 PyObject* BL_ActionActuator::PyGetProperty(PyObject* args, 
432                                                                                    PyObject* kwds) {
433         ShowDeprecationWarning("getProperty()", "the property property");
434
435         PyObject *result;
436         
437         result = Py_BuildValue("s", (const char *)m_propname);
438         
439         return result;
440 }
441
442 /*     getProperty                                                             */
443 const char BL_ActionActuator::GetFrameProperty_doc[] = 
444 "getFrameProperty()\n"
445 "\tReturns the name of the property, that is set to the current frame number.\n";
446
447 PyObject* BL_ActionActuator::PyGetFrameProperty(PyObject* args, 
448                                                                                    PyObject* kwds) {
449         ShowDeprecationWarning("getFrameProperty()", "the frameProperty property");
450
451         PyObject *result;
452         
453         result = Py_BuildValue("s", (const char *)m_framepropname);
454         
455         return result;
456 }
457
458 /*     getFrame                                                              */
459 const char BL_ActionActuator::GetFrame_doc[] = 
460 "getFrame()\n"
461 "\tReturns the current frame number.\n";
462
463 PyObject* BL_ActionActuator::PyGetFrame(PyObject* args, 
464                                                                                 PyObject* kwds) {
465         ShowDeprecationWarning("getFrame()", "the frame property");
466
467         PyObject *result;
468         
469         result = Py_BuildValue("f", m_localtime);
470         
471         return result;
472 }
473
474 /*     getEnd                                                                */
475 const char BL_ActionActuator::GetEnd_doc[] = 
476 "getEnd()\n"
477 "\tReturns the last frame of the action.\n";
478
479 PyObject* BL_ActionActuator::PyGetEnd(PyObject* args, 
480                                                                           PyObject* kwds) {
481         ShowDeprecationWarning("getEnd()", "the end property");
482
483         PyObject *result;
484         
485         result = Py_BuildValue("f", m_endframe);
486         
487         return result;
488 }
489
490 /*     getStart                                                              */
491 const char BL_ActionActuator::GetStart_doc[] = 
492 "getStart()\n"
493 "\tReturns the starting frame of the action.\n";
494
495 PyObject* BL_ActionActuator::PyGetStart(PyObject* args, 
496                                                                                 PyObject* kwds) {
497         ShowDeprecationWarning("getStart()", "the start property");
498
499         PyObject *result;
500         
501         result = Py_BuildValue("f", m_startframe);
502         
503         return result;
504 }
505
506 /*     getBlendin                                                            */
507 const char BL_ActionActuator::GetBlendin_doc[] = 
508 "getBlendin()\n"
509 "\tReturns the number of interpolation animation frames to be\n"
510 "\tgenerated when this actuator is triggered.\n";
511
512 PyObject* BL_ActionActuator::PyGetBlendin(PyObject* args, 
513                                                                                   PyObject* kwds) {
514         ShowDeprecationWarning("getBlendin()", "the blendin property");
515
516         PyObject *result;
517         
518         result = Py_BuildValue("f", m_blendin);
519         
520         return result;
521 }
522
523 /*     getPriority                                                           */
524 const char BL_ActionActuator::GetPriority_doc[] = 
525 "getPriority()\n"
526 "\tReturns the priority for this actuator.  Actuators with lower\n"
527 "\tPriority numbers will override actuators with higher numbers.\n";
528
529 PyObject* BL_ActionActuator::PyGetPriority(PyObject* args, 
530                                                                                    PyObject* kwds) {
531         ShowDeprecationWarning("getPriority()", "the priority property");
532
533         PyObject *result;
534         
535         result = Py_BuildValue("i", m_priority);
536         
537         return result;
538 }
539
540 /*     setAction                                                             */
541 const char BL_ActionActuator::SetAction_doc[] = 
542 "setAction(action, (reset))\n"
543 "\t - action    : The name of the action to set as the current action.\n"
544 "\t - reset     : Optional parameter indicating whether to reset the\n"
545 "\t               blend timer or not.  A value of 1 indicates that the\n"
546 "\t               timer should be reset.  A value of 0 will leave it\n"
547 "\t               unchanged.  If reset is not specified, the timer will"
548 "\t                   be reset.\n";
549
550 PyObject* BL_ActionActuator::PySetAction(PyObject* args, 
551                                                                                  PyObject* kwds) {
552         ShowDeprecationWarning("setAction()", "the action property");
553
554         char *string;
555         int     reset = 1;
556
557         if (PyArg_ParseTuple(args,"s|i:setAction",&string, &reset))
558         {
559                 bAction *action;
560                 
561                 action = (bAction*)SCA_ILogicBrick::m_sCurrentLogicManager->GetActionByName(STR_String(string));
562                 
563                 if (!action){
564                         /* NOTE!  Throw an exception or something */
565                         //                      printf ("setAction failed: Action not found\n", string);
566                 }
567                 else{
568                         m_action=action;
569                         if (reset)
570                                 m_blendframe = 0;
571                 }
572         }
573         else {
574                 return NULL;
575         }
576         
577         Py_RETURN_NONE;
578 }
579
580 /*     setStart                                                              */
581 const char BL_ActionActuator::SetStart_doc[] = 
582 "setStart(start)\n"
583 "\t - start     : Specifies the starting frame of the animation.\n";
584
585 PyObject* BL_ActionActuator::PySetStart(PyObject* args, 
586                                                                                 PyObject* kwds) {
587         ShowDeprecationWarning("setStart()", "the start property");
588
589         float start;
590         
591         if (PyArg_ParseTuple(args,"f:setStart",&start))
592         {
593                 m_startframe = start;
594         }
595         else {
596                 return NULL;
597         }
598         
599         Py_RETURN_NONE;
600 }
601
602 /*     setEnd                                                                */
603 const char BL_ActionActuator::SetEnd_doc[] = 
604 "setEnd(end)\n"
605 "\t - end       : Specifies the ending frame of the animation.\n";
606
607 PyObject* BL_ActionActuator::PySetEnd(PyObject* args, 
608                                                                           PyObject* kwds) {
609         ShowDeprecationWarning("setEnd()", "the end property");
610
611         float end;
612         
613         if (PyArg_ParseTuple(args,"f:setEnd",&end))
614         {
615                 m_endframe = end;
616         }
617         else {
618                 return NULL;
619         }
620         
621         Py_RETURN_NONE;
622 }
623
624 /*     setBlendin                                                            */
625 const char BL_ActionActuator::SetBlendin_doc[] = 
626 "setBlendin(blendin)\n"
627 "\t - blendin   : Specifies the number of frames of animation to generate\n"
628 "\t               when making transitions between actions.\n";
629
630 PyObject* BL_ActionActuator::PySetBlendin(PyObject* args, 
631                                                                                   PyObject* kwds) {
632         ShowDeprecationWarning("setBlendin()", "the blendin property");
633
634         float blendin;
635         
636         if (PyArg_ParseTuple(args,"f:setBlendin",&blendin))
637         {
638                 m_blendin = blendin;
639         }
640         else {
641                 return NULL;
642         }
643         
644         Py_RETURN_NONE;
645 }
646
647 /*     setBlendtime                                                          */
648 const char BL_ActionActuator::SetBlendtime_doc[] = 
649 "setBlendtime(blendtime)\n"
650 "\t - blendtime : Allows the script to directly modify the internal timer\n"
651 "\t               used when generating transitions between actions.  This\n"
652 "\t               parameter must be in the range from 0.0 to 1.0.\n";
653
654 PyObject* BL_ActionActuator::PySetBlendtime(PyObject* args, 
655                                                                                   PyObject* kwds) {
656         ShowDeprecationWarning("setBlendtime()", "the blendtime property");
657
658         float blendframe;
659         
660         if (PyArg_ParseTuple(args,"f:setBlendtime",&blendframe))
661         {
662                 m_blendframe = blendframe * m_blendin;
663                 if (m_blendframe<0)
664                         m_blendframe = 0;
665                 if (m_blendframe>m_blendin)
666                         m_blendframe = m_blendin;
667         }
668         else {
669                 return NULL;
670         }
671         
672         Py_RETURN_NONE;
673 }
674
675 /*     setPriority                                                           */
676 const char BL_ActionActuator::SetPriority_doc[] = 
677 "setPriority(priority)\n"
678 "\t - priority  : Specifies the new priority.  Actuators will lower\n"
679 "\t               priority numbers will override actuators with higher\n"
680 "\t               numbers.\n";
681
682 PyObject* BL_ActionActuator::PySetPriority(PyObject* args, 
683                                                                                    PyObject* kwds) {
684         ShowDeprecationWarning("setPriority()", "the priority property");
685
686         int priority;
687         
688         if (PyArg_ParseTuple(args,"i:setPriority",&priority))
689         {
690                 m_priority = priority;
691         }
692         else {
693                 return NULL;
694         }
695         
696         Py_RETURN_NONE;
697 }
698
699 /*     setFrame                                                              */
700 const char BL_ActionActuator::SetFrame_doc[] = 
701 "setFrame(frame)\n"
702 "\t - frame     : Specifies the new current frame for the animation\n";
703
704 PyObject* BL_ActionActuator::PySetFrame(PyObject* args, 
705                                                                                 PyObject* kwds) {
706         ShowDeprecationWarning("setFrame()", "the frame property");
707
708         float frame;
709         
710         if (PyArg_ParseTuple(args,"f:setFrame",&frame))
711         {
712                 m_localtime = frame;
713                 if (m_localtime<m_startframe)
714                         m_localtime=m_startframe;
715                 else if (m_localtime>m_endframe)
716                         m_localtime=m_endframe;
717         }
718         else {
719                 return NULL;
720         }
721         
722         Py_RETURN_NONE;
723 }
724
725 /*     setProperty                                                           */
726 const char BL_ActionActuator::SetProperty_doc[] = 
727 "setProperty(prop)\n"
728 "\t - prop      : A string specifying the property name to be used in\n"
729 "\t               FromProp playback mode.\n";
730
731 PyObject* BL_ActionActuator::PySetProperty(PyObject* args, 
732                                                                                    PyObject* kwds) {
733         ShowDeprecationWarning("setProperty()", "the property property");
734
735         char *string;
736         
737         if (PyArg_ParseTuple(args,"s:setProperty",&string))
738         {
739                 m_propname = string;
740         }
741         else {
742                 return NULL;
743         }
744         
745         Py_RETURN_NONE;
746 }
747
748 /*     setFrameProperty                                                          */
749 const char BL_ActionActuator::SetFrameProperty_doc[] = 
750 "setFrameProperty(prop)\n"
751 "\t - prop      : A string specifying the property of the frame set up update.\n";
752
753 PyObject* BL_ActionActuator::PySetFrameProperty(PyObject* args, 
754                                                                                    PyObject* kwds) {
755         ShowDeprecationWarning("setFrameProperty()", "the frameProperty property");
756
757         char *string;
758         
759         if (PyArg_ParseTuple(args,"s:setFrameProperty",&string))
760         {
761                 m_framepropname = string;
762         }
763         else {
764                 return NULL;
765         }
766         
767         Py_RETURN_NONE;
768 }
769
770 /*
771 PyObject* BL_ActionActuator::PyGetChannel(PyObject* args, 
772                                                                                    PyObject* kwds) {
773         char *string;
774         
775         if (PyArg_ParseTuple(args,"s:getChannel",&string))
776         {
777                 m_propname = string;
778         }
779         else {
780                 return NULL;
781         }
782         
783         Py_RETURN_NONE;
784 }
785 */
786
787 /* getType */
788 const char BL_ActionActuator::GetType_doc[] =
789 "getType()\n"
790 "\tReturns the operation mode of the actuator.\n";
791 PyObject* BL_ActionActuator::PyGetType(PyObject* args, 
792                                        PyObject* kwds) {
793         ShowDeprecationWarning("getType()", "the type property");
794
795     return Py_BuildValue("h", m_playtype);
796 }
797
798 /* setType */
799 const char BL_ActionActuator::SetType_doc[] =
800 "setType(mode)\n"
801 "\t - mode: Play (0), Flipper (2), LoopStop (3), LoopEnd (4) or Property (6)\n"
802 "\tSet the operation mode of the actuator.\n";
803 PyObject* BL_ActionActuator::PySetType(PyObject* args,
804                                        PyObject* kwds) {
805         ShowDeprecationWarning("setType()", "the type property");
806
807         short typeArg;
808                                                                                                              
809     if (!PyArg_ParseTuple(args, "h:setType", &typeArg)) {
810         return NULL;
811     }
812
813         switch (typeArg) {
814         case ACT_ACTION_PLAY:
815         case ACT_ACTION_FLIPPER:
816         case ACT_ACTION_LOOP_STOP:
817         case ACT_ACTION_LOOP_END:
818         case ACT_ACTION_FROM_PROP:
819                 m_playtype = typeArg;
820                 break;
821         default:
822                 printf("Invalid type for action actuator: %d\n", typeArg); /* error */
823     }
824         Py_RETURN_NONE;
825 }
826
827 PyObject* BL_ActionActuator::PyGetContinue() {
828         ShowDeprecationWarning("getContinue()", "the continue property");
829
830     return PyInt_FromLong((long)(m_end_reset==0));
831 }
832
833 PyObject* BL_ActionActuator::PySetContinue(PyObject* value) {
834         ShowDeprecationWarning("setContinue()", "the continue property");
835
836         int param = PyObject_IsTrue( value );
837         
838         if( param == -1 ) {
839                 PyErr_SetString( PyExc_TypeError, "expected True/False or 0/1" );
840                 return NULL;
841         }
842
843         if (param) {
844                 m_end_reset = 0;
845         } else {
846                 m_end_reset = 1;
847         }
848     Py_RETURN_NONE;
849 }
850
851 //<-----Deprecated
852
853 /*     setChannel                                                         */
854 KX_PYMETHODDEF_DOC(BL_ActionActuator, setChannel,
855 "setChannel(channel, matrix)\n"
856 "\t - channel   : A string specifying the name of the bone channel.\n"
857 "\t - matrix    : A 4x4 matrix specifying the overriding transformation\n"
858 "\t               as an offset from the bone's rest position.\n")
859 {
860         float matrix[4][4];
861         char *string;
862         PyObject* pylist;
863         bool    error = false;
864         int row,col;
865         int     mode = 0;       /* 0 for bone space, 1 for armature/world space */
866         
867         if (!PyArg_ParseTuple(args,"sO|i:setChannel", &string, &pylist, &mode))
868                 return NULL;
869         
870         if (pylist->ob_type == &CListValue::Type)
871         {
872                 CListValue* listval = (CListValue*) pylist;
873                 if (listval->GetCount() == 4)
874                 {
875                         for (row=0;row<4;row++) // each row has a 4-vector [x,y,z, w]
876                         {
877                                 CListValue* vecval = (CListValue*)listval->GetValue(row);
878                                 for (col=0;col<4;col++)
879                                 {
880                                         matrix[row][col] = vecval->GetValue(col)->GetNumber();
881                                         
882                                 }
883                         }
884                 }
885                 else
886                 {
887                         error = true;
888                 }
889         }
890         else
891         {
892                 // assert the list is long enough...
893                 int numitems = PyList_Size(pylist);
894                 if (numitems == 4)
895                 {
896                         for (row=0;row<4;row++) // each row has a 4-vector [x,y,z, w]
897                         {
898                                 
899                                 PyObject* veclist = PyList_GetItem(pylist,row); // here we have a vector4 list
900                                 for (col=0;col<4;col++)
901                                 {
902                                         matrix[row][col] =  PyFloat_AsDouble(PyList_GetItem(veclist,col));
903                                         
904                                 }
905                         }
906                 }
907                 else
908                 {
909                         error = true;
910                 }
911         }
912         
913         if (!error)
914         {
915
916 /*      DO IT HERE */
917                 bPoseChannel *pchan= verify_pose_channel(m_userpose, string);
918
919                 Mat4ToQuat(matrix, pchan->quat);
920                 Mat4ToSize(matrix, pchan->size);
921                 VECCOPY (pchan->loc, matrix[3]);
922                 
923                 pchan->flag |= POSE_ROT|POSE_LOC|POSE_SIZE;
924
925                 if (!m_userpose){
926                         m_userpose = (bPose*)MEM_callocN(sizeof(bPose), "userPose");
927                 }
928         }
929         
930         Py_RETURN_NONE;
931 }
932
933 /* ------------------------------------------------------------------------- */
934 /* Python Integration Hooks                                                                      */
935 /* ------------------------------------------------------------------------- */
936
937 PyTypeObject BL_ActionActuator::Type = {
938 #if (PY_VERSION_HEX >= 0x02060000)
939         PyVarObject_HEAD_INIT(NULL, 0)
940 #else
941         /* python 2.5 and below */
942         PyObject_HEAD_INIT( NULL )  /* required py macro */
943         0,                          /* ob_size */
944 #endif
945                 "BL_ActionActuator",
946                 sizeof(PyObjectPlus_Proxy),
947                 0,
948                 py_base_dealloc,
949                 0,
950                 0,
951                 0,
952                 0,
953                 py_base_repr,
954                 0,0,0,0,0,0,
955                 py_base_getattro,
956                 py_base_setattro,
957                 0,0,0,0,0,0,0,0,0,
958                 Methods
959 };
960
961 PyParentObject BL_ActionActuator::Parents[] = {
962         &BL_ActionActuator::Type,
963                 &SCA_IActuator::Type,
964                 &SCA_ILogicBrick::Type,
965                 &CValue::Type,
966                 NULL
967 };
968
969 PyMethodDef BL_ActionActuator::Methods[] = {
970         //Deprecated ----->
971         {"setAction", (PyCFunction) BL_ActionActuator::sPySetAction, METH_VARARGS, (PY_METHODCHAR)SetAction_doc},
972         {"setStart", (PyCFunction) BL_ActionActuator::sPySetStart, METH_VARARGS, (PY_METHODCHAR)SetStart_doc},
973         {"setEnd", (PyCFunction) BL_ActionActuator::sPySetEnd, METH_VARARGS, (PY_METHODCHAR)SetEnd_doc},
974         {"setBlendin", (PyCFunction) BL_ActionActuator::sPySetBlendin, METH_VARARGS, (PY_METHODCHAR)SetBlendin_doc},
975         {"setPriority", (PyCFunction) BL_ActionActuator::sPySetPriority, METH_VARARGS, (PY_METHODCHAR)SetPriority_doc},
976         {"setFrame", (PyCFunction) BL_ActionActuator::sPySetFrame, METH_VARARGS, (PY_METHODCHAR)SetFrame_doc},
977         {"setProperty", (PyCFunction) BL_ActionActuator::sPySetProperty, METH_VARARGS, (PY_METHODCHAR)SetProperty_doc},
978         {"setFrameProperty", (PyCFunction) BL_ActionActuator::sPySetFrameProperty, METH_VARARGS, (PY_METHODCHAR)SetFrameProperty_doc},
979         {"setBlendtime", (PyCFunction) BL_ActionActuator::sPySetBlendtime, METH_VARARGS, (PY_METHODCHAR)SetBlendtime_doc},
980
981         {"getAction", (PyCFunction) BL_ActionActuator::sPyGetAction, METH_VARARGS, (PY_METHODCHAR)GetAction_doc},
982         {"getStart", (PyCFunction) BL_ActionActuator::sPyGetStart, METH_VARARGS, (PY_METHODCHAR)GetStart_doc},
983         {"getEnd", (PyCFunction) BL_ActionActuator::sPyGetEnd, METH_VARARGS, (PY_METHODCHAR)GetEnd_doc},
984         {"getBlendin", (PyCFunction) BL_ActionActuator::sPyGetBlendin, METH_VARARGS, (PY_METHODCHAR)GetBlendin_doc},
985         {"getPriority", (PyCFunction) BL_ActionActuator::sPyGetPriority, METH_VARARGS, (PY_METHODCHAR)GetPriority_doc},
986         {"getFrame", (PyCFunction) BL_ActionActuator::sPyGetFrame, METH_VARARGS, (PY_METHODCHAR)GetFrame_doc},
987         {"getProperty", (PyCFunction) BL_ActionActuator::sPyGetProperty, METH_VARARGS, (PY_METHODCHAR)GetProperty_doc},
988         {"getFrameProperty", (PyCFunction) BL_ActionActuator::sPyGetFrameProperty, METH_VARARGS, (PY_METHODCHAR)GetFrameProperty_doc},
989 //      {"getChannel", (PyCFunction) BL_ActionActuator::sPyGetChannel, METH_VARARGS},
990         {"getType", (PyCFunction) BL_ActionActuator::sPyGetType, METH_VARARGS, (PY_METHODCHAR)GetType_doc},
991         {"setType", (PyCFunction) BL_ActionActuator::sPySetType, METH_VARARGS, (PY_METHODCHAR)SetType_doc},
992         {"getContinue", (PyCFunction) BL_ActionActuator::sPyGetContinue, METH_NOARGS, 0},       
993         {"setContinue", (PyCFunction) BL_ActionActuator::sPySetContinue, METH_O, 0},
994         //<------
995         KX_PYMETHODTABLE(BL_ActionActuator, setChannel),
996         {NULL,NULL} //Sentinel
997 };
998
999 PyAttributeDef BL_ActionActuator::Attributes[] = {
1000         KX_PYATTRIBUTE_FLOAT_RW("frameStart", 0, MAXFRAMEF, BL_ActionActuator, m_startframe),
1001         KX_PYATTRIBUTE_FLOAT_RW("frameEnd", 0, MAXFRAMEF, BL_ActionActuator, m_endframe),
1002         KX_PYATTRIBUTE_FLOAT_RW("blendIn", 0, MAXFRAMEF, BL_ActionActuator, m_blendin),
1003         KX_PYATTRIBUTE_RW_FUNCTION("action", BL_ActionActuator, pyattr_get_action, pyattr_set_action),
1004         KX_PYATTRIBUTE_SHORT_RW("priority", 0, 100, false, BL_ActionActuator, m_priority),
1005         KX_PYATTRIBUTE_FLOAT_RW_CHECK("frame", 0, MAXFRAMEF, BL_ActionActuator, m_localtime, CheckFrame),
1006         KX_PYATTRIBUTE_STRING_RW("propName", 0, 31, false, BL_ActionActuator, m_propname),
1007         KX_PYATTRIBUTE_STRING_RW("framePropName", 0, 31, false, BL_ActionActuator, m_framepropname),
1008         KX_PYATTRIBUTE_BOOL_RW("useContinue", BL_ActionActuator, m_end_reset),
1009         KX_PYATTRIBUTE_FLOAT_RW_CHECK("blendTime", 0, MAXFRAMEF, BL_ActionActuator, m_blendframe, CheckBlendTime),
1010         KX_PYATTRIBUTE_SHORT_RW_CHECK("mode",0,100,false,BL_ActionActuator,m_playtype,CheckType),
1011         { NULL }        //Sentinel
1012 };
1013
1014 PyObject* BL_ActionActuator::py_getattro(PyObject *attr) {
1015         py_getattro_up(SCA_IActuator);
1016 }
1017
1018 PyObject* BL_ActionActuator::py_getattro_dict() {
1019         py_getattro_dict_up(SCA_IActuator);
1020 }
1021
1022 int BL_ActionActuator::py_setattro(PyObject *attr, PyObject* value) {
1023         py_setattro_up(SCA_IActuator);
1024 }
1025
1026
1027 PyObject* BL_ActionActuator::pyattr_get_action(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
1028 {
1029         BL_ActionActuator* self= static_cast<BL_ActionActuator*>(self_v);
1030         return PyString_FromString(self->GetAction() ? self->GetAction()->id.name+2 : "");
1031 }
1032
1033 int BL_ActionActuator::pyattr_set_action(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
1034 {
1035         BL_ActionActuator* self= static_cast<BL_ActionActuator*>(self_v);
1036         
1037         if (!PyString_Check(value))
1038         {
1039                 PyErr_SetString(PyExc_ValueError, "actuator.action = val: Action Actuator, expected the string name of the action");
1040                 return PY_SET_ATTR_FAIL;
1041         }
1042
1043         bAction *action= NULL;
1044         STR_String val = PyString_AsString(value);
1045         
1046         if (val != "")
1047         {
1048                 action= (bAction*)SCA_ILogicBrick::m_sCurrentLogicManager->GetActionByName(val);
1049                 if (!action)
1050                 {
1051                         PyErr_SetString(PyExc_ValueError, "actuator.action = val: Action Actuator, action not found!");
1052                         return PY_SET_ATTR_FAIL;
1053                 }
1054         }
1055         
1056         self->SetAction(action);
1057         return PY_SET_ATTR_SUCCESS;
1058
1059 }