//===========================================================================//
// File:	AnimationTrigger.cpp                                                 
// Project: MechWarrior 4
// Contents: 
//			Helper class to deal with animation triggers
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 07/01/98  JSE  Initial coding, 
//---------------------------------------------------------------------------//
// Copyright (C) 1998, Fasa Interactive, Inc.
// All Rights reserved worldwide
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL
//===========================================================================//

#include "MW4Headers.hpp"
#include "AnimationTrigger.hpp"

//##########################################################################

AnimationTriggerManager::AnimationTriggerManager():
	Plug(DefaultData),
	animationTriggers(NULL, true)
{
}

//##########################################################################
	
AnimationTriggerManager::~AnimationTriggerManager()
{
	if(!animationTriggers.IsEmpty())
		animationTriggers.DeletePlugs();
}

//##########################################################################
	
void AnimationTriggerManager::RegisterTriggerCallback(int joint_number, int trigger_number, TriggerMessageHandler trigger_message_handler)
{
	Verify(trigger_number >= 0);
	Verify(trigger_number < 16);
	Verify(joint_number >= 0);

	TriggerJointValue value(joint_number, trigger_number);

	AnimationTrigger *trigger = new AnimationTrigger(trigger_message_handler);
	Register_Object(trigger);

	animationTriggers.AddValue(trigger, value);

}

//##########################################################################
	
void AnimationTriggerManager::DispatchTriggerMessage(int joint_number, int trigger_values, void *pass_data)
{
	SortedChainIteratorOf<AnimationTrigger *, TriggerJointValue> iterator(&animationTriggers);

	for (int i = 0; i < 16; ++i)
	{
		
		int flag = trigger_values & 3;
		trigger_values = trigger_values >> 2;

		int flag_number = 15 - i;

		TriggerJointValue value(joint_number, flag_number);

		AnimationTrigger *trigger = iterator.Find(value);

		if (trigger != NULL)
		{
			Check_Object(trigger);

			// dispatch

			(*trigger->triggerMessageHandler)(flag, pass_data);
			
		}

	}
}

