/*****************************************************************************
 * $Id: game_base_joystick.hh,v 1.7 2006/08/03 02:17:21 devel Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Joystick functions (OS-independent).
 *//*
 * LEGAL:	COPYRIGHT (C) 2004 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 *****************************************************************************/

#if COMPILING_GAME
#ifndef GAME_BASE_JOYSTICK_HH
#define GAME_BASE_JOYSTICK_HH 1

#include "joystick.hh"
using namespace gfxInput;

enum eJoyMove
{
	eJoyMove_AT_CENTER,
	eJoyMove_PITCH_UP,
	eJoyMove_PITCH_DOWN,
	eJoyMove_ROLL_LEFT,
	eJoyMove_ROLL_RIGHT,
	eJoyMove_YAW_LEFT,
	eJoyMove_YAW_RIGHT,
	eJoyMove_THROTTLE_SLOW,
	eJoyMove_THROTTLE_FAST
};

/// Config file stores these exact values.
enum eJoyAxis3
{
	eJoyAxis3_DISABLED,
	eJoyAxis3_THROTTLE,
	eJoyAxis3_RUDDER
};

////////////////////////////////////////////////////////////////////////////////
/// @brief For recording joystick values.  Used for calibration.
class Joyval
{
// copying allowed
public:
	Joyval( void )
	{
		Reset();
	}
	void Reset( void )
	{
		mUp = mDown = mLeft = mRight = mSlow = mFast = eJoyval_INVALID;
	}
	enum	{ eJoyval_INVALID = -99999 };
	int		mUp;
	int		mDown;
	int		mLeft;
	int		mRight;
	int		mSlow;	// axis3
	int		mFast;
};

////////////////////////////////////////////////////////////////////////////////
/// @brief GameJoystick class is built over the Joystick class of the gfx input library.
class GameJoystick : public Joystick
{
	PREVENT_COPYING(GameJoystick)
private:
	typedef Joystick Parent;

public:
	GameJoystick( void )
	{
		mAxis3		= eJoyAxis3_THROTTLE;
		mAutofire	= false;
		mPrintJoystick = false;
		mCalibratingJoystick = false;
		mResponseScale	= 2.33;		// init before mResponse[]
		InitArray( mResponse,      ELEMS(mResponse),      mResponseScale ); // for each axis
		InitArray( mResponseSaved, ELEMS(mResponseSaved), mResponseScale );
	}
	bool			PollJoystick( void );
	void			SetJoystickResponse( float r0, float r1, float r2 );
	void			DefaultJoystickResponse( void );
	void			ScaleJoystickResponse( float s0, float s1, float s2 );
	void			GetJoystickResponse( float* r0, float* r1, float* r2 );
	bool			SetJoystickAxis3( eJoyAxis3 a );
	eJoyAxis3		GetJoystickAxis3( void );
	void			EnableJoystickAutofire( bool f );
	void			EnablePrintJoystick( bool f );
	bool			IfPrintJoystick( void );
	void			PrintJoystickRaw( int value, eJoyMove joyMove );
	void			RecordJoystickRaw( int value, eJoyMove joyMove );
	bool			IfJoystickCalibrationMode( void );
	void			SetJoystickCalibrationMode( bool f );
	const Joyval&	GetJoyval( void ) { return mJoyval; }		// used for calibration
	void			ResetJoyval( void ) { mJoyval.Reset(); }	// used for calibration
private:
	bool			IfJoyEye( void );
	bool			IsJoyBeyondCenter( int lo );
	float			JoystickPositionAbs( int high );

	// Joystick objects based on gfx input library.
	Joystick	mJoy;
	Joyval		mJoyval;

	// Joystick state.
	JoyEvent	mJoyEvent;		// dequeued from input queue (don't confuse with Joystick::mEvent)
	eJoyAxis3	mAxis3;
	bool		mAutofire;
	bool		mPrintJoystick;

	// Joystick calibration.
	bool		mCalibratingJoystick;

	// Amplifies/diminishes how software responds to joystick movement.
	// One response value for each of 3 joystick axises.
	float		mResponseScale;
	float		mResponse[3];
	float		mResponseSaved[3];
};
extern GameJoystick gJoy;

#endif // GAME_BASE_JOYSTICK_HH
#endif // COMPILING_GAME
