mirror of
https://github.com/Siccity/xNode.git
synced 2026-02-06 23:34:54 +08:00
Added node box selection
This commit is contained in:
parent
f87af8d62f
commit
d871b7bec4
@ -4,23 +4,21 @@ using UnityEngine;
|
|||||||
|
|
||||||
namespace XNodeEditor {
|
namespace XNodeEditor {
|
||||||
public partial class NodeEditorWindow {
|
public partial class NodeEditorWindow {
|
||||||
|
public enum NodeActivity { Idle, HoldHeader, DragHeader, HoldGrid, DragGrid }
|
||||||
|
public static NodeActivity currentActivity = NodeActivity.Idle;
|
||||||
public static bool isPanning { get; private set; }
|
public static bool isPanning { get; private set; }
|
||||||
public static Vector2[] dragOffset;
|
public static Vector2[] dragOffset;
|
||||||
|
|
||||||
//private bool IsDraggingNode { get { return draggedNode != null; } }
|
|
||||||
private bool IsDraggingPort { get { return draggedOutput != null; } }
|
private bool IsDraggingPort { get { return draggedOutput != null; } }
|
||||||
private bool IsHoveringPort { get { return hoveredPort != null; } }
|
private bool IsHoveringPort { get { return hoveredPort != null; } }
|
||||||
private bool IsHoveringNode { get { return hoveredNode != null; } }
|
private bool IsHoveringNode { get { return hoveredNode != null; } }
|
||||||
public bool CanDragNodeHeader { get; private set; }
|
|
||||||
public bool DidDragNodeHeader { get; private set; }
|
|
||||||
private XNode.Node hoveredNode = null;
|
private XNode.Node hoveredNode = null;
|
||||||
|
|
||||||
//[NonSerialized] private XNode.Node draggedNode = null;
|
|
||||||
[NonSerialized] private XNode.NodePort hoveredPort = null;
|
[NonSerialized] private XNode.NodePort hoveredPort = null;
|
||||||
[NonSerialized] private XNode.NodePort draggedOutput = null;
|
[NonSerialized] private XNode.NodePort draggedOutput = null;
|
||||||
[NonSerialized] private XNode.NodePort draggedOutputTarget = null;
|
[NonSerialized] private XNode.NodePort draggedOutputTarget = null;
|
||||||
private Rect nodeRects;
|
private Rect nodeRects;
|
||||||
|
private Vector2 dragBoxStart;
|
||||||
|
private UnityEngine.Object[] preBoxSelection;
|
||||||
|
|
||||||
public void Controls() {
|
public void Controls() {
|
||||||
wantsMouseMove = true;
|
wantsMouseMove = true;
|
||||||
@ -43,7 +41,7 @@ namespace XNodeEditor {
|
|||||||
draggedOutputTarget = null;
|
draggedOutputTarget = null;
|
||||||
}
|
}
|
||||||
Repaint();
|
Repaint();
|
||||||
} else if (CanDragNodeHeader) {
|
} else if (currentActivity == NodeActivity.HoldHeader || currentActivity == NodeActivity.DragHeader) {
|
||||||
for (int i = 0; i < Selection.objects.Length; i++) {
|
for (int i = 0; i < Selection.objects.Length; i++) {
|
||||||
if (Selection.objects[i] is XNode.Node) {
|
if (Selection.objects[i] is XNode.Node) {
|
||||||
XNode.Node node = Selection.objects[i] as XNode.Node;
|
XNode.Node node = Selection.objects[i] as XNode.Node;
|
||||||
@ -54,7 +52,17 @@ namespace XNodeEditor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DidDragNodeHeader = true;
|
currentActivity = NodeActivity.DragHeader;
|
||||||
|
Repaint();
|
||||||
|
} else if (currentActivity == NodeActivity.HoldGrid) {
|
||||||
|
currentActivity = NodeActivity.DragGrid;
|
||||||
|
preBoxSelection = Selection.objects;
|
||||||
|
dragBoxStart = WindowToGridPosition(e.mousePosition);
|
||||||
|
Repaint();
|
||||||
|
} else if (currentActivity == NodeActivity.DragGrid) {
|
||||||
|
foreach (XNode.Node node in graph.nodes) {
|
||||||
|
|
||||||
|
}
|
||||||
Repaint();
|
Repaint();
|
||||||
}
|
}
|
||||||
} else if (e.button == 1 || e.button == 2) {
|
} else if (e.button == 1 || e.button == 2) {
|
||||||
@ -90,8 +98,7 @@ namespace XNodeEditor {
|
|||||||
if (!Selection.Contains(hoveredNode)) SelectNode(hoveredNode, e.control || e.shift);
|
if (!Selection.Contains(hoveredNode)) SelectNode(hoveredNode, e.control || e.shift);
|
||||||
else if (e.control || e.shift) DeselectNode(hoveredNode);
|
else if (e.control || e.shift) DeselectNode(hoveredNode);
|
||||||
e.Use();
|
e.Use();
|
||||||
DidDragNodeHeader = false;
|
currentActivity = NodeActivity.HoldHeader;
|
||||||
CanDragNodeHeader = true;
|
|
||||||
dragOffset = new Vector2[Selection.objects.Length];
|
dragOffset = new Vector2[Selection.objects.Length];
|
||||||
for (int i = 0; i < dragOffset.Length; i++) {
|
for (int i = 0; i < dragOffset.Length; i++) {
|
||||||
if (Selection.objects[i] is XNode.Node) {
|
if (Selection.objects[i] is XNode.Node) {
|
||||||
@ -102,7 +109,8 @@ namespace XNodeEditor {
|
|||||||
}
|
}
|
||||||
// If mousedown on grid background, deselect all
|
// If mousedown on grid background, deselect all
|
||||||
else if (!IsHoveringNode) {
|
else if (!IsHoveringNode) {
|
||||||
Selection.activeObject = null;
|
currentActivity = NodeActivity.HoldGrid;
|
||||||
|
if (!e.control && !e.shift) Selection.activeObject = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -121,10 +129,8 @@ namespace XNodeEditor {
|
|||||||
draggedOutput = null;
|
draggedOutput = null;
|
||||||
draggedOutputTarget = null;
|
draggedOutputTarget = null;
|
||||||
EditorUtility.SetDirty(graph);
|
EditorUtility.SetDirty(graph);
|
||||||
Repaint();
|
|
||||||
AssetDatabase.SaveAssets();
|
AssetDatabase.SaveAssets();
|
||||||
} else if (CanDragNodeHeader) {
|
} else if (currentActivity == NodeActivity.DragHeader) {
|
||||||
CanDragNodeHeader = false;
|
|
||||||
AssetDatabase.SaveAssets();
|
AssetDatabase.SaveAssets();
|
||||||
} else if (!IsHoveringNode) {
|
} else if (!IsHoveringNode) {
|
||||||
// If click outside node, release field focus
|
// If click outside node, release field focus
|
||||||
@ -132,19 +138,20 @@ namespace XNodeEditor {
|
|||||||
GUIUtility.hotControl = 0;
|
GUIUtility.hotControl = 0;
|
||||||
GUIUtility.keyboardControl = 0;
|
GUIUtility.keyboardControl = 0;
|
||||||
}
|
}
|
||||||
Repaint();
|
|
||||||
AssetDatabase.SaveAssets();
|
AssetDatabase.SaveAssets();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If click node header, select single node.
|
// If click node header, select single node.
|
||||||
if (IsHoveringNode && !DidDragNodeHeader && IsHoveringTitle(hoveredNode) && !(e.control || e.shift)) {
|
if (currentActivity == NodeActivity.HoldHeader && !(e.control || e.shift)) {
|
||||||
SelectNode(hoveredNode, false);
|
SelectNode(hoveredNode, false);
|
||||||
Repaint();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Repaint();
|
||||||
|
currentActivity = NodeActivity.Idle;
|
||||||
} else if (e.button == 1) {
|
} else if (e.button == 1) {
|
||||||
if (!isPanning) {
|
if (!isPanning) {
|
||||||
if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
|
if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
|
||||||
if (!Selection.Contains(hoveredNode)) SelectNode(hoveredNode,false);
|
if (!Selection.Contains(hoveredNode)) SelectNode(hoveredNode, false);
|
||||||
ShowNodeContextMenu();
|
ShowNodeContextMenu();
|
||||||
} else if (!IsHoveringNode) {
|
} else if (!IsHoveringNode) {
|
||||||
ShowGraphContextMenu();
|
ShowGraphContextMenu();
|
||||||
@ -158,6 +165,13 @@ namespace XNodeEditor {
|
|||||||
else if (e.keyCode == KeyCode.D && e.control) DublicateSelectedNodes();
|
else if (e.keyCode == KeyCode.D && e.control) DublicateSelectedNodes();
|
||||||
Repaint();
|
Repaint();
|
||||||
break;
|
break;
|
||||||
|
case EventType.Ignore:
|
||||||
|
// If release mouse outside window
|
||||||
|
if (e.rawType == EventType.MouseUp && currentActivity == NodeActivity.DragGrid) {
|
||||||
|
Repaint();
|
||||||
|
currentActivity = NodeActivity.Idle;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -21,6 +21,7 @@ namespace XNodeEditor {
|
|||||||
DrawConnections();
|
DrawConnections();
|
||||||
DrawDraggedConnection();
|
DrawDraggedConnection();
|
||||||
DrawNodes();
|
DrawNodes();
|
||||||
|
DrawBox();
|
||||||
DrawTooltip();
|
DrawTooltip();
|
||||||
|
|
||||||
GUI.matrix = m;
|
GUI.matrix = m;
|
||||||
@ -71,6 +72,17 @@ namespace XNodeEditor {
|
|||||||
GUI.DrawTextureWithTexCoords(rect, crossTex, new Rect(tileOffset + new Vector2(0.5f, 0.5f), tileAmount));
|
GUI.DrawTextureWithTexCoords(rect, crossTex, new Rect(tileOffset + new Vector2(0.5f, 0.5f), tileAmount));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void DrawBox() {
|
||||||
|
if (currentActivity == NodeActivity.DragGrid) {
|
||||||
|
Vector2 curPos = WindowToGridPosition(Event.current.mousePosition);
|
||||||
|
Vector2 size = curPos - dragBoxStart;
|
||||||
|
Rect r = new Rect(dragBoxStart, size);
|
||||||
|
r.position = GridToWindowPosition(r.position);
|
||||||
|
r.size /= zoom;
|
||||||
|
Handles.DrawSolidRectangleWithOutline(r, new Color(0, 0, 0, 0.1f), new Color(1, 1, 1, 0.6f));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static bool DropdownButton(string name, float width) {
|
public static bool DropdownButton(string name, float width) {
|
||||||
return GUILayout.Button(name, EditorStyles.toolbarDropDown, GUILayout.Width(width));
|
return GUILayout.Button(name, EditorStyles.toolbarDropDown, GUILayout.Width(width));
|
||||||
}
|
}
|
||||||
@ -201,6 +213,8 @@ namespace XNodeEditor {
|
|||||||
hoveredPort = null;
|
hoveredPort = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<UnityEngine.Object> preSelection = new List<UnityEngine.Object>(preBoxSelection);
|
||||||
|
|
||||||
//Save guiColor so we can revert it
|
//Save guiColor so we can revert it
|
||||||
Color guiColor = GUI.color;
|
Color guiColor = GUI.color;
|
||||||
for (int n = 0; n < graph.nodes.Count; n++) {
|
for (int n = 0; n < graph.nodes.Count; n++) {
|
||||||
@ -268,6 +282,19 @@ namespace XNodeEditor {
|
|||||||
Rect windowRect = new Rect(nodePos, nodeSize);
|
Rect windowRect = new Rect(nodePos, nodeSize);
|
||||||
if (windowRect.Contains(mousePos)) hoveredNode = node;
|
if (windowRect.Contains(mousePos)) hoveredNode = node;
|
||||||
|
|
||||||
|
//If dragging a selection box, add nodes inside to selection
|
||||||
|
if (currentActivity == NodeActivity.DragGrid) {
|
||||||
|
Vector2 startPos = GridToWindowPosition(dragBoxStart);
|
||||||
|
Vector2 size = (mousePos - startPos) / zoom;
|
||||||
|
Rect r = new Rect(dragBoxStart, size);
|
||||||
|
Vector2 rpos = GridToWindowPosition(r.position);
|
||||||
|
if (size.x < 0) { rpos.x += size.x; size.x = Mathf.Abs(size.x); }
|
||||||
|
if (size.y < 0) { rpos.y += size.y; size.y = Mathf.Abs(size.y); }
|
||||||
|
r.position = rpos;
|
||||||
|
r.size = size;
|
||||||
|
if (windowRect.Overlaps(r)) preSelection.Add(node);
|
||||||
|
}
|
||||||
|
|
||||||
//Check if we are hovering any of this nodes ports
|
//Check if we are hovering any of this nodes ports
|
||||||
//Check input ports
|
//Check input ports
|
||||||
foreach (XNode.NodePort input in node.Inputs) {
|
foreach (XNode.NodePort input in node.Inputs) {
|
||||||
@ -288,6 +315,7 @@ namespace XNodeEditor {
|
|||||||
GUILayout.EndArea();
|
GUILayout.EndArea();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (e.type != EventType.Layout && currentActivity == NodeActivity.DragGrid) Selection.objects = preSelection.ToArray();
|
||||||
EndZoomed(position, zoom);
|
EndZoomed(position, zoom);
|
||||||
|
|
||||||
//If a change in hash is detected in the selected node, call OnValidate method.
|
//If a change in hash is detected in the selected node, call OnValidate method.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user