InputSourceMouse.cpp 2.8 KB

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