E:/Download/ois-1.0RC1/src/SDL/SDLMouse.cpp

Go to the documentation of this file.
00001 /*
00002 The zlib/libpng License
00003 
00004 Copyright (c) 2006 Phillip Castaneda (pjcast -- www.wreckedgames.com)
00005 
00006 This software is provided 'as-is', without any express or implied warranty. In no event will
00007 the authors be held liable for any damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any purpose, including commercial 
00010 applications, and to alter it and redistribute it freely, subject to the following
00011 restrictions:
00012 
00013     1. The origin of this software must not be misrepresented; you must not claim that 
00014                 you wrote the original software. If you use this software in a product, 
00015                 an acknowledgment in the product documentation would be appreciated but is 
00016                 not required.
00017 
00018     2. Altered source versions must be plainly marked as such, and must not be 
00019                 misrepresented as being the original software.
00020 
00021     3. This notice may not be removed or altered from any source distribution.
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         //Clear old state
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         //Used for going from SDL Button to OIS button
00063         static const MouseButtonID ButtonMask[4] = {MB_Left, MB_Left, MB_Middle, MB_Right};
00064 
00065         //Clear old relative values
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                                 {       //Left, Right, or Middle
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                                 {       //mouse Wheel
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                                 {       //Left, Right, or Middle
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         //Handle X/Y axis move
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         //Handle Z Motion
00124         if( mouseZMoved )
00125         {
00126                 mState.abZ += mState.relZ;
00127                 if( mBuffered && listener )
00128                         listener->mouseMoved(MouseEvent(this, 0, mState));
00129         }
00130 
00131         //Handle Alt-Tabbing
00132         SDLInputManager* man = static_cast<SDLInputManager*>(InputManager::getSingletonPtr());
00133         if( man->_getGrabMode() == false )
00134         {
00135                 if( mRegainFocus == false && mGrabbed == true )
00136                 {       //We had focus, but must release it now
00137                         _setGrab(false);
00138                         _setVisible(true);
00139                 }
00140                 else if( mRegainFocus == true && mGrabbed == false )
00141                 {       //We are gaining focus back (mouse clicked in window)
00142                         _setGrab(true);
00143                         _setVisible(false);
00144                         man->_setGrabMode(true);        //Notify manager
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 }

Generated on Sat Dec 1 20:13:52 2007 for OIS by  doxygen 1.5.4