InputSourceTouch.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * InputSourceTouch.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 "InputSourceTouch.h"
  12. #include "InputHandler.h"
  13. #include "../../lib/CConfigHandler.h"
  14. #include "../CMT.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/EventDispatcher.h"
  17. #include "../gui/MouseButton.h"
  18. #include <SDL_events.h>
  19. #include <SDL_render.h>
  20. #include <SDL_hints.h>
  21. InputSourceTouch::InputSourceTouch()
  22. : multifinger(false)
  23. , isPointerRelativeMode(settings["general"]["userRelativePointer"].Bool())
  24. {
  25. if(isPointerRelativeMode)
  26. {
  27. SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "0");
  28. SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
  29. }
  30. }
  31. void InputSourceTouch::handleEventFingerMotion(const SDL_TouchFingerEvent & tfinger)
  32. {
  33. if(isPointerRelativeMode)
  34. {
  35. GH.input().fakeMoveCursor(tfinger.dx, tfinger.dy);
  36. }
  37. }
  38. void InputSourceTouch::handleEventFingerDown(const SDL_TouchFingerEvent & tfinger)
  39. {
  40. auto fingerCount = SDL_GetNumTouchFingers(tfinger.touchId);
  41. multifinger = fingerCount > 1;
  42. if(isPointerRelativeMode)
  43. {
  44. if(tfinger.x > 0.5)
  45. {
  46. bool isRightClick = tfinger.y < 0.5;
  47. fakeMouseButtonEventRelativeMode(true, isRightClick);
  48. }
  49. }
  50. #ifndef VCMI_IOS
  51. else if(fingerCount == 2)
  52. {
  53. Point position = convertTouchToMouse(tfinger);
  54. GH.events().dispatchMouseMoved(position);
  55. GH.events().dispatchMouseButtonPressed(MouseButton::RIGHT, position);
  56. }
  57. #endif //VCMI_IOS
  58. }
  59. void InputSourceTouch::handleEventFingerUp(const SDL_TouchFingerEvent & tfinger)
  60. {
  61. #ifndef VCMI_IOS
  62. auto fingerCount = SDL_GetNumTouchFingers(tfinger.touchId);
  63. #endif //VCMI_IOS
  64. if(isPointerRelativeMode)
  65. {
  66. if(tfinger.x > 0.5)
  67. {
  68. bool isRightClick = tfinger.y < 0.5;
  69. fakeMouseButtonEventRelativeMode(false, isRightClick);
  70. }
  71. }
  72. #ifndef VCMI_IOS
  73. else if(multifinger)
  74. {
  75. Point position = convertTouchToMouse(tfinger);
  76. GH.events().dispatchMouseMoved(position);
  77. GH.events().dispatchMouseButtonReleased(MouseButton::RIGHT, position);
  78. multifinger = fingerCount != 0;
  79. }
  80. #endif //VCMI_IOS
  81. }
  82. Point InputSourceTouch::convertTouchToMouse(const SDL_TouchFingerEvent & tfinger)
  83. {
  84. return Point(tfinger.x * GH.screenDimensions().x, tfinger.y * GH.screenDimensions().y);
  85. }
  86. void InputSourceTouch::fakeMouseButtonEventRelativeMode(bool down, bool right)
  87. {
  88. SDL_Event event;
  89. SDL_MouseButtonEvent sme = {SDL_MOUSEBUTTONDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  90. if(!down)
  91. {
  92. sme.type = SDL_MOUSEBUTTONUP;
  93. }
  94. sme.button = right ? SDL_BUTTON_RIGHT : SDL_BUTTON_LEFT;
  95. sme.x = GH.getCursorPosition().x;
  96. sme.y = GH.getCursorPosition().y;
  97. float xScale, yScale;
  98. int w, h, rLogicalWidth, rLogicalHeight;
  99. SDL_GetWindowSize(mainWindow, &w, &h);
  100. SDL_RenderGetLogicalSize(mainRenderer, &rLogicalWidth, &rLogicalHeight);
  101. SDL_RenderGetScale(mainRenderer, &xScale, &yScale);
  102. SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  103. moveCursorToPosition(Point((int)(sme.x * xScale) + (w - rLogicalWidth * xScale) / 2, (int)(sme.y * yScale + (h - rLogicalHeight * yScale) / 2)));
  104. SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
  105. event.button = sme;
  106. SDL_PushEvent(&event);
  107. }
  108. void InputSourceTouch::moveCursorToPosition(const Point & position)
  109. {
  110. SDL_WarpMouseInWindow(mainWindow, position.x, position.y);
  111. }