InputSourceTouch.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. , pointerSpeedMultiplier(settings["general"]["relativePointerSpeedMultiplier"].Float())
  25. {
  26. if(isPointerRelativeMode)
  27. {
  28. SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "0");
  29. SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
  30. }
  31. }
  32. void InputSourceTouch::handleEventFingerMotion(const SDL_TouchFingerEvent & tfinger)
  33. {
  34. if(isPointerRelativeMode)
  35. {
  36. Point screenSize = GH.screenDimensions();
  37. Point moveDistance {
  38. static_cast<int>(screenSize.x * pointerSpeedMultiplier * tfinger.dx),
  39. static_cast<int>(screenSize.y * pointerSpeedMultiplier * tfinger.dy)
  40. };
  41. GH.input().moveCursorPosition(moveDistance);
  42. }
  43. }
  44. void InputSourceTouch::handleEventFingerDown(const SDL_TouchFingerEvent & tfinger)
  45. {
  46. if(isPointerRelativeMode)
  47. {
  48. if(tfinger.x > 0.5)
  49. {
  50. bool isRightClick = tfinger.y < 0.5;
  51. if (isRightClick)
  52. GH.events().dispatchMouseButtonPressed(MouseButton::RIGHT, GH.getCursorPosition());
  53. else
  54. GH.events().dispatchMouseButtonPressed(MouseButton::LEFT, GH.getCursorPosition());
  55. }
  56. }
  57. #ifndef VCMI_IOS
  58. else
  59. {
  60. auto fingerCount = SDL_GetNumTouchFingers(tfinger.touchId);
  61. multifinger = fingerCount > 1;
  62. if(fingerCount == 2)
  63. {
  64. Point position = convertTouchToMouse(tfinger);
  65. GH.input().setCursorPosition(position);
  66. GH.events().dispatchMouseButtonPressed(MouseButton::RIGHT, position);
  67. }
  68. }
  69. #endif //VCMI_IOS
  70. }
  71. void InputSourceTouch::handleEventFingerUp(const SDL_TouchFingerEvent & tfinger)
  72. {
  73. if(isPointerRelativeMode)
  74. {
  75. if(tfinger.x > 0.5)
  76. {
  77. bool isRightClick = tfinger.y < 0.5;
  78. if (isRightClick)
  79. GH.events().dispatchMouseButtonReleased(MouseButton::RIGHT, GH.getCursorPosition());
  80. else
  81. GH.events().dispatchMouseButtonReleased(MouseButton::LEFT, GH.getCursorPosition());
  82. }
  83. }
  84. #ifndef VCMI_IOS
  85. else
  86. {
  87. if(multifinger)
  88. {
  89. auto fingerCount = SDL_GetNumTouchFingers(tfinger.touchId);
  90. Point position = convertTouchToMouse(tfinger);
  91. GH.input().setCursorPosition(position);
  92. GH.events().dispatchMouseButtonReleased(MouseButton::RIGHT, position);
  93. multifinger = fingerCount != 0;
  94. }
  95. }
  96. #endif //VCMI_IOS
  97. }
  98. Point InputSourceTouch::convertTouchToMouse(const SDL_TouchFingerEvent & tfinger)
  99. {
  100. return Point(tfinger.x * GH.screenDimensions().x, tfinger.y * GH.screenDimensions().y);
  101. }
  102. bool InputSourceTouch::isMouseButtonPressed(MouseButton button) const
  103. {
  104. return false;
  105. }