Repository navigation
Expand file tree
/
Copy pathWatsonCamera.cs
More file actions
387 lines (324 loc) · 10.9 KB
/
Copy pathWatsonCamera.cs
File metadata and controls
387 lines (324 loc) · 10.9 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* Copyright 2015 IBM Corp. 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.
*
*/
using UnityEngine;
using System.Collections.Generic;
using IBM.Watson.DeveloperCloud.Logging;
using IBM.Watson.DeveloperCloud.Utilities;
namespace IBM.Watson.DeveloperCloud.Camera
{
/// <summary>
/// Watson camera. The main responsible camera on the scene of the Watson applications which handles the camera movements via touch / keyboard inputs / voice commands.
/// </summary>
public class WatsonCamera : MonoBehaviour
{
#region Private Variables
private static WatsonCamera mp_Instance;
private List<CameraTarget> m_ListCameraTarget = new List<CameraTarget>();
private CameraTarget m_TargetCamera = null;
protected Vector3 m_CameraInitialLocation;
protected Quaternion m_CameraInitialRotation;
[SerializeField]
protected float m_PanSpeed = 0.07f;
[SerializeField]
protected float m_ZoomSpeed = 20.0f;
[SerializeField]
protected float m_SpeedForCameraAnimation = 2f;
private float m_CommandMovementModifier = 10.0f;
[SerializeField]
private MonoBehaviour m_AntiAliasing;
[SerializeField]
private MonoBehaviour m_DepthOfField;
protected bool m_DisableInteractivity = false;
#endregion
#region Public Variable
/// <summary>
/// Static instance of WatsonCamera.
/// </summary>
public static WatsonCamera Instance
{
get
{
return mp_Instance;
}
}
/// <summary>
/// The camera's current target.
/// </summary>
public CameraTarget CurrentCameraTarget
{
get
{
if (m_TargetCamera == null)
{
InitializeCameraTargetList();
}
return m_TargetCamera;
}
set
{
if (value != null)
{
m_TargetCamera = value;
if (!m_ListCameraTarget.Contains(value))
{
m_ListCameraTarget.Add(value);
}
}
else
{ //Delete current camera and clear from the list
if (m_ListCameraTarget.Contains(m_TargetCamera))
{
m_ListCameraTarget.Remove(m_TargetCamera);
}
if (m_ListCameraTarget.Count > 0)
{
m_TargetCamera = m_ListCameraTarget[m_ListCameraTarget.Count - 1];
}
else
{
InitializeCameraTargetList();
}
}
}
}
/// <summary>
/// The camera's default target.
/// </summary>
public CameraTarget DefaultCameraTarget
{
get
{
if (m_ListCameraTarget == null || m_ListCameraTarget.Count == 0)
InitializeCameraTargetList();
return m_ListCameraTarget[0];
}
}
#endregion
#region Event Registration
protected virtual void OnEnable()
{
EventManager.Instance.RegisterEventReceiver("OnCameraReset", ResetCameraPosition);
EventManager.Instance.RegisterEventReceiver("OnCameraSetAntiAliasing", OnCameraSetAntiAliasing);
EventManager.Instance.RegisterEventReceiver("OnCameraSetDepthOfField", OnCameraSetDepthOfField);
EventManager.Instance.RegisterEventReceiver("OnCameraSetInteractivity", OnCameraSetTwoFingerDrag);
}
protected virtual void OnDisable()
{
EventManager.Instance.UnregisterEventReceiver("OnCameraReset", ResetCameraPosition);
EventManager.Instance.UnregisterEventReceiver("OnCameraSetAntiAliasing", OnCameraSetAntiAliasing);
EventManager.Instance.UnregisterEventReceiver("OnCameraSetDepthOfField", OnCameraSetDepthOfField);
EventManager.Instance.UnregisterEventReceiver("OnCameraSetInteractivity", OnCameraSetTwoFingerDrag);
}
#endregion
#region Start / Update
protected virtual void Awake()
{
mp_Instance = this;
}
protected virtual void Start()
{
m_CameraInitialLocation = transform.localPosition;
m_CameraInitialRotation = transform.rotation;
}
protected virtual void Update()
{
CameraPositionOnUpdate();
}
protected virtual void CameraPositionOnUpdate()
{
//For Zooming and Panning
if (CurrentCameraTarget != null)
{
transform.localPosition = Vector3.Lerp(transform.localPosition, CurrentCameraTarget.TargetPosition, Time.deltaTime * m_SpeedForCameraAnimation);
transform.rotation = Quaternion.Slerp(transform.localRotation, CurrentCameraTarget.TargetRotation, Time.deltaTime * m_SpeedForCameraAnimation);
}
}
protected virtual void InitializeCameraTargetList()
{
if (m_ListCameraTarget == null)
m_ListCameraTarget = new List<CameraTarget>();
for (int i = 0; m_ListCameraTarget != null && i < m_ListCameraTarget.Count; i++)
{
Destroy(m_ListCameraTarget[i]);
}
m_ListCameraTarget.Clear();
CameraTarget defaultCameraTarget = this.gameObject.GetComponent<CameraTarget>();
if (defaultCameraTarget == null)
defaultCameraTarget = this.gameObject.AddComponent<CameraTarget>();
defaultCameraTarget.TargetPosition = m_CameraInitialLocation;
if (defaultCameraTarget.TargetObject == null)
{
defaultCameraTarget.TargetRotation = m_CameraInitialRotation;
}
else
{
defaultCameraTarget.TargetObject = defaultCameraTarget.TargetObject;
}
m_ListCameraTarget.Add(defaultCameraTarget);
m_TargetCamera = m_ListCameraTarget[0];
}
#endregion
#region Touch Drag Actions
/// <summary>
/// Event handler to pan and zoom with two-finger dragging
/// </summary>
/// <param name="args">Arguments.</param>
public virtual void DragTwoFinger(System.Object[] args)
{
if (m_DisableInteractivity)
return;
if (args != null && args.Length > 0 && args[0] is TouchScript.Gestures.ScreenTransformGesture)
{
TouchScript.Gestures.ScreenTransformGesture transformGesture = args[0] as TouchScript.Gestures.ScreenTransformGesture;
//Pannning with 2-finger
DefaultCameraTarget.TargetPosition += (transformGesture.DeltaPosition * m_PanSpeed * -1.0f);
//Zooming with 2-finger
DefaultCameraTarget.TargetPosition += transform.forward * (transformGesture.DeltaScale - 1.0f) * m_ZoomSpeed;
}
else
{
Log.Warning("WatsonCamera", "TwoFinger drag has invalid argument");
}
}
#endregion
#region Camera Events Received from Outside - Set default position / Move Left - Right - Up - Down / Zoom-in-out
/// <summary>
/// Event Handler for setting Antialiasing event
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void OnCameraSetAntiAliasing(System.Object[] args)
{
if (args != null && args.Length > 0 && args[0] is bool)
{
bool valueSet = (bool)args[0];
if (m_AntiAliasing != null)
{
m_AntiAliasing.enabled = valueSet;
}
}
}
/// <summary>
/// Event Handler for setting Depth of Field event
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void OnCameraSetDepthOfField(System.Object[] args)
{
if (args != null && args.Length > 0 && args[0] is bool)
{
bool valueSet = (bool)args[0];
if (m_DepthOfField != null)
{
m_DepthOfField.enabled = valueSet;
}
}
}
/// <summary>
/// Event Handler for Two Finger Drag
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void OnCameraSetTwoFingerDrag(System.Object[] args)
{
if (args != null && args.Length > 0 && args[0] is bool)
{
m_DisableInteractivity = !(bool)args[0];
}
}
/// <summary>
/// Event handler reseting the camera. Deleting all camera target and set the initial as default.
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void ResetCamera(System.Object[] args)
{
if (m_DisableInteractivity)
return;
InitializeCameraTargetList();
}
/// <summary>
/// Event handler reseting the camera position.
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void ResetCameraPosition(System.Object[] args)
{
if (m_DisableInteractivity)
return;
//Log.Status("WatsonCamera", "Reset Camera Position");
CurrentCameraTarget.TargetPosition = m_CameraInitialLocation;
CurrentCameraTarget.TargetRotation = m_CameraInitialRotation;
}
/// <summary>
/// Event handler moving the camera up.
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void MoveUp(System.Object[] args)
{
if (m_DisableInteractivity)
return;
DefaultCameraTarget.TargetPosition += this.transform.up * m_CommandMovementModifier;
}
/// <summary>
/// Event handler moving the camera down.
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void MoveDown(System.Object[] args)
{
if (m_DisableInteractivity)
return;
DefaultCameraTarget.TargetPosition += this.transform.up * -m_CommandMovementModifier;
}
/// <summary>
/// Event handler moving the camera left.
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void MoveLeft(System.Object[] args)
{
if (m_DisableInteractivity)
return;
DefaultCameraTarget.TargetPosition += this.transform.right * -m_CommandMovementModifier;
}
/// <summary>
/// Event handler moving the camera right.
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void MoveRight(System.Object[] args)
{
if (m_DisableInteractivity)
return;
DefaultCameraTarget.TargetPosition += this.transform.right * m_CommandMovementModifier;
}
/// <summary>
/// Event handler zooming-in the camera.
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void ZoomIn(System.Object[] args)
{
if (m_DisableInteractivity)
return;
DefaultCameraTarget.TargetPosition += transform.forward * m_ZoomSpeed;
}
/// <summary>
/// Event handler zooming-out the camera.
/// </summary>
/// <param name="args">Arguments.</param>
protected virtual void ZoomOut(System.Object[] args)
{
if (m_DisableInteractivity)
return;
DefaultCameraTarget.TargetPosition += transform.forward * m_ZoomSpeed * -1.0f;
}
#endregion
}
}