RouteEditor.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. GUILayout.FlexibleSpace();
  110. EditorGUILayout.EndHorizontal();
  111. for (var i = 0; i < list.arraySize; i++)
  112. {
  113. EditorGUILayout.BeginHorizontal();
  114. EditorGUILayout.PropertyField(list.GetArrayElementAtIndex(i), true);
  115. if (GUILayout.Button("-", EditorStyles.miniButton, GUILayout.Width(32)))
  116. {
  117. list.DeleteArrayElementAtIndex(i);
  118. }
  119. EditorGUILayout.EndHorizontal();
  120. }
  121. EditorGUI.indentLevel -= 1;
  122. }
  123. EditorGUILayout.EndFoldoutHeaderGroup();
  124. }
  125. private void Clear()
  126. {
  127. throw new NotImplementedException();
  128. }
  129. private void SwitchSelectionMode()
  130. {
  131. inSelectionMode = !inSelectionMode;
  132. if (inSelectionMode)
  133. {
  134. selectedTurns.Clear();
  135. }
  136. else
  137. {
  138. ApplySelectedTurns();
  139. }
  140. }
  141. private void ApplySelectedTurns()
  142. {
  143. var items = serializedObject.FindProperty("items");
  144. var size = items.arraySize;
  145. SerializedProperty previousItem = null;
  146. for (var index = 0; index < selectedTurns.Count; index++)
  147. {
  148. var serializedIndex = size + index;
  149. var turn = selectedTurns[index];
  150. var previousTurn = index == 0 ? null : selectedTurns[index - 1];
  151. items.InsertArrayElementAtIndex(serializedIndex);
  152. var newItem = items.GetArrayElementAtIndex(serializedIndex);
  153. newItem.FindPropertyRelative("turn").objectReferenceValue = turn;
  154. Debug.Log($"---- Element {serializedIndex} ----");
  155. Debug.Log("Calculate From");
  156. newItem.FindPropertyRelative("from").enumValueIndex = CalculateDirection(turn.transform,
  157. previousTurn != null ? previousTurn.transform : null);
  158. newItem.FindPropertyRelative("to").enumValueIndex = roadDirectionIndexes[RoadDirection.None];
  159. if (previousItem != null)
  160. {
  161. Debug.Log("Calculate Previous To");
  162. previousItem.FindPropertyRelative("to").enumValueIndex =
  163. CalculateDirection(previousTurn != null ? previousTurn.transform : null, turn.transform);
  164. }
  165. previousItem = newItem;
  166. }
  167. selectedTurns.Clear();
  168. }
  169. private int CalculateDirection(Transform turnTransform, Transform reference)
  170. {
  171. if (reference == null) return roadDirectionIndexes[RoadDirection.None];
  172. var dif = reference.position - turnTransform.position;
  173. Debug.Log("Difference to reference = " + dif);
  174. RoadDirection direction;
  175. if (Mathf.Abs(dif.x) > Mathf.Abs(dif.z))
  176. {
  177. direction = dif.x < 0 ? RoadDirection.West : RoadDirection.East;
  178. }
  179. else
  180. {
  181. direction = dif.z < 0 ? RoadDirection.South : RoadDirection.North;
  182. }
  183. return roadDirectionIndexes[direction];
  184. }
  185. [DrawGizmo(GizmoType.Selected)]
  186. private static void DrawRoutePreview(Route route, GizmoType gizmoType)
  187. {
  188. length = 0f; // a bit hacky but who cares
  189. if (route.items == null) return;
  190. Gizmos.color = Color.red;
  191. Turn previousTurn = null;
  192. Vector3 from;
  193. Vector3 to;
  194. if (route.start != null && route.items.Count > 0)
  195. {
  196. from = route.start.transform.position;
  197. to = route.items[0].turn.transform.position;
  198. length += (to - from).magnitude;
  199. Helpers.DrawLine(from, to, 3);
  200. }
  201. foreach (var item in route.items)
  202. {
  203. var turn = item.turn;
  204. if (previousTurn != null)
  205. {
  206. from = previousTurn.transform.position;
  207. to = turn.transform.position;
  208. length += (to - from).magnitude;
  209. Helpers.DrawLine(from, to, 3);
  210. }
  211. previousTurn = turn;
  212. }
  213. if (route.finish != null && route.items.Count > 0)
  214. {
  215. from = route.items.Last().turn.transform.position;
  216. to = route.finish.transform.position;
  217. length += (to - from).magnitude;
  218. Helpers.DrawLine(from, to, 3);
  219. }
  220. }
  221. }