summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Editor/GraphUpdateSceneEditor.cs
blob: ede0617566881a56ebee3c769cdb6520d24bc5df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

namespace Pathfinding {
	/// <summary>Editor for GraphUpdateScene</summary>
	[CustomEditor(typeof(GraphUpdateScene))]
	[CanEditMultipleObjects]
	public class GraphUpdateSceneEditor : EditorBase {
		int selectedPoint = -1;

		const float pointGizmosRadius = 0.09F;
		static Color PointColor = new Color(1, 0.36F, 0, 0.6F);
		static Color PointSelectedColor = new Color(1, 0.24F, 0, 1.0F);

		GraphUpdateScene[] scripts;

		protected override void Inspector () {
			// Find all properties
			var points = FindProperty("points");
			var legacyMode = FindProperty("legacyMode");

			// Get a list of inspected components
			scripts = new GraphUpdateScene[targets.Length];
			targets.CopyTo(scripts, 0);

			EditorGUI.BeginChangeCheck();

			// Make sure no point arrays are null
			for (int i = 0; i < scripts.Length; i++) {
				scripts[i].points = scripts[i].points ?? new Vector3[0];
			}

			if (!points.hasMultipleDifferentValues && points.arraySize == 0) {
				if (scripts[0].GetComponent<PolygonCollider2D>() != null) {
					EditorGUILayout.HelpBox("Using polygon collider shape", MessageType.Info);
				} else if (scripts[0].GetComponent<Collider>() != null || scripts[0].GetComponent<Collider2D>() != null) {
					EditorGUILayout.HelpBox("No points, using collider.bounds", MessageType.Info);
				} else if (scripts[0].GetComponent<Renderer>() != null) {
					EditorGUILayout.HelpBox("No points, using renderer.bounds", MessageType.Info);
				} else {
					EditorGUILayout.HelpBox("No points and no collider or renderer attached, will not affect anything\nPoints can be added using the transform tool and holding shift", MessageType.Warning);
				}
			}

			DrawPointsField();

			EditorGUI.indentLevel = 0;

			DrawPhysicsField();

			PropertyField("updateErosion", null, "Recalculate erosion for grid graphs.\nSee online documentation for more info");

			DrawConvexField();

			// Minimum bounds height is not applied when using the bounds from a collider or renderer
			if (points.hasMultipleDifferentValues || points.arraySize > 0) {
				FloatField("minBoundsHeight", min: 0.1f);
			}
			PropertyField("applyOnStart");
			PropertyField("applyOnScan");

			DrawWalkableField();
			DrawPenaltyField();
			DrawTagField();

			EditorGUILayout.Separator();

			if (legacyMode.hasMultipleDifferentValues || legacyMode.boolValue) {
				EditorGUILayout.HelpBox("Legacy mode is enabled because you have upgraded from an earlier version of the A* Pathfinding Project. " +
					"Disabling legacy mode is recommended but you may have to tweak the point locations or object rotation in some cases", MessageType.Warning);
				if (GUILayout.Button("Disable Legacy Mode")) {
					for (int i = 0; i < scripts.Length; i++) {
						Undo.RecordObject(scripts[i], "Disable Legacy Mode");
						scripts[i].DisableLegacyMode();
					}
				}
			}

			if (scripts.Length == 1 && scripts[0].points.Length >= 3) {
				var size = scripts[0].GetBounds().size;
				if (Mathf.Min(Mathf.Min(Mathf.Abs(size.x), Mathf.Abs(size.y)), Mathf.Abs(size.z)) < 0.05f) {
					EditorGUILayout.HelpBox("The bounding box is very thin. Your shape might be oriented incorrectly. The shape will be projected down on the XZ plane in local space. Rotate this object " +
						"so that the local XZ plane corresponds to the plane in which you want to create your shape. For example if you want to create your shape in the XY plane then " +
						"this object should have the rotation (-90,0,0). You will need to recreate your shape after rotating this object.", MessageType.Warning);
				}
			}

			if (GUILayout.Button("Edit points")) {
				Tools.current = Tool.Move;
			}

			if (GUILayout.Button("Clear all points")) {
				for (int i = 0; i < scripts.Length; i++) {
					Undo.RecordObject(scripts[i], "Clear points");
					scripts[i].points = new Vector3[0];
					scripts[i].RecalcConvex();
				}
			}

			if (EditorGUI.EndChangeCheck()) {
				for (int i = 0; i < scripts.Length; i++) {
					EditorUtility.SetDirty(scripts[i]);
				}

				// Repaint the scene view if necessary
				if (!Application.isPlaying || EditorApplication.isPaused) SceneView.RepaintAll();
			}
		}

		void DrawPointsField () {
			EditorGUI.BeginChangeCheck();
			PropertyField("points");
			if (EditorGUI.EndChangeCheck()) {
				serializedObject.ApplyModifiedProperties();
				for (int i = 0; i < scripts.Length; i++) {
					scripts[i].RecalcConvex();
				}
				HandleUtility.Repaint();
			}
		}

		void DrawPhysicsField () {
			if (PropertyField("updatePhysics", "Update Physics", "Perform similar calculations on the nodes as during scan.\n" +
				"Grid Graphs will update the position of the nodes and also check walkability using collision.\nSee online documentation for more info.")) {
				EditorGUI.indentLevel++;
				PropertyField("resetPenaltyOnPhysics");
				EditorGUI.indentLevel--;
			}
		}

		void DrawConvexField () {
			EditorGUI.BeginChangeCheck();
			PropertyField("convex");
			if (EditorGUI.EndChangeCheck()) {
				serializedObject.ApplyModifiedProperties();
				for (int i = 0; i < scripts.Length; i++) {
					scripts[i].RecalcConvex();
				}
				HandleUtility.Repaint();
			}
		}

		void DrawWalkableField () {
			if (PropertyField("modifyWalkability")) {
				EditorGUI.indentLevel++;
				PropertyField("setWalkability", "Walkability Value");
				EditorGUI.indentLevel--;
			}
		}

		void DrawPenaltyField () {
			PropertyField("penaltyDelta", "Penalty Delta");

			if (!FindProperty("penaltyDelta").hasMultipleDifferentValues && FindProperty("penaltyDelta").intValue < 0) {
				EditorGUILayout.HelpBox("Be careful when lowering the penalty. Negative penalties are not supported and will instead underflow and get really high.\n" +
					"You can set an initial penalty on graphs (see their settings) and then lower them like this to get regions which are easier to traverse.", MessageType.Warning);
			}
		}

		void DrawTagField () {
			if (PropertyField("modifyTag")) {
				EditorGUI.indentLevel++;
				PropertyField("setTag");

				if (GUILayout.Button("Tags can be used to restrict which units can walk on what ground. Click here for more info", "HelpBox")) {
					Application.OpenURL(AstarUpdateChecker.GetURL("tags"));
				}
				EditorGUI.indentLevel--;
			}
		}

		static void SphereCap (int controlID, Vector3 position, Quaternion rotation, float size) {
#if UNITY_5_5_OR_NEWER
			Handles.SphereHandleCap(controlID, position, rotation, size, Event.current.type);
#else
			Handles.SphereCap(controlID, position, rotation, size);
#endif
		}

		public void OnSceneGUI () {
			var script = target as GraphUpdateScene;

			// Don't allow editing unless it is the active object
			if (Selection.activeGameObject != script.gameObject || script.legacyMode) return;

			// Make sure the points array is not null
			if (script.points == null) {
				script.points = new Vector3[0];
				EditorUtility.SetDirty(script);
			}

			List<Vector3> points = Pathfinding.Util.ListPool<Vector3>.Claim();
			points.AddRange(script.points);

			Matrix4x4 invMatrix = script.transform.worldToLocalMatrix;

			Matrix4x4 matrix = script.transform.localToWorldMatrix;
			for (int i = 0; i < points.Count; i++) points[i] = matrix.MultiplyPoint3x4(points[i]);


			float minScreenDist = float.PositiveInfinity;
			if (Tools.current != Tool.View && (Event.current.type == EventType.Layout || Event.current.type == EventType.MouseMove)) {
				for (int i = 0; i < script.points.Length; i++) {
					float dist = HandleUtility.DistanceToLine(points[i], (points[i] + points[(i+1) % script.points.Length])*0.5f);
					dist = Mathf.Min(dist, HandleUtility.DistanceToLine(points[i], (points[i] + points[(i-1 + script.points.Length) % script.points.Length])*0.5f));
					HandleUtility.AddControl(-i - 1, dist);
					minScreenDist = Mathf.Min(dist, minScreenDist);
				}
			}

			// If there is a point sort of close to the cursor, but not close enough
			// to receive a click event, then prevent the user from accidentally clicking
			// which would deselect the current object and be kinda annoying.
			// Also always add a default control when shift is pressed, to ensure we don't deselect anything.
			// This is particularly important when the user is holding shift to add the first point.
			if (Tools.current != Tool.View && (minScreenDist < 50 || Event.current.shift))
				HandleUtility.AddDefaultControl(0);

			for (int i = 0; i < points.Count; i++) {
				if (i == selectedPoint && Tools.current == Tool.Move) {
					Handles.color = PointSelectedColor;
					SphereCap(-i-1, points[i], Quaternion.identity, HandleUtility.GetHandleSize(points[i])*pointGizmosRadius*2);

					Vector3 pre = points[i];
					Vector3 post = Handles.PositionHandle(points[i], Quaternion.identity);
					if (pre != post) {
						Undo.RecordObject(script, "Moved Point");
						script.points[i] = invMatrix.MultiplyPoint3x4(post);
					}
				} else {
					Handles.color = PointColor;
					SphereCap(-i-1, points[i], Quaternion.identity, HandleUtility.GetHandleSize(points[i])*pointGizmosRadius);
				}
			}

			if (Event.current.type == EventType.MouseDown) {
				int pre = selectedPoint;
				selectedPoint = -(HandleUtility.nearestControl+1);
				if (pre != selectedPoint) GUI.changed = true;
			}

			if (Tools.current == Tool.Move) {
				var darkSkin = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Scene);

				Handles.BeginGUI();
				float width = 220;
				float height = 76;
				float margin = 10;

				AstarPathEditor.LoadStyles();
				GUILayout.BeginArea(new Rect(Camera.current.pixelWidth - width, Camera.current.pixelHeight - height, width - margin, height - margin), "Shortcuts", AstarPathEditor.astarSkin.FindStyle("SceneBoxDark"));

				GUILayout.Label("Shift+Click: Add new point", darkSkin.label);
				GUILayout.Label("Backspace: Delete selected point", darkSkin.label);

				// Invisible button to capture clicks. This prevents a click inside the box from causing some other GameObject to be selected.
				GUI.Button(new Rect(0, 0, width - margin, height - margin), "", GUIStyle.none);
				GUILayout.EndArea();

				Handles.EndGUI();
			}

			if (Tools.current == Tool.Move && Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Backspace && selectedPoint >= 0 && selectedPoint < points.Count) {
				Undo.RecordObject(script, "Removed Point");
				var arr = new List<Vector3>(script.points);
				arr.RemoveAt(selectedPoint);
				points.RemoveAt(selectedPoint);
				script.points = arr.ToArray();
				GUI.changed = true;
			}

			if (Event.current.shift && Tools.current == Tool.Move) {
				HandleUtility.Repaint();

				// Find the closest segment
				int insertionIndex = points.Count;
				float minDist = float.PositiveInfinity;
				for (int i = 0; i < points.Count; i++) {
					float dist = HandleUtility.DistanceToLine(points[i], points[(i+1)%points.Count]);
					if (dist < minDist) {
						insertionIndex = i + 1;
						minDist = dist;
					}
				}

				var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
				System.Object hit = HandleUtility.RaySnap(ray);

				Vector3 rayhit = Vector3.zero;
				bool didHit = false;
				if (hit != null) {
					rayhit = ((RaycastHit)hit).point;
					didHit = true;
				} else {
					var plane = new Plane(script.transform.up, script.transform.position);
					float distance;
					plane.Raycast(ray, out distance);
					if (distance > 0) {
						rayhit = ray.GetPoint(distance);
						didHit = true;
					}
				}

				if (!didHit && SceneView.currentDrawingSceneView.in2DMode) {
					didHit = true;
					rayhit = ray.origin;
					rayhit.z = 0;
				}

				if (didHit) {
					if (Event.current.type == EventType.MouseDown) {
						points.Insert(insertionIndex, rayhit);

						Undo.RecordObject(script, "Added Point");
						var arr = new List<Vector3>(script.points);
						arr.Insert(insertionIndex, invMatrix.MultiplyPoint3x4(rayhit));
						script.points = arr.ToArray();
						GUI.changed = true;
					} else {
						Handles.color = Color.green;
						if (points.Count > 0) {
							Handles.DrawDottedLine(points[(insertionIndex-1 + points.Count) % points.Count], rayhit, 8);
							Handles.DrawDottedLine(points[insertionIndex % points.Count], rayhit, 8);
						}
						SphereCap(0, rayhit, Quaternion.identity, HandleUtility.GetHandleSize(rayhit)*pointGizmosRadius);
						// Project point down onto a plane
						var zeroed = invMatrix.MultiplyPoint3x4(rayhit);
						zeroed.y = 0;
						Handles.color = new Color(1, 1, 1, 0.5f);
						Handles.DrawDottedLine(matrix.MultiplyPoint3x4(zeroed), rayhit, 4);
					}
				}

				if (Event.current.type == EventType.MouseDown) {
					Event.current.Use();
				}
			}

			// Make sure the convex hull stays up to date
			script.RecalcConvex();
			Pathfinding.Util.ListPool<Vector3>.Release(ref points);

			if (GUI.changed) HandleUtility.Repaint();
		}
	}
}