InputSourceMouse.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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(middleClickPosition, newPosition, 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. middleClickPosition = position;
  43. GH.events().dispatchGesturePanningStarted(position);
  44. break;
  45. }
  46. }
  47. void InputSourceMouse::handleEventMouseWheel(const SDL_MouseWheelEvent & wheel)
  48. {
  49. GH.events().dispatchMouseScrolled(Point(wheel.x, wheel.y), GH.getCursorPosition());
  50. }
  51. void InputSourceMouse::handleEventMouseButtonUp(const SDL_MouseButtonEvent & button)
  52. {
  53. Point position(button.x, button.y);
  54. switch(button.button)
  55. {
  56. case SDL_BUTTON_LEFT:
  57. GH.events().dispatchMouseButtonReleased(MouseButton::LEFT, position);
  58. break;
  59. case SDL_BUTTON_RIGHT:
  60. GH.events().dispatchMouseButtonReleased(MouseButton::RIGHT, position);
  61. break;
  62. case SDL_BUTTON_MIDDLE:
  63. GH.events().dispatchGesturePanningEnded(middleClickPosition, position);
  64. break;
  65. }
  66. }
  67. bool InputSourceMouse::isMouseButtonPressed(MouseButton button) const
  68. {
  69. static_assert(static_cast<uint32_t>(MouseButton::LEFT) == SDL_BUTTON_LEFT, "mismatch between VCMI and SDL enum!");
  70. static_assert(static_cast<uint32_t>(MouseButton::MIDDLE) == SDL_BUTTON_MIDDLE, "mismatch between VCMI and SDL enum!");
  71. static_assert(static_cast<uint32_t>(MouseButton::RIGHT) == SDL_BUTTON_RIGHT, "mismatch between VCMI and SDL enum!");
  72. static_assert(static_cast<uint32_t>(MouseButton::EXTRA1) == SDL_BUTTON_X1, "mismatch between VCMI and SDL enum!");
  73. static_assert(static_cast<uint32_t>(MouseButton::EXTRA2) == SDL_BUTTON_X2, "mismatch between VCMI and SDL enum!");
  74. uint32_t index = static_cast<uint32_t>(button);
  75. return mouseButtonsMask & SDL_BUTTON(index);
  76. }