// -----------------------------------------------------------------------
/*
	\author: Jaap Suter
   
	Copyright (C) Electronic Arts Canada Inc. 2002.  All rights reserved.	
*/
// -----------------------------------------------------------------------

/*! \file */

#ifndef REVEAL_REAL_5_INPUT_STATE_IMPL_XBOX_H
#define REVEAL_REAL_5_INPUT_STATE_IMPL_XBOX_H

#ifdef XBOX

#include <stddef.h>
#include <assert.h>
#include <realinput.h>

namespace Reveal {
	namespace Impl {

// -----------------------------------------------------------------------
/*!
	\brief Simple Real 5 implementation of the InputState 
		   class for the Xbox
*/
// -----------------------------------------------------------------------
class RealInputImpl
{
public:

	RealInputImpl( size_t padId ) : mPadId( padId ) {}

	int DoMouseRelX() const 
	{ 
		int x = 0;
		if ( (mPadId != 0) || (!x && PAD_getdataptr( mPadId )) )
		{
			x = static_cast< int >( PAD_getdataptr( mPadId )->fRX * 30 );
			if ( (x < 4) && (x > -4) )  x = 0;
		}
		return x;
	}

	int	DoMouseRelY() const 
	{ 
		int y = 0;
		if ( (mPadId != 0) || (!y && PAD_getdataptr( mPadId )) )
		{
			y = static_cast< int >( PAD_getdataptr( mPadId )->fRY * -30 );
			if ( (y < 4) && (y > -4) ) y = 0;
		}
		return y;
	}

	Reveal::uint32_t DoDown() const 
	{ 
		Reveal::uint32_t down = 0;
		if ( PAD_getdataptr( mPadId ) )
		{
			if ( PAD_getdataptr( mPadId )->bLastAnalogButtons[ XINPUT_GAMEPAD_A ] ) down |= InputState::CONFIRM;
			if ( PAD_getdataptr( mPadId )->bLastAnalogButtons[ XINPUT_GAMEPAD_B ] ) down |= InputState::BACK;
			if ( PAD_getdataptr( mPadId )->bLastAnalogButtons[ XINPUT_GAMEPAD_X ] ) down |= InputState::AUX_1;
			if ( PAD_getdataptr( mPadId )->bLastAnalogButtons[ XINPUT_GAMEPAD_Y ] ) down |= InputState::AUX_2;
			if ( PAD_getdataptr( mPadId )->bLastAnalogButtons[ XINPUT_GAMEPAD_WHITE ] ) down |= InputState::AUX_3;
			if ( PAD_getdataptr( mPadId )->bLastAnalogButtons[ XINPUT_GAMEPAD_BLACK ] ) down |= InputState::AUX_4;

			if ( PAD_getdataptr( mPadId )->wLastButtons & XINPUT_GAMEPAD_START		 ) down |= InputState::START;
			if ( PAD_getdataptr( mPadId )->wLastButtons & XINPUT_GAMEPAD_BACK		 ) down |= InputState::SELECT;
			if ( PAD_getdataptr( mPadId )->wLastButtons & XINPUT_GAMEPAD_LEFT_THUMB  ) down |= InputState::AUX_5;
			if ( PAD_getdataptr( mPadId )->wLastButtons & XINPUT_GAMEPAD_RIGHT_THUMB ) down |= InputState::AUX_6;
			if ( PAD_getdataptr( mPadId )->wLastButtons & XINPUT_GAMEPAD_DPAD_UP	 ) down |= InputState::UP;
			if ( PAD_getdataptr( mPadId )->wLastButtons & XINPUT_GAMEPAD_DPAD_DOWN  ) down |= InputState::DOWN;
			if ( PAD_getdataptr( mPadId )->wLastButtons & XINPUT_GAMEPAD_DPAD_LEFT  ) down |= InputState::LEFT;
			if ( PAD_getdataptr( mPadId )->wLastButtons & XINPUT_GAMEPAD_DPAD_RIGHT ) down |= InputState::RIGHT;
		}
		return down;
	}

	int DoAuxRelX() const 
	{ 
		int x = 0;
		if ( PAD_getdataptr( mPadId ) )
		{
			x = static_cast< int >( PAD_getdataptr( mPadId )->fLX * 20 );
			if ( (x < 4) && (x > -4) ) x = 0;
		}
		return x;
	}

	int	DoAuxRelY() const 
	{ 
		int y = 0;
		if ( PAD_getdataptr( mPadId ) )
		{
			y = static_cast< int >( PAD_getdataptr( mPadId )->fLY * 20 );
			if ( (y < 4) && (y > -4) ) y = 0;
		}
		return y;
	}

	int	DoLeftShoulder() const 
	{ 
		if ( PAD_getdataptr( mPadId ) ) 
			return static_cast< int >( PAD_getdataptr( mPadId )->bLastAnalogButtons[ XINPUT_GAMEPAD_LEFT_TRIGGER ] );
		else 
			return 0;
	}
	
	int	DoRightShoulder() const 
	{ 
		if ( PAD_getdataptr( mPadId ) ) 
			return static_cast< int >( PAD_getdataptr( mPadId )->bLastAnalogButtons[ XINPUT_GAMEPAD_RIGHT_TRIGGER ] );
		else 
			return 0;
	}

private:		
	size_t mPadId;
};

	} // Namespace Impl;
} // Namespace Reveal;

#endif
#endif
