InputSourceMouse.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * InputSourceMouse.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "InputSourceMouse.h"
  12. #include "InputHandler.h"
  13. #include "../../lib/Point.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/EventDispatcher.h"
  16. #include "../gui/MouseButton.h"
  17. #include <SDL_events.h>
  18. void InputSourceMouse::handleEventMouseMotion(const SDL_MouseMotionEvent & motion)
  19. {
  20. Point newPosition(motion.x, motion.y);
  21. Point distance(-motion.xrel, -motion.yrel);
  22. GH.input().setCursorPosition(newPosition);
  23. if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_MIDDLE))
  24. GH.events().dispatchGesturePanning(distance);
  25. mouseButtonsMask = motion.state;
  26. }
  27. void InputSourceMouse::handleEventMouseButtonDown(const SDL_MouseButtonEvent & button)
  28. {
  29. Point position(button.x, button.y);
  30. switch(button.button)
  31. {
  32. case SDL_BUTTON_LEFT:
  33. if(button.clicks > 1)
  34. GH.events().dispatchMouseDoubleClick(position);
  35. else
  36. GH.events().dispatchMouseButtonPressed(MouseButton::LEFT, position);
  37. break;
  38. case SDL_BUTTON_RIGHT:
  39. GH.events().dispatchMouseButtonPressed(MouseButton::RIGHT, position);
  40. break;
  41. case SDL_BUTTON_MIDDLE:
  42. GH.events().dispatchGesturePanningStarted(position);
  43. break;
  44. }
  45. }
  46. void InputSourceMouse::handleEventMouseWheel(const SDL_MouseWheelEvent & wheel)
  47. {
  48. GH.events().dispatchMouseScrolled(Point(wheel.x, wheel.y), GH.getCursorPosition());
  49. }
  50. void InputSourceMouse::handleEventMouseButtonUp(const SDL_MouseButtonEvent & button)
  51. {
  52. Point position(button.x, button.y);
  53. switch(button.button)
  54. {
  55. case SDL_BUTTON_LEFT:
  56. GH.events().dispatchMouseButtonReleased(MouseButton::LEFT, position);
  57. break;
  58. case SDL_BUTTON_RIGHT:
  59. GH.events().dispatchMouseButtonReleased(MouseButton::RIGHT, position);
  60. break;
  61. case SDL_BUTTON_MIDDLE:
  62. GH.events().dispatchGesturePanningEnded();
  63. break;
  64. }
  65. }
  66. bool InputSourceMouse::isMouseButtonPressed(MouseButton button) const
  67. {
  68. static_assert(static_cast<uint32_t>(MouseButton::LEFT) == SDL_BUTTON_LEFT, "mismatch between VCMI and SDL enum!");
  69. static_assert(static_cast<uint32_t>(MouseButton::MIDDLE) == SDL_BUTTON_MIDDLE, "mismatch between VCMI and SDL enum!");
  70. static_assert(static_cast<uint32_t>(MouseButton::RIGHT) == SDL_BUTTON_RIGHT, "mismatch between VCMI and SDL enum!");
  71. static_assert(static_cast<uint32_t>(MouseButton::EXTRA1) == SDL_BUTTON_X1, "mismatch between VCMI and SDL enum!");
  72. static_assert(static_cast<uint32_t>(MouseButton::EXTRA2) == SDL_BUTTON_X2, "mismatch between VCMI and SDL enum!");
  73. uint32_t index = static_cast<uint32_t>(button);
  74. return mouseButtonsMask & SDL_BUTTON(index);
  75. }