//===========================================================================//
// File:	subsystm.cc                                                         //
// Project: MUNGA           Brick: Entity Manager                            //
// Contents: Interface specification of entity subsystem class               //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 01/24/95  JMA  Initial coding.                                            //
//---------------------------------------------------------------------------//
// Copyright (C) 1995, Virtual World Entertainment, Inc. All Rights reserved //
//                      PROPRIETARY AND CONFIDENTIAL                         //
//===========================================================================//

#include <rp.hpp>
#pragma hdrstop

#if !defined(VTVSUB_HPP)
#	include <vtvsub.hpp>
#endif

#if !defined(VTV_HPP)
#	include <vtv.hpp>
#endif

#if !defined(APP_HPP)
#	include <app.hpp>
#endif

#if !defined(VTVMPPR_HPP)
#	include <vtvmppr.hpp>
#endif

VTVSubsystem::SharedData
	VTVSubsystem::DefaultData(
		VTVSubsystem::ClassDerivations,
		VTVSubsystem::MessageHandlers,
		VTVSubsystem::AttributeIndex,
		VTVSubsystem::StateCount
	);
Derivation
	VTVSubsystem::ClassDerivations(Subsystem::ClassDerivations,"VTVSubsystem");

//#############################################################################
// Messaging Support
//
//const Receiver::HandlerEntry
//	VTVSubsystem::MessageHandlerEntries[]=
//{
//};
//
//Receiver::MessageHandlerSet
//	VTVSubsystem::MessageHandlers(
//		ELEMENTS(VTVSubsystem::MessageHandlerEntries),
//		VTVSubsystem::MessageHandlerEntries,
//		Subsystem::MessageHandlers
//	);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
VTVSubsystem::VTVSubsystem(
	VTV *entity,
	int subsystem_ID,
	SubsystemResource *model,
	SharedData &shared_data,
						// these two fields added 12-28-95 by cpb
	void *control_destination,
	Receiver::MessageID message_id
):
	Subsystem(entity, subsystem_ID, model, shared_data)
{
	controlDestination = control_destination;
	controlMessageID = message_id;
	Check_Fpu();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
VTVSubsystem::~VTVSubsystem()
{
	Check(this);
	Check_Fpu();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Logical
	VTVSubsystem::TestInstance() const
{
	return IsDerivedFrom(ClassDerivations);
}

void
	VTVSubsystem::EnterConfiguration(
		ControlsButton *direct_target,
		Receiver *receiver,
		Receiver::MessageID active_message_id,
		Receiver::MessageID config_message_id
	)
{
	Check(this);

	Check(application);
	ControlsManager
		*controls_mgr = application->GetControlsManager();
	Check(controls_mgr);

	//-------------------------------------------
	// Tell controls mgr that we're configuring
	//-------------------------------------------
	controls_mgr->StartMappableButtonsConfigure(
		direct_target,						// direct mapping (if used)
		receiver,							// targeted receiver
		receiver,							// dependant
		active_message_id,				// activation message ID (if used)
		(ModeMask) 0,						// remove_mode_mask
		(ModeMask) 0						// add_mode_mask
	);

	//-----------------------------------------------------------
	// Create temporary mappings for configuration
	//-----------------------------------------------------------
	Check(GetEntity());

	VTVControlsMapper
		*mapper = (VTVControlsMapper*) GetEntity()->GetSubsystem(
			VTV::ControlsMapperSubsystem
		);

	if (mapper != NULL)
	{
		Check(mapper);

		mapper->CreateTemporaryEventMappings(
			receiver,
			config_message_id
		);
	}
	Check_Fpu();
}

void
	VTVSubsystem::ExitConfiguration()
{
	Check(application);
	ControlsManager
		*controls_mgr = application->GetControlsManager();
	Check(controls_mgr);

	//------------------------------------------------
	// Tell controls mgr we're through
	//------------------------------------------------
	controls_mgr->StopMappableButtonsConfigure(
		(ModeMask) 0,						// remove_mode_mask
		(ModeMask) 0						// add_mode_mask
	);
	//-----------------------------------------------------------
	// Remove temporary mappings for configuration
	//-----------------------------------------------------------
	Check(GetEntity());

	VTVControlsMapper
		*mapper = (VTVControlsMapper*) GetEntity()->GetSubsystem(
			VTV::ControlsMapperSubsystem
		);

	if (mapper != NULL)
	{
		Check(mapper);

		mapper->RemoveTemporaryEventMappings();
	}
	Check_Fpu();
}

