Bugfix [#27157] keyframing a constrained bone does not work as before
[blender.git] / source / gameengine / Ketsji / BL_Action.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Mitchell Stokes.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file BL_Action.cpp
26  *  \ingroup ketsji
27  */
28
29 #include <cstdlib>
30
31 #include "BL_Action.h"
32 #include "BL_ArmatureObject.h"
33 #include "BL_DeformableGameObject.h"
34 #include "BL_ShapeDeformer.h"
35 #include "KX_IpoConvert.h"
36 #include "KX_GameObject.h"
37
38 // These three are for getting the action from the logic manager
39 #include "KX_Scene.h"
40 #include "KX_PythonInit.h"
41 #include "SCA_LogicManager.h"
42
43 extern "C" {
44 #include "BKE_animsys.h"
45 #include "BKE_action.h"
46 #include "RNA_access.h"
47 #include "RNA_define.h"
48 }
49
50 BL_Action::BL_Action(class KX_GameObject* gameobj)
51 :
52         m_obj(gameobj),
53         m_startframe(0.f),
54         m_endframe(0.f),
55         m_blendin(0.f),
56         m_playmode(0),
57         m_endtime(0.f),
58         m_localtime(0.f),
59         m_blendframe(0.f),
60         m_blendstart(0.f),
61         m_speed(0.f),
62         m_priority(0),
63         m_ipo_flags(0),
64         m_pose(NULL),
65         m_blendpose(NULL),
66         m_blendinpose(NULL),
67         m_sg_contr(NULL),
68         m_ptrrna(NULL),
69         m_done(true)
70 {
71         if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
72         {
73                 BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
74
75                 m_ptrrna = new PointerRNA();
76                 RNA_id_pointer_create(&obj->GetArmatureObject()->id, m_ptrrna);
77         }
78         else
79         {
80                 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
81                 BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
82
83                 if (shape_deformer)
84                 {
85                         m_ptrrna = new PointerRNA();
86                         RNA_id_pointer_create(&shape_deformer->GetKey()->id, m_ptrrna);
87                 }
88         }
89 }
90
91 BL_Action::~BL_Action()
92 {
93         if (m_pose)
94                 game_free_pose(m_pose);
95         if (m_blendpose)
96                 game_free_pose(m_blendpose);
97         if (m_blendinpose)
98                 game_free_pose(m_blendinpose);
99         if (m_sg_contr)
100         {
101                 m_obj->GetSGNode()->RemoveSGController(m_sg_contr);
102                 delete m_sg_contr;
103         }
104         if (m_ptrrna)
105                 delete m_ptrrna;
106 }
107
108 bool BL_Action::Play(const char* name,
109                                         float start,
110                                         float end,
111                                         short priority,
112                                         float blendin,
113                                         short play_mode,
114                                         float layer_weight,
115                                         short ipo_flags,
116                                         float playback_speed)
117 {
118
119         // Only start playing a new action if we're done, or if
120         // the new action has a higher priority
121         if (priority != 0 && !IsDone() && priority >= m_priority)
122                 return false;
123         m_priority = priority;
124         bAction* prev_action = m_action;
125
126         // First try to load the action
127         m_action = (bAction*)KX_GetActiveScene()->GetLogicManager()->GetActionByName(name);
128         if (!m_action)
129         {
130                 printf("Failed to load action: %s\n", name);
131                 m_done = true;
132                 return false;
133         }
134
135         if (prev_action != m_action)
136         {
137                 // Create an SG_Controller
138                 m_sg_contr = BL_CreateIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
139                 m_obj->GetSGNode()->AddSGController(m_sg_contr);
140                 m_sg_contr->SetObject(m_obj->GetSGNode());
141         }
142         
143         m_ipo_flags = ipo_flags;
144         InitIPO();
145
146         // Setup blendin shapes/poses
147         if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
148         {
149                 BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
150                 obj->GetMRDPose(&m_blendinpose);
151         }
152         else
153         {
154                 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
155                 BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
156                 
157                 if (shape_deformer)
158                 {
159                         obj->GetShape(m_blendinshape);
160
161                         // Now that we have the previous blend shape saved, we can clear out the key to avoid any
162                         // further interference.
163                         KeyBlock *kb;
164                         for (kb=(KeyBlock*)shape_deformer->GetKey()->block.first; kb; kb=(KeyBlock*)kb->next)
165                                 kb->curval = 0.f;
166                 }
167         }
168
169         // Now that we have an action, we have something we can play
170         m_starttime = KX_GetActiveEngine()->GetFrameTime();
171         m_startframe = m_localtime = start;
172         m_endframe = end;
173         m_blendin = blendin;
174         m_playmode = play_mode;
175         m_endtime = 0.f;
176         m_blendframe = 0.f;
177         m_blendstart = 0.f;
178         m_speed = playback_speed;
179         m_layer_weight = layer_weight;
180         
181         m_done = false;
182
183         return true;
184 }
185
186 void BL_Action::Stop()
187 {
188         m_done = true;
189 }
190
191 bool BL_Action::IsDone()
192 {
193         return m_done;
194 }
195
196 void BL_Action::InitIPO()
197 {
198                 // Initialize the IPO
199                 m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_RESET, true);
200                 m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_IPO_AS_FORCE, m_ipo_flags & ACT_IPOFLAG_FORCE);
201                 m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_IPO_ADD, m_ipo_flags & ACT_IPOFLAG_ADD);
202                 m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_LOCAL, m_ipo_flags & ACT_IPOFLAG_LOCAL);
203 }
204
205 bAction *BL_Action::GetAction()
206 {
207         return (IsDone()) ? NULL : m_action;
208 }
209
210 float BL_Action::GetFrame()
211 {
212         return m_localtime;
213 }
214
215 void BL_Action::SetFrame(float frame)
216 {
217         float dt;
218
219         // Clamp the frame to the start and end frame
220         if (frame < min(m_startframe, m_endframe))
221                 frame = min(m_startframe, m_endframe);
222         else if (frame > max(m_startframe, m_endframe))
223                 frame = max(m_startframe, m_endframe);
224         
225         // We don't set m_localtime directly since it's recalculated
226         // in the next update. So, we modify the value (m_starttime) 
227         // used to calculate m_localtime the next time SetLocalTime() is called.
228
229         dt = frame-m_startframe;
230
231         if (m_endframe < m_startframe)
232                 dt = -dt;
233
234         m_starttime -= dt / (KX_KetsjiEngine::GetAnimFrameRate()*m_speed);
235 }
236
237 void BL_Action::SetLocalTime(float curtime)
238 {
239         float dt = (curtime-m_starttime)*KX_KetsjiEngine::GetAnimFrameRate()*m_speed;
240
241         if (m_endframe < m_startframe)
242                 dt = -dt;
243
244         m_localtime = m_startframe + dt;
245 }
246
247 void BL_Action::IncrementBlending(float curtime)
248 {
249         // Setup m_blendstart if we need to
250         if (m_blendstart == 0.f)
251                 m_blendstart = curtime;
252         
253         // Bump the blend frame
254         m_blendframe = (curtime - m_blendstart)*KX_KetsjiEngine::GetAnimFrameRate();
255
256         // Clamp
257         if (m_blendframe>m_blendin)
258                 m_blendframe = m_blendin;
259 }
260
261
262 void BL_Action::BlendShape(Key* key, float srcweight, std::vector<float>& blendshape)
263 {
264         vector<float>::const_iterator it;
265         float dstweight;
266         KeyBlock *kb;
267         
268         dstweight = 1.0F - srcweight;
269         //printf("Dst: %f\tSrc: %f\n", srcweight, dstweight);
270         for (it=blendshape.begin(), kb = (KeyBlock*)key->block.first; 
271                  kb && it != blendshape.end(); 
272                  kb = (KeyBlock*)kb->next, it++) {
273                 //printf("OirgKeys: %f\t%f\n", kb->curval, (*it));
274                 kb->curval = kb->curval * dstweight + (*it) * srcweight;
275                 //printf("NewKey: %f\n", kb->curval);
276         }
277         //printf("\n");
278 }
279
280 void BL_Action::Update(float curtime)
281 {
282         // Don't bother if we're done with the animation
283         if (m_done)
284                 return;
285
286         curtime -= KX_KetsjiEngine::GetSuspendedDelta();
287         SetLocalTime(curtime);
288
289         // Handle wrap around
290         if (m_localtime < min(m_startframe, m_endframe) || m_localtime > max(m_startframe, m_endframe))
291         {
292                 switch(m_playmode)
293                 {
294                 case ACT_MODE_PLAY:
295                         // Clamp
296                         m_localtime = m_endframe;
297                         m_done = true;
298                         break;
299                 case ACT_MODE_LOOP:
300                         // Put the time back to the beginning
301                         m_localtime = m_startframe;
302                         m_starttime = curtime;
303                         break;
304                 case ACT_MODE_PING_PONG:
305                         // Swap the start and end frames
306                         float temp = m_startframe;
307                         m_startframe = m_endframe;
308                         m_endframe = temp;
309
310                         m_starttime = curtime;
311
312                         break;
313                 }
314
315                 if (!m_done && m_sg_contr)
316                         InitIPO();
317         }
318
319         if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
320         {
321                 BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
322                 obj->GetPose(&m_pose);
323
324                 // Extract the pose from the action
325                 {
326                         Object *arm = obj->GetArmatureObject();
327                         bPose *temp = arm->pose;
328
329                         arm->pose = m_pose;
330                         animsys_evaluate_action(m_ptrrna, m_action, NULL, m_localtime);
331
332                         arm->pose = temp;
333                 }
334
335                 // Handle blending between armature actions
336                 if (m_blendin && m_blendframe<m_blendin)
337                 {
338                         IncrementBlending(curtime);
339
340                         // Calculate weight
341                         float weight = 1.f - (m_blendframe/m_blendin);
342
343                         // Blend the poses
344                         game_blend_poses(m_pose, m_blendinpose, weight);
345                 }
346
347
348                 // Handle layer blending
349                 if (m_layer_weight >= 0)
350                 {
351                         obj->GetMRDPose(&m_blendpose);
352                         game_blend_poses(m_pose, m_blendpose, m_layer_weight);
353                 }
354
355                 obj->SetPose(m_pose);
356
357                 obj->SetActiveAction(NULL, 0, curtime);
358         }
359         else
360         {
361                 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
362                 BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
363
364                 // Handle shape actions if we have any
365                 if (shape_deformer)
366                 {
367                         Key *key = shape_deformer->GetKey();
368
369
370                         animsys_evaluate_action(m_ptrrna, m_action, NULL, m_localtime);
371
372                         // Handle blending between shape actions
373                         if (m_blendin && m_blendframe < m_blendin)
374                         {
375                                 IncrementBlending(curtime);
376
377                                 float weight = 1.f - (m_blendframe/m_blendin);
378
379                                 // We go through and clear out the keyblocks so there isn't any interference
380                                 // from other shape actions
381                                 KeyBlock *kb;
382                                 for (kb=(KeyBlock*)key->block.first; kb; kb=(KeyBlock*)kb->next)
383                                         kb->curval = 0.f;
384
385                                 // Now blend the shape
386                                 BlendShape(key, weight, m_blendinshape);
387                         }
388
389                         // Handle layer blending
390                         if (m_layer_weight >= 0)
391                         {
392                                 obj->GetShape(m_blendshape);
393                                 BlendShape(key, m_layer_weight, m_blendshape);
394                         }
395
396                         obj->SetActiveAction(NULL, 0, curtime);
397                 }
398
399
400                 InitIPO();
401                 m_obj->UpdateIPO(m_localtime, m_ipo_flags & ACT_IPOFLAG_CHILD);
402         }
403 }