InputSourceMouse.cpp 2.7 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. if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_MIDDLE))
  23. GH.events().dispatchGesturePanning(middleClickPosition, newPosition, distance);
  24. else
  25. GH.input().setCursorPosition(newPosition);
  26. mouseButtonsMask = motion.state;
  27. }
  28. void InputSourceMouse::handleEventMouseButtonDown(const SDL_MouseButtonEvent & button)
  29. {
  30. Point position(button.x, button.y);
  31. switch(button.button)
  32. {
  33. case SDL_BUTTON_LEFT:
  34. if(button.clicks > 1)
  35. GH.events().dispatchMouseDoubleClick(position);
  36. else
  37. GH.events().dispatchMouseLeftButtonPressed(position);
  38. break;
  39. case SDL_BUTTON_RIGHT:
  40. GH.events().dispatchShowPopup(position);
  41. break;
  42. case SDL_BUTTON_MIDDLE:
  43. middleClickPosition = position;
  44. GH.events().dispatchGesturePanningStarted(position);
  45. break;
  46. }
  47. }
  48. void InputSourceMouse::handleEventMouseWheel(const SDL_MouseWheelEvent & wheel)
  49. {
  50. GH.events().dispatchMouseScrolled(Point(wheel.x, wheel.y), GH.getCursorPosition());
  51. }
  52. void InputSourceMouse::handleEventMouseButtonUp(const SDL_MouseButtonEvent & button)
  53. {
  54. Point position(button.x, button.y);
  55. switch(button.button)
  56. {
  57. case SDL_BUTTON_LEFT:
  58. GH.events().dispatchMouseLeftButtonReleased(position);
  59. break;
  60. case SDL_BUTTON_RIGHT:
  61. GH.events().dispatchClosePopup(position);
  62. break;
  63. case SDL_BUTTON_MIDDLE:
  64. GH.events().dispatchGesturePanningEnded(middleClickPosition, position);
  65. break;
  66. }
  67. }
  68. bool InputSourceMouse::isMouseButtonPressed(MouseButton button) const
  69. {
  70. static_assert(static_cast<uint32_t>(MouseButton::LEFT) == SDL_BUTTON_LEFT, "mismatch between VCMI and SDL enum!");
  71. static_assert(static_cast<uint32_t>(MouseButton::MIDDLE) == SDL_BUTTON_MIDDLE, "mismatch between VCMI and SDL enum!");
  72. static_assert(static_cast<uint32_t>(MouseButton::RIGHT) == SDL_BUTTON_RIGHT, "mismatch between VCMI and SDL enum!");
  73. static_assert(static_cast<uint32_t>(MouseButton::EXTRA1) == SDL_BUTTON_X1, "mismatch between VCMI and SDL enum!");
  74. static_assert(static_cast<uint32_t>(MouseButton::EXTRA2) == SDL_BUTTON_X2, "mismatch between VCMI and SDL enum!");
  75. uint32_t index = static_cast<uint32_t>(button);
  76. return mouseButtonsMask & SDL_BUTTON(index);
  77. }