InputSourceTouch.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. VCMI_LIB_NAMESPACE_BEGIN
  12. class Point;
  13. VCMI_LIB_NAMESPACE_END
  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 onRightClickDown 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 IDLE, generate onRightClickUp() event
  48. TAP_DOWN_LONG,
  49. // Possible transitions:
  50. // -> DOUBLE
  51. // -> PANNING -> IDLE
  52. // IDLE -> DOWN_SHORT -> IDLE
  53. // -> LONG -> IDLE
  54. // -> DOUBLE -> PANNING
  55. // -> IDLE
  56. };
  57. struct TouchInputParameters
  58. {
  59. double relativeModeSpeedFactor = 1.0;
  60. uint32_t longPressTimeMilliseconds = 500;
  61. bool useHoldGesture = true;
  62. bool usePanGesture = true;
  63. bool usePinchGesture = true;
  64. bool useRelativeMode = false;
  65. };
  66. /// Class that handles touchscreen input from SDL events
  67. class InputSourceTouch
  68. {
  69. TouchInputParameters params;
  70. TouchState state;
  71. uint32_t lastTapTimeTicks;
  72. Point convertTouchToMouse(const SDL_TouchFingerEvent & current);
  73. void emitPanningEvent();
  74. void emitPinchEvent();
  75. public:
  76. InputSourceTouch();
  77. void handleEventFingerMotion(const SDL_TouchFingerEvent & current);
  78. void handleEventFingerDown(const SDL_TouchFingerEvent & current);
  79. void handleEventFingerUp(const SDL_TouchFingerEvent & current);
  80. void handleUpdate();
  81. bool isMouseButtonPressed(MouseButton button) const;
  82. };