00001 #include "OSDLMouse.h"
00002
00003 #include "OSDLController.h"
00004 #include "OSDLMouseCommon.h"
00005
00006 #include "OSDLPoint2D.h"
00007
00008 #include "SDL.h"
00009
00010
00011
00012 using std::string ;
00013
00014
00015 using namespace Ceylan::Log ;
00016
00017 using namespace OSDL ;
00018 using namespace OSDL::Events ;
00019 using namespace OSDL::MVC ;
00020 using namespace OSDL::Video ;
00021 using namespace OSDL::Video::TwoDimensional ;
00022
00023
00024 #ifdef OSDL_USES_CONFIG_H
00025 #include <OSDLConfig.h>
00026 #endif // OSDL_USES_CONFIG_H
00027
00028
00029
00030 #if OSDL_VERBOSE_MOUSE
00031
00032 #include <iostream>
00033 #define OSDL_MOUSE_LOG( message ) std::cout << "[OSDL Mouse] " << message << std::endl ;
00034
00035 #else // OSDL_VERBOSE_MOUSE
00036
00037 #define OSDL_MOUSE_LOG( message )
00038
00039 #endif // OSDL_VERBOSE_MOUSE
00040
00041
00042
00043 const MouseButtonNumber Mouse::DefaultButtonTotalNumber = 5 ;
00044
00045
00046 const MouseButtonNumber Mouse::DefaultButtonActualNumber = 3 ;
00047
00048
00049 const MouseWheelNumber Mouse::DefaultWheelNumber = 1 ;
00050
00051
00052
00053
00054 Mouse::Mouse( MouseNumber index, bool classicalMouseMode ) throw() :
00055 OSDL::Events::InputDevice(),
00056 _index( index ),
00057 _inClassicalMode( classicalMouseMode ),
00058 _buttonTotalCount( DefaultButtonTotalNumber ),
00059 _lastRelativeAbscissa( 0 ),
00060 _lastRelativeOrdinate( 0 )
00061 {
00062
00063 }
00064
00065
00066 Mouse::~Mouse() throw()
00067 {
00068
00069 }
00070
00071
00072
00073
00074
00075
00076 Video::Coordinate Mouse::getCursorAbscissa() const throw()
00077 {
00078
00079 int x ;
00080
00081 SDL_GetMouseState( &x, 0 ) ;
00082
00083 return static_cast<Video::Coordinate>( x ) ;
00084
00085 }
00086
00087
00088 Video::Coordinate Mouse::getCursorOrdinate() const throw()
00089 {
00090
00091 int y ;
00092
00093 SDL_GetMouseState( 0, &y ) ;
00094
00095 return static_cast<Video::Coordinate>( y ) ;
00096
00097 }
00098
00099
00100 void Mouse::setCursorPosition( const Point2D & newPosition ) const throw()
00101 {
00102
00103 setCursorPosition( newPosition.getX(), newPosition.getY() ) ;
00104
00105 }
00106
00107
00108 void Mouse::setCursorPosition( Coordinate x, Coordinate y ) const throw()
00109 {
00110
00111 SDL_WarpMouse( static_cast<Ceylan::Uint16>( x ),
00112 static_cast<Ceylan::Uint16>( y ) ) ;
00113
00114 }
00115
00116
00117
00118 MouseButtonNumber Mouse::getNumberOfButtons() const throw()
00119 {
00120
00121 return DefaultButtonActualNumber ;
00122
00123 }
00124
00125
00126 MouseWheelNumber Mouse::getNumberOfWheels() const throw()
00127 {
00128
00129 return DefaultWheelNumber ;
00130
00131 }
00132
00133
00134
00135
00136 bool Mouse::isLeftButtonPressed() const throw()
00137 {
00138
00139 MouseButtonMask buttons = SDL_GetMouseState( 0, 0 ) ;
00140
00141 return static_cast<bool>( buttons & SDL_BUTTON( 1 ) ) ;
00142
00143 }
00144
00145
00146 bool Mouse::isMiddleButtonPressed() const throw()
00147 {
00148
00149 MouseButtonMask buttons = SDL_GetMouseState( 0, 0 ) ;
00150
00151 return static_cast<bool>( buttons & SDL_BUTTON( 2 ) ) ;
00152
00153 }
00154
00155
00156 bool Mouse::isRightButtonPressed() const throw()
00157 {
00158
00159 MouseButtonMask buttons = SDL_GetMouseState( 0, 0 ) ;
00160
00161 return static_cast<bool>( buttons & SDL_BUTTON( 3 ) ) ;
00162
00163 }
00164
00165
00166 bool Mouse::isButtonPressed( MouseButtonNumber buttonNumber )
00167 const throw( MouseException )
00168 {
00169
00170 MouseButtonMask buttons = SDL_GetMouseState( 0, 0 ) ;
00171
00172 return static_cast<bool>( buttons & SDL_BUTTON( buttonNumber ) ) ;
00173
00174 }
00175
00176
00177 MouseButtonMask Mouse::getButtonStates() const throw()
00178 {
00179
00180 return static_cast<MouseButtonMask>(
00181 SDL_GetMouseState( 0, 0 ) ) ;
00182
00183 }
00184
00185
00186 void Mouse::update() throw()
00187 {
00188
00189 int abscissa, ordinate ;
00190
00191
00192 SDL_GetRelativeMouseState( &abscissa, &ordinate ) ;
00193
00194 _lastRelativeAbscissa = static_cast<Video::Coordinate>( abscissa ) ;
00195 _lastRelativeOrdinate = static_cast<Video::Coordinate>( ordinate ) ;
00196
00197 }
00198
00199
00200 const string Mouse::toString( Ceylan::VerbosityLevels level ) const throw()
00201 {
00202
00203 string res = "Mouse " ;
00204
00205 if ( ! _inClassicalMode )
00206 res += "not in classical mode" ;
00207 else
00208 res += "in classical mode" ;
00209
00210 res += ", with a total of "
00211 + Ceylan::toNumericalString( _buttonTotalCount )
00212 + " buttons (to each wheel correspond two buttons)" ;
00213
00214
00215 return res ;
00216
00217 }
00218
00219
00220 bool Mouse::IsPressed( MouseButtonMask mask, MouseButtonNumber buttonToInspect )
00221 throw()
00222 {
00223
00224 return static_cast<bool>( mask && SDL_BUTTON( buttonToInspect ) ) ;
00225
00226 }
00227
00228
00229
00230
00231
00232
00233
00234 void Mouse::focusGained( const FocusEvent & mouseFocusEvent ) throw()
00235 {
00236
00237 if ( isLinkedToController() )
00238 getActualController().mouseFocusGained( mouseFocusEvent ) ;
00239 else
00240 {
00241 OSDL_MOUSE_LOG( "Focus gained for mouse #"
00242 + Ceylan::toNumericalString( DefaultMouse ) + " : "
00243 + EventsModule::DescribeEvent( mouseFocusEvent ) ) ;
00244 }
00245
00246 }
00247
00248
00249 void Mouse::focusLost( const FocusEvent & mouseFocusEvent ) throw()
00250 {
00251
00252 if ( isLinkedToController() )
00253 getActualController().mouseFocusLost( mouseFocusEvent ) ;
00254 else
00255 {
00256 OSDL_MOUSE_LOG( "Focus lost for mouse #"
00257 + Ceylan::toNumericalString( DefaultMouse ) + " : "
00258 + EventsModule::DescribeEvent( mouseFocusEvent ) ) ;
00259 }
00260
00261 }
00262
00263
00264 void Mouse::mouseMoved( const MouseMotionEvent & mouseEvent ) throw()
00265 {
00266
00267 if ( isLinkedToController() )
00268 getActualController().mouseMoved( mouseEvent ) ;
00269 else
00270 {
00271 OSDL_MOUSE_LOG( "Motion for mouse #"
00272 + Ceylan::toNumericalString( DefaultMouse ) + " : "
00273 + EventsModule::DescribeEvent( mouseEvent ) ) ;
00274 }
00275
00276 }
00277
00278
00279 void Mouse::buttonPressed( const MouseButtonEvent & mouseEvent ) throw()
00280 {
00281
00282 if ( isLinkedToController() )
00283 getActualController().mouseButtonPressed( mouseEvent ) ;
00284 else
00285 {
00286 OSDL_MOUSE_LOG( "Mouse button #"
00287 + Ceylan::toNumericalString( mouseEvent.button ) + " pressed : "
00288 + EventsModule::DescribeEvent( mouseEvent ) ) ;
00289 }
00290
00291 }
00292
00293
00294 void Mouse::buttonReleased( const MouseButtonEvent & mouseEvent ) throw()
00295 {
00296
00297 if ( isLinkedToController() )
00298 getActualController().mouseButtonReleased( mouseEvent ) ;
00299 else
00300 {
00301 OSDL_MOUSE_LOG( "Mouse button #"
00302 + Ceylan::toNumericalString( mouseEvent.button ) + " released : "
00303 + EventsModule::DescribeEvent( mouseEvent ) ) ;
00304 }
00305
00306 }
00307
00308
00309 MouseNumber Mouse::getIndex() const throw()
00310 {
00311
00312 return _index ;
00313
00314 }
00315