//===========================================================================//
// File:	L4mppr.cc                                                           //
// Project: MUNGA           Brick: Hardware-specific controls module         //
// Contents: Implementation specification for L4 mapper                      //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  -----------------------------------------------------------//
// 06/20/95   JM   Initial Coding								                    //
//---------------------------------------------------------------------------//
// Copyright (C) 1995, Virtual World Entertainment, Inc. All rights reserved //
//                      PROPRIETARY and CONFIDENTIAL                         //
//===========================================================================//

#include <mungal4.hpp>
#pragma hdrstop

#if !defined(L4MPPR_HPP)
#	include <l4mppr.hpp>
#endif

#if !defined(L4CTRL_HPP)
#	include <l4ctrl.hpp>
#endif

#if !defined(L4KEYBD_HPP)
#	include <l4keybd.hpp>
#endif

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

#define PEDAL_SCALE	2.0f

//##########################################################################
//####################          CameraL4Mapper        #######################
//##########################################################################

//#############################################################################
// Shared Data Support
//

CameraL4Mapper::SharedData
	CameraL4Mapper::DefaultData(
		CameraL4Mapper::ClassDerivations,
		CameraL4Mapper::MessageHandlers,
		CameraL4Mapper::AttributeIndex,
		CameraL4Mapper::StateCount
	);
Derivation
	CameraL4Mapper::ClassDerivations(
		CameraControlsMapper::ClassDerivations,
		"CameraL4Mapper"
	);

//#############################################################################
// Messaging Support
//
const Receiver::HandlerEntry
	CameraL4Mapper::MessageHandlerEntries[]=
{
	MESSAGE_ENTRY(CameraL4Mapper, Keypress)
};

CameraL4Mapper::MessageHandlerSet
	CameraL4Mapper::MessageHandlers(
		ELEMENTS(CameraL4Mapper::MessageHandlerEntries),
		CameraL4Mapper::MessageHandlerEntries,
		CameraControlsMapper::MessageHandlers
	);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	CameraL4Mapper::KeypressMessageHandler(
		ReceiverDataMessageOf<ControlsKey> *message
	)
{
	Check(this);
	Check(message);
	LBE4ControlsManager *controls =
		Cast_Object(LBE4ControlsManager*,application->GetControlsManager());

	switch (message->dataContents)
	{
		case PCK_F1:
			Tell("Aligning Joystick...\n");
			controls->BeginAlignJoystick();
			break;
		case PCK_F2:
			Tell("Joystick Aligned...\n");
			controls->EndAlignJoystick();
			break;

		case '>':
			rightPedal = 1.0f;
			break;
		case '<':
			leftPedal = 1.0f;
			break;
		case ' ':
			leftPedal = rightPedal = 0.0f;
			break;

		default:
			CameraControlsMapper::KeypressMessageHandler(message);
			break;
	}
}

//#############################################################################
// Model Support
//
void
	CameraL4Mapper::InterpretControls(Scalar)
{
	//
	//----------------------------------------
	// Sum the pedal inputs
	//----------------------------------------
	//
	pedalsPosition = rightPedal - leftPedal;
	pedalsPosition *= PEDAL_SCALE;
}

//#############################################################################
// Construction and Destruction Support
//

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
CameraL4Mapper::CameraL4Mapper(
		CameraShip *owner,
		int subsystem_ID,
		SubsystemResource *subsystem_resource,
		SharedData &shared_data
	)
	: CameraControlsMapper(owner, subsystem_ID, subsystem_resource, shared_data)
{
	SetPerformance(&CameraL4Mapper::InterpretControls);

	leftPedal = 0.0f;
	rightPedal = 0.0f;

	LBE4ControlsManager *controls =
		Cast_Object(LBE4ControlsManager*,application->GetControlsManager());

	controls
		->joystickGroup[LBE4ControlsManager::JoystickPhysical]
			.Add(ModeManager::ModeAlwaysActive, &stickPosition, this);

	controls
		->buttonGroup[LBE4ControlsManager::ButtonJoystickTrigger]
			.Add(ModeManager::ModeAlwaysActive, &driveCamera, this);

	controls
		->buttonGroup[LBE4ControlsManager::ButtonJoystickThumbHigh]
			.Add(ModeManager::ModeAlwaysActive, &slideOrClamp, this);

	controls
		->buttonGroup[LBE4ControlsManager::ButtonJoystickHatLeft]
			.Add(
				ModeManager::ModeAlwaysActive,
				this,
				DecrementCameraMessageID,
				this
			);

	controls
		->buttonGroup[LBE4ControlsManager::ButtonJoystickHatRight]
			.Add(
				ModeManager::ModeAlwaysActive,
				this,
				IncrementCameraMessageID,
				this
			);

	controls
		->buttonGroup[LBE4ControlsManager::ButtonJoystickPinky]
			.Add(
				ModeManager::ModeAlwaysActive,
				this,
				DeleteCameraInstanceMessageID,
				this
			);

	controls
		->buttonGroup[LBE4ControlsManager::ButtonJoystickThumbLow]
			.Add(
				ModeManager::ModeAlwaysActive,
				this,
				CreateCameraInstanceMessageID,
				this
			);

	controls
		->keyboardGroup[LBE4ControlsManager::KeyboardPC]
			.Add(
				ModeManager::ModeAlwaysActive,
				this,
				KeypressMessageID,
				this
			);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
CameraL4Mapper::~CameraL4Mapper()
{
}

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

//##########################################################################
//####################    CameraThrustmasterMapper   #######################
//##########################################################################

//#############################################################################
// Shared Data Support
//

CameraThrustmasterMapper::SharedData
	CameraThrustmasterMapper::DefaultData(
		CameraThrustmasterMapper::ClassDerivations,
		CameraThrustmasterMapper::MessageHandlers,
		CameraThrustmasterMapper::AttributeIndex,
		CameraThrustmasterMapper::StateCount
	);
Derivation
	CameraThrustmasterMapper::ClassDerivations(
		CameraL4Mapper::ClassDerivations,
		"CameraThrustmasterMapper"
	);

//#############################################################################
// Model Support
//
void
	CameraThrustmasterMapper::InterpretControls(Scalar time_slice)
{
	//
	//----------------------------------------
	// Use the hat buttons to set the throttle
	//----------------------------------------
	//
	if (throttleUp > 0)
	{
		throttlePosition = 1.0f;
		reverseThrust = 0;
	}
	else if (throttleDown > 0)
	{
		throttlePosition = 1.0f;
		reverseThrust = 1;
	}
	else
	{
		throttlePosition = 0.0f;
		reverseThrust = 0;
	}

	CameraL4Mapper::InterpretControls(time_slice);
}

//#############################################################################
// Construction and Destruction Support
//

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
CameraThrustmasterMapper::CameraThrustmasterMapper(
		CameraShip *owner,
		int subsystem_ID,
		SubsystemResource *subsystem_resource,
		SharedData &shared_data
	)
	: CameraL4Mapper(owner, subsystem_ID, subsystem_resource, shared_data)
{
	SetPerformance(&CameraThrustmasterMapper::InterpretControls);

	throttleUp = throttleDown = 0;

	LBE4ControlsManager *controls =
		Cast_Object(LBE4ControlsManager*,application->GetControlsManager());

	controls
		->buttonGroup[LBE4ControlsManager::ButtonJoystickHatUp]
			.Add(ModeManager::ModeAlwaysActive, &throttleUp, this);

	controls
		->buttonGroup[LBE4ControlsManager::ButtonJoystickHatDown]
			.Add(ModeManager::ModeAlwaysActive, &throttleDown, this);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
CameraThrustmasterMapper::~CameraThrustmasterMapper()
{
}

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

//#############################################################################
//################################ RIOMapper ##################################
//#############################################################################

//#############################################################################
// Shared Data Support
//
CameraRIOMapper::SharedData
	CameraRIOMapper::DefaultData(
		CameraRIOMapper::ClassDerivations,
		CameraRIOMapper::MessageHandlers,
		CameraRIOMapper::AttributeIndex,
		CameraRIOMapper::StateCount
	);

Derivation
	CameraRIOMapper::ClassDerivations(
		CameraL4Mapper::ClassDerivations,
		"CameraRIOMapper"
	);

//#############################################################################
// Construction and Destruction Support
//

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
CameraRIOMapper::CameraRIOMapper(
	CameraShip *owner,
	int subsystem_ID,
	SubsystemResource *subsystem_resource,
	SharedData &shared_data
):
	CameraL4Mapper(owner, subsystem_ID, subsystem_resource, shared_data)
{
	LBE4ControlsManager *controls =
		Cast_Object(LBE4ControlsManager*,application->GetControlsManager());

	//
	//-----------------
	// Throttle mapping
	//-----------------
	//
	controls
		->scalarGroup[LBE4ControlsManager::ScalarThrottle]
			.Add(ModeManager::ModeAlwaysActive, &throttlePosition, this);

	controls
		->buttonGroup[LBE4ControlsManager::ButtonThrottle1]
			.Add(ModeManager::ModeAlwaysActive, &reverseThrust, this);

	//
	//--------------
	// Pedal mapping
	//--------------
	//
	controls
		->scalarGroup[LBE4ControlsManager::ScalarLeftPedal]
			.Add(ModeManager::ModeAlwaysActive, &leftPedal, this);

	controls
		->scalarGroup[LBE4ControlsManager::ScalarRightPedal]
			.Add(ModeManager::ModeAlwaysActive, &rightPedal, this);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
CameraRIOMapper::~CameraRIOMapper()
{
}

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

