InputSourceMouse.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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
  37. GH.input().setCursorPosition(newPosition);
  38. }
  39. void InputSourceMouse::handleEventMouseButtonDown(const SDL_MouseButtonEvent & button)
  40. {
  41. Point position = Point(button.x, button.y) / GH.screenHandler().getScalingFactor();
  42. switch(button.button)
  43. {
  44. case SDL_BUTTON_LEFT:
  45. if(button.clicks > 1)
  46. GH.events().dispatchMouseDoubleClick(position, mouseToleranceDistance);
  47. else
  48. GH.events().dispatchMouseLeftButtonPressed(position, mouseToleranceDistance);
  49. break;
  50. case SDL_BUTTON_RIGHT:
  51. GH.events().dispatchShowPopup(position, mouseToleranceDistance);
  52. break;
  53. case SDL_BUTTON_MIDDLE:
  54. middleClickPosition = position;
  55. GH.events().dispatchGesturePanningStarted(position);
  56. break;
  57. }
  58. }
  59. void InputSourceMouse::handleEventMouseWheel(const SDL_MouseWheelEvent & wheel)
  60. {
  61. #if SDL_VERSION_ATLEAST(2,26,0)
  62. GH.events().dispatchMouseScrolled(Point(wheel.x, wheel.y), Point(wheel.mouseX, wheel.mouseY) / GH.screenHandler().getScalingFactor());
  63. #else
  64. GH.events().dispatchMouseScrolled(Point(wheel.x, wheel.y), GH.getCursorPosition());
  65. #endif
  66. }
  67. void InputSourceMouse::handleEventMouseButtonUp(const SDL_MouseButtonEvent & button)
  68. {
  69. Point position = Point(button.x, button.y) / GH.screenHandler().getScalingFactor();
  70. switch(button.button)
  71. {
  72. case SDL_BUTTON_LEFT:
  73. GH.events().dispatchMouseLeftButtonReleased(position, mouseToleranceDistance);
  74. break;
  75. case SDL_BUTTON_RIGHT:
  76. GH.events().dispatchClosePopup(position);
  77. break;
  78. case SDL_BUTTON_MIDDLE:
  79. GH.events().dispatchGesturePanningEnded(middleClickPosition, position);
  80. break;
  81. }
  82. }