public virtual DSPPortAttribute GetDSPPortAttribute(NodePort port)
    {
        var outputFieldInfo = NodeDataCache.GetNodeFields(port.node.GetType()).Where(_ => _.Name == port.fieldName).FirstOrDefault();

        if (outputFieldInfo != null)
        {
            return(outputFieldInfo.GetCustomAttributes(typeof(DSPPortAttribute), false).FirstOrDefault() as DSPPortAttribute);
        }
        return(null);
    }
Ejemplo n.º 2
0
        private void DrawNodes()
        {
            Event e = Event.current;

            if (e.type == EventType.Layout)
            {
                selectionCache = new List <UnityEngine.Object>(Selection.objects);
            }

            System.Reflection.MethodInfo onValidate = null;
            if (Selection.activeObject != null && Selection.activeObject is XNode.Node)
            {
                onValidate = Selection.activeObject.GetType().GetMethod("OnValidate");
                if (onValidate != null)
                {
                    EditorGUI.BeginChangeCheck();
                }
            }

            BeginZoomed(position, zoom, topPadding);

            Vector2 mousePos = Event.current.mousePosition;

            if (e.type != EventType.Layout)
            {
                hoveredNode = null;
                hoveredPort = null;
                hoveredLink = null;
            }

            List <UnityEngine.Object> preSelection = preBoxSelection != null ? new List <UnityEngine.Object>(preBoxSelection) : new List <UnityEngine.Object>();

            // Selection box stuff
            Vector2 boxStartPos = GridToWindowPositionNoClipped(dragBoxStart);
            Vector2 boxSize     = mousePos - boxStartPos;

            if (boxSize.x < 0)
            {
                boxStartPos.x += boxSize.x; boxSize.x = Mathf.Abs(boxSize.x);
            }
            if (boxSize.y < 0)
            {
                boxStartPos.y += boxSize.y; boxSize.y = Mathf.Abs(boxSize.y);
            }
            Rect selectionBox = new Rect(boxStartPos, boxSize);

            //Save guiColor so we can revert it
            Color guiColor = GUI.color;

            List <XNode.NodePort> removeEntries = new List <XNode.NodePort>();

            if (e.type == EventType.Layout)
            {
                culledNodes = new List <XNode.Node>();
            }

            Action <NodeLinkPort> selectLinkIfHovered = link =>
            {
                if (link == null || !linkConnectionPoints.ContainsKey(link))
                {
                    return;
                }
                Rect r = GridToWindowRectNoClipped(linkConnectionPoints[link]);
                if (r.Contains(mousePos))
                {
                    hoveredLink = link;
                }
            };

            for (int n = 0; n < graph.nodes.Count; n++)
            {
                // Skip null nodes. The user could be in the process of renaming scripts, so removing them at this point is not advisable.
                if (graph.nodes[n] == null)
                {
                    continue;
                }
                if (n >= graph.nodes.Count)
                {
                    return;
                }
                XNode.Node node = graph.nodes[n];

                // Culling
                if (e.type == EventType.Layout)
                {
                    // Cull unselected nodes outside view
                    if (!Selection.Contains(node) && ShouldBeCulled(node))
                    {
                        culledNodes.Add(node);
                        continue;
                    }
                }
                else if (culledNodes.Contains(node))
                {
                    continue;
                }

                if (e.type == EventType.Repaint)
                {
                    removeEntries.Clear();
                    foreach (var kvp in portConnectionPoints)
                    {
                        if (kvp.Key.node == node)
                        {
                            removeEntries.Add(kvp.Key);
                        }
                    }
                    foreach (var k in removeEntries)
                    {
                        portConnectionPoints.Remove(k);
                    }
                }

                NodeEditor nodeEditor = NodeEditor.GetEditor(node, this);

                NodeEditor.portPositions.Clear();

                // Set default label width. This is potentially overridden in OnBodyGUI
                EditorGUIUtility.labelWidth = 84;

                //Get node position
                Vector2 nodePos = GridToWindowPositionNoClipped(node.position);

                GUILayout.BeginArea(new Rect(nodePos, new Vector2(nodeEditor.GetWidth(), 4000)));

                bool selected = selectionCache.Contains(graph.nodes[n]);

                if (selected)
                {
                    GUIStyle style          = new GUIStyle(nodeEditor.GetBodyStyle());
                    GUIStyle highlightStyle = new GUIStyle(nodeEditor.GetBodyHighlightStyle());
                    highlightStyle.padding = style.padding;
                    style.padding          = new RectOffset();
                    GUI.color = nodeEditor.GetTint();
                    GUILayout.BeginVertical(style);
                    GUI.color = NodeEditorPreferences.GetSettings().highlightColor;
                    GUILayout.BeginVertical(new GUIStyle(highlightStyle));
                }
                else
                {
                    GUIStyle style = new GUIStyle(nodeEditor.GetBodyStyle());
                    GUI.color = nodeEditor.GetTint();
                    GUILayout.BeginVertical(style);
                }

                GUI.color = guiColor;
                EditorGUI.BeginChangeCheck();

                //Draw node contents
                nodeEditor.OnHeaderGUI();
                nodeEditor.OnBodyGUI();

                //If user changed a value, notify other scripts through onUpdateNode
                if (EditorGUI.EndChangeCheck())
                {
                    if (NodeEditor.onUpdateNode != null)
                    {
                        NodeEditor.onUpdateNode(node);
                    }
                    EditorUtility.SetDirty(node);
                    nodeEditor.serializedObject.ApplyModifiedProperties();
                }

                GUILayout.EndVertical();

                //Cache data about the node for next frame
                if (e.type == EventType.Repaint)
                {
                    Vector2 size = GUILayoutUtility.GetLastRect().size;
                    if (nodeSizes.ContainsKey(node))
                    {
                        nodeSizes[node] = size;
                    }
                    else
                    {
                        nodeSizes.Add(node, size);
                    }

                    foreach (var kvp in NodeEditor.portPositions)
                    {
                        Vector2 portHandlePos = kvp.Value;
                        portHandlePos += node.position;
                        Rect rect = new Rect(portHandlePos.x - 8, portHandlePos.y - 8, 16, 16);
                        portConnectionPoints[kvp.Key] = rect;
                    }

                    foreach (var kvp in NodeEditor.linkPositions)
                    {
                        if (kvp.Key.Node != node)
                        {
                            continue;
                        }
                        Vector2 portHandlePos = kvp.Value;
                        portHandlePos += node.position;
                        Rect rect = new Rect(portHandlePos.x - 8, portHandlePos.y - 8, 16, 16);
                        linkConnectionPoints[kvp.Key] = rect;
                    }
                }

                if (selected)
                {
                    GUILayout.EndVertical();
                }

                if (e.type != EventType.Layout)
                {
                    //Check if we are hovering this node
                    Vector2 nodeSize   = GUILayoutUtility.GetLastRect().size;
                    Rect    windowRect = new Rect(nodePos, nodeSize);
                    if (windowRect.Contains(mousePos))
                    {
                        hoveredNode = node;
                    }

                    //If dragging a selection box, add nodes inside to selection
                    if (currentActivity == NodeActivity.DragGrid)
                    {
                        if (windowRect.Overlaps(selectionBox))
                        {
                            preSelection.Add(node);
                        }
                    }

                    //Check if we are hovering any of this nodes ports
                    //Check input ports
                    foreach (XNode.NodePort input in node.Inputs)
                    {
                        //Check if port rect is available
                        if (!portConnectionPoints.ContainsKey(input))
                        {
                            continue;
                        }
                        Rect r = GridToWindowRectNoClipped(portConnectionPoints[input]);
                        if (r.Contains(mousePos))
                        {
                            hoveredPort = input;
                        }
                    }
                    //Check all output ports
                    foreach (XNode.NodePort output in node.Outputs)
                    {
                        //Check if port rect is available
                        if (!portConnectionPoints.ContainsKey(output))
                        {
                            continue;
                        }
                        Rect r = GridToWindowRectNoClipped(portConnectionPoints[output]);
                        if (r.Contains(mousePos))
                        {
                            hoveredPort = output;
                        }
                    }

                    var links = NodeDataCache.GetLinks(node.GetType()).Select(x => new NodeLinkPort(node, x));
                    foreach (NodeLinkPort link in links)
                    {
                        selectLinkIfHovered(link);
                    }
                }

                var selectedLink = Selection.activeObject as NodeLink;
                selectLinkIfHovered(selectedLink?.GetFromPort());
                selectLinkIfHovered(selectedLink?.GetToPort());

                GUILayout.EndArea();
            }

            if (e.type != EventType.Layout && currentActivity == NodeActivity.DragGrid)
            {
                Selection.objects = preSelection.ToArray();
            }
            EndZoomed(position, zoom, topPadding);

            //If a change in is detected in the selected node, call OnValidate method.
            //This is done through reflection because OnValidate is only relevant in editor,
            //and thus, the code should not be included in build.
            if (onValidate != null && EditorGUI.EndChangeCheck())
            {
                onValidate.Invoke(Selection.activeObject, null);
            }
        }
    public override void Init()
    {
        base.Init();

        if (DSPReady)
        {
            using (var block = DSPGraph.CreateCommandBlock())
            {
                _DSPNode = block.CreateDSPNode <TParameters, TProviders, TAudioKernel>();

                // gather possible inlets and outlets
                List <DSPPortAttribute> inlets  = new List <DSPPortAttribute>();
                List <DSPPortAttribute> outlets = new List <DSPPortAttribute>();
                {
                    var fields = NodeDataCache.GetNodeFields(GetType());
                    foreach (var field in fields)
                    {
                        var attributes = field.GetCustomAttributes(false).ToList();
                        var dspPort    = attributes.Find(x => x is DSPPortAttribute) as DSPPortAttribute;
                        if (dspPort != null)
                        {
                            if (attributes.Find(x => x is InputAttribute) != null)
                            {
                                inlets.Add(dspPort);
                            }
                            else if (attributes.Find(x => x is OutputAttribute) != null)
                            {
                                outlets.Add(dspPort);
                            }
                        }
                    }
                }

                // sort by index
                inlets.Sort((l, r) =>
                {
                    return(l.portIndex - r.portIndex);
                });
                outlets.Sort((l, r) =>
                {
                    return(l.portIndex - r.portIndex);
                });
#if UNITY_ASSERTIONS
                // validate
                for (int i = 0; i < inlets.Count; ++i)
                {
                    Debug.Assert(inlets[i].portIndex == i);
                }
                for (int i = 0; i < outlets.Count; ++i)
                {
                    Debug.Assert(outlets[i].portIndex == i);
                }
#endif

                // add inlets and outlets
                foreach (var inlet in inlets)
                {
                    block.AddInletPort(_DSPNode, inlet.channels, inlet.format);
                }
                foreach (var outlet in outlets)
                {
                    block.AddOutletPort(_DSPNode, outlet.channels, outlet.format);
                }

                AddUpdateParametersToBlock(block);
            }
        }
    }