Tanoda
LeapGraphicGroupEditorApi.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;
13#if UNITY_EDITOR
14using UnityEditor;
15#endif
16using Leap.Unity.Query;
17
19
20 public interface ILeapInternalGraphicGroup {
22 }
23
24 public partial class LeapGraphicGroup {
25
26#if UNITY_EDITOR
27 public readonly EditorApi editor;
28
29 public LeapGraphicGroup(LeapGraphicRenderer renderer, Type renderingMethodType) {
30 _groupName = LeapGraphicTagAttribute.GetTagName(renderingMethodType);
31
32 AssertHelper.AssertEditorOnly();
33 Assert.IsNotNull(renderer);
34 Assert.IsNotNull(renderingMethodType);
35 _renderer = renderer;
36
37 editor = new EditorApi(this);
38 editor.ChangeRenderingMethod(renderingMethodType, addFeatures: true);
39 }
40
41 public class EditorApi {
42 private readonly LeapGraphicGroup _group;
43
44 public EditorApi(LeapGraphicGroup group) {
45 _group = group;
46 }
47
48 public void OnValidate() {
49 if (!Application.isPlaying) {
50 _group._addRemoveSupported = true;
51 if (_group._renderingMethod.Value != null) {
52 _group._addRemoveSupported &= typeof(ISupportsAddRemove).IsAssignableFrom(_group._renderingMethod.Value.GetType());
53 }
54 }
55
56 for (int i = _group._features.Count; i-- != 0;) {
57 if (_group._features[i] == null) {
58 _group._features.RemoveAt(i);
59 }
60 }
61
62 Assert.IsNotNull(_group._renderingMethod.Value, "Rendering method of a group should never be null.");
63 }
64
65 public void OnDestroy() {
66 if (_group._renderingMethod.Value != null) {
67 _group._renderingMethod.Value.OnDisableRendererEditor();
68 }
69 }
70
78 public void ChangeRenderingMethod(Type renderingMethodType, bool addFeatures) {
79 AssertHelper.AssertEditorOnly();
80 Assert.IsNotNull(renderingMethodType);
81
82 if (_group._renderingMethod.Value != null) {
83 _group._renderingMethod.Value.OnDisableRendererEditor();
84 _group._renderingMethod.Value = null;
85 }
86
87 _group._renderingMethod.Value = Activator.CreateInstance(renderingMethodType) as LeapRenderingMethod;
88 Assert.IsNotNull(_group._renderingMethod.Value);
89
90 ILeapInternalRenderingMethod renderingMethodInternal = _group._renderingMethod.Value;
91 renderingMethodInternal.renderer = _group._renderer;
92 renderingMethodInternal.group = _group;
93
94 if (addFeatures) {
95 List<Type> dataObjTypes = new List<Type>();
96 var allGraphics = _group.renderer.GetComponentsInChildren<LeapGraphic>();
97 foreach (var graphic in allGraphics) {
98 if (_group._renderingMethod.Value.IsValidGraphic(graphic)) {
99
100 List<Type> types = new List<Type>();
101 for (int i = 0; i < graphic.featureData.Count; i++) {
102 var dataObj = graphic.featureData[i];
103 var dataType = dataObj.GetType();
104 if (!dataObjTypes.Contains(dataType)) {
105 types.Add(dataType);
106 }
107 }
108
109 foreach (var type in types) {
110 if (dataObjTypes.Query().Count(t => t == type) < types.Query().Count(t => t == type)) {
111 dataObjTypes.Add(type);
112 }
113 }
114 }
115 }
116
117 foreach (var type in dataObjTypes) {
118 var featureType = LeapFeatureData.GetFeatureType(type);
119 if (featureType != null) {
120 AddFeature(featureType);
121 }
122 }
123 }
124
125 _group._renderingMethod.Value.OnEnableRendererEditor();
126
127 OnValidate();
128 }
129
137 public LeapGraphicFeatureBase AddFeature(Type featureType) {
138 AssertHelper.AssertEditorOnly();
139 _group._renderer.editor.ScheduleRebuild();
140
141 Undo.RecordObject(_group.renderer, "Added feature");
142
143 var feature = Activator.CreateInstance(featureType) as LeapGraphicFeatureBase;
144 _group._features.Add(feature);
145
146 _group.RebuildFeatureData();
147 _group.RebuildFeatureSupportInfo();
148
149 return feature;
150 }
151
156 public void RemoveFeature(int featureIndex) {
157 AssertHelper.AssertEditorOnly();
158
159 Undo.RecordObject(_group.renderer, "Removed feature");
160
161 _group._features.RemoveAt(featureIndex);
162
163 _group.RebuildFeatureData();
164 _group.RebuildFeatureSupportInfo();
165
166 _group._renderer.editor.ScheduleRebuild();
167 }
168
174 public void RebuildEditorPickingMeshes() {
175 using (new ProfilerSample("Rebuild Picking Meshes")) {
176 foreach (var graphic in _group._graphics) {
177 graphic.editor.RebuildEditorPickingMesh();
178 }
179 }
180 }
181
182 public void ValidateGraphicList() {
183 //Make sure there are no duplicates, that is not allowed!
184 var set = Pool<HashSet<LeapGraphic>>.Spawn();
185 try {
186 for (int i = _group._graphics.Count; i-- != 0;) {
187 var graphic = _group._graphics[i];
188 if (set.Contains(graphic)) {
189 Debug.LogWarning("Removing duplicate graphic " + graphic);
190 _group._graphics.RemoveAt(i);
191 } else {
192 set.Add(graphic);
193 }
194 }
195 } finally {
196 set.Clear();
197 Pool<HashSet<LeapGraphic>>.Recycle(set);
198 }
199
200 for (int i = _group._graphics.Count; i-- != 0;) {
201 if (_group._graphics[i] == null || _group.graphics[i].attachedGroup != _group) {
202 _group._graphics.RemoveAt(i);
203 continue;
204 }
205
206 if (!_group._graphics[i].transform.IsChildOf(_group.renderer.transform)) {
207 _group.TryRemoveGraphic(_group._graphics[i]);
208 continue;
209 }
210 }
211 }
212
213 public void UpdateRendererEditor() {
214 AssertHelper.AssertEditorOnly();
215
216 _group._renderingMethod.Value.OnUpdateRendererEditor();
217 }
218 }
219#endif
220 }
221}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
LeapGraphicRenderer renderer
Gets the renderer this group is attached to.
A utility struct for ease of use when you want to wrap a piece of code in a Profiler....