Tanoda
LeapGraphicEditor.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 UnityEngine;
10using UnityEditor;
11using Leap.Unity;
12using Leap.Unity.Query;
13
15
16 [CanEditMultipleObjects]
17 [CustomEditor(typeof(LeapGraphic), editorForChildClasses: true, isFallback = true)]
18 public class LeapGraphicEditor : LeapGraphicEditorBase<LeapGraphic> { }
19
20 public abstract class LeapGraphicEditorBase<T> : CustomEditorBase<T> where T : LeapGraphic {
21 //Used ONLY for feature data drawers
22 public static LeapGraphicFeatureBase currentFeature { get; private set; }
23
24 private SerializedProperty _featureList;
25 private SerializedProperty _featureTable;
26
27 protected override void OnEnable() {
28 base.OnEnable();
29
30 hideField("_anchor");
31 hideField("_featureData");
32 hideField("_attachedRenderer");
33 hideField("_attachedGroupIndex");
34 hideField("_preferredRendererType");
35 hideField("_favoriteGroupName");
36
37 _featureList = serializedObject.FindProperty("_featureData");
38 _featureTable = MultiTypedListUtil.GetTableProperty(_featureList);
39
41 }
42
43 public override void OnInspectorGUI() {
44 LeapGraphicGroup mainGroup = null;
45 LeapGraphicGroup sharedGroup = null;
46
47 if (targets.Query().All(g => g.isAttachedToGroup)) {
48 var mainRenderer = targets[0].attachedGroup.renderer;
49 if (targets.Query().All(g => g.attachedGroup.renderer == mainRenderer)) {
50 mainGroup = targets[0].attachedGroup;
51 if (targets.Query().All(g => g.attachedGroup == mainGroup)) {
52 sharedGroup = mainGroup;
53 }
54 }
55 }
56
57 drawScriptAndGroupGui(mainGroup);
58
59 base.OnInspectorGUI();
60
61 drawFeatureData(sharedGroup);
62 }
63
64 protected void drawScriptAndGroupGui(LeapGraphicGroup mainGroup) {
65 using (new GUILayout.HorizontalScope()) {
67
68 Color originalColor = GUI.color;
69 try {
70 string buttonText;
71 if (!targets.Query().All(g => g.attachedGroup == mainGroup)) {
72 buttonText = "-";
73 } else if (mainGroup == null) {
74 buttonText = "None";
75 GUI.color = Color.yellow;
76 } else {
77 buttonText = mainGroup.name;
78 }
79
80 var renderer = targets.Query().
81 Select(t => t.GetComponentInParent<LeapGraphicRenderer>()).
82 UniformOrDefault();
83
84 if (GUILayout.Button(buttonText, EditorStyles.miniButton, GUILayout.Width(60))) {
85 GenericMenu groupMenu = new GenericMenu();
86 groupMenu.AddItem(new GUIContent("None"), false, () => {
87 foreach (var graphic in targets) {
88 serializedObject.ApplyModifiedProperties();
89 graphic.TryDetach();
90
91 EditorUtility.SetDirty(graphic);
92
93 serializedObject.SetIsDifferentCacheDirty();
94 serializedObject.Update();
95 }
96 });
97
98 if (renderer != null) {
99 int index = 0;
100 foreach (var group in renderer.groups.Query().Where(g => g.renderingMethod.IsValidGraphic(targets[0]))) {
101 groupMenu.AddItem(new GUIContent(index.ToString() + ": " + group.name), false, () => {
102
103 bool areFeaturesUnequal = false;
104 var typesA = group.features.Query().Select(f => f.GetType()).ToList();
105 foreach (var graphic in targets) {
106 if (!graphic.isAttachedToGroup) {
107 continue;
108 }
109
110 var typesB = graphic.attachedGroup.features.Query().Select(f => f.GetType()).ToList();
111 if (!Utils.AreEqualUnordered(typesA, typesB)) {
112 areFeaturesUnequal = true;
113 break;
114 }
115 }
116
117 if (areFeaturesUnequal && LeapGraphicPreferences.promptWhenGroupChange) {
118 if (!EditorUtility.DisplayDialog("Features Are Different!",
119 "The group you are moving to has a different feature set than the current group, " +
120 "this can result in data loss! Are you sure you want to change group?",
121 "Continue",
122 "Cancel")) {
123 return;
124 }
125 }
126
127 foreach (var graphic in targets) {
128 serializedObject.ApplyModifiedProperties();
129
130 if (graphic.isAttachedToGroup) {
131 if (graphic.TryDetach()) {
132 group.TryAddGraphic(graphic);
133 }
134 } else {
135 group.TryAddGraphic(graphic);
136 }
137
138 EditorUtility.SetDirty(graphic);
139 EditorUtility.SetDirty(renderer);
140
141 serializedObject.SetIsDifferentCacheDirty();
142 serializedObject.Update();
143 }
144
145 renderer.editor.ScheduleRebuild();
146 });
147 index++;
148 }
149 }
150
151 groupMenu.ShowAsContext();
152 }
153 } finally {
154 GUI.color = originalColor;
155 }
156 }
157 }
158
159 protected void drawFeatureData(LeapGraphicGroup sharedGroup) {
160 using (new ProfilerSample("Draw Leap Gui Graphic Editor")) {
161 if (targets.Length == 0) return;
162 var mainGraphic = targets[0];
163
164 if (mainGraphic.featureData.Count == 0) {
165 return;
166 }
167
168 if (mainGraphic.attachedGroup != null) {
169 SpriteAtlasUtil.ShowInvalidSpriteWarning(mainGraphic.attachedGroup.features);
170 }
171
172 int maxGraphics = LeapGraphicPreferences.graphicMax;
173 if (targets.Query().Any(e => e.attachedGroup != null && e.attachedGroup.graphics.IndexOf(e) >= maxGraphics)) {
174 string noun = targets.Length == 1 ? "This graphic" : "Some of these graphics";
175 string rendererName = targets.Length == 1 ? "its renderer" : "their renderers";
176 EditorGUILayout.HelpBox(noun + " may not be properly displayed because there are too many graphics on " + rendererName + ". " +
177 "Either lower the number of graphics or increase the maximum graphic count by visiting " +
178 "Edit->Preferences.", MessageType.Warning);
179 }
180
181 //If we are not all attached to the same group we cannot show features
182 if (!targets.Query().Select(g => g.attachedGroup).AllEqual()) {
183 return;
184 }
185
186 EditorGUILayout.Space();
187
188 using (new GUILayout.HorizontalScope()) {
189 EditorGUILayout.LabelField("Feature Data: ", EditorStyles.boldLabel);
190
191 if (sharedGroup != null) {
192 var meshRendering = sharedGroup.renderingMethod as LeapMesherBase;
193 if (meshRendering != null && meshRendering.IsAtlasDirty && !EditorApplication.isPlaying) {
194 if (GUILayout.Button("Refresh Atlas", GUILayout.MaxHeight(EditorGUIUtility.singleLineHeight))) {
195 meshRendering.RebuildAtlas(new ProgressBar());
196 sharedGroup.renderer.editor.ScheduleRebuild();
197 }
198 }
199 }
200 }
201
202 for (int i = 0; i < _featureTable.arraySize; i++) {
203 var idIndex = _featureTable.GetArrayElementAtIndex(i);
204 var dataProp = MultiTypedListUtil.GetReferenceProperty(_featureList, idIndex);
205 EditorGUILayout.LabelField(LeapGraphicTagAttribute.GetTagName(dataProp.type));
206
207 if (mainGraphic.attachedGroup != null) {
208 currentFeature = mainGraphic.attachedGroup.features[i];
209 }
210
211 EditorGUI.indentLevel++;
212
213 EditorGUILayout.PropertyField(dataProp, includeChildren: true);
214
215 EditorGUI.indentLevel--;
216
217 currentFeature = null;
218 }
219
220 serializedObject.ApplyModifiedProperties();
221 }
222 }
223
224 protected bool HasFrameBounds() {
225 return targets.Query().
226 Any(t => t.editor.pickingMesh != null);
227 }
228
229 protected Bounds OnGetFrameBounds() {
230 return targets.Query().
231 Select(e => e.editor.pickingMesh).
232 ValidUnityObjs().
233 Select(m => m.bounds).
234 Fold((a, b) => {
235 a.Encapsulate(b);
236 return a;
237 });
238 }
239 }
240}
UnityEngine.Color Color
Definition: TestScript.cs:32
void hideField(string propertyName)
void drawScriptField(bool disable=true)
void drawFeatureData(LeapGraphicGroup sharedGroup)
void drawScriptAndGroupGui(LeapGraphicGroup mainGroup)
LeapRenderingMethod renderingMethod
Gets the rendering method used for this group. This can only be changed at edit time using either the...
LeapGraphicRenderer renderer
Gets the renderer this group is attached to.
This class allows you to easily give feedback of an action as it completes.
Definition: ProgressBar.cs:66
A utility struct for ease of use when you want to wrap a piece of code in a Profiler....