Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5.  
  6. using Assets.Scripts.Touch;
  7. using DigitalRubyShared;
  8.  
  9. public class TouchController : MonoBehaviour
  10. {
  11. private readonly Dictionary<int, Vector2> _recordedTouches = new Dictionary<int, Vector2>();
  12. private readonly Dictionary<int, GestureTouch> _virtualTouches = new Dictionary<int, GestureTouch>();
  13. private readonly List<int> idsToRemove = new List<int>();
  14.  
  15. private void Awake()
  16. {
  17. TouchManager.OnStart += OnTouchStarted;
  18. TouchManager.OnEnd += OnTouchStopped;
  19.  
  20. CreateGestures();
  21.  
  22. FingersScript.Instance.VirtualTouchCountHandler = GetVirtualTouchCount;
  23. FingersScript.Instance.VirtualTouchObjectHandler = id => _virtualTouches.ElementAt(id).Value;
  24. FingersScript.Instance.VirtualTouchUpdateHandler = RemoveExpiredTouches;
  25. FingersScript.Instance.VirtualTouchResetHandler = () =>
  26. {
  27. _recordedTouches.Clear();
  28. _virtualTouches.Clear();
  29. };
  30. }
  31.  
  32. private void OnTouchStarted(int touchId)
  33. {
  34. Vector2 touchPos = TouchManager.GetPosition(touchId);
  35. if (touchPos == Vector2.zero) return;
  36.  
  37. _recordedTouches[touchId] = touchPos;
  38.  
  39. AddTouch(touchId, touchPos, UnityEngine.TouchPhase.Began);
  40. }
  41.  
  42. private void OnTouchStopped(int touchId)
  43. {
  44. Vector2 touchPos = TouchManager.GetPosition(touchId);
  45.  
  46. AddTouch(touchId, touchPos, UnityEngine.TouchPhase.Ended);
  47.  
  48. _recordedTouches.Remove(touchId);
  49. }
  50.  
  51. private int GetVirtualTouchCount()
  52. {
  53. ProcessVirtualTouches();
  54.  
  55. return _virtualTouches.Count;
  56. }
  57.  
  58. private void ProcessVirtualTouches()
  59. {
  60. for (int i = _recordedTouches.Count - 1; i >= 0; i--)
  61. {
  62. KeyValuePair<int, Vector2> current = _recordedTouches.ElementAt(i);
  63. Vector2 pos = TouchManager.GetPosition(current.Key);
  64.  
  65. UnityEngine.TouchPhase phase = Mathf.Approximately(Vector2.Distance(pos, current.Value), 0.01f)
  66. ? UnityEngine.TouchPhase.Stationary
  67. : UnityEngine.TouchPhase.Moved;
  68.  
  69. AddTouch(current.Key, pos, phase);
  70. }
  71. }
  72.  
  73. private void AddTouch(int id, Vector2 position, UnityEngine.TouchPhase phase, float pressure = 1.0f)
  74. {
  75. GestureTouch touch = FingersScript.Instance.GestureTouchFromVirtualTouch(id, position, phase, pressure);
  76. _virtualTouches[id] = touch;
  77. }
  78.  
  79. private void CreateGestures()
  80. {
  81. TapGestureRecognizer tap = new TapGestureRecognizer();
  82. tap.StateUpdated += gesture =>
  83. {
  84. Debug.Log($"State updated: {gesture.State}");
  85.  
  86. if (gesture.State == GestureRecognizerState.Ended)
  87. {
  88. Debug.Log($"Tap detected at {gesture.FocusX}, {gesture.FocusY}");
  89. }
  90. };
  91. FingersScript.Instance.AddGesture(tap);
  92. }
  93.  
  94. private void RemoveExpiredTouches()
  95. {
  96. foreach (GestureTouch touch in _virtualTouches.Values)
  97. {
  98. if (touch.TouchPhase == DigitalRubyShared.TouchPhase.Ended ||
  99. touch.TouchPhase == DigitalRubyShared.TouchPhase.Cancelled ||
  100. touch.TouchPhase == DigitalRubyShared.TouchPhase.Unknown)
  101. {
  102. idsToRemove.Add(touch.Id);
  103. }
  104. }
  105.  
  106. foreach (int id in idsToRemove)
  107. {
  108. _recordedTouches.Remove(id);
  109. _virtualTouches.Remove(id);
  110. }
  111.  
  112. idsToRemove.Clear();
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement