/src/ois/src/SDL/SDLMouse.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 174 lines · 121 code · 18 blank · 35 comment · 36 complexity · 59026597d374ab9f13cb08557f0d2690 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 "SDL/SDLMouse.h"
  18. #include "SDL/SDLInputManager.h"
  19. #include "OISException.h"
  20. #include "OISEvents.h"
  21. using namespace OIS;
  22. //-------------------------------------------------------------------//
  23. SDLMouse::SDLMouse( bool buffered ) : mGrabbed(false), mRegainFocus(false)
  24. {
  25. mBuffered = buffered;
  26. mType = OISMouse;
  27. listener = 0;
  28. }
  29. //-------------------------------------------------------------------//
  30. void SDLMouse::_initialize()
  31. {
  32. //Clear old state
  33. mState.clear();
  34. mRegainFocus = false;
  35. _setGrab(true);
  36. _setVisible(false);
  37. static_cast<SDLInputManager*>(InputManager::getSingletonPtr())->_setGrabMode(true);
  38. }
  39. //-------------------------------------------------------------------//
  40. SDLMouse::~SDLMouse()
  41. {
  42. _setGrab(true);
  43. _setVisible(true);
  44. static_cast<SDLInputManager*>(InputManager::getSingletonPtr())->_setGrabMode(false);
  45. }
  46. //-------------------------------------------------------------------//
  47. void SDLMouse::capture()
  48. {
  49. //Used for going from SDL Button to OIS button
  50. static const MouseButtonID ButtonMask[4] = {MB_Left, MB_Left, MB_Middle, MB_Right};
  51. //Clear old relative values
  52. mState.relX = mState.relY = mState.relZ = 0;
  53. SDL_Event events[OIS_SDL_MOUSE_BUFF];
  54. int count = SDL_PeepEvents(events, OIS_SDL_MOUSE_BUFF, SDL_GETEVENT, SDL_MOUSEEVENTMASK);
  55. bool mouseXYMoved = false;
  56. bool mouseZMoved = false;
  57. for( int i = 0; i < count; ++i )
  58. {
  59. switch( events[i].type )
  60. {
  61. case SDL_MOUSEMOTION: mouseXYMoved = true; break;
  62. case SDL_MOUSEBUTTONDOWN:
  63. {
  64. mRegainFocus = true;
  65. int sdlButton = events[i].button.button;
  66. if( sdlButton <= SDL_BUTTON_RIGHT )
  67. { //Left, Right, or Middle
  68. mState.buttons |= (1 << ButtonMask[sdlButton]);
  69. if( mBuffered && listener )
  70. if( listener->mousePressed(MouseEvent(this,0,mState), ButtonMask[sdlButton]) == false )
  71. return;
  72. }
  73. else
  74. { //mouse Wheel
  75. mouseZMoved = true;
  76. if( sdlButton == SDL_BUTTON_WHEELUP )
  77. mState.relZ += 120;
  78. else if( sdlButton == SDL_BUTTON_WHEELDOWN )
  79. mState.relZ -= 120;
  80. }
  81. break;
  82. }
  83. case SDL_MOUSEBUTTONUP:
  84. {
  85. int sdlButton = events[i].button.button;
  86. if( sdlButton <= SDL_BUTTON_RIGHT )
  87. { //Left, Right, or Middle
  88. mState.buttons &= ~(1 << ButtonMask[sdlButton]);
  89. if( mBuffered && listener )
  90. if( listener->mouseReleased(MouseEvent(this,0,mState), ButtonMask[sdlButton]) == false )
  91. return;
  92. }
  93. break;
  94. }
  95. }
  96. }
  97. //Handle X/Y axis move
  98. if( mouseXYMoved )
  99. {
  100. SDL_GetMouseState( &mState.abX, &mState.abY );
  101. SDL_GetRelativeMouseState( &mState.relX, &mState.relY );
  102. if( mBuffered && listener )
  103. listener->mouseMoved(MouseEvent(this, 0, mState));
  104. }
  105. //Handle Z Motion
  106. if( mouseZMoved )
  107. {
  108. mState.abZ += mState.relZ;
  109. if( mBuffered && listener )
  110. listener->mouseMoved(MouseEvent(this, 0, mState));
  111. }
  112. //Handle Alt-Tabbing
  113. SDLInputManager* man = static_cast<SDLInputManager*>(InputManager::getSingletonPtr());
  114. if( man->_getGrabMode() == false )
  115. {
  116. if( mRegainFocus == false && mGrabbed == true )
  117. { //We had focus, but must release it now
  118. _setGrab(false);
  119. _setVisible(true);
  120. }
  121. else if( mRegainFocus == true && mGrabbed == false )
  122. { //We are gaining focus back (mouse clicked in window)
  123. _setGrab(true);
  124. _setVisible(false);
  125. man->_setGrabMode(true); //Notify manager
  126. }
  127. }
  128. }
  129. //-------------------------------------------------------------------//
  130. void SDLMouse::setBuffered(bool buffered)
  131. {
  132. mBuffered = buffered;
  133. }
  134. //-------------------------------------------------------------------//
  135. void SDLMouse::_setGrab(bool grabbed)
  136. {
  137. if( grabbed )
  138. SDL_WM_GrabInput(SDL_GRAB_ON);
  139. else
  140. SDL_WM_GrabInput(SDL_GRAB_OFF);
  141. mGrabbed = grabbed;
  142. }
  143. //-------------------------------------------------------------------//
  144. void SDLMouse::_setVisible(bool visible)
  145. {
  146. if( visible )
  147. SDL_ShowCursor(SDL_ENABLE);
  148. else
  149. SDL_ShowCursor(SDL_DISABLE);
  150. }