/src/ois/src/win32/Win32Mouse.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 216 lines · 153 code · 26 blank · 37 comment · 38 complexity · 594a5e8897797966a4e04afab8653d9f MD5 · raw file

  1. /*
  2. The zlib/libpng License
  3. Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
  4. This software is provided 'as-is', without any express or implied warranty. In no event will
  5. the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose, including commercial
  7. applications, and to alter it and redistribute it freely, subject to the following
  8. restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that
  10. you wrote the original software. If you use this software in a product,
  11. an acknowledgment in the product documentation would be appreciated but is
  12. not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. #include "win32/Win32Mouse.h"
  18. #include "win32/Win32InputManager.h"
  19. #include "OISException.h"
  20. #include "OISEvents.h"
  21. using namespace OIS;
  22. //--------------------------------------------------------------------------------------------------//
  23. Win32Mouse::Win32Mouse( InputManager* creator, IDirectInput8* pDI, bool buffered, DWORD coopSettings )
  24. : Mouse(creator->inputSystemName(), buffered, 0, creator)
  25. {
  26. mMouse = 0;
  27. mDirectInput = pDI;
  28. coopSetting = coopSettings;
  29. mHwnd = 0;
  30. static_cast<Win32InputManager*>(mCreator)->_setMouseUsed(true);
  31. }
  32. //--------------------------------------------------------------------------------------------------//
  33. void Win32Mouse::_initialize()
  34. {
  35. DIPROPDWORD dipdw;
  36. //Clear old state
  37. mState.clear();
  38. dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  39. dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  40. dipdw.diph.dwObj = 0;
  41. dipdw.diph.dwHow = DIPH_DEVICE;
  42. dipdw.dwData = MOUSE_DX_BUFFERSIZE;
  43. if( FAILED(mDirectInput->CreateDevice(GUID_SysMouse, &mMouse, NULL)) )
  44. OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to create device" );
  45. if( FAILED(mMouse->SetDataFormat(&c_dfDIMouse2)) )
  46. OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to set format" );
  47. mHwnd = ((Win32InputManager*)mCreator)->getWindowHandle();
  48. if( FAILED(mMouse->SetCooperativeLevel(mHwnd, coopSetting)) )
  49. OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to set coop level" );
  50. if( FAILED(mMouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph )) )
  51. OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to set property" );
  52. HRESULT hr = mMouse->Acquire();
  53. if (FAILED(hr) && hr != DIERR_OTHERAPPHASPRIO)
  54. OIS_EXCEPT( E_General, "Win32Mouse::Win32Mouse >> Failed to aquire mouse!" );
  55. }
  56. //--------------------------------------------------------------------------------------------------//
  57. Win32Mouse::~Win32Mouse()
  58. {
  59. if (mMouse)
  60. {
  61. mMouse->Unacquire();
  62. mMouse->Release();
  63. mMouse = 0;
  64. }
  65. static_cast<Win32InputManager*>(mCreator)->_setMouseUsed(false);
  66. }
  67. //--------------------------------------------------------------------------------------------------//
  68. void Win32Mouse::capture()
  69. {
  70. //Clear old relative values
  71. mState.X.rel = mState.Y.rel = mState.Z.rel = 0;
  72. DIDEVICEOBJECTDATA diBuff[MOUSE_DX_BUFFERSIZE];
  73. DWORD entries = MOUSE_DX_BUFFERSIZE;
  74. HRESULT hr = mMouse->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );
  75. if( hr != DI_OK )
  76. {
  77. hr = mMouse->Acquire();
  78. while( hr == DIERR_INPUTLOST )
  79. hr = mMouse->Acquire();
  80. hr = mMouse->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );
  81. //Perhaps the user just tabbed away, and coop settings
  82. //are nonexclusive..so just ignore
  83. if( FAILED(hr) )
  84. return;
  85. }
  86. bool axesMoved = false;
  87. //Accumulate all axis movements for one axesMove message..
  88. //Buttons are fired off as they are found
  89. for(unsigned int i = 0; i < entries; ++i )
  90. {
  91. switch( diBuff[i].dwOfs )
  92. {
  93. case DIMOFS_BUTTON0:
  94. if(!_doMouseClick(0, diBuff[i])) return;
  95. break;
  96. case DIMOFS_BUTTON1:
  97. if(!_doMouseClick(1, diBuff[i])) return;
  98. break;
  99. case DIMOFS_BUTTON2:
  100. if(!_doMouseClick(2, diBuff[i])) return;
  101. break;
  102. case DIMOFS_BUTTON3:
  103. if(!_doMouseClick(3, diBuff[i])) return;
  104. break;
  105. case DIMOFS_BUTTON4:
  106. if(!_doMouseClick(4, diBuff[i])) return;
  107. break;
  108. case DIMOFS_BUTTON5:
  109. if(!_doMouseClick(5, diBuff[i])) return;
  110. break;
  111. case DIMOFS_BUTTON6:
  112. if(!_doMouseClick(6, diBuff[i])) return;
  113. break;
  114. case DIMOFS_BUTTON7:
  115. if(!_doMouseClick(7, diBuff[i])) return;
  116. break;
  117. case DIMOFS_X:
  118. mState.X.rel += diBuff[i].dwData;
  119. axesMoved = true;
  120. break;
  121. case DIMOFS_Y:
  122. mState.Y.rel += diBuff[i].dwData;
  123. axesMoved = true;
  124. break;
  125. case DIMOFS_Z:
  126. mState.Z.rel += diBuff[i].dwData;
  127. axesMoved = true;
  128. break;
  129. default: break;
  130. } //end switch
  131. }//end for
  132. if( axesMoved )
  133. {
  134. if( coopSetting & DISCL_NONEXCLUSIVE )
  135. {
  136. //DirectInput provides us with meaningless values, so correct that
  137. POINT point;
  138. GetCursorPos(&point);
  139. ScreenToClient(mHwnd, &point);
  140. mState.X.abs = point.x;
  141. mState.Y.abs = point.y;
  142. }
  143. else
  144. {
  145. mState.X.abs += mState.X.rel;
  146. mState.Y.abs += mState.Y.rel;
  147. }
  148. mState.Z.abs += mState.Z.rel;
  149. //Clip values to window
  150. if( mState.X.abs < 0 )
  151. mState.X.abs = 0;
  152. else if( mState.X.abs > mState.width )
  153. mState.X.abs = mState.width;
  154. if( mState.Y.abs < 0 )
  155. mState.Y.abs = 0;
  156. else if( mState.Y.abs > mState.height )
  157. mState.Y.abs = mState.height;
  158. //Do the move
  159. if( mListener && mBuffered )
  160. mListener->mouseMoved( MouseEvent( this, mState ) );
  161. }
  162. }
  163. //--------------------------------------------------------------------------------------------------//
  164. bool Win32Mouse::_doMouseClick( int mouseButton, DIDEVICEOBJECTDATA& di )
  165. {
  166. if( di.dwData & 0x80 )
  167. {
  168. mState.buttons |= 1 << mouseButton; //turn the bit flag on
  169. if( mListener && mBuffered )
  170. return mListener->mousePressed( MouseEvent( this, mState ), (MouseButtonID)mouseButton );
  171. }
  172. else
  173. {
  174. mState.buttons &= ~(1 << mouseButton); //turn the bit flag off
  175. if( mListener && mBuffered )
  176. return mListener->mouseReleased( MouseEvent( this, mState ), (MouseButtonID)mouseButton );
  177. }
  178. return true;
  179. }
  180. //--------------------------------------------------------------------------------------------------//
  181. void Win32Mouse::setBuffered(bool buffered)
  182. {
  183. mBuffered = buffered;
  184. }