InputSourceMouse.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. InputSourceMouse::InputSourceMouse()
  22. :mouseToleranceDistance(settings["input"]["mouseToleranceDistance"].Integer())
  23. {
  24. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  25. }
  26. void InputSourceMouse::handleEventMouseMotion(const SDL_MouseMotionEvent & motion)
  27. {
  28. Point newPosition = Point(motion.x, motion.y) / GH.screenHandler().getScalingFactor();
  29. Point distance= Point(-motion.xrel, -motion.yrel) / GH.screenHandler().getScalingFactor();
  30. mouseButtonsMask = motion.state;
  31. if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_MIDDLE))
  32. GH.events().dispatchGesturePanning(middleClickPosition, newPosition, distance);
  33. else if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_LEFT))
  34. GH.events().dispatchMouseDragged(newPosition, distance);
  35. else
  36. GH.input().setCursorPosition(newPosition);
  37. }
  38. void InputSourceMouse::handleEventMouseButtonDown(const SDL_MouseButtonEvent & button)
  39. {
  40. Point position = Point(button.x, button.y) / GH.screenHandler().getScalingFactor();
  41. switch(button.button)
  42. {
  43. case SDL_BUTTON_LEFT:
  44. if(button.clicks > 1)
  45. GH.events().dispatchMouseDoubleClick(position, mouseToleranceDistance);
  46. else
  47. GH.events().dispatchMouseLeftButtonPressed(position, mouseToleranceDistance);
  48. break;
  49. case SDL_BUTTON_RIGHT:
  50. GH.events().dispatchShowPopup(position, mouseToleranceDistance);
  51. break;
  52. case SDL_BUTTON_MIDDLE:
  53. middleClickPosition = position;
  54. GH.events().dispatchGesturePanningStarted(position);
  55. break;
  56. }
  57. }
  58. void InputSourceMouse::handleEventMouseWheel(const SDL_MouseWheelEvent & wheel)
  59. {
  60. GH.events().dispatchMouseScrolled(Point(wheel.x, wheel.y) / GH.screenHandler().getScalingFactor(), GH.getCursorPosition());
  61. }
  62. void InputSourceMouse::handleEventMouseButtonUp(const SDL_MouseButtonEvent & button)
  63. {
  64. Point position = Point(button.x, button.y) / GH.screenHandler().getScalingFactor();
  65. switch(button.button)
  66. {
  67. case SDL_BUTTON_LEFT:
  68. GH.events().dispatchMouseLeftButtonReleased(position, mouseToleranceDistance);
  69. break;
  70. case SDL_BUTTON_RIGHT:
  71. GH.events().dispatchClosePopup(position);
  72. break;
  73. case SDL_BUTTON_MIDDLE:
  74. GH.events().dispatchGesturePanningEnded(middleClickPosition, position);
  75. break;
  76. }
  77. }