InputSourceMouse.cpp 2.4 KB

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