| 
									
										
										
										
											2011-10-23 17:52:20 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2011-07-07 03:53:24 +00:00
										 |  |  |  * Contributor(s): Mitchell Stokes. | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-07 03:53:24 +00:00
										 |  |  | /** \file BL_ActionManager.cpp
 | 
					
						
							|  |  |  |  *  \ingroup ketsji | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 23:31:49 +00:00
										 |  |  | #include "BL_Action.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | #include "BL_ActionManager.h"
 | 
					
						
							| 
									
										
										
										
											2015-07-01 16:38:31 +02:00
										 |  |  | #include "DNA_ID.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define IS_TAGGED(_id) ((_id) && (((ID *)_id)->flag & LIB_DOIT))
 | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | BL_ActionManager::BL_ActionManager(class KX_GameObject *obj): | 
					
						
							| 
									
										
										
										
											2015-06-22 16:44:16 +02:00
										 |  |  | 	m_obj(obj), | 
					
						
							|  |  |  | 	m_prevUpdate(-1.0f) | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | BL_ActionManager::~BL_ActionManager() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 	BL_ActionMap::iterator it; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (it = m_layers.begin(); it != m_layers.end(); it++) | 
					
						
							|  |  |  | 		delete it->second; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m_layers.clear(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | BL_Action *BL_ActionManager::GetAction(short layer) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BL_ActionMap::iterator it = m_layers.find(layer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return (it != m_layers.end()) ? it->second : 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | BL_Action* BL_ActionManager::AddAction(short layer) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BL_Action *action = new BL_Action(m_obj); | 
					
						
							|  |  |  | 	m_layers[layer] = action; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return action; | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-01 07:42:40 +00:00
										 |  |  | float BL_ActionManager::GetActionFrame(short layer) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 	BL_Action *action = GetAction(layer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return action ? action->GetFrame() : 0.f; | 
					
						
							| 
									
										
										
										
											2011-06-01 07:42:40 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												BGE: Added getActionName() function to KX_GameObject()
It works similar to getActionFrame(), you have to give a layer or not (for layer 0) as the argument and it returns the name of the animation that the object is currently playing.
Example:
```
import bge
own = bge.logic.getCurrentController().owner
own.playAction("SomeAction",0,20)
print(own.getActionName())
```
>> SomeAction
Here is an example file, just open the blend file with the terminal opened
and press P, you can see how the current animation is being printed:
{F217484}
Reviewers: moguri, hg1, panzergame, campbellbarton
Reviewed By: panzergame
Subscribers: campbellbarton, hg1, #game_engine
Projects: #game_engine
Differential Revision: https://developer.blender.org/D1443
											
										 
											2015-08-03 23:47:46 +02:00
										 |  |  | const char *BL_ActionManager::GetActionName(short layer) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BL_Action *action = GetAction(layer); | 
					
						
							|  |  |  | 	return action ? action->GetName() : ""; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-01 07:42:40 +00:00
										 |  |  | void BL_ActionManager::SetActionFrame(short layer, float frame) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 	BL_Action *action = GetAction(layer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (action) action->SetFrame(frame); | 
					
						
							| 
									
										
										
										
											2011-06-01 07:42:40 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-05 05:22:02 +00:00
										 |  |  | struct bAction *BL_ActionManager::GetCurrentAction(short layer) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 	BL_Action *action = GetAction(layer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return action ? action->GetAction() : 0; | 
					
						
							| 
									
										
										
										
											2011-07-05 05:22:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-08 04:28:30 +00:00
										 |  |  | void BL_ActionManager::SetPlayMode(short layer, short mode) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 	BL_Action *action = GetAction(layer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (action) action->SetPlayMode(mode); | 
					
						
							| 
									
										
										
										
											2011-08-08 04:28:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BL_ActionManager::SetTimes(short layer, float start, float end) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 	BL_Action *action = GetAction(layer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (action) action->SetTimes(start, end); | 
					
						
							| 
									
										
										
										
											2011-08-08 04:28:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-29 01:53:17 +00:00
										 |  |  | bool BL_ActionManager::PlayAction(const char* name, | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  | 								float start, | 
					
						
							|  |  |  | 								float end, | 
					
						
							|  |  |  | 								short layer, | 
					
						
							| 
									
										
										
										
											2011-06-23 22:12:49 +00:00
										 |  |  | 								short priority, | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  | 								float blendin, | 
					
						
							|  |  |  | 								short play_mode, | 
					
						
							| 
									
										
										
										
											2011-07-03 01:59:17 +00:00
										 |  |  | 								float layer_weight, | 
					
						
							| 
									
										
										
										
											2011-06-16 01:18:52 +00:00
										 |  |  | 								short ipo_flags, | 
					
						
							| 
									
										
										
										
											2013-08-14 23:31:49 +00:00
										 |  |  | 								float playback_speed, | 
					
						
							|  |  |  | 								short blend_mode) | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 	// Only this method will create layer if non-existent
 | 
					
						
							|  |  |  | 	BL_Action *action = GetAction(layer); | 
					
						
							|  |  |  | 	if (!action) | 
					
						
							|  |  |  | 		action = AddAction(layer); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-29 02:42:46 +00:00
										 |  |  | 	// Disable layer blending on the first layer
 | 
					
						
							| 
									
										
										
										
											2011-07-03 01:59:17 +00:00
										 |  |  | 	if (layer == 0) layer_weight = -1.f; | 
					
						
							| 
									
										
										
										
											2011-06-29 02:42:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 	return action->Play(name, start, end, priority, blendin, play_mode, layer_weight, ipo_flags, playback_speed, blend_mode); | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BL_ActionManager::StopAction(short layer) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 	BL_Action *action = GetAction(layer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (action) action->Stop(); | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-01 16:38:31 +02:00
										 |  |  | void BL_ActionManager::RemoveTaggedActions() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (BL_ActionMap::iterator it = m_layers.begin(); it != m_layers.end();) { | 
					
						
							|  |  |  | 		if (IS_TAGGED(it->second->GetAction())) { | 
					
						
							|  |  |  | 			delete it->second; | 
					
						
							|  |  |  | 			m_layers.erase(it++); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			++it; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-01 05:46:19 +00:00
										 |  |  | bool BL_ActionManager::IsActionDone(short layer) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 	BL_Action *action = GetAction(layer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return action ? action->IsDone() : true; | 
					
						
							| 
									
										
										
										
											2011-06-01 05:46:19 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  | void BL_ActionManager::Update(float curtime) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-22 16:44:16 +02:00
										 |  |  | 	if (m_prevUpdate == curtime) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	m_prevUpdate = curtime; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 	BL_ActionMap::iterator it; | 
					
						
							|  |  |  | 	for (it = m_layers.begin(); it != m_layers.end(); ) | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 		if (it->second->IsDone()) { | 
					
						
							|  |  |  | 			delete it->second; | 
					
						
							|  |  |  | 			m_layers.erase(it++); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			it->second->Update(curtime); | 
					
						
							|  |  |  | 			++it; | 
					
						
							| 
									
										
										
										
											2011-05-24 07:52:29 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-05-04 15:37:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | void BL_ActionManager::UpdateIPOs() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 	BL_ActionMap::iterator it; | 
					
						
							|  |  |  | 	for (it = m_layers.begin(); it != m_layers.end(); ++it) | 
					
						
							| 
									
										
										
										
											2014-05-04 15:37:18 -07:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-05-07 18:14:36 -07:00
										 |  |  | 		if (!it->second->IsDone()) | 
					
						
							|  |  |  | 			it->second->UpdateIPOs(); | 
					
						
							| 
									
										
										
										
											2014-05-04 15:37:18 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |