InputSourceTouch.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /// moving finger for distance larger than specified will be qualified as panning gesture instead of long press
  62. uint32_t panningSensitivityThreshold = 10;
  63. /// gesture will be qualified as pinch if distance between fingers is at least specified here
  64. uint32_t pinchSensitivityThreshold = 10;
  65. bool useRelativeMode = false;
  66. bool hapticFeedbackEnabled = false;
  67. };
  68. /// Class that handles touchscreen input from SDL events
  69. class InputSourceTouch
  70. {
  71. TouchInputParameters params;
  72. TouchState state;
  73. uint32_t lastTapTimeTicks;
  74. Point lastTapPosition;
  75. Point convertTouchToMouse(const SDL_TouchFingerEvent & current);
  76. Point convertTouchToMouse(float x, float y);
  77. void emitPanningEvent(const SDL_TouchFingerEvent & tfinger);
  78. void emitPinchEvent(const SDL_TouchFingerEvent & tfinger);
  79. void hapticFeedback();
  80. public:
  81. InputSourceTouch();
  82. void handleEventFingerMotion(const SDL_TouchFingerEvent & current);
  83. void handleEventFingerDown(const SDL_TouchFingerEvent & current);
  84. void handleEventFingerUp(const SDL_TouchFingerEvent & current);
  85. void handleUpdate();
  86. bool hasTouchInputDevice() const;
  87. };