No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StateCanvas.cs 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. yield return new DropdownOption((Action<Vector2>)(NewSticky), "Create Sticky Note");
  98. foreach (var baseOption in base.GetContextOptions())
  99. {
  100. yield return baseOption;
  101. }
  102. }
  103. private void CreateFlowState(Vector2 position)
  104. {
  105. var flowState = FlowState.WithEnterUpdateExit();
  106. if (!graph.states.Any())
  107. {
  108. flowState.isStart = true;
  109. flowState.nest.embed.title = "Start";
  110. }
  111. AddState(flowState, position);
  112. }
  113. private void CreateSuperState(Vector2 position)
  114. {
  115. var superState = SuperState.WithStart();
  116. if (!graph.states.Any())
  117. {
  118. superState.isStart = true;
  119. superState.nest.embed.title = "Start";
  120. }
  121. AddState(superState, position);
  122. }
  123. private void CreateAnyState(Vector2 position)
  124. {
  125. AddState(new AnyState(), position);
  126. }
  127. private void NewSticky(Vector2 position)
  128. {
  129. var stickyNote = new StickyNote() { position = new Rect(position, new Vector2(100, 100)) };
  130. graph.elements.Add(stickyNote);
  131. selection.Select(stickyNote);
  132. GUI.changed = true;
  133. }
  134. public void AddState(IState state, Vector2 position)
  135. {
  136. UndoUtility.RecordEditedObject("Create State");
  137. state.position = position;
  138. graph.states.Add(state);
  139. state.position -= this.Widget(state).position.size / 2;
  140. state.position = state.position.PixelPerfect();
  141. this.Widget(state).Reposition();
  142. selection.Select(state);
  143. GUI.changed = true;
  144. }
  145. #endregion
  146. #region Lifecycle
  147. public override void Close()
  148. {
  149. base.Close();
  150. CancelTransition();
  151. }
  152. protected override void HandleHighPriorityInput()
  153. {
  154. if (isCreatingTransition)
  155. {
  156. if (e.IsMouseDrag(MouseButton.Left))
  157. {
  158. // Priority over lasso
  159. e.Use();
  160. }
  161. else if (e.IsKeyDown(KeyCode.Escape))
  162. {
  163. CancelTransition();
  164. e.Use();
  165. }
  166. if (e.IsMouseDown(MouseButton.Left) || e.IsMouseUp(MouseButton.Left))
  167. {
  168. CompleteTransitionToNewState();
  169. e.Use();
  170. }
  171. }
  172. base.HandleHighPriorityInput();
  173. }
  174. public void CompleteTransitionToNewState()
  175. {
  176. var startRect = this.Widget(transitionSource).position;
  177. var end = mousePosition;
  178. GraphGUI.GetConnectionEdge
  179. (
  180. startRect.center,
  181. end,
  182. out var startEdge,
  183. out var endEdge
  184. );
  185. var destination = FlowState.WithEnterUpdateExit();
  186. graph.states.Add(destination);
  187. Vector2 offset;
  188. var size = this.Widget(destination).position.size;
  189. switch (endEdge)
  190. {
  191. case Edge.Left:
  192. offset = new Vector2(0, -size.y / 2);
  193. break;
  194. case Edge.Right:
  195. offset = new Vector2(-size.x, -size.y / 2);
  196. break;
  197. case Edge.Top:
  198. offset = new Vector2(-size.x / 2, 0);
  199. break;
  200. case Edge.Bottom:
  201. offset = new Vector2(-size.x / 2, -size.y);
  202. break;
  203. default:
  204. throw new UnexpectedEnumValueException<Edge>(endEdge);
  205. }
  206. destination.position = mousePosition + offset;
  207. destination.position = destination.position.PixelPerfect();
  208. EndTransition(destination);
  209. }
  210. #endregion
  211. #region Drag & Drop
  212. public override bool AcceptsDragAndDrop()
  213. {
  214. return DragAndDropUtility.Is<ScriptGraphAsset>() || DragAndDropUtility.Is<StateGraphAsset>();
  215. }
  216. public override void PerformDragAndDrop()
  217. {
  218. if (DragAndDropUtility.Is<ScriptGraphAsset>())
  219. {
  220. var flowMacro = DragAndDropUtility.Get<ScriptGraphAsset>();
  221. var flowState = new FlowState(flowMacro);
  222. AddState(flowState, DragAndDropUtility.position);
  223. }
  224. else if (DragAndDropUtility.Is<StateGraphAsset>())
  225. {
  226. var asset = DragAndDropUtility.Get<StateGraphAsset>();
  227. var superState = new SuperState(asset);
  228. AddState(superState, DragAndDropUtility.position);
  229. }
  230. }
  231. public override void DrawDragAndDropPreview()
  232. {
  233. if (DragAndDropUtility.Is<ScriptGraphAsset>())
  234. {
  235. GraphGUI.DrawDragAndDropPreviewLabel(DragAndDropUtility.offsetedPosition, DragAndDropUtility.Get<ScriptGraphAsset>().name, typeof(ScriptGraphAsset).Icon());
  236. }
  237. else if (DragAndDropUtility.Is<StateGraphAsset>())
  238. {
  239. GraphGUI.DrawDragAndDropPreviewLabel(DragAndDropUtility.offsetedPosition, DragAndDropUtility.Get<StateGraphAsset>().name, typeof(StateGraphAsset).Icon());
  240. }
  241. }
  242. #endregion
  243. #region Transition Creation
  244. public IState transitionSource { get; set; }
  245. public bool isCreatingTransition => transitionSource != null;
  246. public void StartTransition(IState source)
  247. {
  248. transitionSource = source;
  249. window.Focus();
  250. }
  251. public void EndTransition(IState destination)
  252. {
  253. UndoUtility.RecordEditedObject("Create State Transition");
  254. var transition = FlowStateTransition.WithDefaultTrigger(transitionSource, destination);
  255. graph.transitions.Add(transition);
  256. transitionSource = null;
  257. this.Widget(transition).BringToFront();
  258. selection.Select(transition);
  259. GUI.changed = true;
  260. }
  261. public void CancelTransition()
  262. {
  263. transitionSource = null;
  264. }
  265. #endregion
  266. }
  267. }