StateCanvas.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace Unity.VisualScripting
  7. {
  8. [Canvas(typeof(StateGraph))]
  9. public sealed class StateCanvas : VisualScriptingCanvas<StateGraph>
  10. {
  11. public StateCanvas(StateGraph graph) : base(graph) { }
  12. #region View
  13. protected override bool shouldEdgePan => base.shouldEdgePan || isCreatingTransition;
  14. #endregion
  15. #region Drawing
  16. protected override void DrawBackground()
  17. {
  18. base.DrawBackground();
  19. if (isCreatingTransition)
  20. {
  21. var startRect = this.Widget(transitionSource).position;
  22. var end = mousePosition;
  23. Edge startEdge, endEdge;
  24. GraphGUI.GetConnectionEdge
  25. (
  26. startRect.center,
  27. end,
  28. out startEdge,
  29. out endEdge
  30. );
  31. var start = startRect.GetEdgeCenter(startEdge);
  32. GraphGUI.DrawConnectionArrow(Color.white, start, end, startEdge, endEdge);
  33. }
  34. }
  35. #endregion
  36. #region Clipboard
  37. public override void ShrinkCopyGroup(HashSet<IGraphElement> copyGroup)
  38. {
  39. copyGroup.RemoveWhere(element =>
  40. {
  41. if (element is IStateTransition)
  42. {
  43. var transition = (IStateTransition)element;
  44. if (!copyGroup.Contains(transition.source) ||
  45. !copyGroup.Contains(transition.destination))
  46. {
  47. return true;
  48. }
  49. }
  50. return false;
  51. });
  52. }
  53. #endregion
  54. #region Window
  55. public override void OnToolbarGUI()
  56. {
  57. if (graph.states.Any(u => u.GetException(reference) != null) || graph.transitions.Any(t => t.GetException(reference) != null))
  58. {
  59. if (GUILayout.Button("Clear Errors", LudiqStyles.toolbarButton))
  60. {
  61. foreach (var state in graph.states)
  62. {
  63. state.SetException(reference, null);
  64. }
  65. foreach (var transition in graph.transitions)
  66. {
  67. transition.SetException(reference, null);
  68. }
  69. }
  70. }
  71. EditorGUI.BeginChangeCheck();
  72. BoltCore.Configuration.dimInactiveNodes = GUILayout.Toggle(BoltCore.Configuration.dimInactiveNodes, "Dim", LudiqStyles.toolbarButton);
  73. if (EditorGUI.EndChangeCheck())
  74. {
  75. BoltCore.Configuration.Save();
  76. }
  77. base.OnToolbarGUI();
  78. }
  79. #endregion
  80. #region Context
  81. protected override void OnContext()
  82. {
  83. if (isCreatingTransition)
  84. {
  85. CancelTransition();
  86. }
  87. else
  88. {
  89. base.OnContext();
  90. }
  91. }
  92. protected override IEnumerable<DropdownOption> GetContextOptions()
  93. {
  94. yield return new DropdownOption((Action<Vector2>)CreateFlowState, "Create Script State");
  95. yield return new DropdownOption((Action<Vector2>)CreateSuperState, "Create Super State");
  96. yield return new DropdownOption((Action<Vector2>)CreateAnyState, "Create Any State");
  97. foreach (var baseOption in base.GetContextOptions())
  98. {
  99. yield return baseOption;
  100. }
  101. }
  102. private void CreateFlowState(Vector2 position)
  103. {
  104. var flowState = FlowState.WithEnterUpdateExit();
  105. if (!graph.states.Any())
  106. {
  107. flowState.isStart = true;
  108. flowState.nest.embed.title = "Start";
  109. }
  110. AddState(flowState, position);
  111. }
  112. private void CreateSuperState(Vector2 position)
  113. {
  114. var superState = SuperState.WithStart();
  115. if (!graph.states.Any())
  116. {
  117. superState.isStart = true;
  118. superState.nest.embed.title = "Start";
  119. }
  120. AddState(superState, position);
  121. }
  122. private void CreateAnyState(Vector2 position)
  123. {
  124. AddState(new AnyState(), position);
  125. }
  126. public void AddState(IState state, Vector2 position)
  127. {
  128. UndoUtility.RecordEditedObject("Create State");
  129. state.position = position;
  130. graph.states.Add(state);
  131. state.position -= this.Widget(state).position.size / 2;
  132. state.position = state.position.PixelPerfect();
  133. this.Widget(state).Reposition();
  134. selection.Select(state);
  135. GUI.changed = true;
  136. }
  137. #endregion
  138. #region Lifecycle
  139. public override void Close()
  140. {
  141. base.Close();
  142. CancelTransition();
  143. }
  144. protected override void HandleHighPriorityInput()
  145. {
  146. if (isCreatingTransition)
  147. {
  148. if (e.IsMouseDrag(MouseButton.Left))
  149. {
  150. // Priority over lasso
  151. e.Use();
  152. }
  153. else if (e.IsKeyDown(KeyCode.Escape))
  154. {
  155. CancelTransition();
  156. e.Use();
  157. }
  158. if (e.IsMouseDown(MouseButton.Left) || e.IsMouseUp(MouseButton.Left))
  159. {
  160. CompleteTransitionToNewState();
  161. e.Use();
  162. }
  163. }
  164. base.HandleHighPriorityInput();
  165. }
  166. public void CompleteTransitionToNewState()
  167. {
  168. var startRect = this.Widget(transitionSource).position;
  169. var end = mousePosition;
  170. GraphGUI.GetConnectionEdge
  171. (
  172. startRect.center,
  173. end,
  174. out var startEdge,
  175. out var endEdge
  176. );
  177. var destination = FlowState.WithEnterUpdateExit();
  178. graph.states.Add(destination);
  179. Vector2 offset;
  180. var size = this.Widget(destination).position.size;
  181. switch (endEdge)
  182. {
  183. case Edge.Left:
  184. offset = new Vector2(0, -size.y / 2);
  185. break;
  186. case Edge.Right:
  187. offset = new Vector2(-size.x, -size.y / 2);
  188. break;
  189. case Edge.Top:
  190. offset = new Vector2(-size.x / 2, 0);
  191. break;
  192. case Edge.Bottom:
  193. offset = new Vector2(-size.x / 2, -size.y);
  194. break;
  195. default:
  196. throw new UnexpectedEnumValueException<Edge>(endEdge);
  197. }
  198. destination.position = mousePosition + offset;
  199. destination.position = destination.position.PixelPerfect();
  200. EndTransition(destination);
  201. }
  202. #endregion
  203. #region Drag & Drop
  204. public override bool AcceptsDragAndDrop()
  205. {
  206. return DragAndDropUtility.Is<ScriptGraphAsset>() || DragAndDropUtility.Is<StateGraphAsset>();
  207. }
  208. public override void PerformDragAndDrop()
  209. {
  210. if (DragAndDropUtility.Is<ScriptGraphAsset>())
  211. {
  212. var flowMacro = DragAndDropUtility.Get<ScriptGraphAsset>();
  213. var flowState = new FlowState(flowMacro);
  214. AddState(flowState, DragAndDropUtility.position);
  215. }
  216. else if (DragAndDropUtility.Is<StateGraphAsset>())
  217. {
  218. var asset = DragAndDropUtility.Get<StateGraphAsset>();
  219. var superState = new SuperState(asset);
  220. AddState(superState, DragAndDropUtility.position);
  221. }
  222. }
  223. public override void DrawDragAndDropPreview()
  224. {
  225. if (DragAndDropUtility.Is<ScriptGraphAsset>())
  226. {
  227. GraphGUI.DrawDragAndDropPreviewLabel(DragAndDropUtility.offsetedPosition, DragAndDropUtility.Get<ScriptGraphAsset>().name, typeof(ScriptGraphAsset).Icon());
  228. }
  229. else if (DragAndDropUtility.Is<StateGraphAsset>())
  230. {
  231. GraphGUI.DrawDragAndDropPreviewLabel(DragAndDropUtility.offsetedPosition, DragAndDropUtility.Get<StateGraphAsset>().name, typeof(StateGraphAsset).Icon());
  232. }
  233. }
  234. #endregion
  235. #region Transition Creation
  236. public IState transitionSource { get; set; }
  237. public bool isCreatingTransition => transitionSource != null;
  238. public void StartTransition(IState source)
  239. {
  240. transitionSource = source;
  241. window.Focus();
  242. }
  243. public void EndTransition(IState destination)
  244. {
  245. UndoUtility.RecordEditedObject("Create State Transition");
  246. var transition = FlowStateTransition.WithDefaultTrigger(transitionSource, destination);
  247. graph.transitions.Add(transition);
  248. transitionSource = null;
  249. this.Widget(transition).BringToFront();
  250. selection.Select(transition);
  251. GUI.changed = true;
  252. }
  253. public void CancelTransition()
  254. {
  255. transitionSource = null;
  256. }
  257. #endregion
  258. }
  259. }