forked from mtysgithub/Qualcomm.Vuforia.UnityExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirtualButtonAbstractBehaviour.cs
390 lines (357 loc) · 11.3 KB
/
VirtualButtonAbstractBehaviour.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
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
388
389
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public abstract class VirtualButtonAbstractBehaviour : MonoBehaviour, IEditorVirtualButtonBehaviour
{
private List<IVirtualButtonEventHandler> mHandlers = new List<IVirtualButtonEventHandler>();
[HideInInspector, SerializeField]
private bool mHasUpdatedPose = false;
private Vector2 mLeftTop;
[SerializeField, HideInInspector]
private string mName = "";
private bool mPressed = false;
private bool mPreviouslyEnabled;
[SerializeField, HideInInspector]
private GameObject mPrevParent;
[SerializeField, HideInInspector]
private Matrix4x4 mPrevTransform = Matrix4x4.zero;
private Vector2 mRightBottom;
[SerializeField, HideInInspector]
private VirtualButton.Sensitivity mSensitivity = VirtualButton.Sensitivity.LOW;
private bool mSensitivityDirty = false;
private bool mUnregisterOnDestroy;
private VirtualButton mVirtualButton;
public const float TARGET_OFFSET = 0.001f;
public bool CalculateButtonArea(out Vector2 topLeft, out Vector2 bottomRight)
{
ImageTargetAbstractBehaviour imageTargetBehaviour = this.GetImageTargetBehaviour();
if (imageTargetBehaviour == null)
{
topLeft = bottomRight = Vector2.zero;
return false;
}
Vector3 vector = imageTargetBehaviour.transform.InverseTransformPoint(base.transform.position);
float num = imageTargetBehaviour.transform.lossyScale[0];
Vector2 vector2 = new Vector2(vector[0] * num, vector[2] * num);
Vector2 vector3 = new Vector2(base.transform.lossyScale[0], base.transform.lossyScale[2]);
Vector2 vector4 = Vector2.Scale((Vector2) (vector3 * 0.5f), new Vector2(1f, -1f));
topLeft = vector2 - vector4;
bottomRight = vector2 + vector4;
return true;
}
private static bool Equals(Vector2 vec1, Vector2 vec2, float threshold)
{
Vector2 vector = vec1 - vec2;
return ((Math.Abs(vector.x) < threshold) && (Math.Abs(vector.y) < threshold));
}
public ImageTargetAbstractBehaviour GetImageTargetBehaviour()
{
if (base.transform.parent != null)
{
for (GameObject obj2 = base.transform.parent.gameObject; obj2 != null; obj2 = obj2.transform.parent.gameObject)
{
ImageTargetAbstractBehaviour component = obj2.GetComponent<ImageTargetAbstractBehaviour>();
if (component != null)
{
return component;
}
if (obj2.transform.parent == null)
{
return null;
}
}
}
return null;
}
void IEditorVirtualButtonBehaviour.InitializeVirtualButton(VirtualButton virtualButton)
{
this.mVirtualButton = virtualButton;
}
bool IEditorVirtualButtonBehaviour.SetPosAndScaleFromButtonArea(Vector2 topLeft, Vector2 bottomRight)
{
ImageTargetAbstractBehaviour imageTargetBehaviour = this.GetImageTargetBehaviour();
if (imageTargetBehaviour == null)
{
return false;
}
float num = imageTargetBehaviour.transform.lossyScale[0];
Vector2 vector = (Vector2) ((topLeft + bottomRight) * 0.5f);
Vector2 vector2 = new Vector2(bottomRight[0] - topLeft[0], topLeft[1] - bottomRight[1]);
Vector3 position = new Vector3(vector[0] / num, 0.001f, vector[1] / num);
Vector3 vector4 = new Vector3(vector2[0], (vector2[0] + vector2[1]) * 0.5f, vector2[1]);
base.transform.position = imageTargetBehaviour.transform.TransformPoint(position);
base.transform.localScale = (Vector3) (vector4 / base.transform.parent.lossyScale[0]);
return true;
}
bool IEditorVirtualButtonBehaviour.SetPreviousParent(GameObject parent)
{
if (this.mVirtualButton == null)
{
this.mPrevParent = parent;
return true;
}
return false;
}
bool IEditorVirtualButtonBehaviour.SetPreviousTransform(Matrix4x4 transform)
{
if (this.mVirtualButton == null)
{
this.mPrevTransform = transform;
return true;
}
return false;
}
bool IEditorVirtualButtonBehaviour.SetSensitivitySetting(VirtualButton.Sensitivity sensibility)
{
if (this.mVirtualButton == null)
{
this.mSensitivity = sensibility;
this.mSensitivityDirty = true;
return true;
}
return false;
}
bool IEditorVirtualButtonBehaviour.SetVirtualButtonName(string virtualButtonName)
{
if (this.mVirtualButton == null)
{
this.mName = virtualButtonName;
return true;
}
return false;
}
private void LateUpdate()
{
if (this.UpdatePose())
{
this.UpdateAreaRectangle();
}
if (this.mSensitivityDirty && this.UpdateSensitivity())
{
this.mSensitivityDirty = false;
}
}
private void OnDestroy()
{
if (Application.isPlaying && this.mUnregisterOnDestroy)
{
ImageTargetAbstractBehaviour imageTargetBehaviour = this.GetImageTargetBehaviour();
if (imageTargetBehaviour != null)
{
imageTargetBehaviour.ImageTarget.DestroyVirtualButton(this.mVirtualButton);
}
}
}
private void OnDisable()
{
if (QCARRuntimeUtilities.IsQCAREnabled())
{
if (this.mPreviouslyEnabled != base.enabled)
{
this.mPreviouslyEnabled = base.enabled;
this.UpdateEnabled();
}
if (this.mPressed && (this.mHandlers != null))
{
foreach (IVirtualButtonEventHandler handler in this.mHandlers)
{
handler.OnButtonReleased(this);
}
}
this.mPressed = false;
}
}
public void OnTrackerUpdated(bool pressed)
{
if (this.mPreviouslyEnabled != base.enabled)
{
this.mPreviouslyEnabled = base.enabled;
this.UpdateEnabled();
}
if (base.enabled)
{
if ((this.mPressed != pressed) && (this.mHandlers != null))
{
if (pressed)
{
foreach (IVirtualButtonEventHandler handler in this.mHandlers)
{
handler.OnButtonPressed(this);
}
}
else
{
foreach (IVirtualButtonEventHandler handler2 in this.mHandlers)
{
handler2.OnButtonReleased(this);
}
}
}
this.mPressed = pressed;
}
}
public void RegisterEventHandler(IVirtualButtonEventHandler eventHandler)
{
this.mHandlers.Add(eventHandler);
}
public bool UnregisterEventHandler(IVirtualButtonEventHandler eventHandler)
{
return this.mHandlers.Remove(eventHandler);
}
public bool UpdateAreaRectangle()
{
RectangleData area = new RectangleData {
leftTopX = this.mLeftTop.x,
leftTopY = this.mLeftTop.y,
rightBottomX = this.mRightBottom.x,
rightBottomY = this.mRightBottom.y
};
if (this.mVirtualButton == null)
{
return false;
}
return this.mVirtualButton.SetArea(area);
}
private bool UpdateEnabled()
{
return this.mVirtualButton.SetEnabled(base.enabled);
}
public bool UpdatePose()
{
Vector2 vector3;
Vector2 vector4;
ImageTargetAbstractBehaviour imageTargetBehaviour = this.GetImageTargetBehaviour();
if (imageTargetBehaviour == null)
{
return false;
}
for (Transform transform = base.transform.parent; transform != null; transform = transform.parent)
{
if ((transform.localScale[0] != transform.localScale[1]) || (transform.localScale[0] != transform.localScale[2]))
{
Debug.LogWarning("Detected non-uniform scale in virtual button object hierarchy. Forcing uniform scaling of object '" + transform.name + "'.");
transform.localScale = new Vector3(transform.localScale[0], transform.localScale[0], transform.localScale[0]);
}
}
this.mHasUpdatedPose = true;
if ((base.transform.parent != null) && (base.transform.parent.gameObject != imageTargetBehaviour.gameObject))
{
base.transform.localPosition = Vector3.zero;
}
Vector3 position = imageTargetBehaviour.transform.InverseTransformPoint(base.transform.position);
position.y = 0.001f;
Vector3 vector2 = imageTargetBehaviour.transform.TransformPoint(position);
base.transform.position = vector2;
base.transform.rotation = imageTargetBehaviour.transform.rotation;
this.CalculateButtonArea(out vector3, out vector4);
float threshold = imageTargetBehaviour.transform.localScale[0] * 0.001f;
if (Equals(vector3, this.mLeftTop, threshold) && Equals(vector4, this.mRightBottom, threshold))
{
return false;
}
this.mLeftTop = vector3;
this.mRightBottom = vector4;
return true;
}
public bool UpdateSensitivity()
{
if (this.mVirtualButton == null)
{
return false;
}
return this.mVirtualButton.SetSensitivity(this.mSensitivity);
}
public bool HasUpdatedPose
{
get
{
return this.mHasUpdatedPose;
}
}
bool IEditorVirtualButtonBehaviour.enabled
{
get
{
return base.enabled;
}
set
{
base.enabled = value;
}
}
GameObject IEditorVirtualButtonBehaviour.gameObject
{
get
{
return base.gameObject;
}
}
GameObject IEditorVirtualButtonBehaviour.PreviousParent
{
get
{
return this.mPrevParent;
}
}
Matrix4x4 IEditorVirtualButtonBehaviour.PreviousTransform
{
get
{
return this.mPrevTransform;
}
}
Renderer IEditorVirtualButtonBehaviour.renderer
{
get
{
return base.renderer;
}
}
VirtualButton.Sensitivity IEditorVirtualButtonBehaviour.SensitivitySetting
{
get
{
return this.mSensitivity;
}
}
Transform IEditorVirtualButtonBehaviour.transform
{
get
{
return base.transform;
}
}
public bool Pressed
{
get
{
return this.mPressed;
}
}
public bool UnregisterOnDestroy
{
get
{
return this.mUnregisterOnDestroy;
}
set
{
this.mUnregisterOnDestroy = value;
}
}
public VirtualButton VirtualButton
{
get
{
return this.mVirtualButton;
}
}
public string VirtualButtonName
{
get
{
return this.mName;
}
}
}