Home | History | Annotate | Download | only in SDL
      1 /*
      2 The zlib/libpng License
      3 
      4 Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
      5 
      6 This software is provided 'as-is', without any express or implied warranty. In no event will
      7 the authors be held liable for any damages arising from the use of this software.
      8 
      9 Permission is granted to anyone to use this software for any purpose, including commercial
     10 applications, and to alter it and redistribute it freely, subject to the following
     11 restrictions:
     12 
     13     1. The origin of this software must not be misrepresented; you must not claim that
     14 		you wrote the original software. If you use this software in a product,
     15 		an acknowledgment in the product documentation would be appreciated but is
     16 		not required.
     17 
     18     2. Altered source versions must be plainly marked as such, and must not be
     19 		misrepresented as being the original software.
     20 
     21     3. This notice may not be removed or altered from any source distribution.
     22 */
     23 #include "SDL/SDLMouse.h"
     24 #include "SDL/SDLInputManager.h"
     25 #include "OISException.h"
     26 #include "OISEvents.h"
     27 
     28 using namespace OIS;
     29 
     30 //-------------------------------------------------------------------//
     31 SDLMouse::SDLMouse( bool buffered ) : mGrabbed(false), mRegainFocus(false)
     32 {
     33 	mBuffered = buffered;
     34 	mType = OISMouse;
     35 	listener = 0;
     36 }
     37 
     38 //-------------------------------------------------------------------//
     39 void SDLMouse::_initialize()
     40 {
     41 	//Clear old state
     42 	mState.clear();
     43 	mRegainFocus = false;
     44 
     45 	_setGrab(true);
     46 	_setVisible(false);
     47 	static_cast<SDLInputManager*>(InputManager::getSingletonPtr())->_setGrabMode(true);
     48 }
     49 
     50 //-------------------------------------------------------------------//
     51 SDLMouse::~SDLMouse()
     52 {
     53 	_setGrab(true);
     54 	_setVisible(true);
     55 
     56 	static_cast<SDLInputManager*>(InputManager::getSingletonPtr())->_setGrabMode(false);
     57 }
     58 
     59 //-------------------------------------------------------------------//
     60 void SDLMouse::capture()
     61 {
     62 	//Used for going from SDL Button to OIS button
     63 	static const MouseButtonID ButtonMask[4] = {MB_Left, MB_Left, MB_Middle, MB_Right};
     64 
     65 	//Clear old relative values
     66 	mState.relX = mState.relY = mState.relZ = 0;
     67 
     68 	SDL_Event events[OIS_SDL_MOUSE_BUFF];
     69 	int count = SDL_PeepEvents(events, OIS_SDL_MOUSE_BUFF, SDL_GETEVENT, SDL_MOUSEEVENTMASK);
     70 
     71 	bool mouseXYMoved = false;
     72 	bool mouseZMoved = false;
     73 	for( int i = 0; i < count; ++i )
     74 	{
     75 		switch( events[i].type )
     76 		{
     77 			case SDL_MOUSEMOTION: mouseXYMoved = true; break;
     78 			case SDL_MOUSEBUTTONDOWN:
     79 			{
     80 				mRegainFocus = true;
     81 				int sdlButton = events[i].button.button;
     82 				if( sdlButton <= SDL_BUTTON_RIGHT )
     83 				{	//Left, Right, or Middle
     84 					mState.buttons |= (1 << ButtonMask[sdlButton]);
     85 					if( mBuffered && listener )
     86 						if( listener->mousePressed(MouseEvent(this,0,mState), ButtonMask[sdlButton]) == false )
     87 							return;
     88 				}
     89 				else
     90 				{	//mouse Wheel
     91 					mouseZMoved = true;
     92 					if( sdlButton == SDL_BUTTON_WHEELUP )
     93 						mState.relZ += 120;
     94 					else if( sdlButton == SDL_BUTTON_WHEELDOWN )
     95 						mState.relZ -= 120;
     96 				}
     97 				break;
     98 			}
     99 			case SDL_MOUSEBUTTONUP:
    100 			{
    101 				int sdlButton = events[i].button.button;
    102 				if( sdlButton <= SDL_BUTTON_RIGHT )
    103 				{	//Left, Right, or Middle
    104 					mState.buttons &= ~(1 << ButtonMask[sdlButton]);
    105 					if( mBuffered && listener )
    106 						if( listener->mouseReleased(MouseEvent(this,0,mState), ButtonMask[sdlButton]) == false )
    107 							return;
    108 				}
    109 				break;
    110 			}
    111 		}
    112 	}
    113 
    114 	//Handle X/Y axis move
    115 	if( mouseXYMoved )
    116 	{
    117 		SDL_GetMouseState( &mState.abX, &mState.abY );
    118 		SDL_GetRelativeMouseState( &mState.relX, &mState.relY );
    119 
    120 		if( mBuffered && listener )
    121 			listener->mouseMoved(MouseEvent(this, 0, mState));
    122 	}
    123 	//Handle Z Motion
    124 	if( mouseZMoved )
    125 	{
    126 		mState.abZ += mState.relZ;
    127 		if( mBuffered && listener )
    128 			listener->mouseMoved(MouseEvent(this, 0, mState));
    129 	}
    130 
    131 	//Handle Alt-Tabbing
    132 	SDLInputManager* man = static_cast<SDLInputManager*>(InputManager::getSingletonPtr());
    133 	if( man->_getGrabMode() == false )
    134 	{
    135 		if( mRegainFocus == false && mGrabbed == true )
    136 		{	//We had focus, but must release it now
    137 			_setGrab(false);
    138 			_setVisible(true);
    139 		}
    140 		else if( mRegainFocus == true && mGrabbed == false )
    141 		{	//We are gaining focus back (mouse clicked in window)
    142 			_setGrab(true);
    143 			_setVisible(false);
    144 			man->_setGrabMode(true);	//Notify manager
    145 		}
    146 	}
    147 }
    148 
    149 //-------------------------------------------------------------------//
    150 void SDLMouse::setBuffered(bool buffered)
    151 {
    152 	mBuffered = buffered;
    153 }
    154 
    155 //-------------------------------------------------------------------//
    156 void SDLMouse::_setGrab(bool grabbed)
    157 {
    158 	if( grabbed )
    159 		SDL_WM_GrabInput(SDL_GRAB_ON);
    160 	else
    161 		SDL_WM_GrabInput(SDL_GRAB_OFF);
    162 
    163 	mGrabbed = grabbed;
    164 }
    165 
    166 //-------------------------------------------------------------------//
    167 void SDLMouse::_setVisible(bool visible)
    168 {
    169 
    170 	if( visible )
    171 		SDL_ShowCursor(SDL_ENABLE);
    172 	else
    173 		SDL_ShowCursor(SDL_DISABLE);
    174 }
    175