forked from TouchScript/TouchScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTouchLayer.cs
More file actions
246 lines (205 loc) · 8.16 KB
/
Copy pathTouchLayer.cs
File metadata and controls
246 lines (205 loc) · 8.16 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* @author Valentin Simonov / http://va.lent.in/
*/
using System;
using System.Collections;
using TouchScript.Hit;
using TouchScript.Utils;
using UnityEngine;
namespace TouchScript.Layers
{
/// <summary>
/// Base class for all touch layers. Used to check if some object is hit by a touch point.
/// <seealso cref="ITouchManager"/>
/// <seealso cref="ITouchHit"/>
/// <seealso cref="ITouch"/>
/// </summary>
/// <remarks>
/// <para>In <b>TouchScript</b> it's a layer's job to determine if a touch on the screen hits anything in Unity's 3d/2d world.</para>
/// <para><see cref="ITouchManager"/> keeps a sorted list of all layers in <see cref="ITouchManager.Layers"/> which it queries when a new touch appears. It's a layer's job to return <see cref="LayerHitResult.Hit"/> if this touch hits an object. Layers can even be used to "hit" objects outside of Unity's 3d world, for example <b>Scaleform</b> integration is implemented this way.</para>
/// <para>Layers can be configured in a scene using <see cref="TouchManager"/> or from code using <see cref="ITouchManager"/> API.</para>
/// <para>If you want to route touches and manually control which objects they should "touch" it's better to create a new layer extending <see cref="TouchLayer"/>.</para>
/// </remarks>
[ExecuteInEditMode]
public abstract class TouchLayer : MonoBehaviour
{
#region Constants
/// <summary>
/// Result of a touch's hit test with a layer.
/// </summary>
public enum LayerHitResult
{
/// <summary>
/// Something wrong happened.
/// </summary>
Error = 0,
/// <summary>
/// Touch hit an object.
/// </summary>
Hit = 1,
/// <summary>
/// Touch didn't hit any object.
/// </summary>
Miss = 2
}
#endregion
#region Events
/// <summary>
/// Occurs when layer determines that a touch has hit something.
/// </summary>
public event EventHandler<TouchLayerEventArgs> TouchBegan
{
add { touchBeganInvoker += value; }
remove { touchBeganInvoker -= value; }
}
// Needed to overcome iOS AOT limitations
private EventHandler<TouchLayerEventArgs> touchBeganInvoker;
#endregion
#region Public properties
/// <summary>
/// Touch layer's name.
/// </summary>
public String Name;
/// <summary>
/// Layer's screen to world projection normal.
/// </summary>
public virtual Vector3 WorldProjectionNormal { get { return Vector3.forward; } }
/// <summary>
/// Layer's world projection origin.
/// </summary>
public virtual Vector3 LayerOrigin { get { return Vector3.zero; } }
#endregion
#region Public methods
/// <summary>
/// Checks if a point in screen coordinates hits something in this layer.
/// </summary>
/// <param name="position">Position in screen coordinates.</param>
/// <param name="hit">Hit result.</param>
/// <returns><see cref="LayerHitResult.Hit"/>, if an object is hit, <see cref="LayerHitResult.Miss"/> or <see cref="LayerHitResult.Error"/> otherwise.</returns>
public virtual LayerHitResult Hit(Vector2 position, out ITouchHit hit)
{
hit = null;
if (enabled == false || gameObject.activeInHierarchy == false) return LayerHitResult.Miss;
return LayerHitResult.Error;
}
/// <summary>
/// Projects a screen point on a plane defined by origin and normal.
/// </summary>
/// <param name="screenPosition">Screen point to project.</param>
/// <param name="origin">Projection plane's (0,0,0).</param>
/// <param name="normal">Projection plane's normal.</param>
/// <returns>Projected point in world coordinates.</returns>
public abstract Vector3 ProjectTo(Vector2 screenPosition, Vector3 origin, Vector3 normal);
#endregion
#region Unity methods
/// <summary>
/// Unity Awake callback.
/// </summary>
protected virtual void Awake()
{
setName();
if (Application.isPlaying) StartCoroutine(lateAwake());
}
/// <summary>
/// Unity OnDestroy callback.
/// </summary>
protected virtual void OnDestroy()
{
if (Application.isPlaying && TouchManager.Instance != null) TouchManager.Instance.RemoveLayer(this);
}
#endregion
#region Internal methods
internal bool BeginTouch(TouchPoint touch)
{
ITouchHit hit;
var result = beginTouch(touch, out hit);
if (result == LayerHitResult.Hit)
{
touch.Layer = this;
touch.Hit = hit;
if (hit != null) touch.Target = hit.Transform;
if (touchBeganInvoker != null) touchBeganInvoker(this, new TouchLayerEventArgs(touch));
return true;
}
return false;
}
internal void UpdateTouch(ITouch touch)
{
updateTouch(touch);
}
internal void EndTouch(ITouch touch)
{
endTouch(touch);
}
internal void CancelTouch(ITouch touch)
{
cancelTouch(touch);
}
#endregion
#region Protected functions
/// <summary>
/// Updates touch layers's name.
/// </summary>
protected virtual void setName()
{
Name = "undefined";
}
/// <summary>
/// Called when a layer is touched to query the layer if this touch hits something.
/// </summary>
/// <param name="touch">Touch.</param>
/// <param name="hit">Hit result.</param>
/// <returns><see cref="LayerHitResult.Hit"/>, if an object is hit, <see cref="LayerHitResult.Miss"/> or <see cref="LayerHitResult.Error"/> otherwise.</returns>
/// <remarks>This method may also be used to update some internal state or resend this event somewhere.</remarks>
protected virtual LayerHitResult beginTouch(ITouch touch, out ITouchHit hit)
{
var result = Hit(touch.Position, out hit);
return result;
}
/// <summary>
/// Called when a touch is moved.
/// </summary>
/// <param name="touch">Touch.</param>
/// <remarks>This method may also be used to update some internal state or resend this event somewhere.</remarks>
protected virtual void updateTouch(ITouch touch) {}
/// <summary>
/// Called when a touch ends.
/// </summary>
/// <param name="touch">Touch.</param>
/// <remarks>This method may also be used to update some internal state or resend this event somewhere.</remarks>
protected virtual void endTouch(ITouch touch) {}
/// <summary>
/// Called when a touch is cancelled.
/// </summary>
/// <param name="touch">Touch.</param>
/// <remarks>This method may also be used to update some internal state or resend this event somewhere.</remarks>
protected virtual void cancelTouch(ITouch touch) {}
#endregion
#region Private functions
private IEnumerator lateAwake()
{
yield return new WaitForEndOfFrame();
TouchManager.Instance.AddLayer(this);
}
#endregion
}
/// <summary>
/// Arguments used with <see cref="TouchLayer"/> events.
/// </summary>
public class TouchLayerEventArgs : EventArgs
{
/// <summary>
/// Gets the touch associated with the event.
/// </summary>
public ITouch Touch { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="TouchLayerEventArgs"/> class.
/// </summary>
/// <param name="touch">The touch associated with the event.</param>
public TouchLayerEventArgs(ITouch touch)
: base()
{
Touch = touch;
}
}
}