-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathSwipeDetector.cs
134 lines (117 loc) · 4.59 KB
/
SwipeDetector.cs
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Oculus.Interaction;
using UnityEngine;
using UnityEngine.Events;
namespace CrypticCabinet.Puzzles.Safe
{
/// <summary>
/// Detects if the user hands (or the hands controllers) perform a swipe over a specific area.
/// This is used for the dial interactions. The mechanism uses three colliders, to detect swipe up or
/// swipe down actions with the user finger.
/// </summary>
public class SwipeDetector : MonoBehaviour
{
[SerializeField] private CollisionDetector m_topCollisionDetector;
[SerializeField] private CollisionDetector m_midCollisionDetector;
[SerializeField] private CollisionDetector m_botCollisionDetector;
[Space(10)]
[Header("Events")]
[Space(10)]
[Tooltip("Triggered when the end position of a swipe up action is reached")]
[SerializeField] private UnityEvent m_onSwipeUp;
[Tooltip("Triggered when the end position of a swipe down action is reached")]
[SerializeField] private UnityEvent m_onSwipeDown;
[Tooltip("Triggered when the start position of a swipe is reached")]
[SerializeField] private UnityEvent m_onSwipeReady;
[Tooltip("Triggered when the start position of a swipe is reached")]
[SerializeField] private UnityEvent m_onSwipeNotReady;
private bool m_midColliderEntered;
private void Start()
{
this.AssertField(m_topCollisionDetector, nameof(m_topCollisionDetector));
this.AssertField(m_midCollisionDetector, nameof(m_midCollisionDetector));
this.AssertField(m_botCollisionDetector, nameof(m_botCollisionDetector));
if (m_topCollisionDetector != null)
{
m_topCollisionDetector.OnTriggerEntered += TopColliderEntered;
}
if (m_midCollisionDetector != null)
{
m_midCollisionDetector.OnTriggerEntered += MidColliderEntered;
m_midCollisionDetector.OnTriggerExited += MidColliderExited;
}
if (m_botCollisionDetector != null)
{
m_botCollisionDetector.OnTriggerEntered += BotColliderEntered;
}
}
private void OnDestroy()
{
if (m_topCollisionDetector != null)
{
m_topCollisionDetector.OnTriggerEntered -= TopColliderEntered;
}
if (m_midCollisionDetector != null)
{
m_midCollisionDetector.OnTriggerEntered -= MidColliderEntered;
m_midCollisionDetector.OnTriggerExited -= MidColliderExited;
}
if (m_botCollisionDetector != null)
{
m_botCollisionDetector.OnTriggerEntered -= BotColliderEntered;
}
}
private void MidColliderEntered(Collider otherCollider)
{
Debug.Log("Dial middle collider trigger entered by: " + otherCollider.gameObject.name);
m_midColliderEntered = true;
ReadyForSwipe();
}
private void MidColliderExited(Collider otherCollider)
{
Debug.Log("Dial middle collider trigger exited by: " + otherCollider.gameObject.name);
m_midColliderEntered = false;
NotReadyForSwipe();
}
private void TopColliderEntered(Collider otherCollider)
{
Debug.Log("Dial top collider trigger entered by: " + otherCollider.gameObject.name);
if (!m_midColliderEntered)
{
return;
}
SwipeUpAction();
m_midColliderEntered = false;
}
private void BotColliderEntered(Collider otherCollider)
{
Debug.Log("Dial bottom collider trigger entered by: " + otherCollider.gameObject.name);
if (!m_midColliderEntered)
{
return;
}
SwipeDownAction();
m_midColliderEntered = false;
}
private void SwipeUpAction()
{
Debug.Log("Dial Swipe Up Action Triggered");
m_onSwipeUp?.Invoke();
}
private void SwipeDownAction()
{
Debug.Log("Dial Swipe Down Action Triggered");
m_onSwipeDown?.Invoke();
}
private void ReadyForSwipe()
{
Debug.Log("Dial is ready for a swipe");
m_onSwipeReady?.Invoke();
}
private void NotReadyForSwipe()
{
Debug.Log("Dial is not ready for a swipe");
m_onSwipeNotReady?.Invoke();
}
}
}