diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 937e107..2c58bcd 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -6,7 +6,7 @@ using UnityEngine; namespace XNodeEditor { public partial class NodeEditorWindow { - public enum NodeActivity { Idle, HoldNode, DragNode, HoldGrid, DragGrid, HoldComment, ResizeComment } + public enum NodeActivity { Idle, HoldNode, DragNode, HoldGrid, DragGrid, HoldGroup, ResizeGroup } public static NodeActivity currentActivity = NodeActivity.Idle; public static bool isPanning { get; private set; } public static Vector2[] dragOffset; @@ -15,18 +15,18 @@ namespace XNodeEditor { private bool IsHoveringPort { get { return hoveredPort != null; } } private bool IsHoveringNode { get { return hoveredNode != null; } } private bool IsHoveringReroute { get { return hoveredReroute.port != null; } } - private bool IsHoveringComment { get { return hoveredComment != null; } } - private bool IsResizingComment { get { return resizingComment != null; } } + private bool IsHoveringGroup { get { return hoveredGroup != null; } } + private bool IsResizingGroup { get { return resizingGroup != null; } } private XNode.Node hoveredNode = null; [NonSerialized] private XNode.NodePort hoveredPort = null; [NonSerialized] private XNode.NodePort draggedOutput = null; [NonSerialized] private XNode.NodePort draggedOutputTarget = null; [NonSerialized] private List draggedOutputReroutes = new List(); - [NonSerialized] private XNode.NodeGraphComment hoveredComment = null; - [NonSerialized] private XNode.NodeGraphComment resizingComment = null; - private bool deselectingComment = false; - public enum NodeGraphCommentSide { Top, TopRight, Right, BottomRight, Bottom, BottomLeft, Left, TopLeft } - public static NodeGraphCommentSide resizingCommentSide; + [NonSerialized] private XNode.NodeGroup hoveredGroup = null; + [NonSerialized] private XNode.NodeGroup resizingGroup = null; + private bool deselectingGroup = false; + public enum NodeGroupSide { Top, TopRight, Right, BottomRight, Bottom, BottomLeft, Left, TopLeft } + public static NodeGroupSide resizingGroupSide; private RerouteReference hoveredReroute = new RerouteReference(); private List selectedReroutes = new List(); private Rect nodeRects; @@ -75,8 +75,8 @@ namespace XNodeEditor { draggedOutputTarget = null; } Repaint(); - } else if (currentActivity == NodeActivity.HoldNode || currentActivity == NodeActivity.HoldComment) { - deselectingComment = false; + } else if (currentActivity == NodeActivity.HoldNode || currentActivity == NodeActivity.HoldGroup) { + deselectingGroup = false; RecalculateDragOffsets(e); currentActivity = NodeActivity.DragNode; Repaint(); @@ -118,13 +118,13 @@ namespace XNodeEditor { } } } - else if(Selection.objects[i] is XNode.NodeGraphComment) { - XNode.NodeGraphComment comment = Selection.objects[i] as XNode.NodeGraphComment; - Vector2 initial = comment.position; - comment.position = mousePos + dragOffset[i]; + else if(Selection.objects[i] is XNode.NodeGroup) { + XNode.NodeGroup group = Selection.objects[i] as XNode.NodeGroup; + Vector2 initial = group.position; + group.position = mousePos + dragOffset[i]; if (gridSnap) { - comment.position.x = (Mathf.Round((comment.position.x + 8) / 16) * 16) - 8; - comment.position.y = (Mathf.Round((comment.position.y + 8) / 16) * 16) - 8; + group.position.x = (Mathf.Round((group.position.x + 8) / 16) * 16) - 8; + group.position.y = (Mathf.Round((group.position.y + 8) / 16) * 16) - 8; } } } @@ -151,40 +151,40 @@ namespace XNodeEditor { if (boxSize.y < 0) { boxStartPos.y += boxSize.y; boxSize.y = Mathf.Abs(boxSize.y); } selectionBox = new Rect(boxStartPos, boxSize); Repaint(); - } else if (currentActivity == NodeActivity.ResizeComment) { - switch (resizingCommentSide) { - case NodeGraphCommentSide.Top: - resizingComment.size.y -= e.delta.y; - resizingComment.position.y += e.delta.y; + } else if (currentActivity == NodeActivity.ResizeGroup) { + switch (resizingGroupSide) { + case NodeGroupSide.Top: + resizingGroup.size.y -= e.delta.y; + resizingGroup.position.y += e.delta.y; break; - case NodeGraphCommentSide.TopRight: - resizingComment.size.y -= e.delta.y; - resizingComment.position.y += e.delta.y; - resizingComment.size.x += e.delta.x; + case NodeGroupSide.TopRight: + resizingGroup.size.y -= e.delta.y; + resizingGroup.position.y += e.delta.y; + resizingGroup.size.x += e.delta.x; break; - case NodeGraphCommentSide.Right: - resizingComment.size.x += e.delta.x; + case NodeGroupSide.Right: + resizingGroup.size.x += e.delta.x; break; - case NodeGraphCommentSide.BottomRight: - resizingComment.size += e.delta; + case NodeGroupSide.BottomRight: + resizingGroup.size += e.delta; break; - case NodeGraphCommentSide.Bottom: - resizingComment.size.y += e.delta.y; + case NodeGroupSide.Bottom: + resizingGroup.size.y += e.delta.y; break; - case NodeGraphCommentSide.BottomLeft: - resizingComment.size.x -= e.delta.x; - resizingComment.position.x += e.delta.x; - resizingComment.size.y += e.delta.y; + case NodeGroupSide.BottomLeft: + resizingGroup.size.x -= e.delta.x; + resizingGroup.position.x += e.delta.x; + resizingGroup.size.y += e.delta.y; break; - case NodeGraphCommentSide.Left: - resizingComment.size.x -= e.delta.x; - resizingComment.position.x += e.delta.x; + case NodeGroupSide.Left: + resizingGroup.size.x -= e.delta.x; + resizingGroup.position.x += e.delta.x; break; - case NodeGraphCommentSide.TopLeft: - resizingComment.size.x -= e.delta.x; - resizingComment.position.x += e.delta.x; - resizingComment.size.y -= e.delta.y; - resizingComment.position.y += e.delta.y; + case NodeGroupSide.TopLeft: + resizingGroup.size.x -= e.delta.x; + resizingGroup.position.x += e.delta.x; + resizingGroup.size.y -= e.delta.y; + resizingGroup.position.y += e.delta.y; break; } @@ -246,26 +246,26 @@ namespace XNodeEditor { e.Use(); currentActivity = NodeActivity.HoldNode; } - else if (IsHoveringComment && !IsHoveringNode) { - if (!Selection.Contains(hoveredComment)) { + else if (IsHoveringGroup && !IsHoveringNode) { + if (!Selection.Contains(hoveredGroup)) { if (e.shift) { - SelectComment(hoveredComment, true); - SelectNodesInComment(hoveredComment); + SelectGroup(hoveredGroup, true); + SelectNodesInGroup(hoveredGroup); } - else SelectComment(hoveredComment, e.control || e.shift); - } else deselectingComment = true; + else SelectGroup(hoveredGroup, e.control || e.shift); + } else deselectingGroup = true; e.Use(); - currentActivity = NodeActivity.HoldComment; - } else if (IsResizingComment && !e.control && !e.shift) { + currentActivity = NodeActivity.HoldGroup; + } else if (IsResizingGroup && !e.control && !e.shift) { selectedReroutes.Clear(); Selection.activeObject = null; e.Use(); - currentActivity = NodeActivity.ResizeComment; + currentActivity = NodeActivity.ResizeGroup; } // If mousedown on grid background, deselect all - else if (!IsHoveringNode && !IsHoveringComment) { + else if (!IsHoveringNode && !IsHoveringGroup) { currentActivity = NodeActivity.HoldGrid; if (!e.control && !e.shift) { selectedReroutes.Clear(); @@ -300,7 +300,7 @@ namespace XNodeEditor { IEnumerable nodes = Selection.objects.Where(x => x is XNode.Node).Select(x => x as XNode.Node); foreach (XNode.Node node in nodes) EditorUtility.SetDirty(node); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); - } else if (!IsHoveringNode && !IsHoveringComment && !IsResizingComment) { + } else if (!IsHoveringNode && !IsHoveringGroup && !IsResizingGroup) { // If click outside node, release field focus if (!isPanning) { EditorGUI.FocusTextInControl(null); @@ -314,13 +314,13 @@ namespace XNodeEditor { SelectNode(hoveredNode, false); } - if (currentActivity == NodeActivity.HoldComment && deselectingComment) { - DeselectComment(hoveredComment); + if (currentActivity == NodeActivity.HoldGroup && deselectingGroup) { + DeselectGroup(hoveredGroup); if (e.shift) { - DeselectNodesInComment(hoveredComment); + DeselectNodesInGroup(hoveredGroup); } - deselectingComment = false; + deselectingGroup = false; } // If click reroute, select it. @@ -347,10 +347,10 @@ namespace XNodeEditor { GenericMenu menu = new GenericMenu(); NodeEditor.GetEditor(hoveredNode).AddContextMenuItems(menu); menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero)); - } else if (IsHoveringComment && !IsHoveringNode) { - if (!Selection.Contains(hoveredComment)) SelectComment(hoveredComment, false); + } else if (IsHoveringGroup && !IsHoveringNode) { + if (!Selection.Contains(hoveredGroup)) SelectGroup(hoveredGroup, false); GenericMenu menu = new GenericMenu(); - graphEditor.AddCommentContextMenuItems(menu); + graphEditor.AddGroupContextMenuItems(menu); menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero)); } else if (!IsHoveringNode) { GenericMenu menu = new GenericMenu(); @@ -402,9 +402,9 @@ namespace XNodeEditor { XNode.Node node = Selection.objects[i] as XNode.Node; dragOffset[i] = node.position - WindowToGridPosition(current.mousePosition); } - else if (Selection.objects[i] is XNode.NodeGraphComment) { - XNode.NodeGraphComment comment = Selection.objects[i] as XNode.NodeGraphComment; - dragOffset[i] = comment.position - WindowToGridPosition(current.mousePosition); + else if (Selection.objects[i] is XNode.NodeGroup) { + XNode.NodeGroup group = Selection.objects[i] as XNode.NodeGroup; + dragOffset[i] = group.position - WindowToGridPosition(current.mousePosition); } } @@ -433,9 +433,9 @@ namespace XNodeEditor { XNode.Node node = item as XNode.Node; graphEditor.RemoveNode(node); } - else if (item is XNode.NodeGraphComment) { - XNode.NodeGraphComment comment = item as XNode.NodeGraphComment; - graphEditor.RemoveComment(comment); + else if (item is XNode.NodeGroup) { + XNode.NodeGroup group = item as XNode.NodeGroup; + graphEditor.RemoveGroup(group); } } } @@ -448,10 +448,10 @@ namespace XNodeEditor { } } - /// Initiate a rename on the currently selected comment - public void RenameSelectedComment() { - if (Selection.objects.Length == 1 && Selection.activeObject is XNode.NodeGraphComment) { - renamingComment = Selection.activeObject as XNode.NodeGraphComment; + /// Initiate a rename on the currently selected group + public void RenameSelectedGroup() { + if (Selection.objects.Length == 1 && Selection.activeObject is XNode.NodeGroup) { + renamingGroup = Selection.activeObject as XNode.NodeGroup; } } @@ -476,12 +476,12 @@ namespace XNodeEditor { substitutes.Add(srcNode, newNode); newNode.position = srcNode.position + new Vector2(30, 30); newNodes[i] = newNode; - } else if (Selection.objects[i] is XNode.NodeGraphComment) { - XNode.NodeGraphComment srcComment = Selection.objects[i] as XNode.NodeGraphComment; - if (srcComment.graph != graph) continue; // ignore comments selected in another graph - XNode.NodeGraphComment newComment = graphEditor.CopyComment(srcComment); - newComment.position = srcComment.position + new Vector2(30, 30); - newNodes[i] = newComment; + } else if (Selection.objects[i] is XNode.NodeGroup) { + XNode.NodeGroup srcGroup = Selection.objects[i] as XNode.NodeGroup; + if (srcGroup.graph != graph) continue; // ignore groups selected in another graph + XNode.NodeGroup newGroup = graphEditor.CopyGroup(srcGroup); + newGroup.position = srcGroup.position + new Vector2(30, 30); + newNodes[i] = newGroup; } } diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 3afba94..95c5192 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -13,7 +13,7 @@ namespace XNodeEditor { private int topPadding { get { return isDocked() ? 19 : 22; } } /// Executed after all other window GUI. Useful if Zoom is ruining your day. Automatically resets after being run. public event Action onLateGUI; - public XNode.NodeGraphComment renamingComment; + public XNode.NodeGroup renamingGroup; public bool renamingStarted = false; private Matrix4x4 _prevGuiMatrix; @@ -31,7 +31,7 @@ namespace XNodeEditor { } DrawGrid(position, zoom, panOffset); - DrawComments(); + DrawGroups(); DrawConnections(); DrawDraggedConnection(); DrawNodes(); @@ -441,7 +441,7 @@ namespace XNodeEditor { } } - private void DrawComments() + private void DrawGroups() { Event e = Event.current; @@ -449,8 +449,8 @@ namespace XNodeEditor { Vector2 mousePos = Event.current.mousePosition; if (e.type != EventType.Layout) { - hoveredComment = null; - if (currentActivity != NodeActivity.ResizeComment) resizingComment = null; + hoveredGroup = null; + if (currentActivity != NodeActivity.ResizeGroup) resizingGroup = null; } List preSelection = preBoxSelection != null ? new List(preBoxSelection) : new List(); @@ -461,17 +461,17 @@ namespace XNodeEditor { if (boxSize.y < 0) { boxStartPos.y += boxSize.y; boxSize.y = Mathf.Abs(boxSize.y); } Rect selectionBox = new Rect(boxStartPos, boxSize); - for (int n = 0; n < graph.comments.Count; n++) { - XNode.NodeGraphComment comment = graph.comments[n]; - if (comment == null) continue; + for (int n = 0; n < graph.groups.Count; n++) { + XNode.NodeGroup group = graph.groups[n]; + if (group == null) continue; - Vector2 commentPos = GridToWindowPositionNoClipped(comment.position); - GUILayout.BeginArea(new Rect(commentPos, new Vector2(comment.size.x, comment.size.y))); + Vector2 groupPos = GridToWindowPositionNoClipped(group.position); + GUILayout.BeginArea(new Rect(groupPos, new Vector2(group.size.x, group.size.y))); - bool selected = selectionCache.Contains(comment); + bool selected = selectionCache.Contains(group); if (selected) { - GUIStyle style = new GUIStyle(NodeEditorResources.styles.commentBody); + GUIStyle style = new GUIStyle(NodeEditorResources.styles.groupBody); GUIStyle highlightStyle = new GUIStyle(NodeEditorResources.styles.nodeHighlight); highlightStyle.padding = style.padding; style.padding = new RectOffset(); @@ -480,41 +480,41 @@ namespace XNodeEditor { GUI.color = NodeEditorPreferences.GetSettings().highlightColor; GUILayout.BeginVertical(new GUIStyle(highlightStyle)); } else { - GUIStyle style = new GUIStyle(NodeEditorResources.styles.commentBody); + GUIStyle style = new GUIStyle(NodeEditorResources.styles.groupBody); GUI.color = Color.white; GUILayout.BeginVertical(style); } - if (renamingComment == comment) { - if (Selection.Contains(renamingComment)) { + if (renamingGroup == group) { + if (Selection.Contains(renamingGroup)) { int controlID = EditorGUIUtility.GetControlID(FocusType.Keyboard) + 1; if (!renamingStarted) { EditorGUIUtility.keyboardControl = controlID; EditorGUIUtility.editingTextField = true; renamingStarted = true; } - comment.comment = EditorGUILayout.TextField(comment.comment, NodeEditorResources.styles.commentHeader, GUILayout.Height(26)); + group.name = EditorGUILayout.TextField(group.name, NodeEditorResources.styles.groupHeader, GUILayout.Height(26)); if (!EditorGUIUtility.editingTextField) { Debug.Log("Finish renaming"); - renamingComment = null; + renamingGroup = null; renamingStarted = false; } } else { // Selection changed, so stop renaming. - GUILayout.Label(comment.comment, NodeEditorResources.styles.commentHeader, GUILayout.Height(26)); - renamingComment = null; + GUILayout.Label(group.name, NodeEditorResources.styles.groupHeader, GUILayout.Height(26)); + renamingGroup = null; renamingStarted = false; } } else { - GUIStyle blackStyle = new GUIStyle(NodeEditorResources.styles.commentHeader); + GUIStyle blackStyle = new GUIStyle(NodeEditorResources.styles.groupHeader); blackStyle.normal.textColor = new Color(0.2f, 0.2f, 0.2f); - GUILayout.Label(comment.comment, blackStyle, GUILayout.Height(26)); + GUILayout.Label(group.name, blackStyle, GUILayout.Height(26)); Rect lastRect = GUILayoutUtility.GetLastRect(); lastRect.x -= 0.5f; lastRect.y -= 1; - GUI.Label(lastRect, comment.comment, NodeEditorResources.styles.commentHeader); + GUI.Label(lastRect, group.name, NodeEditorResources.styles.groupHeader); } GUILayout.FlexibleSpace(); @@ -523,23 +523,23 @@ namespace XNodeEditor { if (selected) GUILayout.EndVertical(); - if (e.type != EventType.Layout && currentActivity != NodeActivity.ResizeComment) { + if (e.type != EventType.Layout && currentActivity != NodeActivity.ResizeGroup) { //Check if we are hovering this node - Vector2 commentSize = GUILayoutUtility.GetLastRect().size; - Rect windowRect = new Rect(commentPos, commentSize); + Vector2 groupSize = GUILayoutUtility.GetLastRect().size; + Rect windowRect = new Rect(groupPos, groupSize); float padding = 12; // Resizing areas - // Follows the NodeGraphCommentSide order + // Follows the NodeGroupSide order Rect[] resizeRects = new[] { - new Rect(padding, 0, commentSize.x - padding * 2, padding), - new Rect(commentSize.x - padding, 0, padding, padding), - new Rect(commentSize.x - padding, padding, padding, commentSize.y - padding * 2), - new Rect(commentSize.x - padding, commentSize.y - padding, padding, padding), - new Rect(padding, commentSize.y - padding, commentSize.x - padding * 2, padding), - new Rect(0, commentSize.y - padding, padding, padding), - new Rect(0, padding, padding, commentSize.y - padding * 2), + new Rect(padding, 0, groupSize.x - padding * 2, padding), + new Rect(groupSize.x - padding, 0, padding, padding), + new Rect(groupSize.x - padding, padding, padding, groupSize.y - padding * 2), + new Rect(groupSize.x - padding, groupSize.y - padding, padding, padding), + new Rect(padding, groupSize.y - padding, groupSize.x - padding * 2, padding), + new Rect(0, groupSize.y - padding, padding, padding), + new Rect(0, padding, padding, groupSize.y - padding * 2), new Rect(0, 0, padding, padding), }; @@ -559,35 +559,35 @@ namespace XNodeEditor { EditorGUIUtility.AddCursorRect(resizeRects[i], resizeIcons[i]); // Transform the locations now to gui space locations - resizeRects[i].position += commentPos; + resizeRects[i].position += groupPos; } if (windowRect.Contains(mousePos)) { //If dragging a selection box, add nodes inside to selection if (currentActivity == NodeActivity.DragGrid) { - if (windowRect.Overlaps(selectionBox)) preSelection.Add(comment); + if (windowRect.Overlaps(selectionBox)) preSelection.Add(group); } else { // Check if we should resize or select bool resizeAreaClicked = false; for (int i = 0; i < resizeRects.Length; i++) { if (resizeRects[i].Contains(mousePos)) { - resizingComment = comment; - // i can be cast to NodeGraphCommentSide as resizeRects - // has one element per NodeGraphCommentSide value and + resizingGroup = group; + // i can be cast to NodeGroupSide as resizeRects + // has one element per NodeGroupSide value and // uses the same order - resizingCommentSide = (NodeGraphCommentSide)i; + resizingGroupSide = (NodeGroupSide)i; resizeAreaClicked = true; break; } } - if (!resizeAreaClicked) hoveredComment = comment; + if (!resizeAreaClicked) hoveredGroup = group; } } //If dragging a selection box, add nodes inside to selection if (currentActivity == NodeActivity.DragGrid) { - if (windowRect.Overlaps(selectionBox)) preSelection.Add(comment); + if (windowRect.Overlaps(selectionBox)) preSelection.Add(group); } } diff --git a/Scripts/Editor/NodeEditorResources.cs b/Scripts/Editor/NodeEditorResources.cs index 18e803b..88bdf7a 100644 --- a/Scripts/Editor/NodeEditorResources.cs +++ b/Scripts/Editor/NodeEditorResources.cs @@ -12,15 +12,15 @@ namespace XNodeEditor { private static Texture2D _nodeBody; public static Texture2D nodeHighlight { get { return _nodeHighlight != null ? _nodeHighlight : _nodeHighlight = Resources.Load("xnode_node_highlight"); } } private static Texture2D _nodeHighlight; - public static Texture2D commentBody { get { return _commentBody != null ? _commentBody : _commentBody = Resources.Load("xnode_comment"); } } - private static Texture2D _commentBody; + public static Texture2D groupBody { get { return _groupBody != null ? _groupBody : _groupBody = Resources.Load("xnode_group"); } } + private static Texture2D _groupBody; // Styles public static Styles styles { get { return _styles != null ? _styles : _styles = new Styles(); } } public static Styles _styles = null; public static GUIStyle OutputPort { get { return new GUIStyle(EditorStyles.label) { alignment = TextAnchor.UpperRight }; } } public class Styles { - public GUIStyle inputPort, nodeHeader, nodeBody, tooltip, nodeHighlight, commentHeader, commentBody; + public GUIStyle inputPort, nodeHeader, nodeBody, tooltip, nodeHighlight, groupHeader, groupBody; public Styles() { GUIStyle baseStyle = new GUIStyle("Label"); @@ -47,16 +47,16 @@ namespace XNodeEditor { tooltip = new GUIStyle("helpBox"); tooltip.alignment = TextAnchor.MiddleCenter; - commentHeader = new GUIStyle(); - commentHeader.alignment = TextAnchor.MiddleCenter; - commentHeader.fontStyle = FontStyle.Bold; - commentHeader.fontSize = 20; - commentHeader.normal.textColor = new Color(0.93f, 0.93f, 0.93f); + groupHeader = new GUIStyle(); + groupHeader.alignment = TextAnchor.MiddleCenter; + groupHeader.fontStyle = FontStyle.Bold; + groupHeader.fontSize = 20; + groupHeader.normal.textColor = new Color(0.93f, 0.93f, 0.93f); - commentBody = new GUIStyle(); - commentBody.normal.background = NodeEditorResources.commentBody; - commentBody.border = new RectOffset(32, 32, 32, 32); - commentBody.padding = new RectOffset(16, 16, 4, 16); + groupBody = new GUIStyle(); + groupBody.normal.background = NodeEditorResources.groupBody; + groupBody.border = new RectOffset(32, 32, 32, 32); + groupBody.padding = new RectOffset(16, 16, 4, 16); } } diff --git a/Scripts/Editor/NodeEditorWindow.cs b/Scripts/Editor/NodeEditorWindow.cs index b945eef..a223f4d 100644 --- a/Scripts/Editor/NodeEditorWindow.cs +++ b/Scripts/Editor/NodeEditorWindow.cs @@ -142,38 +142,38 @@ namespace XNodeEditor { Selection.objects = selection.ToArray(); } - public void SelectComment(XNode.NodeGraphComment comment, bool add) { + public void SelectGroup(XNode.NodeGroup group, bool add) { if (add) { List selection = new List(Selection.objects); - selection.Add(comment); + selection.Add(group); Selection.objects = selection.ToArray(); - } else Selection.objects = new Object[] { comment }; + } else Selection.objects = new Object[] { group }; } - public void DeselectComment(XNode.NodeGraphComment comment) { + public void DeselectGroup(XNode.NodeGroup group) { List selection = new List(Selection.objects); - selection.Remove(comment); + selection.Remove(group); Selection.objects = selection.ToArray(); } - public void SelectNodesInComment(XNode.NodeGraphComment comment) { - Rect commentRect = new Rect(comment.position, comment.size); + public void SelectNodesInGroup(XNode.NodeGroup group) { + Rect groupRect = new Rect(group.position, group.size); for (int i = 0; i < graph.nodes.Count; i++) { XNode.Node node = graph.nodes[i]; if (!node) continue; - if (commentRect.Contains(node.position)) SelectNode(node, true); + if (groupRect.Contains(node.position)) SelectNode(node, true); } } - public void DeselectNodesInComment(XNode.NodeGraphComment comment) + public void DeselectNodesInGroup(XNode.NodeGroup group) { List selection = new List(Selection.objects); - Rect commentRect = new Rect(comment.position, comment.size); + Rect groupRect = new Rect(group.position, group.size); for (int i = 0; i < graph.nodes.Count; i++) { XNode.Node node = graph.nodes[i]; if (!node) continue; - if (commentRect.Contains(node.position)) selection.Remove(node); + if (groupRect.Contains(node.position)) selection.Remove(node); } Selection.objects = selection.ToArray(); diff --git a/Scripts/Editor/NodeGraphEditor.cs b/Scripts/Editor/NodeGraphEditor.cs index db76c51..a44c4db 100644 --- a/Scripts/Editor/NodeGraphEditor.cs +++ b/Scripts/Editor/NodeGraphEditor.cs @@ -53,14 +53,14 @@ namespace XNodeEditor { }); } menu.AddSeparator(""); - menu.AddItem(new GUIContent("Add comment"), false, () => CreateComment(pos)); + menu.AddItem(new GUIContent("Add group"), false, () => CreateGroup(pos)); menu.AddSeparator(""); menu.AddItem(new GUIContent("Preferences"), false, () => NodeEditorWindow.OpenPreferences()); NodeEditorWindow.AddCustomContextMenuItems(menu, target); } /// Add items for the context menu when right-clicking this node. Override to add custom menu items. - public virtual void AddCommentContextMenuItems(GenericMenu menu) { + public virtual void AddGroupContextMenuItems(GenericMenu menu) { Vector2 pos = NodeEditorWindow.current.WindowToGridPosition(Event.current.mousePosition); for (int i = 0; i < NodeEditorWindow.nodeTypes.Length; i++) { Type type = NodeEditorWindow.nodeTypes[i]; @@ -75,12 +75,12 @@ namespace XNodeEditor { } menu.AddSeparator(""); - menu.AddItem(new GUIContent("Add comment"), false, () => CreateComment(pos)); + menu.AddItem(new GUIContent("Add group"), false, () => CreateGroup(pos)); menu.AddSeparator(""); // Actions if only one node is selected - if (Selection.objects.Length == 1 && Selection.activeObject is XNode.NodeGraphComment) { - XNode.NodeGraphComment comment = Selection.activeObject as XNode.NodeGraphComment; - menu.AddItem(new GUIContent("Rename"), false, NodeEditorWindow.current.RenameSelectedComment); + if (Selection.objects.Length == 1 && Selection.activeObject is XNode.NodeGroup) { + XNode.NodeGroup group = Selection.activeObject as XNode.NodeGroup; + menu.AddItem(new GUIContent("Rename"), false, NodeEditorWindow.current.RenameSelectedGroup); } // Add actions to any number of selected nodes @@ -118,29 +118,29 @@ namespace XNodeEditor { if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); } - /// Create a comment and save it in the graph asset - public void CreateComment(Vector2 position) { - XNode.NodeGraphComment comment = target.AddComment(); - comment.position = position; - comment.comment = "New comment"; - AssetDatabase.AddObjectToAsset(comment, target); + /// Create a group and save it in the graph asset + public void CreateGroup(Vector2 position) { + XNode.NodeGroup group = target.AddGroup(); + group.position = position; + group.name = "New group"; + AssetDatabase.AddObjectToAsset(group, target); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); NodeEditorWindow.RepaintAll(); } - /// Creates a copy of the original comment in the graph - public XNode.NodeGraphComment CopyComment(XNode.NodeGraphComment original) { - XNode.NodeGraphComment comment = target.CopyComment(original); - comment.name = original.name; - AssetDatabase.AddObjectToAsset(comment, target); + /// Creates a copy of the original group in the graph + public XNode.NodeGroup CopyGroup(XNode.NodeGroup original) { + XNode.NodeGroup group = target.CopyGroup(original); + group.name = original.name; + AssetDatabase.AddObjectToAsset(group, target); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); - return comment; + return group; } - /// Safely remove a comment - public void RemoveComment(XNode.NodeGraphComment comment) { - UnityEngine.Object.DestroyImmediate(comment, true); - target.RemoveComment(comment); + /// Safely remove a group + public void RemoveGroup(XNode.NodeGroup group) { + UnityEngine.Object.DestroyImmediate(group, true); + target.RemoveGroup(group); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); } diff --git a/Scripts/Editor/Resources/xnode_comment.png b/Scripts/Editor/Resources/xnode_group.png similarity index 100% rename from Scripts/Editor/Resources/xnode_comment.png rename to Scripts/Editor/Resources/xnode_group.png diff --git a/Scripts/Editor/Resources/xnode_comment.png.meta b/Scripts/Editor/Resources/xnode_group.png.meta similarity index 100% rename from Scripts/Editor/Resources/xnode_comment.png.meta rename to Scripts/Editor/Resources/xnode_group.png.meta diff --git a/Scripts/Editor/Resources/xnode_node_comment_workfile.psd b/Scripts/Editor/Resources/xnode_group_workfile.psd similarity index 100% rename from Scripts/Editor/Resources/xnode_node_comment_workfile.psd rename to Scripts/Editor/Resources/xnode_group_workfile.psd diff --git a/Scripts/Editor/Resources/xnode_node_comment_workfile.psd.meta b/Scripts/Editor/Resources/xnode_group_workfile.psd.meta similarity index 100% rename from Scripts/Editor/Resources/xnode_node_comment_workfile.psd.meta rename to Scripts/Editor/Resources/xnode_group_workfile.psd.meta diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs index 8d40853..e027aab 100644 --- a/Scripts/NodeGraph.cs +++ b/Scripts/NodeGraph.cs @@ -10,9 +10,9 @@ namespace XNode { /// All nodes in the graph. /// See: [SerializeField] public List nodes = new List(); - /// All comments in the graph. - /// See: - [SerializeField] public List comments = new List(); + /// All groups in the graph. + /// See: + [SerializeField] public List groups = new List(); /// Add a node to the graph by type public T AddNode() where T : Node { @@ -56,26 +56,26 @@ namespace XNode { nodes.Clear(); } - /// Add a comment to the graph - public NodeGraphComment AddComment() { - NodeGraphComment comment = ScriptableObject.CreateInstance(); - comment.graph = this; - comments.Add(comment); - return comment; + /// Add a group to the graph + public NodeGroup AddGroup() { + NodeGroup group = ScriptableObject.CreateInstance(); + group.graph = this; + groups.Add(group); + return group; } - /// Creates a copy of the comment node in the graph - public virtual NodeGraphComment CopyComment(NodeGraphComment original) { - NodeGraphComment comment = ScriptableObject.Instantiate(original); - comment.graph = this; - comments.Add(comment); - return comment; + /// Creates a copy of the group node in the graph + public virtual NodeGroup CopyGroup(NodeGroup original) { + NodeGroup group = ScriptableObject.Instantiate(original); + group.graph = this; + groups.Add(group); + return group; } - /// Safely remove a comment - public void RemoveComment(NodeGraphComment comment) { - comments.Remove(comment); - if (Application.isPlaying) Destroy(comment); + /// Safely remove a group + public void RemoveGroup(NodeGroup group) { + groups.Remove(group); + if (Application.isPlaying) Destroy(group); } /// Create a new deep copy of this graph @@ -91,6 +91,14 @@ namespace XNode { graph.nodes[i] = node; } + // Instantiate all groups inside the graph + for (int i = 0; i < groups.Count; i++) { + if (groups[i] == null) continue; + NodeGroup group = Instantiate(groups[i]) as NodeGroup; + group.graph = graph; + graph.groups[i] = group; + } + // Redirect all connections for (int i = 0; i < graph.nodes.Count; i++) { if (graph.nodes[i] == null) continue; diff --git a/Scripts/NodeGraphComment.cs b/Scripts/NodeGroup.cs similarity index 71% rename from Scripts/NodeGraphComment.cs rename to Scripts/NodeGroup.cs index 47ae11b..1dacbff 100644 --- a/Scripts/NodeGraphComment.cs +++ b/Scripts/NodeGroup.cs @@ -4,11 +4,10 @@ using UnityEngine; namespace XNode { [Serializable] - public class NodeGraphComment : ScriptableObject + public class NodeGroup : ScriptableObject { [SerializeField] public NodeGraph graph; [SerializeField] public Vector2 position; [SerializeField] public Vector2 size = new Vector2(200, 300); - [SerializeField] public string comment; } } \ No newline at end of file diff --git a/Scripts/NodeGraphComment.cs.meta b/Scripts/NodeGroup.cs.meta similarity index 100% rename from Scripts/NodeGraphComment.cs.meta rename to Scripts/NodeGroup.cs.meta