OSDLClassicalJoystick.cc

Go to the documentation of this file.
00001 #include "OSDLClassicalJoystick.h"       
00002 
00003 #include "OSDLController.h"    // for joystickAxisChanged, etc.
00004 
00005 
00006 using std::string ;
00007 
00008 
00009 using namespace Ceylan::Log ;
00010 
00011 using namespace OSDL::Events ;
00012 
00013 
00014 const AxisPosition ClassicalJoystick::DefaultDeadZoneExtent = 100 ;
00015 
00016 
00017 #ifdef OSDL_USES_CONFIG_H
00018 #include <OSDLConfig.h>        // for OSDL_VERBOSE_JOYSTICK and al 
00019 #endif // OSDL_USES_CONFIG_H
00020 
00021 
00022 
00023 #if OSDL_VERBOSE_JOYSTICK
00024 
00025 #include <iostream>
00026 #define OSDL_JOYSTICK_LOG( message ) std::cout << "[OSDL Joystick] " << message << std::endl ;
00027 
00028 #else // OSDL_VERBOSE_JOYSTICK
00029 
00030 #define OSDL_JOYSTICK_LOG( message )
00031 
00032 #endif // OSDL_VERBOSE_JOYSTICK
00033 
00034 
00035 
00036 /*
00037  * Not used currently since event loop is prefered to polling :
00038  *   - SDL_JoystickUpdate
00039  *   - SDL_JoystickEventState (used in OSDLJoysticksHandler)
00040  *
00041  */
00042  
00043 
00044 ClassicalJoystick::ClassicalJoystick( 
00045         JoystickNumber index, AxisPosition deadZoneExtent ) throw() :
00046     Joystick( index ),
00047     _deadZoneExtentFirstAxis( deadZoneExtent ),
00048     _deadZoneExtentSecondAxis( deadZoneExtent )
00049 {
00050 
00051 }
00052 
00053 
00054 ClassicalJoystick::~ClassicalJoystick() throw()
00055 {
00056             
00057 }
00058 
00059 
00060 void ClassicalJoystick::getDeadZoneValues( AxisPosition & firstAxisExtent, 
00061     AxisPosition & secondAxisExtent ) const throw()
00062 {
00063 
00064     firstAxisExtent  = _deadZoneExtentFirstAxis ;
00065     secondAxisExtent = _deadZoneExtentSecondAxis ;
00066     
00067 }
00068     
00069                     
00070 void ClassicalJoystick::setDeadZoneValues( AxisPosition firstAxisExtent, 
00071     AxisPosition secondAxisExtent ) throw()
00072 {
00073 
00074     _deadZoneExtentFirstAxis  = firstAxisExtent ;
00075     _deadZoneExtentSecondAxis = secondAxisExtent ;
00076     
00077 }
00078     
00079 
00080 const string ClassicalJoystick::toString( Ceylan::VerbosityLevels level ) 
00081     const throw()
00082 {
00083 
00084     return "Classical joystick : " + Joystick::toString( level )
00085         + ". The first axis deadzone extent is "   
00086         + Ceylan::toString( _deadZoneExtentFirstAxis ) 
00087         + ", the second axis deadzone extent is " 
00088         + Ceylan::toString( _deadZoneExtentSecondAxis ) ;
00089                 
00090 }
00091 
00092 
00093 
00094 
00095 // Protected section.
00096 
00097 
00098 void ClassicalJoystick::axisChanged( const JoystickAxisEvent & joystickEvent )
00099     throw()
00100 {
00101     
00102     if ( isLinkedToController() )
00103     {
00104     
00105         // If in deadzone, do not notify the controller.
00106         
00107         if ( joystickEvent.axis == 0 )
00108         {
00109             if ( joystickEvent.value > _deadZoneExtentFirstAxis )
00110                 getActualController().joystickRight( joystickEvent.value ) ;
00111             else
00112             {
00113                 // -1 offset to avoid overflow (range: -32768 to 32767)
00114                 AxisPosition pos = -1 - joystickEvent.value ;
00115                 if ( pos > _deadZoneExtentFirstAxis )
00116                     getActualController().joystickLeft( pos ) ;
00117             }   
00118             
00119             return ;        
00120         }
00121         
00122         
00123         if ( joystickEvent.axis == 1 )
00124         {
00125             if ( joystickEvent.value > _deadZoneExtentSecondAxis )
00126                 getActualController().joystickDown( joystickEvent.value ) ;
00127             else
00128             {
00129                 // -1 offset to avoid overflow (range: -32768 to 32767)
00130                 AxisPosition pos = -1 - joystickEvent.value ;
00131                 if ( pos > _deadZoneExtentSecondAxis )
00132                     getActualController().joystickUp( pos ) ;
00133             }   
00134             
00135             return ;        
00136         }
00137             
00138     }       
00139     else
00140     {
00141         OSDL_JOYSTICK_LOG( 
00142             "Axis changed for controller-less classical joystick #" 
00143             + Ceylan::toNumericalString( joystickEvent.which ) + " : "
00144             + EventsModule::DescribeEvent( joystickEvent ) ) ;
00145     }
00146         
00147 }
00148 
00149 
00150 
00151 void ClassicalJoystick::buttonPressed( 
00152     const JoystickButtonEvent & joystickEvent ) throw()
00153 {
00154     
00155     if ( isLinkedToController() )
00156     {
00157     
00158         switch( joystickEvent.button )
00159         {
00160         
00161             case 0:
00162                 getActualController().joystickFirstButtonPressed() ;
00163                 break ;
00164                 
00165             case 1:
00166                 getActualController().joystickSecondButtonPressed() ;
00167                 break ;
00168             
00169             default:
00170                 OSDL_JOYSTICK_LOG( 
00171                     "Button (neither first or second, hence ignored) "
00172                     "pressed for classical joystick #" 
00173                     + Ceylan::toNumericalString( joystickEvent.which ) + " : "
00174                     + EventsModule::DescribeEvent( joystickEvent ) ) ;
00175                 break ;
00176         }
00177         
00178         
00179     }   
00180     else
00181     {
00182         OSDL_JOYSTICK_LOG( 
00183             "Button pressed for controller-less classical joystick #" 
00184             + Ceylan::toNumericalString( joystickEvent.which ) + " : "
00185             + EventsModule::DescribeEvent( joystickEvent ) ) ;
00186     }
00187             
00188 }
00189 
00190 
00191 
00192 void ClassicalJoystick::buttonReleased( 
00193     const JoystickButtonEvent & joystickEvent ) throw()
00194 {
00195     
00196     if ( isLinkedToController() )
00197     {
00198     
00199         switch( joystickEvent.button )
00200         {
00201         
00202             case 0:
00203                 getActualController().joystickFirstButtonReleased() ;
00204                 break ;
00205                 
00206             case 1:
00207                 getActualController().joystickSecondButtonReleased() ;
00208                 break ;
00209             
00210             default:
00211                 OSDL_JOYSTICK_LOG( 
00212                     "Button (neither first or second, hence ignored) "
00213                     "released for classical joystick #" 
00214                     + Ceylan::toNumericalString( joystickEvent.which ) + " : "
00215                     + EventsModule::DescribeEvent( joystickEvent ) ) ;
00216                 break ;
00217         }
00218         
00219     }   
00220     else
00221     {
00222     
00223         OSDL_JOYSTICK_LOG( 
00224             "Button released for controller-less classical joystick #" 
00225             + Ceylan::toNumericalString( joystickEvent.which ) + " : "
00226             + EventsModule::DescribeEvent( joystickEvent ) ) ;
00227     }
00228             
00229 }
00230 

Generated on Fri Mar 30 14:46:59 2007 for OSDL by  doxygen 1.5.1