Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using Assets.Scripts.Touch;
- using DigitalRubyShared;
- public class TouchController : MonoBehaviour
- {
- private readonly Dictionary<int, Vector2> _recordedTouches = new Dictionary<int, Vector2>();
- private readonly Dictionary<int, GestureTouch> _virtualTouches = new Dictionary<int, GestureTouch>();
- private readonly List<int> idsToRemove = new List<int>();
- private void Awake()
- {
- TouchManager.OnStart += OnTouchStarted;
- TouchManager.OnEnd += OnTouchStopped;
- CreateGestures();
- FingersScript.Instance.VirtualTouchCountHandler = GetVirtualTouchCount;
- FingersScript.Instance.VirtualTouchObjectHandler = id => _virtualTouches.ElementAt(id).Value;
- FingersScript.Instance.VirtualTouchUpdateHandler = RemoveExpiredTouches;
- FingersScript.Instance.VirtualTouchResetHandler = () =>
- {
- _recordedTouches.Clear();
- _virtualTouches.Clear();
- };
- }
- private void OnTouchStarted(int touchId)
- {
- Vector2 touchPos = TouchManager.GetPosition(touchId);
- if (touchPos == Vector2.zero) return;
- _recordedTouches[touchId] = touchPos;
- AddTouch(touchId, touchPos, UnityEngine.TouchPhase.Began);
- }
- private void OnTouchStopped(int touchId)
- {
- Vector2 touchPos = TouchManager.GetPosition(touchId);
- AddTouch(touchId, touchPos, UnityEngine.TouchPhase.Ended);
- _recordedTouches.Remove(touchId);
- }
- private int GetVirtualTouchCount()
- {
- ProcessVirtualTouches();
- return _virtualTouches.Count;
- }
- private void ProcessVirtualTouches()
- {
- for (int i = _recordedTouches.Count - 1; i >= 0; i--)
- {
- KeyValuePair<int, Vector2> current = _recordedTouches.ElementAt(i);
- Vector2 pos = TouchManager.GetPosition(current.Key);
- UnityEngine.TouchPhase phase = Mathf.Approximately(Vector2.Distance(pos, current.Value), 0.01f)
- ? UnityEngine.TouchPhase.Stationary
- : UnityEngine.TouchPhase.Moved;
- AddTouch(current.Key, pos, phase);
- }
- }
- private void AddTouch(int id, Vector2 position, UnityEngine.TouchPhase phase, float pressure = 1.0f)
- {
- GestureTouch touch = FingersScript.Instance.GestureTouchFromVirtualTouch(id, position, phase, pressure);
- _virtualTouches[id] = touch;
- }
- private void CreateGestures()
- {
- TapGestureRecognizer tap = new TapGestureRecognizer();
- tap.StateUpdated += gesture =>
- {
- Debug.Log($"State updated: {gesture.State}");
- if (gesture.State == GestureRecognizerState.Ended)
- {
- Debug.Log($"Tap detected at {gesture.FocusX}, {gesture.FocusY}");
- }
- };
- FingersScript.Instance.AddGesture(tap);
- }
- private void RemoveExpiredTouches()
- {
- foreach (GestureTouch touch in _virtualTouches.Values)
- {
- if (touch.TouchPhase == DigitalRubyShared.TouchPhase.Ended ||
- touch.TouchPhase == DigitalRubyShared.TouchPhase.Cancelled ||
- touch.TouchPhase == DigitalRubyShared.TouchPhase.Unknown)
- {
- idsToRemove.Add(touch.Id);
- }
- }
- foreach (int id in idsToRemove)
- {
- _recordedTouches.Remove(id);
- _virtualTouches.Remove(id);
- }
- idsToRemove.Clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement