Tanoda
LeapSlicedGraphicEditor.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 UnityEditor;
13using Leap.Unity.Query;
14
16
17 [CanEditMultipleObjects]
18 [CustomEditor(typeof(LeapSlicedGraphic), editorForChildClasses: true)]
19 public class LeapSlicedGraphicEditor : LeapGraphicEditorBase<LeapSlicedGraphic> {
20
21 protected override void OnEnable() {
22 base.OnEnable();
23
24 specifyCustomDrawer("_sourceDataIndex", drawSourceData);
25
26 specifyCustomDrawer("_resolution_verts_per_meter", drawResolution);
27
28 specifyCustomDecorator("_size", decorateSize);
29 specifyCustomPostDecorator("_size", postDecorateSize);
30
31 specifyCustomDrawer("_nineSliced", drawSize);
32 }
33
34 public override void OnInspectorGUI() {
35 base.OnInspectorGUI();
36
37 serializedObject.ApplyModifiedProperties();
38 foreach (var target in targets) {
39 if (!target.canNineSlice) {
40 target.nineSliced = false;
41 }
42 }
43 serializedObject.Update();
44 }
45
46 private void drawSourceData(SerializedProperty property) {
47 serializedObject.ApplyModifiedProperties();
48
49 var mainGroup = targets.Query().
50 Select(t => t.attachedGroup).
51 FirstOrDefault(g => g != null);
52
53 //If no element is connected to a gui, we can't draw anything
54 if (mainGroup == null) {
55 return;
56 }
57
58 //If any of the elements are not connected to the same gui, we can't draw anything
59 if (targets.Query().Any(p => p.attachedGroup != mainGroup)) {
60 return;
61 }
62
63 var features = new List<LeapGraphicFeatureBase>();
64 foreach (var feature in mainGroup.features) {
65 if (feature is LeapTextureFeature || feature is LeapSpriteFeature) {
66 features.Add(feature);
67 }
68 }
69
70 if (features.Count == 1) {
71 return;
72 }
73
74 int index = -1;
75 foreach (var target in targets) {
76 //If any of the targets have no source data, you can't use them
77 if (target.sourceData == null) {
78 return;
79 }
80
81 int dataIndex = features.IndexOf(target.sourceData.feature);
82
83 if (index == -1) {
84 index = dataIndex;
85 }
86 else if (index != dataIndex) {
87 index = -1;
88 break;
89 }
90 }
91
92 string[] options = features.Query().Select(f => {
93 if (f is LeapTextureFeature) {
94 return (f as LeapTextureFeature).propertyName + " (Texture)";
95 }
96 else {
97 return (f as LeapSpriteFeature).propertyName + " (Sprite)";
98 }
99 }).ToArray();
100
101 EditorGUI.BeginChangeCheck();
102
103 if (index == -1) {
104 EditorGUI.showMixedValue = true;
105 }
106
107 int newIndex = EditorGUILayout.Popup("Data Source", index, options);
108
109 EditorGUI.showMixedValue = false;
110
111 if (EditorGUI.EndChangeCheck()) {
112 foreach (var target in targets) {
113 List<LeapFeatureData> data = target.featureData.Query().Where(f => f is LeapTextureData || f is LeapSpriteData).ToList();
114
115 Undo.RecordObject(target, "Setting source data");
116 target.sourceData = data[newIndex];
117 }
118 }
119
120 serializedObject.Update();
121 }
122
123 private void drawResolution(SerializedProperty property) {
124 LeapPanelOutlineGraphic.ResolutionType mainType = targets[0].resolutionType;
125 bool allSameType = targets.Query().All(p => p.resolutionType == mainType);
126
127 if (!allSameType) {
128 return;
129 }
130
131 Rect rect = EditorGUILayout.GetControlRect();
132
133 GUIContent resolutionContent = new GUIContent("Resolution");
134 if (mainType == LeapPanelOutlineGraphic.ResolutionType.Vertices) {
135 resolutionContent.tooltip = "How many vertices this panel should have in the x and y direction. These values ignore the edges (0 is a valid resolution).";
136 }
137 else {
138 resolutionContent.tooltip = "How many vertices this panel should spawn relative to the width and height of the panel. The panel will always have enough vertices to form a quad.";
139 }
140 EditorGUI.LabelField(rect, resolutionContent);
141
142 rect.x += EditorGUIUtility.labelWidth - 2;
143 rect.width -= EditorGUIUtility.labelWidth;
144 rect.width *= 2.0f / 3.0f;
145
146 float originalWidth = EditorGUIUtility.labelWidth;
147 EditorGUIUtility.labelWidth = 14;
148
149 Rect left = rect;
150 left.width /= 2;
151 Rect right = left;
152 right.x += right.width + 1;
153
154 if (mainType == LeapPanelOutlineGraphic.ResolutionType.Vertices) {
155 SerializedProperty x = serializedObject.FindProperty("_resolution_vert_x");
156 SerializedProperty y = serializedObject.FindProperty("_resolution_vert_y");
157
158 x.intValue = EditorGUI.IntField(left, "X", x.intValue);
159 y.intValue = EditorGUI.IntField(right, "Y", y.intValue);
160 }
161 else {
162 Vector2 value = property.vector2Value;
163
164 value.x = EditorGUI.FloatField(left, "X", value.x);
165 value.y = EditorGUI.FloatField(right, "Y", value.y);
166
167 property.vector2Value = value;
168 }
169
170 EditorGUIUtility.labelWidth = originalWidth;
171 }
172
173 private void decorateSize(SerializedProperty property) {
174 EditorGUI.BeginDisabledGroup(targets.Query().Any((t) => t.GetComponent<RectTransform>() != null));
175 }
176
177 private void postDecorateSize(SerializedProperty property) {
178 EditorGUI.EndDisabledGroup();
179 }
180
181 private void drawSize(SerializedProperty property) {
182 using (new GUILayout.HorizontalScope()) {
183 var canAllNineSlice = targets.Query().All(p => p.canNineSlice);
184 using (new EditorGUI.DisabledGroupScope(!canAllNineSlice)) {
185 EditorGUILayout.PropertyField(property);
186 }
187
188 if (targets.Length == 1) {
189 var rectTransform = target.GetComponent<RectTransform>();
190 if (rectTransform == null) {
191 if (GUILayout.Button("Add Rect Transform", GUILayout.MaxWidth(150))) {
192 Vector2 initialSize = target.rect.size;
193
194 rectTransform = target.gameObject.AddComponent<RectTransform>();
195 rectTransform.sizeDelta = initialSize;
196 }
197 }
198 }
199 }
200 }
201 }
202}