RouteEditor.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using ICSharpCode.NRefactory.PrettyPrinter;
  6. using Roads;
  7. using Routes;
  8. using UnityEditor;
  9. using UnityEngine;
  10. using UnityEngine.Rendering;
  11. using UnityEngine.Rendering.UI;
  12. [CustomEditor(typeof(Route))]
  13. [CanEditMultipleObjects]
  14. // ReSharper disable once CheckNamespace
  15. public class RoutesEditor : UnityEditor.Editor
  16. {
  17. private bool showListContent;
  18. private bool inSelectionMode;
  19. private SceneView sceneView;
  20. private readonly List<Turn> selectedTurns = new List<Turn>();
  21. private Route route;
  22. private static float length = 0;
  23. private static readonly Dictionary<RoadDirection, int> roadDirectionIndexes = new Dictionary<RoadDirection, int>
  24. {
  25. {RoadDirection.West, 0},
  26. {RoadDirection.North, 1},
  27. {RoadDirection.East, 2},
  28. {RoadDirection.South, 3},
  29. {RoadDirection.None, 4}
  30. };
  31. public override void OnInspectorGUI()
  32. {
  33. serializedObject.Update();
  34. EditorGUILayout.PropertyField(serializedObject.FindProperty("start"));
  35. EditorGUILayout.PropertyField(serializedObject.FindProperty("finish"));
  36. RoutesList(serializedObject.FindProperty("items"));
  37. EditorGUILayout.LabelField($"Current length: {length}");
  38. //EditorGUILayout.PropertyField(routes.FindPropertyRelative("arraySize"), true);true);
  39. serializedObject.ApplyModifiedProperties();
  40. }
  41. private void OnEnable()
  42. {
  43. route = (Route) target;
  44. }
  45. private void OnSceneGUI()
  46. {
  47. if (inSelectionMode)
  48. {
  49. if (Event.current.type == EventType.Layout)
  50. {
  51. HandleUtility.AddDefaultControl(0);
  52. }
  53. sceneView = SceneView.currentDrawingSceneView;
  54. if (sceneView == null)
  55. {
  56. Debug.LogError("RouteEditor: No Scene View found");
  57. }
  58. if (sceneView != null && Event.current.type == EventType.MouseDown && Event.current.button == 0)
  59. {
  60. /*var coords = Event.current.mousePosition;
  61. var pos = sceneView.camera.ScreenToWorldPoint(new Vector3(coords.x, coords.y, sceneView.camera.nearClipPlane));
  62. Debug.Log("pos: "+pos);
  63. Debug.DrawRay(pos, Vector3.down * 1000, Color.magenta);*/
  64. Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
  65. RaycastHit hitInfo;
  66. if (Physics.Raycast(worldRay, out hitInfo))
  67. {
  68. var intersection = GetIntersection(hitInfo.collider.gameObject);
  69. selectedTurns.Add(intersection);
  70. }
  71. }
  72. }
  73. if (GUI.changed)
  74. EditorUtility.SetDirty(target);
  75. }
  76. private Turn GetIntersection(GameObject gameObject)
  77. {
  78. Turn turn = null;
  79. for (var i = 0; i < 3; i++)
  80. {
  81. turn = gameObject.GetComponent<Turn>();
  82. if (turn != null || gameObject.transform.parent == null) break;
  83. gameObject = gameObject.transform.parent.gameObject;
  84. }
  85. Debug.Log($"Intersected Turn: {turn}");
  86. return turn;
  87. }
  88. private void RoutesList(SerializedProperty list)
  89. {
  90. showListContent =
  91. EditorGUILayout.BeginFoldoutHeaderGroup(showListContent, $"{list.displayName} (Length = {list.arraySize})");
  92. if (showListContent)
  93. {
  94. EditorGUI.indentLevel += 1;
  95. EditorGUILayout.BeginHorizontal();
  96. EditorGUILayout.PropertyField(list.FindPropertyRelative("Array.size"));
  97. if (GUILayout.Button("+", EditorStyles.miniButton, GUILayout.Width(32)))
  98. {
  99. list.InsertArrayElementAtIndex(list.arraySize);
  100. }
  101. if (GUILayout.Button(!inSelectionMode ? "Selection Mode" : "Quit Selection Mode", GUILayout.Width(128)))
  102. {
  103. SwitchSelectionMode();
  104. }
  105. /*if (GUILayout.Button("Clear", GUILayout.Width(56)))
  106. {
  107. Clear();
  108. }*/
  109. if (GUILayout.Button("Invert", GUILayout.Width(56)))
  110. {
  111. Invert();
  112. }
  113. GUILayout.FlexibleSpace();
  114. EditorGUILayout.EndHorizontal();
  115. for (var i = 0; i < list.arraySize; i++)
  116. {
  117. EditorGUILayout.BeginHorizontal();
  118. EditorGUILayout.PropertyField(list.GetArrayElementAtIndex(i), true);
  119. if (GUILayout.Button("-", EditorStyles.miniButton, GUILayout.Width(32)))
  120. {
  121. list.DeleteArrayElementAtIndex(i);
  122. }
  123. EditorGUILayout.EndHorizontal();
  124. }
  125. EditorGUI.indentLevel -= 1;
  126. }
  127. EditorGUILayout.EndFoldoutHeaderGroup();
  128. }
  129. private void Invert()
  130. {
  131. var items = serializedObject.FindProperty("items");
  132. var size = items.arraySize;
  133. var start = serializedObject.FindProperty("start");
  134. var finish = serializedObject.FindProperty("finish");
  135. var oldStart = start.objectReferenceValue;
  136. start.objectReferenceValue = finish.objectReferenceValue;
  137. finish.objectReferenceValue = oldStart;
  138. var inverted = new List<JunctionExtras>();
  139. for (int i = size - 1; i >= 0; i--)
  140. {
  141. var item = (JunctionExtras) items.GetArrayElementAtIndex(i).objectReferenceValue;
  142. inverted.Add(item);
  143. }
  144. items.ClearArray();
  145. for (int i = 0; i < inverted.Count; i++)
  146. {
  147. items.InsertArrayElementAtIndex(i);
  148. inverted[i].comingFrom = RoadDirectionHelper.OppositeOf(inverted[i].comingFrom);
  149. inverted[i].goingTo = RoadDirectionHelper.OppositeOf(inverted[i].goingTo);
  150. items.GetArrayElementAtIndex(i).objectReferenceValue = inverted[i];
  151. }
  152. }
  153. private void Clear()
  154. {
  155. throw new NotImplementedException();
  156. }
  157. private void SwitchSelectionMode()
  158. {
  159. inSelectionMode = !inSelectionMode;
  160. if (inSelectionMode)
  161. {
  162. selectedTurns.Clear();
  163. }
  164. else
  165. {
  166. ApplySelectedTurns();
  167. }
  168. }
  169. private void ApplySelectedTurns()
  170. {
  171. var items = serializedObject.FindProperty("items");
  172. var size = items.arraySize;
  173. SerializedProperty previousItem = null;
  174. for (var index = 0; index < selectedTurns.Count; index++)
  175. {
  176. var serializedIndex = size + index;
  177. var turn = selectedTurns[index];
  178. var previousTurn = index == 0 ? null : selectedTurns[index - 1];
  179. items.InsertArrayElementAtIndex(serializedIndex);
  180. var newItem = items.GetArrayElementAtIndex(serializedIndex);
  181. newItem.FindPropertyRelative("turn").objectReferenceValue = turn;
  182. Debug.Log($"---- Element {serializedIndex} ----");
  183. Debug.Log("Calculate From");
  184. newItem.FindPropertyRelative("from").enumValueIndex = CalculateDirection(turn.transform,
  185. previousTurn != null ? previousTurn.transform : null);
  186. newItem.FindPropertyRelative("to").enumValueIndex = roadDirectionIndexes[RoadDirection.None];
  187. if (previousItem != null)
  188. {
  189. Debug.Log("Calculate Previous To");
  190. previousItem.FindPropertyRelative("to").enumValueIndex =
  191. CalculateDirection(previousTurn != null ? previousTurn.transform : null, turn.transform);
  192. }
  193. previousItem = newItem;
  194. }
  195. selectedTurns.Clear();
  196. }
  197. private int CalculateDirection(Transform turnTransform, Transform reference)
  198. {
  199. if (reference == null) return roadDirectionIndexes[RoadDirection.None];
  200. var dif = reference.position - turnTransform.position;
  201. Debug.Log("Difference to reference = " + dif);
  202. RoadDirection direction;
  203. if (Mathf.Abs(dif.x) > Mathf.Abs(dif.z))
  204. {
  205. direction = dif.x < 0 ? RoadDirection.West : RoadDirection.East;
  206. }
  207. else
  208. {
  209. direction = dif.z < 0 ? RoadDirection.South : RoadDirection.North;
  210. }
  211. return roadDirectionIndexes[direction];
  212. }
  213. [DrawGizmo(GizmoType.Selected)]
  214. private static void DrawRoutePreview(Route route, GizmoType gizmoType)
  215. {
  216. length = 0f; // a bit hacky but who cares
  217. if (route.items == null) return;
  218. Gizmos.color = Color.red;
  219. Turn previousTurn = null;
  220. Vector3 from;
  221. Vector3 to;
  222. if (route.start != null && route.items.Count > 0)
  223. {
  224. from = route.start.transform.position;
  225. to = route.items[0].turn.transform.position;
  226. length += (to - from).magnitude;
  227. Helpers.DrawLine(from, to, 3);
  228. }
  229. foreach (var item in route.items)
  230. {
  231. var turn = item.turn;
  232. if (previousTurn != null)
  233. {
  234. from = previousTurn.transform.position;
  235. to = turn.transform.position;
  236. length += (to - from).magnitude;
  237. Helpers.DrawLine(from, to, 3);
  238. }
  239. previousTurn = turn;
  240. }
  241. if (route.finish != null && route.items.Count > 0)
  242. {
  243. from = route.items.Last().turn.transform.position;
  244. to = route.finish.transform.position;
  245. length += (to - from).magnitude;
  246. Helpers.DrawLine(from, to, 3);
  247. }
  248. }
  249. }