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

/*! \file */

#ifndef REVEAL_REAL_5_INPUT_STATE_IMPL_PS2_H
#define REVEAL_REAL_5_INPUT_STATE_IMPL_PS2_H

#ifdef PS2

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

namespace Reveal {
	namespace Impl {

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

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

	int DoMouseRelX() const 
	{ 
		const CONTROLLER_STICKINFO *pad = NULL;
		if ( PAD_active( mPadId ) ) pad = PAD_getstickinfo( mPadId );
		if ( pad && ((pad->rx < 80) || (pad->rx > 176)) ) return (pad->rx - 127) / 10;
		else return 0;
	}

	int	DoMouseRelY() const 
	{ 
		const CONTROLLER_STICKINFO *pad = NULL;
		if ( PAD_active( mPadId ) ) pad = PAD_getstickinfo( mPadId );
		if ( pad && ((pad->ry < 80) || (pad->ry > 176)) ) return (pad->ry - 127) / 10;
		else return 0;
	}

	Reveal::uint32_t DoDown() const 
	{ 
		Reveal::uint32_t down = 0;
		if ( PAD_active( mPadId ) )
		{		
			const CONTROLLER_PRESSUREINFO *pad = NULL;
			if ( PAD_active( mPadId ) ) 
			{
				pad = PAD_getpressureinfo( mPadId );
				if ( pad && pad->l2 ) down |= InputState::AUX_3;
				if ( pad && pad->r2 ) down |= InputState::AUX_4;
			}
			if ( (PAD_state( mPadId ) & PS_CROSS    ) ) down |= InputState::CONFIRM;
			if ( (PAD_state( mPadId ) & PS_CIRCLE   ) ) down |= InputState::BACK;
			if ( (PAD_state( mPadId ) & PS_SQUARE   ) ) down |= InputState::AUX_1;
			if ( (PAD_state( mPadId ) & PS_TRIANGLE ) ) down |= InputState::AUX_2;
			if ( (PAD_state( mPadId ) & PS_LEFT	    ) ) down |= InputState::LEFT;
			if ( (PAD_state( mPadId ) & PS_RIGHT    ) ) down |= InputState::RIGHT;
			if ( (PAD_state( mPadId ) & PS_UP	    ) ) down |= InputState::UP;
			if ( (PAD_state( mPadId ) & PS_DOWN     ) ) down |= InputState::DOWN;
			if ( (PAD_state( mPadId ) & PS_START    ) ) down |= InputState::START;
			if ( (PAD_state( mPadId ) & PS_SELECT   ) ) down |= InputState::SELECT;
			if ( (PAD_state( mPadId ) & PS_L3	    ) ) down |= InputState::AUX_5;
			if ( (PAD_state( mPadId ) & PS_R3	    ) ) down |= InputState::AUX_6;
		}
		return down;
	}

	int DoAuxRelX() const 
	{ 
		const CONTROLLER_STICKINFO *pad = NULL;
		if ( PAD_active( mPadId ) ) pad = PAD_getstickinfo( mPadId );
		if ( pad && ((pad->lx < 80) || (pad->lx> 176)) ) return (pad->lx - 127) / 12;
		else return 0;
	}

	int	DoAuxRelY() const 
	{ 
		const CONTROLLER_STICKINFO *pad = NULL;
		if ( PAD_active( mPadId ) ) pad = PAD_getstickinfo( mPadId );
		if ( pad && ((pad->ly < 80) || (pad->ly > 176)) ) return (pad->ly - 127) / -12;
		else return 0;
	}

	int	DoLeftShoulder() const 
	{ 
		const CONTROLLER_PRESSUREINFO *pad = NULL;
		if ( PAD_active( mPadId ) ) 
		{
			pad = PAD_getpressureinfo( mPadId );
			if ( pad ) return pad->l1;
		}
		return 0;
	}
	
	int	DoRightShoulder() const 
	{ 
		const CONTROLLER_PRESSUREINFO *pad = NULL;
		if ( PAD_active( mPadId ) ) 
		{
			pad = PAD_getpressureinfo( mPadId );
			if ( pad ) return pad->r1;
		}
		return 0;
	}

private:		
	size_t mPadId;
};

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

#endif
#endif
