InputSourceMouse.cpp 2.2 KB

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