1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 17:26:02 +08:00

Node hover update

This commit is contained in:
Thor Brigsted 2017-10-16 21:48:23 +02:00
parent 94b9318858
commit 23a3e0e732
3 changed files with 29 additions and 53 deletions

View File

@ -31,14 +31,12 @@ public partial class NodeEditorWindow {
Event e = Event.current;
switch (e.type) {
case EventType.MouseMove:
UpdateHovered();
break;
case EventType.ScrollWheel:
if (e.delta.y > 0) zoom += 0.1f * zoom;
else zoom -= 0.1f * zoom;
break;
case EventType.MouseDrag:
UpdateHovered();
if (e.button == 0) {
if (IsDraggingPort) {
if (IsHoveringPort && hoveredPort.IsInput) {
@ -62,7 +60,6 @@ public partial class NodeEditorWindow {
if (e.keyCode == KeyCode.F) Home();
break;
case EventType.MouseDown:
UpdateHovered();
Repaint();
SelectNode(hoveredNode);
if (IsHoveringPort) {
@ -103,7 +100,6 @@ public partial class NodeEditorWindow {
if (!isPanning) ShowContextMenu();
isPanning = false;
}
UpdateHovered();
AssetDatabase.SaveAssets();
break;
}
@ -133,49 +129,6 @@ public partial class NodeEditorWindow {
}
}
/// <summary> Updates <see cref="hoveredNode"/> and <see cref="hoveredPort"/> </summary>
void UpdateHovered() {
Vector2 mousePos = Event.current.mousePosition;
Node newHoverNode = null;
foreach (Node node in graph.nodes) {
if (node == null) return;
//Get node position
Vector2 nodePos = GridToWindowPosition(node.rect.position);
Rect windowRect = new Rect(nodePos, new Vector2(node.rect.size.x / zoom, node.rect.size.y / zoom));
if (windowRect.Contains(mousePos)) {
newHoverNode = node;
}
}
if (newHoverNode != hoveredNode) {
hoveredNode = newHoverNode;
Repaint();
}
//If we are hovering a node, check if we are also hovering a port
NodePort newHoverPort = null;
//Check all input ports
for (int k = 0; k < graph.nodes.Count; k++) {
for (int i = 0; i < graph.nodes[k].InputCount; i++) {
NodePort port = graph.nodes[k].inputs[i];
//Check if port rect is available
if (!portConnectionPoints.ContainsKey(port)) continue;
Rect r = GridToWindowRect(portConnectionPoints[port]);
if (r.Contains(mousePos)) newHoverPort = port;
}
//Check all output ports
for (int i = 0; i < graph.nodes[k].OutputCount; i++) {
NodePort port = graph.nodes[k].outputs[i];
//Check if port rect is available
if (!portConnectionPoints.ContainsKey(port)) continue;
Rect r = GridToWindowRect(portConnectionPoints[port]);
if (r.Contains(mousePos)) newHoverPort = port;
}
}
if (newHoverPort != hoveredPort) {
hoveredPort = newHoverPort;
}
}
bool IsHoveringTitle(Node node) {
Vector2 mousePos = Event.current.mousePosition;
//Get node position

View File

@ -151,6 +151,9 @@ public partial class NodeEditorWindow {
BeginZoomed(position, zoom);
Vector2 mousePos = Event.current.mousePosition;
foreach (Node node in graph.nodes) {
if (node == null) return;
NodeEditor nodeEditor = GetNodeEditor(node.GetType());
@ -169,7 +172,6 @@ public partial class NodeEditorWindow {
//Draw node contents
Dictionary<NodePort, Vector2> portHandlePoints;
nodeEditor.OnNodeGUI(out portHandlePoints);
EditorGUILayout.Space();
if (e.type == EventType.Repaint) {
foreach (var kvp in portHandlePoints) {
@ -182,7 +184,31 @@ public partial class NodeEditorWindow {
GUILayout.EndVertical();
//if (e.type == EventType.Repaint) node.rect.size = GUILayoutUtility.GetLastRect().size;
//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;
}
GUILayout.EndArea();
}
EndZoomed(position, zoom);

View File

@ -48,13 +48,10 @@ public static class NodeEditorResources {
nodeBody = new GUIStyle();
nodeBody.normal.background = NodeEditorResources.nodeBody;
nodeBody.border = new RectOffset(32, 32, 32, 32);
nodeBody.padding = new RectOffset(16, 16, 4, 6);
nodeBody.padding = new RectOffset(16, 16, 4, 16);
tooltip = new GUIStyle("helpBox");
tooltip.alignment = TextAnchor.MiddleCenter;
//tooltip.border = new RectOffset(0, 0, 0, 0);
//tooltip.normal.background
//tooltip.padding = new RectOffset(0, 0, 0, 0);
}
}