EmulatorManager.cs
9.47 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
315
316
317
318
319
320
321
//-----------------------------------------------------------------------
// <copyright file="EmulatorManager.cs" company="Google Inc.">
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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>
//-----------------------------------------------------------------------
// This class is only used in the Editor, so make sure to only compile it on that platform.
// Additionally, it depends on EmulatorClientSocket which is only compiled in the editor.
// This MonoBehaviour is only ever instantiated dynamically, so it is fine that it is only compiled in the Editor,
// Otherwise it would cause serialization issues.
#if UNITY_EDITOR
using System.Collections;
using UnityEngine;
using proto;
/// @cond
namespace Gvr.Internal
{
class EmulatorManager : MonoBehaviour
{
private IEnumerator emulatorUpdate;
private WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
public static EmulatorManager Instance
{
get
{
if (instance == null)
{
var gameObject = new GameObject("PhoneRemote");
instance = gameObject.AddComponent<EmulatorManager>();
// This object should survive all scene transitions.
GameObject.DontDestroyOnLoad(instance);
}
return instance;
}
}
private static EmulatorManager instance = null;
public delegate void OnGyroEvent(EmulatorGyroEvent gyroEvent);
public event OnGyroEvent gyroEventListeners
{
add
{
if (value != null)
{
value(currentGyroEvent);
}
gyroEventListenersInternal += value;
}
remove
{
gyroEventListenersInternal -= value;
}
}
public delegate void OnAccelEvent(EmulatorAccelEvent accelEvent);
public event OnAccelEvent accelEventListeners
{
add
{
if (value != null)
{
value(currentAccelEvent);
}
accelEventListenersInternal += value;
}
remove
{
accelEventListenersInternal -= value;
}
}
public delegate void OnTouchEvent(EmulatorTouchEvent touchEvent);
public event OnTouchEvent touchEventListeners
{
add
{
if (value != null && currentTouchEvent.pointers != null /* null only during init */)
{
value(currentTouchEvent);
}
touchEventListenersInternal += value;
}
remove
{
touchEventListenersInternal -= value;
}
}
public delegate void OnOrientationEvent(EmulatorOrientationEvent orientationEvent);
public event OnOrientationEvent orientationEventListeners
{
add
{
if (value != null)
{
value(currentOrientationEvent);
}
orientationEventListenersInternal += value;
}
remove
{
orientationEventListenersInternal -= value;
}
}
public delegate void OnButtonEvent(EmulatorButtonEvent buttonEvent);
public event OnButtonEvent buttonEventListeners
{
add
{
if (value != null)
{
value(currentButtonEvent);
}
buttonEventListenersInternal += value;
}
remove
{
buttonEventListenersInternal -= value;
}
}
private void onGyroEvent(EmulatorGyroEvent e)
{
currentGyroEvent = e;
if (gyroEventListenersInternal != null)
{
gyroEventListenersInternal(e);
}
}
private void onAccelEvent(EmulatorAccelEvent e)
{
currentAccelEvent = e;
if (accelEventListenersInternal != null)
{
accelEventListenersInternal(e);
}
}
private void onTouchEvent(EmulatorTouchEvent e)
{
currentTouchEvent = e;
if (touchEventListenersInternal != null)
{
touchEventListenersInternal(e);
}
}
private void onOrientationEvent(EmulatorOrientationEvent e)
{
currentOrientationEvent = e;
if (orientationEventListenersInternal != null)
{
orientationEventListenersInternal(e);
}
}
private void onButtonEvent(EmulatorButtonEvent e)
{
currentButtonEvent = e;
if (buttonEventListenersInternal != null)
{
buttonEventListenersInternal(e);
}
}
EmulatorGyroEvent currentGyroEvent;
EmulatorAccelEvent currentAccelEvent;
EmulatorTouchEvent currentTouchEvent;
EmulatorOrientationEvent currentOrientationEvent;
EmulatorButtonEvent currentButtonEvent;
private event OnGyroEvent gyroEventListenersInternal;
private event OnAccelEvent accelEventListenersInternal;
private event OnTouchEvent touchEventListenersInternal;
private event OnOrientationEvent orientationEventListenersInternal;
private event OnButtonEvent buttonEventListenersInternal;
private Queue pendingEvents = Queue.Synchronized(new Queue());
private EmulatorClientSocket socket;
private long lastDownTimeMs;
public bool Connected
{
get
{
return socket != null && socket.connected == EmulatorClientSocketConnectionState.Connected;
}
}
public bool Connecting
{
get
{
return socket != null && socket.connected == EmulatorClientSocketConnectionState.Connecting;
}
}
public void Awake()
{
if (instance == null)
{
instance = this;
}
if (instance != this)
{
Debug.LogWarning("PhoneRemote must be a singleton.");
enabled = false;
return;
}
}
public void Start()
{
socket = gameObject.AddComponent<EmulatorClientSocket>();
socket.Init(this);
emulatorUpdate = EndOfFrame();
StartCoroutine(emulatorUpdate);
}
IEnumerator EndOfFrame()
{
while (true)
{
yield return waitForEndOfFrame;
lock (pendingEvents.SyncRoot)
{
while (pendingEvents.Count > 0)
{
PhoneEvent phoneEvent = (PhoneEvent)pendingEvents.Dequeue();
ProcessEventAtEndOfFrame(phoneEvent);
}
}
}
}
public void OnPhoneEvent(PhoneEvent e)
{
pendingEvents.Enqueue(e);
}
private void ProcessEventAtEndOfFrame(PhoneEvent e)
{
switch (e.Type)
{
case PhoneEvent.Types.Type.MOTION:
EmulatorTouchEvent touchEvent = new EmulatorTouchEvent(e.MotionEvent, lastDownTimeMs);
onTouchEvent(touchEvent);
if (touchEvent.getActionMasked() == EmulatorTouchEvent.Action.kActionDown)
{
lastDownTimeMs = e.MotionEvent.Timestamp;
}
break;
case PhoneEvent.Types.Type.GYROSCOPE:
EmulatorGyroEvent gyroEvent = new EmulatorGyroEvent(e.GyroscopeEvent);
onGyroEvent(gyroEvent);
break;
case PhoneEvent.Types.Type.ACCELEROMETER:
EmulatorAccelEvent accelEvent = new EmulatorAccelEvent(e.AccelerometerEvent);
onAccelEvent(accelEvent);
break;
case PhoneEvent.Types.Type.ORIENTATION:
EmulatorOrientationEvent orientationEvent =
new EmulatorOrientationEvent(e.OrientationEvent);
onOrientationEvent(orientationEvent);
break;
case PhoneEvent.Types.Type.KEY:
EmulatorButtonEvent buttonEvent = new EmulatorButtonEvent(e.KeyEvent);
onButtonEvent(buttonEvent);
break;
default:
Debug.Log("Unsupported PhoneEvent type: " + e.Type);
break;
}
}
}
}
/// @endcond
#endif // UNITY_EDITOR