ExposedParameterView.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEditor.UIElements;
  6. using UnityEditor.Experimental.GraphView;
  7. using UnityEngine.UIElements;
  8. using System.Linq;
  9. using System;
  10. namespace GraphProcessor
  11. {
  12. public class ExposedParameterView : PinnedElementView
  13. {
  14. protected BaseGraphView graphView;
  15. new const string title = "Parameters";
  16. readonly string exposedParameterViewStyle = "GraphProcessorStyles/ExposedParameterView";
  17. List<Rect> blackboardLayouts = new List<Rect>();
  18. public ExposedParameterView()
  19. {
  20. var style = Resources.Load<StyleSheet>(exposedParameterViewStyle);
  21. if (style != null)
  22. styleSheets.Add(style);
  23. }
  24. protected virtual void OnAddClicked()
  25. {
  26. var parameterType = new GenericMenu();
  27. foreach (var paramType in GetExposedParameterTypes())
  28. parameterType.AddItem(new GUIContent(GetNiceNameFromType(paramType)), false, () =>
  29. {
  30. string uniqueName = "New " + GetNiceNameFromType(paramType);
  31. uniqueName = GetUniqueExposedPropertyName(uniqueName);
  32. graphView.graph.AddExposedParameter(uniqueName, paramType);
  33. });
  34. parameterType.ShowAsContext();
  35. }
  36. protected string GetNiceNameFromType(Type type)
  37. {
  38. string name = type.Name;
  39. // Remove parameter in the name of the type if it exists
  40. name = name.Replace("Parameter", "");
  41. return ObjectNames.NicifyVariableName(name);
  42. }
  43. protected string GetUniqueExposedPropertyName(string name)
  44. {
  45. // Generate unique name
  46. string uniqueName = name;
  47. int i = 0;
  48. while (graphView.graph.exposedParameters.Any(e => e.name == name))
  49. name = uniqueName + " " + i++;
  50. return name;
  51. }
  52. protected virtual IEnumerable< Type > GetExposedParameterTypes()
  53. {
  54. foreach (var type in TypeCache.GetTypesDerivedFrom<ExposedParameter>())
  55. {
  56. if (type.IsGenericType)
  57. continue ;
  58. yield return type;
  59. }
  60. }
  61. protected virtual void UpdateParameterList()
  62. {
  63. content.Clear();
  64. foreach (var param in graphView.graph.exposedParameters)
  65. {
  66. var row = new BlackboardRow(new ExposedParameterFieldView(graphView, param), new ExposedParameterPropertyView(graphView, param));
  67. row.expanded = param.settings.expanded;
  68. row.RegisterCallback<GeometryChangedEvent>(e => {
  69. param.settings.expanded = row.expanded;
  70. });
  71. content.Add(row);
  72. }
  73. }
  74. protected override void Initialize(BaseGraphView graphView)
  75. {
  76. this.graphView = graphView;
  77. base.title = title;
  78. scrollable = true;
  79. graphView.onExposedParameterListChanged += UpdateParameterList;
  80. graphView.initialized += UpdateParameterList;
  81. Undo.undoRedoPerformed += UpdateParameterList;
  82. RegisterCallback<DragUpdatedEvent>(OnDragUpdatedEvent);
  83. RegisterCallback<DragPerformEvent>(OnDragPerformEvent);
  84. RegisterCallback<MouseDownEvent>(OnMouseDownEvent, TrickleDown.TrickleDown);
  85. RegisterCallback<DetachFromPanelEvent>(OnViewClosed);
  86. UpdateParameterList();
  87. // Add exposed parameter button
  88. header.Add(new Button(OnAddClicked){
  89. text = "+"
  90. });
  91. }
  92. void OnViewClosed(DetachFromPanelEvent evt)
  93. => Undo.undoRedoPerformed -= UpdateParameterList;
  94. void OnMouseDownEvent(MouseDownEvent evt)
  95. {
  96. blackboardLayouts = content.Children().Select(c => c.layout).ToList();
  97. }
  98. int GetInsertIndexFromMousePosition(Vector2 pos)
  99. {
  100. pos = content.WorldToLocal(pos);
  101. // We only need to look for y axis;
  102. float mousePos = pos.y;
  103. if (mousePos < 0)
  104. return 0;
  105. int index = 0;
  106. foreach (var layout in blackboardLayouts)
  107. {
  108. if (mousePos > layout.yMin && mousePos < layout.yMax)
  109. return index + 1;
  110. index++;
  111. }
  112. return content.childCount;
  113. }
  114. void OnDragUpdatedEvent(DragUpdatedEvent evt)
  115. {
  116. DragAndDrop.visualMode = DragAndDropVisualMode.Move;
  117. int newIndex = GetInsertIndexFromMousePosition(evt.mousePosition);
  118. var graphSelectionDragData = DragAndDrop.GetGenericData("DragSelection");
  119. if (graphSelectionDragData == null)
  120. return;
  121. foreach (var obj in graphSelectionDragData as List<ISelectable>)
  122. {
  123. if (obj is ExposedParameterFieldView view)
  124. {
  125. var blackBoardRow = view.parent.parent.parent.parent.parent.parent;
  126. int oldIndex = content.Children().ToList().FindIndex(c => c == blackBoardRow);
  127. // Try to find the blackboard row
  128. content.Remove(blackBoardRow);
  129. if (newIndex > oldIndex)
  130. newIndex--;
  131. content.Insert(newIndex, blackBoardRow);
  132. }
  133. }
  134. }
  135. void OnDragPerformEvent(DragPerformEvent evt)
  136. {
  137. bool updateList = false;
  138. int newIndex = GetInsertIndexFromMousePosition(evt.mousePosition);
  139. foreach (var obj in DragAndDrop.GetGenericData("DragSelection") as List<ISelectable>)
  140. {
  141. if (obj is ExposedParameterFieldView view)
  142. {
  143. if (!updateList)
  144. graphView.RegisterCompleteObjectUndo("Moved parameters");
  145. int oldIndex = graphView.graph.exposedParameters.FindIndex(e => e == view.parameter);
  146. var parameter = graphView.graph.exposedParameters[oldIndex];
  147. graphView.graph.exposedParameters.RemoveAt(oldIndex);
  148. // Patch new index after the remove operation:
  149. if (newIndex > oldIndex)
  150. newIndex--;
  151. graphView.graph.exposedParameters.Insert(newIndex, parameter);
  152. updateList = true;
  153. }
  154. }
  155. if (updateList)
  156. {
  157. graphView.graph.NotifyExposedParameterListChanged();
  158. evt.StopImmediatePropagation();
  159. UpdateParameterList();
  160. }
  161. }
  162. }
  163. }