InputSourceMouse.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "../GameEngine.h"
  14. #include "../gui/EventDispatcher.h"
  15. #include "../gui/MouseButton.h"
  16. #include "../render/IScreenHandler.h"
  17. #include "../../lib/Point.h"
  18. #include "../../lib/CConfigHandler.h"
  19. #include <SDL_events.h>
  20. #include <SDL_hints.h>
  21. #include <SDL_version.h>
  22. InputSourceMouse::InputSourceMouse()
  23. :mouseToleranceDistance(settings["input"]["mouseToleranceDistance"].Integer())
  24. ,motionAccumulatedX(.0f)
  25. ,motionAccumulatedY(.0f)
  26. {
  27. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  28. }
  29. void InputSourceMouse::handleEventMouseMotion(const SDL_MouseMotionEvent & motion)
  30. {
  31. Point newPosition = Point(motion.x, motion.y) / ENGINE->screenHandler().getScalingFactor();
  32. motionAccumulatedX += static_cast<float>(-motion.xrel) / ENGINE->screenHandler().getScalingFactor();
  33. motionAccumulatedY += static_cast<float>(-motion.yrel) / ENGINE->screenHandler().getScalingFactor();
  34. Point distance = Point(motionAccumulatedX, motionAccumulatedY);
  35. motionAccumulatedX -= distance.x;
  36. motionAccumulatedY -= distance.y;
  37. mouseButtonsMask = motion.state;
  38. if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_MIDDLE))
  39. ENGINE->events().dispatchGesturePanning(middleClickPosition, newPosition, distance);
  40. else if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_LEFT))
  41. ENGINE->events().dispatchMouseDragged(newPosition, distance);
  42. else if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_RIGHT))
  43. ENGINE->events().dispatchMouseDraggedPopup(newPosition, distance);
  44. else
  45. ENGINE->input().setCursorPosition(newPosition);
  46. }
  47. void InputSourceMouse::handleEventMouseButtonDown(const SDL_MouseButtonEvent & button)
  48. {
  49. Point position = Point(button.x, button.y) / ENGINE->screenHandler().getScalingFactor();
  50. switch(button.button)
  51. {
  52. case SDL_BUTTON_LEFT:
  53. if(button.clicks > 1)
  54. ENGINE->events().dispatchMouseDoubleClick(position, mouseToleranceDistance);
  55. else
  56. ENGINE->events().dispatchMouseLeftButtonPressed(position, mouseToleranceDistance);
  57. break;
  58. case SDL_BUTTON_RIGHT:
  59. ENGINE->events().dispatchShowPopup(position, mouseToleranceDistance);
  60. break;
  61. case SDL_BUTTON_MIDDLE:
  62. middleClickPosition = position;
  63. ENGINE->events().dispatchGesturePanningStarted(position);
  64. break;
  65. }
  66. }
  67. void InputSourceMouse::handleEventMouseWheel(const SDL_MouseWheelEvent & wheel)
  68. {
  69. //NOTE: while mouseX / mouseY properties are available since 2.26.0, they are not converted into logical coordinates so don't account for resolution scaling
  70. // This SDL bug was fixed in 2.30.1: https://github.com/libsdl-org/SDL/issues/9097
  71. #if SDL_VERSION_ATLEAST(2,30,1)
  72. ENGINE->events().dispatchMouseScrolled(Point(wheel.x, wheel.y), Point(wheel.mouseX, wheel.mouseY) / ENGINE->screenHandler().getScalingFactor());
  73. #else
  74. ENGINE->events().dispatchMouseScrolled(Point(wheel.x, wheel.y), ENGINE->getCursorPosition());
  75. #endif
  76. }
  77. void InputSourceMouse::handleEventMouseButtonUp(const SDL_MouseButtonEvent & button)
  78. {
  79. Point position = Point(button.x, button.y) / ENGINE->screenHandler().getScalingFactor();
  80. switch(button.button)
  81. {
  82. case SDL_BUTTON_LEFT:
  83. ENGINE->events().dispatchMouseLeftButtonReleased(position, mouseToleranceDistance);
  84. break;
  85. case SDL_BUTTON_RIGHT:
  86. ENGINE->events().dispatchClosePopup(position);
  87. break;
  88. case SDL_BUTTON_MIDDLE:
  89. ENGINE->events().dispatchGesturePanningEnded(middleClickPosition, position);
  90. break;
  91. }
  92. }