E:/Download/ois-1.0RC1/src/win32/Win32Mouse.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 "Win32/Win32Mouse.h"
00024 #include "Win32/Win32InputManager.h"
00025 #include "OISException.h"
00026 #include "OISEvents.h"
00027 
00028 using namespace OIS;
00029 
00030 //--------------------------------------------------------------------------------------------------//
00031 Win32Mouse::Win32Mouse( InputManager* creator, IDirectInput8* pDI, bool buffered, DWORD coopSettings )
00032 {
00033         mCreator = creator;
00034         mBuffered = buffered;
00035         mMouse = 0;
00036         mDirectInput = pDI;
00037         coopSetting = coopSettings;
00038         mType = OISMouse;
00039         listener = 0;
00040         mHwnd = 0;
00041 }
00042 
00043 //--------------------------------------------------------------------------------------------------//
00044 void Win32Mouse::_initialize()
00045 {
00046         DIPROPDWORD dipdw;
00047 
00048         //Clear old state
00049         mState.clear();
00050 
00051     dipdw.diph.dwSize       = sizeof(DIPROPDWORD);
00052     dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
00053     dipdw.diph.dwObj        = 0;
00054     dipdw.diph.dwHow        = DIPH_DEVICE;
00055         dipdw.dwData            = MOUSE_DX_BUFFERSIZE;
00056         
00057         if( FAILED(mDirectInput->CreateDevice(GUID_SysMouse, &mMouse, NULL)) )
00058                 OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to create device" );
00059 
00060         if( FAILED(mMouse->SetDataFormat(&c_dfDIMouse2)) )
00061                 OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to set format" );
00062         
00063         mHwnd = ((Win32InputManager*)mCreator)->getWindowHandle();
00064 
00065         if( FAILED(mMouse->SetCooperativeLevel(mHwnd, coopSetting)) )
00066                 OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to set coop level" );
00067         
00068         if( FAILED(mMouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph )) )
00069                 OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to set property" );
00070 
00071         HRESULT hr = mMouse->Acquire();
00072         if (FAILED(hr) && hr != DIERR_OTHERAPPHASPRIO)
00073                 OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to aquire mouse!" );
00074 }
00075 
00076 //--------------------------------------------------------------------------------------------------//
00077 Win32Mouse::~Win32Mouse()
00078 {
00079         if (mMouse)
00080         {
00081                 mMouse->Unacquire();
00082                 mMouse->Release();
00083                 mMouse = 0;
00084         }
00085 }
00086 
00087 //--------------------------------------------------------------------------------------------------//
00088 void Win32Mouse::capture()
00089 {
00090         //Clear old relative values
00091         mState.X.rel = mState.Y.rel = mState.Z.rel = 0;
00092 
00093         DIDEVICEOBJECTDATA diBuff[MOUSE_DX_BUFFERSIZE];
00094         DWORD entries = MOUSE_DX_BUFFERSIZE;
00095 
00096         HRESULT hr = mMouse->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );
00097         if( hr != DI_OK )
00098         {
00099                 hr = mMouse->Acquire();
00100                 while( hr == DIERR_INPUTLOST ) 
00101                         hr = mMouse->Acquire();
00102 
00103                 hr = mMouse->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );
00104                 
00105                 //Perhaps the user just tabbed away, and coop settings
00106                 //are nonexclusive..so just ignore
00107                 if( FAILED(hr) )
00108                         return;
00109         }
00110 
00111         bool axesMoved = false;
00112         //Accumulate all axis movements for one axesMove message..
00113         //Buttons are fired off as they are found
00114         for(unsigned int i = 0; i < entries; ++i )
00115         {
00116                 switch( diBuff[i].dwOfs )
00117                 {
00118                         case DIMOFS_BUTTON0:
00119                                 if(!_doMouseClick(0, diBuff[i])) return;
00120                                 break;
00121                         case DIMOFS_BUTTON1:
00122                                 if(!_doMouseClick(1, diBuff[i])) return;
00123                                 break;
00124                         case DIMOFS_BUTTON2:
00125                                 if(!_doMouseClick(2, diBuff[i])) return;
00126                                 break;
00127                         case DIMOFS_BUTTON3:
00128                                 if(!_doMouseClick(3, diBuff[i])) return;
00129                                 break;
00130                         case DIMOFS_BUTTON4:
00131                                 if(!_doMouseClick(4, diBuff[i])) return;
00132                                 break;  
00133                         case DIMOFS_BUTTON5:
00134                                 if(!_doMouseClick(5, diBuff[i])) return;
00135                                 break;
00136                         case DIMOFS_BUTTON6:
00137                                 if(!_doMouseClick(6, diBuff[i])) return;
00138                                 break;
00139                         case DIMOFS_BUTTON7:
00140                                 if(!_doMouseClick(7, diBuff[i])) return;
00141                                 break;
00142                         case DIMOFS_X:
00143                                 mState.X.rel += diBuff[i].dwData;
00144                                 axesMoved = true;
00145                                 break;
00146                         case DIMOFS_Y:
00147                                 mState.Y.rel += diBuff[i].dwData;
00148                                 axesMoved = true;
00149                                 break;
00150                         case DIMOFS_Z:
00151                                 mState.Z.rel += diBuff[i].dwData;
00152                                 axesMoved = true;
00153                                 break;
00154                         default: break;
00155                 } //end switch
00156         }//end for
00157 
00158         if( axesMoved )
00159         {
00160                 if( coopSetting & DISCL_NONEXCLUSIVE )
00161                 {
00162                         //DirectInput provides us with meaningless values, so correct that
00163                         POINT point;
00164                         GetCursorPos(&point);
00165                         ScreenToClient(mHwnd, &point);
00166                         mState.X.abs = point.x;
00167                         mState.Y.abs = point.y;
00168                 }
00169                 else
00170                 {
00171                         mState.X.abs +=  mState.X.rel;
00172                         mState.Y.abs +=  mState.Y.rel;
00173                 }
00174                 mState.Z.abs +=  mState.Z.rel;
00175 
00176                 //Clip values to window
00177                 if( mState.X.abs < 0 )
00178                         mState.X.abs = 0;
00179                 else if( mState.X.abs > mState.width )
00180                         mState.X.abs = mState.width;
00181                 if( mState.Y.abs < 0 )
00182                         mState.Y.abs = 0;
00183                 else if( mState.Y.abs > mState.height )
00184                         mState.Y.abs = mState.height;
00185 
00186                 //Do the move
00187                 if( listener && mBuffered )
00188                         listener->mouseMoved( MouseEvent( this, mState ) );
00189         }
00190 }
00191 
00192 //--------------------------------------------------------------------------------------------------//
00193 bool Win32Mouse::_doMouseClick( int mouseButton, DIDEVICEOBJECTDATA& di )
00194 {
00195         if( di.dwData & 0x80 )
00196         {
00197                 mState.buttons |= 1 << mouseButton; //turn the bit flag on
00198                 if( listener && mBuffered )
00199                         return listener->mousePressed( MouseEvent( this, mState ), (MouseButtonID)mouseButton );
00200         }
00201         else
00202         {
00203                 mState.buttons &= ~(1 << mouseButton); //turn the bit flag off
00204                 if( listener && mBuffered )
00205                         return listener->mouseReleased( MouseEvent( this, mState ), (MouseButtonID)mouseButton );
00206         }
00207 
00208         return true;
00209 }
00210 
00211 //--------------------------------------------------------------------------------------------------//
00212 void Win32Mouse::setBuffered(bool buffered)
00213 {
00214         mBuffered = buffered;
00215 }

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