Tanoda
LeapGraphic.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections.Generic;
11using UnityEngine;
12using UnityEngine.Assertions;
13using Leap.Unity;
14using Leap.Unity.Space;
15using Leap.Unity.Query;
16
18
19 [ExecuteInEditMode]
20 [DisallowMultipleComponent]
21 public abstract partial class LeapGraphic : MonoBehaviour, ISpaceComponent, ISerializationCallbackReceiver {
22
23 #region INSPECTOR FIELDS
24 [SerializeField]
26
27 [SerializeField]
29
30 [SerializeField]
32
33 [SerializeField]
34 protected int _attachedGroupIndex = -1;
35
36 [SerializeField]
37 protected string _favoriteGroupName;
38
39 [SerializeField]
41 #endregion
42
43 #region PUBLIC API
44
45 [NonSerialized]
46 private bool _willBeAttached = false;
47 [NonSerialized]
48 private bool _willBeDetached = false;
49 [NonSerialized]
50 private LeapGraphicGroup _groupToBeAttachedTo = null;
51 [NonSerialized]
52 private bool _isRepresentationDirty = true;
53
54 public Action<LeapGraphicGroup> OnAttachedToGroupEvent;
56
65 get {
66 return _isRepresentationDirty;
67 }
68 set {
69 _isRepresentationDirty = value;
70 }
71 }
72
78 get {
79#if UNITY_EDITOR
80 if (!Application.isPlaying) {
81 return true;
82 }
83#endif
84 return _isRepresentationDirty;
85 }
86 }
87
94 public string favoriteGroupName {
95 get {
96 return _favoriteGroupName;
97 }
98 set {
99 _favoriteGroupName = value;
100 }
101 }
102
109 get {
110 return _anchor;
111 }
112 }
113
121 get {
123 }
124 }
125
131 public IList<LeapFeatureData> featureData {
132 get {
133 return _featureData;
134 }
135 }
136
141 get {
142 if (_attachedRenderer == null) {
143 return null;
144 } else {
145#if UNITY_EDITOR
146 if (_attachedGroupIndex < 0 || _attachedGroupIndex >= _attachedRenderer.groups.Count) {
147 _attachedRenderer = null;
149 return null;
150 }
151#endif
153 }
154 }
155 }
156
162 public bool isAttachedToGroup {
163 get {
164 return attachedGroup != null;
165 }
166 }
167
173 public bool willbeAttached {
174 get {
175 return _willBeAttached;
176 }
177 }
178
184 public bool willbeDetached {
185 get {
186 return _willBeDetached;
187 }
188 }
189
196 get {
198 }
199 }
200
207 public bool TryDetach() {
208 var attachedGroup = this.attachedGroup;
209 if (attachedGroup == null) {
210 return false;
211 } else {
212 return attachedGroup.TryRemoveGraphic(this);
213 }
214 }
215
220 public T GetFeatureData<T>() where T : LeapFeatureData {
221 return _featureData.Query().OfType<T>().FirstOrDefault();
222 }
223
228 public virtual void NotifyWillBeAttached(LeapGraphicGroup toBeAttachedTo) {
229 Assert.IsFalse(_willBeAttached);
230 Assert.IsNull(_groupToBeAttachedTo);
231
232 _willBeAttached = true;
233 _groupToBeAttachedTo = toBeAttachedTo;
234 }
235
240 public virtual void CancelWillBeAttached() {
241 Assert.IsTrue(_willBeAttached);
242 Assert.IsNotNull(_groupToBeAttachedTo);
243
244 _willBeAttached = false;
245 _groupToBeAttachedTo = null;
246 }
247
252 public virtual void NotifyWillBeDetached(LeapGraphicGroup toBeDetachedFrom) {
253 Assert.IsFalse(_willBeDetached);
254
255 _willBeDetached = true;
256 }
257
262 public virtual void CancelWillBeDetached() {
263 Assert.IsTrue(_willBeDetached);
264
265 _willBeDetached = false;
266 }
267
273#if UNITY_EDITOR
274 editor.OnAttachedToGroup(group, anchor);
275#endif
277 _willBeAttached = false;
278 _groupToBeAttachedTo = null;
279 _favoriteGroupName = group.name;
280
283 _anchor = anchor;
284
285 patchReferences();
286
287 if (OnAttachedToGroupEvent != null) {
289 }
290 }
291
296 _anchor = anchor;
297 }
298
303 public virtual void OnDetachedFromGroup() {
304 _willBeDetached = false;
305
306 _attachedRenderer = null;
308 _anchor = null;
309
310 for (int i = 0; i < _featureData.Count; i++) {
311 _featureData[i].feature = null;
312 }
313
314 if (OnDetachedFromGroupEvent != null) {
316 }
317 }
318
323 public virtual void OnAssignFeatureData(List<LeapFeatureData> data) {
325 foreach (var dataObj in data) {
326 _featureData.Add(dataObj);
327 }
328 }
329 #endregion
330
331 #region UNITY CALLBACKS
332 protected virtual void Reset() {
333 var rectTransform = GetComponent<RectTransform>();
334 if (rectTransform != null &&
335 Mathf.Abs(rectTransform.sizeDelta.x - 100) < Mathf.Epsilon &&
336 Mathf.Abs(rectTransform.sizeDelta.y - 100) < Mathf.Epsilon) {
337 rectTransform.sizeDelta = Vector3.one * 0.1f;
338 }
339
340 var parentRenderer = GetComponentInParent<LeapGraphicRenderer>();
341 if (parentRenderer != null) {
342 parentRenderer.TryAddGraphic(this);
343 }
344 }
345
346 protected virtual void OnValidate() {
347#if UNITY_EDITOR
348 editor.OnValidate();
349#endif
350 patchReferences();
351 }
352
353 protected virtual void Awake() {
354 if (isAttachedToGroup && !attachedGroup.graphics.Contains(this)) {
355 var preferredGroup = attachedGroup;
357
358 //If this fails for any reason don't worry, we will be auto-added
359 //to a group if we can.
360 preferredGroup.TryAddGraphic(this);
361 }
362 }
363
364 protected virtual void OnEnable() {
365 patchReferences();
366 }
367
368 protected virtual void OnDestroy() {
369 if (Application.isPlaying && isAttachedToGroup) {
370 TryDetach();
371 }
372 }
373
374 protected virtual void OnDrawGizmos() {
375#if UNITY_EDITOR
376 editor.OnDrawGizmos();
377#endif
378 }
379
380 #endregion
381
382 #region PRIVATE IMPLEMENTATION
383
384#if UNITY_EDITOR
385 protected LeapGraphic() {
386 editor = new EditorApi(this);
387 }
388
389 protected LeapGraphic(EditorApi editor) {
390 this.editor = editor;
391 }
392#endif
393
394 public virtual void OnBeforeSerialize() { }
395
396 public virtual void OnAfterDeserialize() {
397 for (int i = 0; i < _featureData.Count; i++) {
398 _featureData[i].graphic = this;
399 }
400 }
401
402 private void patchReferences() {
403 if (isAttachedToGroup) {
405 for (int i = 0; i < _featureData.Count; i++) {
406 _featureData[i].feature = group.features[i];
407 }
408 }
409 }
410
411 private T getFeatureDataOrThrow<T>() where T : LeapFeatureData {
412 var data = _featureData.Query().OfType<T>().FirstOrDefault();
413 if (data == null) {
414 throw new Exception("There is not a feature data object of type " + typeof(T).Name + " attached to this graphic.");
415 }
416 return data;
417 }
418
419 [Serializable]
420 public class FeatureDataList : MultiTypedList<LeapFeatureData, LeapTextureData,
421 LeapSpriteData,
422 LeapRuntimeTintData,
423 LeapBlendShapeData,
424 CustomFloatChannelData,
425 CustomVectorChannelData,
426 CustomColorChannelData,
427 CustomMatrixChannelData> { }
428 #endregion
429 }
430}
List< LeapGraphic > graphics
Returns the list of graphics attached to this group. This getter returns a regular mutable list for s...
bool TryRemoveGraphic(LeapGraphic graphic)
Tries to remove the given graphic from this group. This can safely be called during runtime or edit t...
LeapGraphicRenderer renderer
Gets the renderer this group is attached to.
bool isAttachedToGroup
Returns whether or not this graphic is attached to any group. Can still return false at runtime even ...
Definition: LeapGraphic.cs:162
IList< LeapFeatureData > featureData
Returns a list of feature data attached to this graphic. If this graphic is attached to a group,...
Definition: LeapGraphic.cs:131
bool TryDetach()
This method tries to detach this graphic from whatever group it is currently attached to....
Definition: LeapGraphic.cs:207
bool isRepresentationDirty
An internal flag that returns true if the visual representation of this graphic needs to be updated....
Definition: LeapGraphic.cs:64
virtual void NotifyWillBeAttached(LeapGraphicGroup toBeAttachedTo)
Called by the system to notify that this graphic will be attached within the next frame....
Definition: LeapGraphic.cs:228
void OnUpdateAnchor(LeapSpaceAnchor anchor)
Called by graphic groups when a renderer's attached space changes.
Definition: LeapGraphic.cs:295
bool willbeDetached
Returns whether or not this graphic will be detached from a group within the next frame....
Definition: LeapGraphic.cs:184
LeapSpaceAnchor anchor
Returns the space anchor for this graphic. This will be null if the graphic is not currently part of ...
Definition: LeapGraphic.cs:108
T GetFeatureData< T >()
Gets a single feature data object of a given type T. This will return null if there is no feature dat...
Definition: LeapGraphic.cs:220
virtual void CancelWillBeDetached()
Called by the system to notify that a previous notification that this graphic would be detached has b...
Definition: LeapGraphic.cs:262
LeapGraphicGroup attachedGroup
Returns the group this graphic is attached to.
Definition: LeapGraphic.cs:140
virtual void CancelWillBeAttached()
Called by the system to notify that a previous notification that this graphic would be attached has b...
Definition: LeapGraphic.cs:240
virtual void OnAttachedToGroup(LeapGraphicGroup group, LeapSpaceAnchor anchor)
Called by the system when this graphic is attached to a group. This method is invoked both at runtime...
Definition: LeapGraphic.cs:272
string favoriteGroupName
Gets or sets the name of the group that this graphic likes to be attached to. Whenever a graphic is e...
Definition: LeapGraphic.cs:94
bool isRepresentationDirtyOrEditTime
A simple utility getter that returns true if isRepresentationDirty is true, OR it is currently edit t...
Definition: LeapGraphic.cs:77
virtual void OnDetachedFromGroup()
Called by the system when this graphic is detached from a group. This method is invoked both at runti...
Definition: LeapGraphic.cs:303
virtual void NotifyWillBeDetached(LeapGraphicGroup toBeDetachedFrom)
Called by the system to notify that this graphic will be detached within the next frame....
Definition: LeapGraphic.cs:252
Action< LeapGraphicGroup > OnAttachedToGroupEvent
Definition: LeapGraphic.cs:54
ITransformer transformer
A utility getter that returns a transformer for this graphic. Even if the space anchor for this graph...
Definition: LeapGraphic.cs:120
bool willbeAttached
Returns whether or not this graphic will be attached to a group within the next frame....
Definition: LeapGraphic.cs:173
virtual void OnAssignFeatureData(List< LeapFeatureData > data)
Called by the system whenever feature data is re-assigned to this graphic. This is only called at edi...
Definition: LeapGraphic.cs:323
Type preferredRendererType
Returns the type this graphic prefers to be attached to. When calling LeapGraphicRenderer....
Definition: LeapGraphic.cs:195
List< LeapGraphicGroup > groups
Returns a list of all graphic groups contained withinin this renderer. This getter returns a regular ...
Represents an ordered collection of objects of type BaseType.
abstract void Add(BaseType obj)
abstract void Clear()
static readonly IdentityTransformer single
Definition: ITransformer.cs:55