Fix for bug #28979 "Action actuator breaks animation" reported by Goran Milovanovic...
[blender.git] / source / gameengine / Ketsji / BL_Action.cpp
index dec34289bbb32cbe7501d1c8b5b7216cee30b44f..9e8f53ad21980c17712689d60b6188f1c2423e7a 100644 (file)
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
- * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
- * All rights reserved.
- *
- * The Original Code is: all of this file.
- *
- * Contributor(s): none yet.
+ * Contributor(s): Mitchell Stokes.
  *
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file BL_Action.cpp
+ *  \ingroup ketsji
+ */
+
 #include <cstdlib>
 
 #include "BL_Action.h"
@@ -46,29 +45,29 @@ extern "C" {
 #include "BKE_action.h"
 #include "RNA_access.h"
 #include "RNA_define.h"
-#include "DNA_nla_types.h"
 }
 
 BL_Action::BL_Action(class KX_GameObject* gameobj)
 :
+       m_action(NULL),
+       m_pose(NULL),
+       m_blendpose(NULL),
+       m_blendinpose(NULL),
+       m_ptrrna(NULL),
        m_obj(gameobj),
        m_startframe(0.f),
        m_endframe(0.f),
-       m_blendin(0.f),
-       m_playmode(0),
        m_endtime(0.f),
        m_localtime(0.f),
+       m_blendin(0.f),
        m_blendframe(0.f),
        m_blendstart(0.f),
        m_speed(0.f),
        m_priority(0),
+       m_playmode(0),
        m_ipo_flags(0),
-       m_pose(NULL),
-       m_blendpose(NULL),
-       m_sg_contr(NULL),
-       m_ptrrna(NULL),
        m_done(true),
-       m_bcalc_local_time(true)
+       m_calc_localtime(true)
 {
        if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
        {
@@ -96,13 +95,24 @@ BL_Action::~BL_Action()
                game_free_pose(m_pose);
        if (m_blendpose)
                game_free_pose(m_blendpose);
-       if (m_sg_contr)
-       {
-               m_obj->GetSGNode()->RemoveSGController(m_sg_contr);
-               delete m_sg_contr;
-       }
+       if (m_blendinpose)
+               game_free_pose(m_blendinpose);
        if (m_ptrrna)
                delete m_ptrrna;
+       ClearControllerList();
+}
+
+void BL_Action::ClearControllerList()
+{
+       // Clear out the controller list
+       std::vector<SG_Controller*>::iterator it;
+       for (it = m_sg_contr_list.begin(); it != m_sg_contr_list.end(); it++)
+       {
+               m_obj->GetSGNode()->RemoveSGController((*it));
+               delete *it;
+       }
+
+       m_sg_contr_list.clear();
 }
 
 bool BL_Action::Play(const char* name,
@@ -111,7 +121,7 @@ bool BL_Action::Play(const char* name,
                                        short priority,
                                        float blendin,
                                        short play_mode,
-                                       short blend_mode,
+                                       float layer_weight,
                                        short ipo_flags,
                                        float playback_speed)
 {
@@ -134,10 +144,30 @@ bool BL_Action::Play(const char* name,
 
        if (prev_action != m_action)
        {
+               // First get rid of any old controllers
+               ClearControllerList();
+
                // Create an SG_Controller
-               m_sg_contr = BL_CreateIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
-               m_obj->GetSGNode()->AddSGController(m_sg_contr);
-               m_sg_contr->SetObject(m_obj->GetSGNode());
+               SG_Controller *sg_contr = BL_CreateIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
+               m_sg_contr_list.push_back(sg_contr);
+               m_obj->GetSGNode()->AddSGController(sg_contr);
+               sg_contr->SetObject(m_obj->GetSGNode());
+
+               // Extra controllers
+               if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_LIGHT)
+               {
+                       sg_contr = BL_CreateLampIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
+                       m_sg_contr_list.push_back(sg_contr);
+                       m_obj->GetSGNode()->AddSGController(sg_contr);
+                       sg_contr->SetObject(m_obj->GetSGNode());
+               }
+               else if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_CAMERA)
+               {
+                       sg_contr = BL_CreateCameraIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
+                       m_sg_contr_list.push_back(sg_contr);
+                       m_obj->GetSGNode()->AddSGController(sg_contr);
+                       sg_contr->SetObject(m_obj->GetSGNode());
+               }
        }
        
        m_ipo_flags = ipo_flags;
@@ -146,25 +176,24 @@ bool BL_Action::Play(const char* name,
        // Setup blendin shapes/poses
        if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
        {
-               if (!m_blendpose)
-               {
-                       BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
-                       obj->GetMRDPose(&m_blendpose);
-               }
+               BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
+               obj->GetMRDPose(&m_blendinpose);
        }
        else
        {
                BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
                BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
                
-               obj->GetShape(m_blendshape);
-
-               // Now that we have the previous blend shape saved, we can clear out the key to avoid any
-               // further interference.
-               KeyBlock *kb;
-               for (kb=(KeyBlock*)shape_deformer->GetKey()->block.first; kb; kb=(KeyBlock*)kb->next)
-                       kb->curval = 0.f;
+               if (shape_deformer && shape_deformer->GetKey())
+               {
+                       obj->GetShape(m_blendinshape);
 
+                       // Now that we have the previous blend shape saved, we can clear out the key to avoid any
+                       // further interference.
+                       KeyBlock *kb;
+                       for (kb=(KeyBlock*)shape_deformer->GetKey()->block.first; kb; kb=(KeyBlock*)kb->next)
+                               kb->curval = 0.f;
+               }
        }
 
        // Now that we have an action, we have something we can play
@@ -173,11 +202,11 @@ bool BL_Action::Play(const char* name,
        m_endframe = end;
        m_blendin = blendin;
        m_playmode = play_mode;
-       m_blendmode = blend_mode;
        m_endtime = 0.f;
        m_blendframe = 0.f;
        m_blendstart = 0.f;
        m_speed = playback_speed;
+       m_layer_weight = layer_weight;
        
        m_done = false;
 
@@ -196,11 +225,20 @@ bool BL_Action::IsDone()
 
 void BL_Action::InitIPO()
 {
-               // Initialize the IPO
-               m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_RESET, true);
-               m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_IPO_AS_FORCE, m_ipo_flags & ACT_IPOFLAG_FORCE);
-               m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_IPO_ADD, m_ipo_flags & ACT_IPOFLAG_ADD);
-               m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_LOCAL, m_ipo_flags & ACT_IPOFLAG_LOCAL);
+       // Initialize the IPOs
+       std::vector<SG_Controller*>::iterator it;
+       for (it = m_sg_contr_list.begin(); it != m_sg_contr_list.end(); it++)
+       {
+               (*it)->SetOption(SG_Controller::SG_CONTR_IPO_RESET, true);
+               (*it)->SetOption(SG_Controller::SG_CONTR_IPO_IPO_AS_FORCE, m_ipo_flags & ACT_IPOFLAG_FORCE);
+               (*it)->SetOption(SG_Controller::SG_CONTR_IPO_IPO_ADD, m_ipo_flags & ACT_IPOFLAG_ADD);
+               (*it)->SetOption(SG_Controller::SG_CONTR_IPO_LOCAL, m_ipo_flags & ACT_IPOFLAG_LOCAL);
+       }
+}
+
+bAction *BL_Action::GetAction()
+{
+       return (IsDone()) ? NULL : m_action;
 }
 
 float BL_Action::GetFrame()
@@ -215,9 +253,20 @@ void BL_Action::SetFrame(float frame)
                frame = min(m_startframe, m_endframe);
        else if (frame > max(m_startframe, m_endframe))
                frame = max(m_startframe, m_endframe);
-
+       
        m_localtime = frame;
-       m_bcalc_local_time = false;
+       m_calc_localtime = false;
+}
+
+void BL_Action::SetPlayMode(short play_mode)
+{
+       m_playmode = play_mode;
+}
+
+void BL_Action::SetTimes(float start, float end)
+{
+       m_startframe = start;
+       m_endframe = end;
 }
 
 void BL_Action::SetLocalTime(float curtime)
@@ -230,6 +279,14 @@ void BL_Action::SetLocalTime(float curtime)
        m_localtime = m_startframe + dt;
 }
 
+void BL_Action::ResetStartTime(float curtime)
+{
+       float dt = m_localtime - m_startframe;
+
+       m_starttime = curtime - dt / (KX_KetsjiEngine::GetAnimFrameRate()*m_speed);
+       SetLocalTime(curtime);
+}
+
 void BL_Action::IncrementBlending(float curtime)
 {
        // Setup m_blendstart if we need to
@@ -245,7 +302,7 @@ void BL_Action::IncrementBlending(float curtime)
 }
 
 
-void BL_Action::BlendShape(Key* key, float srcweight)
+void BL_Action::BlendShape(Key* key, float srcweight, std::vector<float>& blendshape)
 {
        vector<float>::const_iterator it;
        float dstweight;
@@ -253,8 +310,8 @@ void BL_Action::BlendShape(Key* key, float srcweight)
        
        dstweight = 1.0F - srcweight;
        //printf("Dst: %f\tSrc: %f\n", srcweight, dstweight);
-       for (it=m_blendshape.begin(), kb = (KeyBlock*)key->block.first; 
-                kb && it != m_blendshape.end(); 
+       for (it=blendshape.begin(), kb = (KeyBlock*)key->block.first; 
+                kb && it != blendshape.end(); 
                 kb = (KeyBlock*)kb->next, it++) {
                //printf("OirgKeys: %f\t%f\n", kb->curval, (*it));
                kb->curval = kb->curval * dstweight + (*it) * srcweight;
@@ -269,15 +326,14 @@ void BL_Action::Update(float curtime)
        if (m_done)
                return;
 
-       // We only want to calculate the current time if we weren't given a frame (e.g., from SetFrame())
-       if (m_bcalc_local_time)
-       {
-               curtime -= KX_KetsjiEngine::GetSuspendedDelta();
+       curtime -= KX_KetsjiEngine::GetSuspendedDelta();
+
+       if (m_calc_localtime)
                SetLocalTime(curtime);
-       }
        else
        {
-               m_bcalc_local_time = true;
+               ResetStartTime(curtime);
+               m_calc_localtime = true;
        }
 
        // Handle wrap around
@@ -305,9 +361,6 @@ void BL_Action::Update(float curtime)
 
                        break;
                }
-
-               if (!m_done && m_sg_contr)
-                       InitIPO();
        }
 
        if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
@@ -335,15 +388,15 @@ void BL_Action::Update(float curtime)
                        float weight = 1.f - (m_blendframe/m_blendin);
 
                        // Blend the poses
-                       game_blend_poses(m_pose, m_blendpose, weight, ACTSTRIPMODE_BLEND);
+                       game_blend_poses(m_pose, m_blendinpose, weight);
                }
 
 
                // Handle layer blending
-               if (m_blendmode != ACT_BLEND_NONE)
+               if (m_layer_weight >= 0)
                {
                        obj->GetMRDPose(&m_blendpose);
-                       game_blend_poses(m_pose, m_blendpose, 1.f, ACTSTRIPMODE_ADD);
+                       game_blend_poses(m_pose, m_blendpose, m_layer_weight);
                }
 
                obj->SetPose(m_pose);
@@ -356,7 +409,7 @@ void BL_Action::Update(float curtime)
                BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
 
                // Handle shape actions if we have any
-               if (shape_deformer)
+               if (shape_deformer && shape_deformer->GetKey())
                {
                        Key *key = shape_deformer->GetKey();
 
@@ -377,14 +430,19 @@ void BL_Action::Update(float curtime)
                                        kb->curval = 0.f;
 
                                // Now blend the shape
-                               BlendShape(key, weight);
+                               BlendShape(key, weight, m_blendinshape);
                        }
 
-                       obj->SetActiveAction(NULL, 0, m_localtime);
-               }
+                       // Handle layer blending
+                       if (m_layer_weight >= 0)
+                       {
+                               obj->GetShape(m_blendshape);
+                               BlendShape(key, m_layer_weight, m_blendshape);
+                       }
 
+                       obj->SetActiveAction(NULL, 0, curtime);
+               }
 
-               InitIPO();
                m_obj->UpdateIPO(m_localtime, m_ipo_flags & ACT_IPOFLAG_CHILD);
        }
 }