InputSourceTouch.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * InputSourceTouch.h, 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. #pragma once
  11. #include "../../lib/Point.h"
  12. // Debug option. If defined, mouse events will instead generate touch events, allowing testing of touch input on desktop
  13. // #define VCMI_EMULATE_TOUCHSCREEN_WITH_MOUSE
  14. enum class MouseButton;
  15. struct SDL_TouchFingerEvent;
  16. /// Enumeration that describes current state of gesture recognition
  17. enum class TouchState
  18. {
  19. // special state that allows no transitions
  20. // used when player selects "relative mode" in Launcher
  21. // in this mode touchscreen acts like touchpad, moving cursor at certains speed
  22. // and generates events for positions below cursor instead of positions below touch events
  23. RELATIVE_MODE,
  24. // no active touch events
  25. // DOWN -> transition to TAP_DOWN_SHORT
  26. // MOTION / UP -> not expected
  27. IDLE,
  28. // single finger is touching the screen for a short time
  29. // DOWN -> transition to TAP_DOWN_DOUBLE
  30. // MOTION -> transition to TAP_DOWN_PANNING
  31. // UP -> transition to IDLE, emit onLeftClickDown and onLeftClickUp
  32. // on timer -> transition to TAP_DOWN_LONG, emit showPopup() event
  33. TAP_DOWN_SHORT,
  34. // single finger is moving across screen
  35. // DOWN -> transition to TAP_DOWN_DOUBLE
  36. // MOTION -> emit panning event
  37. // UP -> transition to IDLE
  38. TAP_DOWN_PANNING,
  39. // two fingers are touching the screen
  40. // DOWN -> ??? how to handle 3rd finger? Ignore?
  41. // MOTION -> emit pinch event
  42. // UP -> transition to TAP_DOWN
  43. TAP_DOWN_DOUBLE,
  44. // single finger is down for long period of time
  45. // DOWN -> ignored
  46. // MOTION -> ignored
  47. // UP -> transition to TAP_DOWN_LONG_AWAIT
  48. TAP_DOWN_LONG,
  49. // right-click popup is active, waiting for new tap to hide popup
  50. // DOWN -> ignored
  51. // MOTION -> ignored
  52. // UP -> transition to IDLE, generate closePopup() event
  53. TAP_DOWN_LONG_AWAIT,
  54. };
  55. struct TouchInputParameters
  56. {
  57. /// Speed factor of mouse pointer when relative mode is used
  58. double relativeModeSpeedFactor = 1.0;
  59. /// tap for period longer than specified here will be qualified as "long tap", triggering corresponding gesture
  60. uint32_t longTouchTimeMilliseconds = 750;
  61. /// time span in where the second tap has to happen for qualifing as "double click"
  62. uint32_t doubleTouchTimeMilliseconds = 500;
  63. /// max distance in where the second tap has to happen for qualifing as "double click"
  64. uint32_t doubleTouchToleranceDistance = 50;
  65. /// moving finger for distance larger than specified will be qualified as panning gesture instead of long press
  66. uint32_t panningSensitivityThreshold = 10;
  67. /// gesture will be qualified as pinch if distance between fingers is at least specified here
  68. uint32_t pinchSensitivityThreshold = 10;
  69. /// touch event will trigger clicking of elements up to X pixels away from actual touch position
  70. uint32_t touchToleranceDistance = 20;
  71. bool useRelativeMode = false;
  72. bool hapticFeedbackEnabled = false;
  73. };
  74. /// Class that handles touchscreen input from SDL events
  75. class InputSourceTouch
  76. {
  77. TouchInputParameters params;
  78. TouchState state;
  79. uint32_t lastTapTimeTicks;
  80. Point lastTapPosition;
  81. uint32_t lastLeftClickTimeTicks;
  82. Point lastLeftClickPosition;
  83. Point convertTouchToMouse(const SDL_TouchFingerEvent & current);
  84. Point convertTouchToMouse(float x, float y);
  85. void emitPanningEvent(const SDL_TouchFingerEvent & tfinger);
  86. void emitPinchEvent(const SDL_TouchFingerEvent & tfinger);
  87. public:
  88. InputSourceTouch();
  89. void handleEventFingerMotion(const SDL_TouchFingerEvent & current);
  90. void handleEventFingerDown(const SDL_TouchFingerEvent & current);
  91. void handleEventFingerUp(const SDL_TouchFingerEvent & current);
  92. void hapticFeedback();
  93. void handleUpdate();
  94. bool hasTouchInputDevice() const;
  95. };