mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 09:16:01 +08:00
Replaced node Rect rect with node Vector2 position. Improved node hover check.
This commit is contained in:
parent
fb28fd9e24
commit
9db84d1608
Binary file not shown.
@ -32,7 +32,7 @@ public class NodeEditor {
|
||||
FieldInfo[] fields = GetInspectorFields(target);
|
||||
for (int i = 0; i < fields.Length; i++) {
|
||||
object[] fieldAttribs = fields[i].GetCustomAttributes(false);
|
||||
if (fields[i].Name == "graph" || fields[i].Name == "rect") continue;
|
||||
if (fields[i].Name == "graph" || fields[i].Name == "position") continue;
|
||||
NodeEditorGUILayout.PropertyField(target, fields[i], portPositions);
|
||||
}
|
||||
//If user changed a value, notify other scripts through onUpdateNode
|
||||
|
||||
@ -48,7 +48,7 @@ public partial class NodeEditorWindow {
|
||||
}
|
||||
Repaint();
|
||||
} else if (IsDraggingNode) {
|
||||
draggedNode.rect.position = WindowToGridPosition(e.mousePosition) + dragOffset;
|
||||
draggedNode.position = WindowToGridPosition(e.mousePosition) + dragOffset;
|
||||
Repaint();
|
||||
}
|
||||
} else if (e.button == 1) {
|
||||
@ -76,7 +76,7 @@ public partial class NodeEditorWindow {
|
||||
}
|
||||
} else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
|
||||
draggedNode = hoveredNode;
|
||||
dragOffset = hoveredNode.rect.position - WindowToGridPosition(e.mousePosition);
|
||||
dragOffset = hoveredNode.position - WindowToGridPosition(e.mousePosition);
|
||||
}
|
||||
break;
|
||||
case EventType.MouseUp:
|
||||
@ -113,7 +113,7 @@ public partial class NodeEditorWindow {
|
||||
|
||||
public void CreateNode(Type type, Vector2 position) {
|
||||
Node node = graph.AddNode(type);
|
||||
node.rect.position = position;
|
||||
node.position = position;
|
||||
Repaint();
|
||||
}
|
||||
|
||||
@ -132,8 +132,8 @@ public partial class NodeEditorWindow {
|
||||
bool IsHoveringTitle(Node node) {
|
||||
Vector2 mousePos = Event.current.mousePosition;
|
||||
//Get node position
|
||||
Vector2 nodePos = GridToWindowPosition(node.rect.position);
|
||||
Rect windowRect = new Rect(nodePos, new Vector2(node.rect.size.x / zoom, 30 / zoom));
|
||||
Vector2 nodePos = GridToWindowPosition(node.position);
|
||||
Rect windowRect = new Rect(nodePos, new Vector2(/*Node width*/200 / zoom, 30 / zoom));
|
||||
return windowRect.Contains(mousePos);
|
||||
}
|
||||
}
|
||||
@ -153,8 +153,11 @@ public partial class NodeEditorWindow {
|
||||
|
||||
Vector2 mousePos = Event.current.mousePosition;
|
||||
|
||||
hoveredPort = null;
|
||||
hoveredNode = null;
|
||||
|
||||
if (e.type != EventType.Layout) {
|
||||
hoveredNode = null;
|
||||
hoveredPort = null;
|
||||
}
|
||||
|
||||
foreach (Node node in graph.nodes) {
|
||||
if (node == null) return;
|
||||
@ -162,7 +165,7 @@ public partial class NodeEditorWindow {
|
||||
nodeEditor.target = node;
|
||||
|
||||
//Get node position
|
||||
Vector2 nodePos = GridToWindowPositionNoClipped(node.rect.position);
|
||||
Vector2 nodePos = GridToWindowPositionNoClipped(node.position);
|
||||
|
||||
//GUIStyle style = (node == selectedNode) ? (GUIStyle)"flow node 0 on" : (GUIStyle)"flow node 0";
|
||||
|
||||
@ -178,7 +181,7 @@ public partial class NodeEditorWindow {
|
||||
if (e.type == EventType.Repaint) {
|
||||
foreach (var kvp in portHandlePoints) {
|
||||
Vector2 portHandlePos = kvp.Value;
|
||||
portHandlePos += node.rect.position;
|
||||
portHandlePos += node.position;
|
||||
Rect rect = new Rect(portHandlePos.x - 8, portHandlePos.y - 8, 16, 16);
|
||||
portConnectionPoints.Add(kvp.Key, rect);
|
||||
}
|
||||
@ -186,27 +189,29 @@ public partial class NodeEditorWindow {
|
||||
|
||||
GUILayout.EndVertical();
|
||||
|
||||
//Check if we are hovering this node
|
||||
Vector2 nodeSize = GUILayoutUtility.GetLastRect().size / zoom;
|
||||
Rect windowRect = new Rect(nodePos, nodeSize);
|
||||
if (windowRect.Contains(mousePos)) hoveredNode = node;
|
||||
if (e.type != EventType.Layout) {
|
||||
//Check if we are hovering this node
|
||||
Vector2 nodeSize = GUILayoutUtility.GetLastRect().size / zoom;
|
||||
Rect windowRect = new Rect(nodePos, nodeSize);
|
||||
if (windowRect.Contains(mousePos)) hoveredNode = node;
|
||||
|
||||
//Check if we are hovering any of this nodes ports
|
||||
//Check input ports
|
||||
for (int i = 0; i < node.InputCount; i++) {
|
||||
NodePort port = node.inputs[i];
|
||||
//Check if port rect is available
|
||||
if (!portConnectionPoints.ContainsKey(port)) continue;
|
||||
Rect r = GridToWindowRect(portConnectionPoints[port]);
|
||||
if (r.Contains(mousePos)) hoveredPort = port;
|
||||
}
|
||||
//Check all output ports
|
||||
for (int i = 0; i < node.OutputCount; i++) {
|
||||
NodePort port = node.outputs[i];
|
||||
//Check if port rect is available
|
||||
if (!portConnectionPoints.ContainsKey(port)) continue;
|
||||
Rect r = GridToWindowRect(portConnectionPoints[port]);
|
||||
if (r.Contains(mousePos)) hoveredPort = port;
|
||||
//Check if we are hovering any of this nodes ports
|
||||
//Check input ports
|
||||
for (int i = 0; i < node.InputCount; i++) {
|
||||
NodePort port = node.inputs[i];
|
||||
//Check if port rect is available
|
||||
if (!portConnectionPoints.ContainsKey(port)) continue;
|
||||
Rect r = GridToWindowRect(portConnectionPoints[port]);
|
||||
if (r.Contains(mousePos)) hoveredPort = port;
|
||||
}
|
||||
//Check all output ports
|
||||
for (int i = 0; i < node.OutputCount; i++) {
|
||||
NodePort port = node.outputs[i];
|
||||
//Check if port rect is available
|
||||
if (!portConnectionPoints.ContainsKey(port)) continue;
|
||||
Rect r = GridToWindowRect(portConnectionPoints[port]);
|
||||
if (r.Contains(mousePos)) hoveredPort = port;
|
||||
}
|
||||
}
|
||||
|
||||
GUILayout.EndArea();
|
||||
|
||||
@ -10,7 +10,7 @@ public abstract class Node : ScriptableObject {
|
||||
|
||||
/// <summary> Name of the node </summary>
|
||||
[SerializeField] public NodeGraph graph;
|
||||
[SerializeField] public Rect rect = new Rect(0, 0, 200, 200);
|
||||
[SerializeField] public Vector2 position;
|
||||
/// <summary> Input <see cref="NodePort"/>s. It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> </summary>
|
||||
[SerializeField] public List<NodePort> inputs = new List<NodePort>();
|
||||
/// <summary> Output <see cref="NodePort"/>s. It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> </summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user