00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "SDL/SDLMouse.h"
00024 #include "SDL/SDLInputManager.h"
00025 #include "OISException.h"
00026 #include "OISEvents.h"
00027
00028 using namespace OIS;
00029
00030
00031 SDLMouse::SDLMouse( bool buffered ) : mGrabbed(false), mRegainFocus(false)
00032 {
00033 mBuffered = buffered;
00034 mType = OISMouse;
00035 listener = 0;
00036 }
00037
00038
00039 void SDLMouse::_initialize()
00040 {
00041
00042 mState.clear();
00043 mRegainFocus = false;
00044
00045 _setGrab(true);
00046 _setVisible(false);
00047 static_cast<SDLInputManager*>(InputManager::getSingletonPtr())->_setGrabMode(true);
00048 }
00049
00050
00051 SDLMouse::~SDLMouse()
00052 {
00053 _setGrab(true);
00054 _setVisible(true);
00055
00056 static_cast<SDLInputManager*>(InputManager::getSingletonPtr())->_setGrabMode(false);
00057 }
00058
00059
00060 void SDLMouse::capture()
00061 {
00062
00063 static const MouseButtonID ButtonMask[4] = {MB_Left, MB_Left, MB_Middle, MB_Right};
00064
00065
00066 mState.relX = mState.relY = mState.relZ = 0;
00067
00068 SDL_Event events[OIS_SDL_MOUSE_BUFF];
00069 int count = SDL_PeepEvents(events, OIS_SDL_MOUSE_BUFF, SDL_GETEVENT, SDL_MOUSEEVENTMASK);
00070
00071 bool mouseXYMoved = false;
00072 bool mouseZMoved = false;
00073 for( int i = 0; i < count; ++i )
00074 {
00075 switch( events[i].type )
00076 {
00077 case SDL_MOUSEMOTION: mouseXYMoved = true; break;
00078 case SDL_MOUSEBUTTONDOWN:
00079 {
00080 mRegainFocus = true;
00081 int sdlButton = events[i].button.button;
00082 if( sdlButton <= SDL_BUTTON_RIGHT )
00083 {
00084 mState.buttons |= (1 << ButtonMask[sdlButton]);
00085 if( mBuffered && listener )
00086 if( listener->mousePressed(MouseEvent(this,0,mState), ButtonMask[sdlButton]) == false )
00087 return;
00088 }
00089 else
00090 {
00091 mouseZMoved = true;
00092 if( sdlButton == SDL_BUTTON_WHEELUP )
00093 mState.relZ += 120;
00094 else if( sdlButton == SDL_BUTTON_WHEELDOWN )
00095 mState.relZ -= 120;
00096 }
00097 break;
00098 }
00099 case SDL_MOUSEBUTTONUP:
00100 {
00101 int sdlButton = events[i].button.button;
00102 if( sdlButton <= SDL_BUTTON_RIGHT )
00103 {
00104 mState.buttons &= ~(1 << ButtonMask[sdlButton]);
00105 if( mBuffered && listener )
00106 if( listener->mouseReleased(MouseEvent(this,0,mState), ButtonMask[sdlButton]) == false )
00107 return;
00108 }
00109 break;
00110 }
00111 }
00112 }
00113
00114
00115 if( mouseXYMoved )
00116 {
00117 SDL_GetMouseState( &mState.abX, &mState.abY );
00118 SDL_GetRelativeMouseState( &mState.relX, &mState.relY );
00119
00120 if( mBuffered && listener )
00121 listener->mouseMoved(MouseEvent(this, 0, mState));
00122 }
00123
00124 if( mouseZMoved )
00125 {
00126 mState.abZ += mState.relZ;
00127 if( mBuffered && listener )
00128 listener->mouseMoved(MouseEvent(this, 0, mState));
00129 }
00130
00131
00132 SDLInputManager* man = static_cast<SDLInputManager*>(InputManager::getSingletonPtr());
00133 if( man->_getGrabMode() == false )
00134 {
00135 if( mRegainFocus == false && mGrabbed == true )
00136 {
00137 _setGrab(false);
00138 _setVisible(true);
00139 }
00140 else if( mRegainFocus == true && mGrabbed == false )
00141 {
00142 _setGrab(true);
00143 _setVisible(false);
00144 man->_setGrabMode(true);
00145 }
00146 }
00147 }
00148
00149
00150 void SDLMouse::setBuffered(bool buffered)
00151 {
00152 mBuffered = buffered;
00153 }
00154
00155
00156 void SDLMouse::_setGrab(bool grabbed)
00157 {
00158 if( grabbed )
00159 SDL_WM_GrabInput(SDL_GRAB_ON);
00160 else
00161 SDL_WM_GrabInput(SDL_GRAB_OFF);
00162
00163 mGrabbed = grabbed;
00164 }
00165
00166
00167 void SDLMouse::_setVisible(bool visible)
00168 {
00169
00170 if( visible )
00171 SDL_ShowCursor(SDL_ENABLE);
00172 else
00173 SDL_ShowCursor(SDL_DISABLE);
00174 }