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);
|
FieldInfo[] fields = GetInspectorFields(target);
|
||||||
for (int i = 0; i < fields.Length; i++) {
|
for (int i = 0; i < fields.Length; i++) {
|
||||||
object[] fieldAttribs = fields[i].GetCustomAttributes(false);
|
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);
|
NodeEditorGUILayout.PropertyField(target, fields[i], portPositions);
|
||||||
}
|
}
|
||||||
//If user changed a value, notify other scripts through onUpdateNode
|
//If user changed a value, notify other scripts through onUpdateNode
|
||||||
|
|||||||
@ -48,7 +48,7 @@ public partial class NodeEditorWindow {
|
|||||||
}
|
}
|
||||||
Repaint();
|
Repaint();
|
||||||
} else if (IsDraggingNode) {
|
} else if (IsDraggingNode) {
|
||||||
draggedNode.rect.position = WindowToGridPosition(e.mousePosition) + dragOffset;
|
draggedNode.position = WindowToGridPosition(e.mousePosition) + dragOffset;
|
||||||
Repaint();
|
Repaint();
|
||||||
}
|
}
|
||||||
} else if (e.button == 1) {
|
} else if (e.button == 1) {
|
||||||
@ -76,7 +76,7 @@ public partial class NodeEditorWindow {
|
|||||||
}
|
}
|
||||||
} else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
|
} else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
|
||||||
draggedNode = hoveredNode;
|
draggedNode = hoveredNode;
|
||||||
dragOffset = hoveredNode.rect.position - WindowToGridPosition(e.mousePosition);
|
dragOffset = hoveredNode.position - WindowToGridPosition(e.mousePosition);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case EventType.MouseUp:
|
case EventType.MouseUp:
|
||||||
@ -113,7 +113,7 @@ public partial class NodeEditorWindow {
|
|||||||
|
|
||||||
public void CreateNode(Type type, Vector2 position) {
|
public void CreateNode(Type type, Vector2 position) {
|
||||||
Node node = graph.AddNode(type);
|
Node node = graph.AddNode(type);
|
||||||
node.rect.position = position;
|
node.position = position;
|
||||||
Repaint();
|
Repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,8 +132,8 @@ public partial class NodeEditorWindow {
|
|||||||
bool IsHoveringTitle(Node node) {
|
bool IsHoveringTitle(Node node) {
|
||||||
Vector2 mousePos = Event.current.mousePosition;
|
Vector2 mousePos = Event.current.mousePosition;
|
||||||
//Get node position
|
//Get node position
|
||||||
Vector2 nodePos = GridToWindowPosition(node.rect.position);
|
Vector2 nodePos = GridToWindowPosition(node.position);
|
||||||
Rect windowRect = new Rect(nodePos, new Vector2(node.rect.size.x / zoom, 30 / zoom));
|
Rect windowRect = new Rect(nodePos, new Vector2(/*Node width*/200 / zoom, 30 / zoom));
|
||||||
return windowRect.Contains(mousePos);
|
return windowRect.Contains(mousePos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -153,8 +153,11 @@ public partial class NodeEditorWindow {
|
|||||||
|
|
||||||
Vector2 mousePos = Event.current.mousePosition;
|
Vector2 mousePos = Event.current.mousePosition;
|
||||||
|
|
||||||
hoveredPort = null;
|
|
||||||
hoveredNode = null;
|
if (e.type != EventType.Layout) {
|
||||||
|
hoveredNode = null;
|
||||||
|
hoveredPort = null;
|
||||||
|
}
|
||||||
|
|
||||||
foreach (Node node in graph.nodes) {
|
foreach (Node node in graph.nodes) {
|
||||||
if (node == null) return;
|
if (node == null) return;
|
||||||
@ -162,7 +165,7 @@ public partial class NodeEditorWindow {
|
|||||||
nodeEditor.target = node;
|
nodeEditor.target = node;
|
||||||
|
|
||||||
//Get node position
|
//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";
|
//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) {
|
if (e.type == EventType.Repaint) {
|
||||||
foreach (var kvp in portHandlePoints) {
|
foreach (var kvp in portHandlePoints) {
|
||||||
Vector2 portHandlePos = kvp.Value;
|
Vector2 portHandlePos = kvp.Value;
|
||||||
portHandlePos += node.rect.position;
|
portHandlePos += node.position;
|
||||||
Rect rect = new Rect(portHandlePos.x - 8, portHandlePos.y - 8, 16, 16);
|
Rect rect = new Rect(portHandlePos.x - 8, portHandlePos.y - 8, 16, 16);
|
||||||
portConnectionPoints.Add(kvp.Key, rect);
|
portConnectionPoints.Add(kvp.Key, rect);
|
||||||
}
|
}
|
||||||
@ -186,27 +189,29 @@ public partial class NodeEditorWindow {
|
|||||||
|
|
||||||
GUILayout.EndVertical();
|
GUILayout.EndVertical();
|
||||||
|
|
||||||
//Check if we are hovering this node
|
if (e.type != EventType.Layout) {
|
||||||
Vector2 nodeSize = GUILayoutUtility.GetLastRect().size / zoom;
|
//Check if we are hovering this node
|
||||||
Rect windowRect = new Rect(nodePos, nodeSize);
|
Vector2 nodeSize = GUILayoutUtility.GetLastRect().size / zoom;
|
||||||
if (windowRect.Contains(mousePos)) hoveredNode = node;
|
Rect windowRect = new Rect(nodePos, nodeSize);
|
||||||
|
if (windowRect.Contains(mousePos)) hoveredNode = 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
|
||||||
for (int i = 0; i < node.InputCount; i++) {
|
for (int i = 0; i < node.InputCount; i++) {
|
||||||
NodePort port = node.inputs[i];
|
NodePort port = node.inputs[i];
|
||||||
//Check if port rect is available
|
//Check if port rect is available
|
||||||
if (!portConnectionPoints.ContainsKey(port)) continue;
|
if (!portConnectionPoints.ContainsKey(port)) continue;
|
||||||
Rect r = GridToWindowRect(portConnectionPoints[port]);
|
Rect r = GridToWindowRect(portConnectionPoints[port]);
|
||||||
if (r.Contains(mousePos)) hoveredPort = port;
|
if (r.Contains(mousePos)) hoveredPort = port;
|
||||||
}
|
}
|
||||||
//Check all output ports
|
//Check all output ports
|
||||||
for (int i = 0; i < node.OutputCount; i++) {
|
for (int i = 0; i < node.OutputCount; i++) {
|
||||||
NodePort port = node.outputs[i];
|
NodePort port = node.outputs[i];
|
||||||
//Check if port rect is available
|
//Check if port rect is available
|
||||||
if (!portConnectionPoints.ContainsKey(port)) continue;
|
if (!portConnectionPoints.ContainsKey(port)) continue;
|
||||||
Rect r = GridToWindowRect(portConnectionPoints[port]);
|
Rect r = GridToWindowRect(portConnectionPoints[port]);
|
||||||
if (r.Contains(mousePos)) hoveredPort = port;
|
if (r.Contains(mousePos)) hoveredPort = port;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GUILayout.EndArea();
|
GUILayout.EndArea();
|
||||||
|
|||||||
@ -10,7 +10,7 @@ public abstract class Node : ScriptableObject {
|
|||||||
|
|
||||||
/// <summary> Name of the node </summary>
|
/// <summary> Name of the node </summary>
|
||||||
[SerializeField] public NodeGraph graph;
|
[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>
|
/// <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>();
|
[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>
|
/// <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