Repository navigation
Expand file tree
/
Copy pathTouchInputDevice.cs
More file actions
81 lines (72 loc) · 2.65 KB
/
Copy pathTouchInputDevice.cs
File metadata and controls
81 lines (72 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using UnityEngine;
namespace Code.Services.Input.Device
{
public class TouchInputDevice : IInputDevice
{
private int? _aimingFinger;
public Vector2 Direction { get; private set; }
public Vector3 TouchPositionToWorldPosition { get; private set; }
public Vector3 TouchPosition { get; private set; }
public bool CanAim => _aimingFinger.HasValue;
public event Action<Vector2> TapEvent;
public event Action<Vector2> AlternatePointerEvent;
public event Action PointerDownEvent;
public event Action PointerUpEvent;
public void UpdateInput(Func<Vector2, int, bool> allowsPointer)
{
int count = UnityEngine.Input.touchCount;
if (count == 0)
{
CancelGesture();
return;
}
int? aimingFingerAtStart = _aimingFinger;
for (int i = 0; i < count; i++)
{
Touch touch = UnityEngine.Input.GetTouch(i);
if (touch.fingerId == _aimingFinger)
{
UpdateAimingFinger(touch, allowsPointer);
}
else if (touch.phase == TouchPhase.Began && allowsPointer(touch.position, touch.fingerId))
{
if (aimingFingerAtStart.HasValue || _aimingFinger.HasValue)
AlternatePointerEvent?.Invoke(touch.position);
else
BeginAim(touch);
}
}
}
public void CancelGesture()
{
_aimingFinger = null;
Direction = Vector2.zero;
}
private void BeginAim(Touch touch)
{
_aimingFinger = touch.fingerId;
TouchPosition = touch.position;
PointerDownEvent?.Invoke();
}
private void UpdateAimingFinger(Touch touch, Func<Vector2, int, bool> allowsPointer)
{
Direction = touch.position - (Vector2)TouchPosition;
TouchPosition = touch.position;
Camera camera = Camera.main;
if (camera != null)
TouchPositionToWorldPosition = camera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10f));
if (touch.phase == TouchPhase.Ended)
{
if (allowsPointer(touch.position, touch.fingerId))
TapEvent?.Invoke(touch.position);
PointerUpEvent?.Invoke();
CancelGesture();
}
else if (touch.phase == TouchPhase.Canceled)
{
CancelGesture();
}
}
}
}