GvrPointerInputModule.cs
11 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//-----------------------------------------------------------------------
// <copyright file="GvrPointerInputModule.cs" company="Google Inc.">
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the MIT License, you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
//
// http://www.opensource.org/licenses/mit-license.php
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-----------------------------------------------------------------------
using System.Collections.Generic;
using Gvr.Internal;
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>This script provides an implemention of Unity's `BaseInputModule` class.</summary>
/// <remarks><para>
/// Exists so that Canvas-based (`uGUI`) UI elements and 3D scene objects can be interacted with in
/// a Gvr Application.
/// </para><para>
/// This script is intended for use with either a 3D Pointer with the Daydream Controller
/// (Recommended for Daydream), or a Gaze-based-Pointer (Recommended for Cardboard).
/// </para><para>
/// To use, attach to the scene's **EventSystem** object. Be sure to move it above the
/// other modules, such as `TouchInputModule` and `StandaloneInputModule`, in order
/// for the Pointer to take priority in the event system.
/// </para><para>
/// If you are using a **Canvas**, set the `Render Mode` to **World Space**, and add the
/// `GvrPointerGraphicRaycaster` script to the object.
/// </para><para>
/// If you'd like pointers to work with 3D scene objects, add a `GvrPointerPhysicsRaycaster` to the
/// main camera, and add a component that implements one of the `Event` interfaces (`EventTrigger`
/// will work nicely) to an object with a collider.
/// </para><para>
/// `GvrPointerInputModule` emits the following events: `Enter`, `Exit`, `Down`, `Up`, `Click`,
/// `Select`, `Deselect`, `UpdateSelected`, and `GvrPointerHover`. Scroll, move, and submit/cancel
/// events are not emitted.
/// </para><para>
/// To use a 3D Pointer with the Daydream Controller:
/// - Add the prefab GoogleVR/Prefabs/UI/GvrControllerPointer to your scene.
/// - Set the parent of `GvrControllerPointer` to the same parent as the main camera
/// (With a local position of 0,0,0).
/// </para><para>
/// To use a Gaze-based-pointer:
/// - Add the prefab GoogleVR/Prefabs/UI/GvrReticlePointer to your scene.
/// - Set the parent of `GvrReticlePointer` to the main camera.
/// </para></remarks>
[AddComponentMenu("GoogleVR/GvrPointerInputModule")]
[HelpURL("https://developers.google.com/vr/unity/reference/class/GvrPointerInputModule")]
public class GvrPointerInputModule : BaseInputModule, IGvrInputModuleController
{
/// <summary>
/// If `true`, pointer input is active in VR Mode only.
/// If `false`, pointer input is active all of the time.
/// </summary>
/// <remarks>
/// Set to false if you plan to use direct screen taps or other input when not in VR Mode.
/// </remarks>
[Tooltip("Whether Pointer input is active in VR Mode only (true), or all the time (false).")]
public bool vrModeOnly = false;
/// <summary>Manages scroll events for the input module.</summary>
[Tooltip("Manages scroll events for the input module.")]
public GvrPointerScrollInput scrollInput = new GvrPointerScrollInput();
/// <summary>Gets or sets the static reference to the `GvrBasePointer`.</summary>
/// <value>The static reference to the `GvrBasePointer`.</value>
public static GvrBasePointer Pointer
{
get
{
GvrPointerInputModule module = FindInputModule();
if (module == null || module.Impl == null)
{
return null;
}
return module.Impl.Pointer;
}
set
{
GvrPointerInputModule module = FindInputModule();
if (module == null || module.Impl == null)
{
return;
}
module.Impl.Pointer = value;
}
}
/// <summary>Gets the current `RaycastResult`.</summary>
/// <value>The current `RaycastResult`.</value>
public static RaycastResult CurrentRaycastResult
{
get
{
GvrPointerInputModule inputModule = GvrPointerInputModule.FindInputModule();
if (inputModule == null)
{
return new RaycastResult();
}
if (inputModule.Impl == null)
{
return new RaycastResult();
}
if (inputModule.Impl.CurrentEventData == null)
{
return new RaycastResult();
}
return inputModule.Impl.CurrentEventData.pointerCurrentRaycast;
}
}
/// <summary>Gets the implementation object of this module.</summary>
/// <value>The implementation object of this module.</value>
public GvrPointerInputModuleImpl Impl { get; private set; }
/// <summary>Gets the executor this module uses to process events.</summary>
/// <value>The executor this module uses to process events.</value>
public GvrEventExecutor EventExecutor { get; private set; }
/// <summary>Gets the event system reference.</summary>
/// <value>The event system reference.</value>
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"UnityRules.LegacyGvrStyleRules",
"VR1001:AccessibleNonConstantPropertiesMustBeUpperCamelCase",
Justification = "Legacy Public API.")]
public new EventSystem eventSystem
{
get
{
return base.eventSystem;
}
}
/// <summary>Gets the list of raycast results used as a cache.</summary>
/// <value>The list of raycast results used as a cache.</value>
public List<RaycastResult> RaycastResultCache
{
get
{
return m_RaycastResultCache;
}
}
/// <summary>The `GvrBasePointer` calls this when it is created.</summary>
/// <remarks>
/// If a pointer hasn't already been assigned, it will assign the newly created one by default.
/// This simplifies the common case of having only one `GvrBasePointer` so it can be
/// automatically hooked up to the manager. If multiple `GvrBasePointers` are in the scene,
/// the app has to take responsibility for setting which one is active.
/// </remarks>
/// <param name="createdPointer">The pointer whose creation triggered this call.</param>
public static void OnPointerCreated(GvrBasePointer createdPointer)
{
GvrPointerInputModule module = FindInputModule();
if (module == null || module.Impl == null)
{
return;
}
if (module.Impl.Pointer == null)
{
module.Impl.Pointer = createdPointer;
}
}
/// <summary>
/// Helper function to find the Event executor that is part of the input module if one exists
/// in the scene.
/// </summary>
/// <returns>A found GvrEventExecutor or null.</returns>
public static GvrEventExecutor FindEventExecutor()
{
GvrPointerInputModule gvrInputModule = FindInputModule();
if (gvrInputModule == null)
{
return null;
}
return gvrInputModule.EventExecutor;
}
/// <summary>
/// Helper function to find the input module if one exists in the scene and it is the active
/// module.
/// </summary>
/// <returns>A found `GvrPointerInputModule` or null.</returns>
public static GvrPointerInputModule FindInputModule()
{
if (EventSystem.current == null)
{
return null;
}
EventSystem eventSystem = EventSystem.current;
if (eventSystem == null)
{
return null;
}
GvrPointerInputModule gvrInputModule =
eventSystem.GetComponent<GvrPointerInputModule>();
return gvrInputModule;
}
/// <inheritdoc/>
[SuppressMemoryAllocationError(IsWarning = true, Reason = "Pending documentation.")]
public override bool ShouldActivateModule()
{
return Impl.ShouldActivateModule();
}
/// <inheritdoc/>
[SuppressMemoryAllocationError(IsWarning = true, Reason = "Pending documentation.")]
public override void DeactivateModule()
{
Impl.DeactivateModule();
}
/// <inheritdoc/>
public override bool IsPointerOverGameObject(int pointerId)
{
return Impl.IsPointerOverGameObject(pointerId);
}
/// <inheritdoc/>
[SuppressMemoryAllocationError(IsWarning = true, Reason = "Pending documentation.")]
public override void Process()
{
UpdateImplProperties();
Impl.Process();
}
/// <summary>Whether the module should be activated.</summary>
/// <returns>Returns `true` if this module should be activated, `false` otherwise.</returns>
[SuppressMemoryAllocationError(IsWarning = true, Reason = "Pending documentation.")]
public bool ShouldActivate()
{
return base.ShouldActivateModule();
}
/// <summary>Deactivate this instance.</summary>
public void Deactivate()
{
base.DeactivateModule();
}
/// <summary>Finds the common root between two `GameObject`s.</summary>
/// <returns>The common root.</returns>
/// <param name="g1">The first `GameObject`.</param>
/// <param name="g2">The second `GameObject`.</param>
[SuppressMemoryAllocationError(IsWarning = true, Reason = "Pending documentation.")]
public new GameObject FindCommonRoot(GameObject g1, GameObject g2)
{
return BaseInputModule.FindCommonRoot(g1, g2);
}
/// <summary>Gets the base event data.</summary>
/// <returns>The base event data.</returns>
[SuppressMemoryAllocationError(IsWarning = true, Reason = "Pending documentation.")]
public new BaseEventData GetBaseEventData()
{
return base.GetBaseEventData();
}
/// <summary>Finds the first raycast.</summary>
/// <returns>The first raycast.</returns>
/// <param name="candidates">
/// The list of `RaycastResult`s to search for the first Raycast.
/// </param>
public new RaycastResult FindFirstRaycast(List<RaycastResult> candidates)
{
return BaseInputModule.FindFirstRaycast(candidates);
}
/// @cond
/// <inheritdoc/>
protected override void Awake()
{
base.Awake();
Impl = new GvrPointerInputModuleImpl();
EventExecutor = new GvrEventExecutor();
UpdateImplProperties();
}
/// @endcond
/// <summary>Update implementation properties.</summary>
private void UpdateImplProperties()
{
if (Impl == null)
{
return;
}
Impl.ScrollInput = scrollInput;
Impl.VrModeOnly = vrModeOnly;
Impl.ModuleController = this;
Impl.EventExecutor = EventExecutor;
}
}