1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-02-04 22:34:54 +08:00

Refactored 'comment' to 'group'

This commit is contained in:
Jeroen van de Haterd 2019-01-23 20:40:49 +01:00
parent 0020bf87e3
commit ab87555c63
12 changed files with 191 additions and 184 deletions

View File

@ -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<Vector2> draggedOutputReroutes = new List<Vector2>();
[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<RerouteReference> selectedReroutes = new List<RerouteReference>();
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<XNode.Node> 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 {
}
}
/// <summary> Initiate a rename on the currently selected comment </summary>
public void RenameSelectedComment() {
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.NodeGraphComment) {
renamingComment = Selection.activeObject as XNode.NodeGraphComment;
/// <summary> Initiate a rename on the currently selected group </summary>
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;
}
}

View File

@ -13,7 +13,7 @@ namespace XNodeEditor {
private int topPadding { get { return isDocked() ? 19 : 22; } }
/// <summary> Executed after all other window GUI. Useful if Zoom is ruining your day. Automatically resets after being run.</summary>
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<UnityEngine.Object> preSelection = preBoxSelection != null ? new List<UnityEngine.Object>(preBoxSelection) : new List<UnityEngine.Object>();
@ -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);
}
}

View File

@ -12,15 +12,15 @@ namespace XNodeEditor {
private static Texture2D _nodeBody;
public static Texture2D nodeHighlight { get { return _nodeHighlight != null ? _nodeHighlight : _nodeHighlight = Resources.Load<Texture2D>("xnode_node_highlight"); } }
private static Texture2D _nodeHighlight;
public static Texture2D commentBody { get { return _commentBody != null ? _commentBody : _commentBody = Resources.Load<Texture2D>("xnode_comment"); } }
private static Texture2D _commentBody;
public static Texture2D groupBody { get { return _groupBody != null ? _groupBody : _groupBody = Resources.Load<Texture2D>("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);
}
}

View File

@ -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<Object> selection = new List<Object>(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<Object> selection = new List<Object>(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<Object> selection = new List<Object>(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();

View File

@ -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);
}
/// <summary> Add items for the context menu when right-clicking this node. Override to add custom menu items. </summary>
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();
}
/// <summary> Create a comment and save it in the graph asset </summary>
public void CreateComment(Vector2 position) {
XNode.NodeGraphComment comment = target.AddComment();
comment.position = position;
comment.comment = "New comment";
AssetDatabase.AddObjectToAsset(comment, target);
/// <summary> Create a group and save it in the graph asset </summary>
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();
}
/// <summary> Creates a copy of the original comment in the graph </summary>
public XNode.NodeGraphComment CopyComment(XNode.NodeGraphComment original) {
XNode.NodeGraphComment comment = target.CopyComment(original);
comment.name = original.name;
AssetDatabase.AddObjectToAsset(comment, target);
/// <summary> Creates a copy of the original group in the graph </summary>
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;
}
/// <summary> Safely remove a comment </summary>
public void RemoveComment(XNode.NodeGraphComment comment) {
UnityEngine.Object.DestroyImmediate(comment, true);
target.RemoveComment(comment);
/// <summary> Safely remove a group </summary>
public void RemoveGroup(XNode.NodeGroup group) {
UnityEngine.Object.DestroyImmediate(group, true);
target.RemoveGroup(group);
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
}

View File

Before

Width:  |  Height:  |  Size: 999 B

After

Width:  |  Height:  |  Size: 999 B

View File

@ -10,9 +10,9 @@ namespace XNode {
/// <summary> All nodes in the graph. <para/>
/// See: <see cref="AddNode{T}"/> </summary>
[SerializeField] public List<Node> nodes = new List<Node>();
/// <summary> All comments in the graph. <para/>
/// See: <see cref="AddComment"/> </summary>
[SerializeField] public List<NodeGraphComment> comments = new List<NodeGraphComment>();
/// <summary> All groups in the graph. <para/>
/// See: <see cref="AddGroup"/> </summary>
[SerializeField] public List<NodeGroup> groups = new List<NodeGroup>();
/// <summary> Add a node to the graph by type </summary>
public T AddNode<T>() where T : Node {
@ -56,26 +56,26 @@ namespace XNode {
nodes.Clear();
}
/// <summary> Add a comment to the graph</summary>
public NodeGraphComment AddComment() {
NodeGraphComment comment = ScriptableObject.CreateInstance<NodeGraphComment>();
comment.graph = this;
comments.Add(comment);
return comment;
/// <summary> Add a group to the graph</summary>
public NodeGroup AddGroup() {
NodeGroup group = ScriptableObject.CreateInstance<NodeGroup>();
group.graph = this;
groups.Add(group);
return group;
}
/// <summary> Creates a copy of the comment node in the graph </summary>
public virtual NodeGraphComment CopyComment(NodeGraphComment original) {
NodeGraphComment comment = ScriptableObject.Instantiate(original);
comment.graph = this;
comments.Add(comment);
return comment;
/// <summary> Creates a copy of the group node in the graph </summary>
public virtual NodeGroup CopyGroup(NodeGroup original) {
NodeGroup group = ScriptableObject.Instantiate(original);
group.graph = this;
groups.Add(group);
return group;
}
/// <summary> Safely remove a comment </summary>
public void RemoveComment(NodeGraphComment comment) {
comments.Remove(comment);
if (Application.isPlaying) Destroy(comment);
/// <summary> Safely remove a group </summary>
public void RemoveGroup(NodeGroup group) {
groups.Remove(group);
if (Application.isPlaying) Destroy(group);
}
/// <summary> Create a new deep copy of this graph </summary>
@ -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;

View File

@ -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;
}
}