Tanoda
WaypointListDrawer.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using UnityEngine;
4using UnityEditor;
5
6namespace Waypoint
7{
8 [CustomPropertyDrawer(typeof(WaypointCircuit.WaypointList))]
9 public class WaypointListDrawer : PropertyDrawer
10 {
11 private const float LINE_HEIGHT = 18;
12 private const float SPACING = 4;
13
14 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
15 {
16 EditorGUI.BeginProperty(position, label, property);
17
18 var x = position.x;
19 var y = position.y;
20 var inspectorWidth = position.width;
21
22 var indent = EditorGUI.indentLevel;
23 EditorGUI.indentLevel = 0;
24
25 var items = property.FindPropertyRelative("_items");
26 var titles = new string[] {"Transform", "", "", ""};
27 var props = new string[] {"transform", "^", "v", "-"};
28 var widths = new float[] {.7f, .1f, .1f, .1f};
29 var lineHeight = 18.0f;
30 var changedLength = false;
31
32 if (items.arraySize > 0)
33 {
34 for (int i = 0; i < items.arraySize; i++)
35 {
36 var item = items.GetArrayElementAtIndex(i);
37
38 float rowX = x;
39 for (int n = 0; n < props.Length; ++n)
40 {
41 float w = widths[n] * inspectorWidth;
42
43 Rect rect = new Rect(rowX, y, w, lineHeight);
44 rowX += w;
45
46 if (i == -1)
47 {
48 EditorGUI.LabelField(rect, titles[n]);
49 }
50 else
51 {
52 if (n == 0)
53 {
54 EditorGUI.ObjectField(rect, item.objectReferenceValue, typeof(Transform), true);
55 }
56 else
57 {
58 if (GUI.Button(rect, props[n]))
59 {
60 switch (props[n])
61 {
62 case "-":
63 items.DeleteArrayElementAtIndex(i);
64 items.DeleteArrayElementAtIndex(i);
65 changedLength = true;
66 break;
67 case "v":
68 if (i > 0)
69 {
70 items.MoveArrayElement(i, i + 1);
71 }
72
73 break;
74 case "^":
75 if (i < items.arraySize - 1)
76 {
77 items.MoveArrayElement(i, i - 1);
78 }
79
80 break;
81 }
82 }
83 }
84 }
85 }
86
87 y += lineHeight + SPACING;
88 if (changedLength)
89 {
90 break;
91 }
92 }
93 }
94 else
95 {
96 var addButtonRect = new Rect((x + position.width) - widths[widths.Length - 1] * inspectorWidth, y, widths[widths.Length - 1] * inspectorWidth, lineHeight);
97 if (GUI.Button(addButtonRect, "+"))
98 {
99 items.InsertArrayElementAtIndex(items.arraySize);
100 }
101
102 y += lineHeight + SPACING;
103 }
104
105 var addAllButtonRect = new Rect(x, y, inspectorWidth, lineHeight);
106 if (GUI.Button(addAllButtonRect, "Assign using all child objects"))
107 {
108 var circuit = property.FindPropertyRelative("_circuit").objectReferenceValue as WaypointCircuit;
109 var children = new Transform[circuit.transform.childCount];
110 int n = 0;
111 foreach (Transform child in circuit.transform)
112 {
113 children[n++] = child;
114 }
115
116 Array.Sort(children, new TransformNameComparer());
117 circuit._waypointList._items = new Transform[children.Length];
118 for (n = 0; n < children.Length; ++n)
119 {
120 circuit._waypointList._items[n] = children[n];
121 }
122 }
123
124 y += lineHeight + SPACING;
125
126 var renameButtonRect = new Rect(x, y, inspectorWidth, lineHeight);
127 if (GUI.Button(renameButtonRect, "Auto Rename numerically from this order"))
128 {
129 var circuit = property.FindPropertyRelative("_circuit").objectReferenceValue as WaypointCircuit;
130 int n = 0;
131 foreach (var child in circuit._waypointList._items)
132 {
133 child.name = "Waypoint " + (n++).ToString("000");
134 }
135 }
136
137 y += lineHeight + SPACING;
138
139 EditorGUI.indentLevel = indent;
140 EditorGUI.EndProperty();
141 }
142
143 public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
144 {
145 var items = property.FindPropertyRelative("_items");
146 const float lineAndSpace = LINE_HEIGHT + SPACING;
147 return 40 + (items.arraySize * lineAndSpace) + lineAndSpace;
148 }
149
150 private class TransformNameComparer : IComparer
151 {
152 public int Compare(object x, object y)
153 {
154 return String.Compare(((Transform) x)?.name, ((Transform) y)?.name, StringComparison.Ordinal);
155 }
156 }
157 }
158}
override float GetPropertyHeight(SerializedProperty property, GUIContent label)
override void OnGUI(Rect position, SerializedProperty property, GUIContent label)