GroupView.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using UnityEngine;
  2. using UnityEditor.Experimental.GraphView;
  3. using UnityEditor.UIElements;
  4. using UnityEngine.UIElements;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. namespace GraphProcessor
  9. {
  10. public class GroupView : UnityEditor.Experimental.GraphView.Group
  11. {
  12. public BaseGraphView owner;
  13. public Group group;
  14. Label titleLabel;
  15. ColorField colorField;
  16. readonly string groupStyle = "GraphProcessorStyles/GroupView";
  17. public GroupView()
  18. {
  19. styleSheets.Add(Resources.Load<StyleSheet>(groupStyle));
  20. }
  21. private static void BuildContextualMenu(ContextualMenuPopulateEvent evt) {}
  22. public void Initialize(BaseGraphView graphView, Group block)
  23. {
  24. group = block;
  25. owner = graphView;
  26. title = block.title;
  27. SetPosition(block.position);
  28. this.AddManipulator(new ContextualMenuManipulator(BuildContextualMenu));
  29. headerContainer.Q<TextField>().RegisterCallback<ChangeEvent<string>>(TitleChangedCallback);
  30. titleLabel = headerContainer.Q<Label>();
  31. colorField = new ColorField{ value = group.color, name = "headerColorPicker" };
  32. colorField.RegisterValueChangedCallback(e =>
  33. {
  34. UpdateGroupColor(e.newValue);
  35. });
  36. UpdateGroupColor(group.color);
  37. headerContainer.Add(colorField);
  38. InitializeInnerNodes();
  39. }
  40. void InitializeInnerNodes()
  41. {
  42. foreach (var nodeGUID in group.innerNodeGUIDs.ToList())
  43. {
  44. if (!owner.graph.nodesPerGUID.ContainsKey(nodeGUID))
  45. {
  46. Debug.LogWarning("Node GUID not found: " + nodeGUID);
  47. group.innerNodeGUIDs.Remove(nodeGUID);
  48. continue ;
  49. }
  50. var node = owner.graph.nodesPerGUID[nodeGUID];
  51. var nodeView = owner.nodeViewsPerNode[node];
  52. AddElement(nodeView);
  53. }
  54. }
  55. protected override void OnElementsAdded(IEnumerable<GraphElement> elements)
  56. {
  57. foreach (var element in elements)
  58. {
  59. var node = element as BaseNodeView;
  60. // Adding an element that is not a node currently supported
  61. if (node == null)
  62. continue;
  63. if (!group.innerNodeGUIDs.Contains(node.nodeTarget.GUID))
  64. group.innerNodeGUIDs.Add(node.nodeTarget.GUID);
  65. }
  66. base.OnElementsAdded(elements);
  67. }
  68. protected override void OnElementsRemoved(IEnumerable<GraphElement> elements)
  69. {
  70. // Only remove the nodes when the group exists in the hierarchy
  71. if (parent != null)
  72. {
  73. foreach (var elem in elements)
  74. {
  75. if (elem is BaseNodeView nodeView)
  76. {
  77. group.innerNodeGUIDs.Remove(nodeView.nodeTarget.GUID);
  78. }
  79. }
  80. }
  81. base.OnElementsRemoved(elements);
  82. }
  83. public void UpdateGroupColor(Color newColor)
  84. {
  85. group.color = newColor;
  86. style.backgroundColor = newColor;
  87. }
  88. void TitleChangedCallback(ChangeEvent< string > e)
  89. {
  90. group.title = e.newValue;
  91. }
  92. public override void SetPosition(Rect newPos)
  93. {
  94. base.SetPosition(newPos);
  95. group.position = newPos;
  96. }
  97. }
  98. }