Tanoda
LeapSpace.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.Collections.Generic;
10using UnityEngine;
11using UnityEngine.Assertions;
12using Leap.Unity.Query;
13
14namespace Leap.Unity.Space {
15
16 [ExecuteInEditMode]
17 public abstract class LeapSpace : LeapSpaceAnchor {
18 private static List<LeapSpace> _enabledSpaces = new List<LeapSpace>();
19 public static List<LeapSpace> allEnabled {
20 get {
21#if UNITY_EDITOR
22 //if we are in the editor, don't bother with enable/disable lifecycle
23 //just use FindObjectsOfType!
24 if (!Application.isPlaying) {
25 FindObjectsOfType<LeapSpace>().Query().Where(c => c.enabled).FillList(_enabledSpaces);
26 }
27#endif
28 return _enabledSpaces;
29 }
30 }
31
32 private List<LeapSpaceAnchor> _anchors = new List<LeapSpaceAnchor>();
33
34 protected override void OnEnable() {
35 base.OnEnable();
36
37 _enabledSpaces.Add(this);
38
41 }
42
43 protected override void OnDisable() {
44 base.OnDisable();
45
46 _enabledSpaces.Remove(this);
47
48 for (int i = 0; i < _anchors.Count; i++) {
49 _anchors[i].space = null;
50 _anchors[i].transformer = null;
51 }
52 }
53
57 public List<LeapSpaceAnchor> anchors {
58 get {
59 return _anchors;
60 }
61 }
62
71 public void RebuildHierarchy() {
72 Assert.IsTrue(enabled, "Should never call RebuildHierarchy on a disabled space.");
73
74 _anchors.Clear();
75 rebuildHierarchyRecursively(transform);
76
77 Assert.IsTrue(_anchors.Count > 0, "There should always be at least a single anchor (this one).");
78 Assert.AreEqual(_anchors[0], this, "A space should always be the first element in the anchor list.");
79 }
80
86 Assert.IsTrue(enabled, "Should never call RecalculateTransformers on a disabled space.");
87
89
90 //Depth-first pre-order ensures that a parent already has it's transformer up-to-date
91 //by the time a direct child needs to be updated.
92 for (int i = 1; i < _anchors.Count; i++) {
93 var anchor = _anchors[i];
94 var parent = anchor.parent;
95
96 Assert.IsNotNull(anchor, "Make sure to call RebuildHierarchy before RecalculateTransformers if you delete an anchor.");
97 Assert.IsTrue(anchor.enabled, "Make sure to all RebuildHierarchy before RecalculateTransformers if you disable an anchor.");
98
99 UpdateTransformer(anchor.transformer, parent.transformer);
100 }
101 }
102
107 public abstract Hash GetSettingHash();
108
112
113 private void rebuildHierarchyRecursively(Transform root) {
114 var anchor = root.GetComponent<LeapSpaceAnchor>();
115 if (anchor != null && anchor.enabled) {
116 anchor.space = this;
118 anchor.transformer = ConstructTransformer(anchor);
119
120 _anchors.Add(anchor);
121 }
122
123 int childCount = root.childCount;
124 for (int i = 0; i < childCount; i++) {
125 rebuildHierarchyRecursively(root.GetChild(i));
126 }
127 }
128 }
129}
List< LeapSpaceAnchor > anchors
Returns all active anchors in depth-first pre-order
Definition: LeapSpace.cs:57
abstract ITransformer CosntructBaseTransformer()
abstract void UpdateTransformer(ITransformer transformer, ITransformer parent)
abstract Hash GetSettingHash()
Get a hash of all features in this space. This is useful if you want to know if anything has changed ...
static List< LeapSpace > allEnabled
Definition: LeapSpace.cs:19
override void OnDisable()
Definition: LeapSpace.cs:43
override void OnEnable()
Definition: LeapSpace.cs:34
void RecalculateTransformers()
Call to update all transformers in the space. Call this whenever any anchor or parent of an anchor ch...
Definition: LeapSpace.cs:85
void RebuildHierarchy()
Call to traverse the entire hierarchy and rebuild the relationship between anchors....
Definition: LeapSpace.cs:71
abstract ITransformer ConstructTransformer(LeapSpaceAnchor anchor)