/*****************************************************************************
 * $Id: mod_base_joystick.hh,v 1.1 2006/11/24 21:56:57 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_MODULE
#ifndef MOD_BASE_JOYSTICK_HH
#define MOD_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 ModuleJoystick class is built over the Joystick class of the gfx input library.
class ModuleJoystick : public Joystick
{
	PREVENT_COPYING(ModuleJoystick)
private:
	typedef Joystick Parent;

public:
	ModuleJoystick( void )
	:	mResponseDefault(2.33)  // init before mResponse[]
	{
		mAxis3		= eJoyAxis3_THROTTLE;
		mAutofire	= false;
		mPrintJoystick = false;
		mCalibratingJoystick = false;
		InitArray( mResponse,      ELEMS(mResponse),      mResponseDefault ); // for each axis
		InitArray( mResponseSaved, ELEMS(mResponseSaved), mResponseDefault );
	}
	bool			PollJoystick( void );
	void			SetJoystickResponse( float r0, float r1, float r2 );
	void			DefaultJoystickResponse( void );
	void			ScaleJoystickResponse( float s0, float s1, float s2 );
	void			ScaleJoystickResponse( float s );
	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.
	const float		mResponseDefault;  // singular
	float			mResponse[3];
	float			mResponseSaved[3];
};
extern ModuleJoystick gJoy;

#endif // MOD_BASE_JOYSTICK_HH
#endif // COMPILING_MOD
