InputSourceMouse.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "../gui/CGuiHandler.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. {
  25. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  26. }
  27. void InputSourceMouse::handleEventMouseMotion(const SDL_MouseMotionEvent & motion)
  28. {
  29. Point newPosition = Point(motion.x, motion.y) / GH.screenHandler().getScalingFactor();
  30. Point distance = Point(-motion.xrel, -motion.yrel) / GH.screenHandler().getScalingFactor();
  31. mouseButtonsMask = motion.state;
  32. if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_MIDDLE))
  33. GH.events().dispatchGesturePanning(middleClickPosition, newPosition, distance);
  34. else if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_LEFT))
  35. GH.events().dispatchMouseDragged(newPosition, distance);
  36. else if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_RIGHT))
  37. GH.events().dispatchMouseDraggedPopup(newPosition, distance);
  38. else
  39. GH.input().setCursorPosition(newPosition);
  40. }
  41. void InputSourceMouse::handleEventMouseButtonDown(const SDL_MouseButtonEvent & button)
  42. {
  43. Point position = Point(button.x, button.y) / GH.screenHandler().getScalingFactor();
  44. switch(button.button)
  45. {
  46. case SDL_BUTTON_LEFT:
  47. if(button.clicks > 1)
  48. GH.events().dispatchMouseDoubleClick(position, mouseToleranceDistance);
  49. else
  50. GH.events().dispatchMouseLeftButtonPressed(position, mouseToleranceDistance);
  51. break;
  52. case SDL_BUTTON_RIGHT:
  53. GH.events().dispatchShowPopup(position, mouseToleranceDistance);
  54. break;
  55. case SDL_BUTTON_MIDDLE:
  56. middleClickPosition = position;
  57. GH.events().dispatchGesturePanningStarted(position);
  58. break;
  59. }
  60. }
  61. void InputSourceMouse::handleEventMouseWheel(const SDL_MouseWheelEvent & wheel)
  62. {
  63. //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
  64. // This SDL bug was fixed in 2.30.1: https://github.com/libsdl-org/SDL/issues/9097
  65. #if SDL_VERSION_ATLEAST(2,30,1)
  66. GH.events().dispatchMouseScrolled(Point(wheel.x, wheel.y), Point(wheel.mouseX, wheel.mouseY) / GH.screenHandler().getScalingFactor());
  67. #else
  68. GH.events().dispatchMouseScrolled(Point(wheel.x, wheel.y), GH.getCursorPosition());
  69. #endif
  70. }
  71. void InputSourceMouse::handleEventMouseButtonUp(const SDL_MouseButtonEvent & button)
  72. {
  73. Point position = Point(button.x, button.y) / GH.screenHandler().getScalingFactor();
  74. switch(button.button)
  75. {
  76. case SDL_BUTTON_LEFT:
  77. GH.events().dispatchMouseLeftButtonReleased(position, mouseToleranceDistance);
  78. break;
  79. case SDL_BUTTON_RIGHT:
  80. GH.events().dispatchClosePopup(position);
  81. break;
  82. case SDL_BUTTON_MIDDLE:
  83. GH.events().dispatchGesturePanningEnded(middleClickPosition, position);
  84. break;
  85. }
  86. }