예제 #1
0
        /// <summary>
        /// Create a node instance from the reflected type data
        /// </summary>
        public AbstractNode CreateInstance()
        {
            AbstractNode node = ScriptableObject.CreateInstance(type) as AbstractNode;

            node.RegenerateGuid();
            node.name = name;

            // Setup ports
            foreach (var port in ports)
            {
                // Now it's basically everything from reflection.
                // TODO Get rid of reflection?
                node.AddPort(new NodePort()
                {
                    node      = node,
                    portName  = port.portName,
                    isMulti   = port.isMulti,
                    isInput   = port.isInput,
                    type      = port.type,
                    fieldName = port.fieldName
                });
            }

            // If we spawned a FuncNode, bind it to the method.
            if (method != null && node is FuncNode func)
            {
                func.CreateDelegate(method);
            }

            return(node);
        }
예제 #2
0
        private VisualElement AddItem(string property_path, int index)
        {
            var port_name = $"{this.list_name}[{index}]";
            var port      = parent_node.GetPort(port_name);
            var prop      = m_SerializedList.GetArrayElementAtIndex(index);

            if (port == null)
            {
                Debug.Log("Added port!");
                port = new Port {
                    name    = port_name,
                    isInput = false,
                    acceptsMultipleConnections = false,
                    type      = list_type,
                    fieldName = prop.propertyPath
                };
                parent_node.AddPort(port);
                m_SerializedList.serializedObject.Update();
            }
            var view = PortView.Create(port, port.type, m_ConnectorListener);

            view.hideEditorFieldOnConnection = false;

            var field = new PropertyField(prop, "");

            field.Bind(m_SerializedList.serializedObject);
            field.RegisterCallback((FocusOutEvent e) => m_OnPropertyChange());

            var container = new VisualElement();

            container.AddToClassList("property-field-container");
            container.style.flexDirection = FlexDirection.Row;
            var remove_button = new Button(() => {
                m_UpdateBinding();
                m_SerializedList.DeleteArrayElementAtIndex(index);
                m_SerializedList.serializedObject.ApplyModifiedProperties();
                m_UpdateBinding();
                int new_size    = m_SerializedList.arraySize;
                var canvas      = GetFirstAncestorOfType <CanvasView>();
                var connections = new List <Edge>(items[index].connections);
                foreach (var connection in connections)
                {
                    canvas.RemoveEdge(connection, false);
                }
                for (int i = index; i < new_size; i++)
                {
                    connections = new List <Edge>(items[i + 1].connections);
                    foreach (Edge connection in connections)
                    {
                        var connect_to = connection.input;
                        canvas.RemoveEdge(connection, false);
                        if (connect_to != null)
                        {
                            canvas.AddEdge(new Edge {
                                input  = connect_to,
                                output = items[i]
                            }, false);
                        }
                    }
                }
                canvas.DirtyAll();
                // Remove the last port from the list!
                parent_node.RemovePort(parent_node.GetPort($"{list_name}[{new_size}]"));
                m_SerializedList.serializedObject.Update();
                m_UpdateBinding();
                m_OnPropertyChange();
            });

            remove_button.text                          = "-";
            remove_button.style.color                   = new StyleColor(UnityEngine.Color.white);
            remove_button.style.backgroundColor         = new StyleColor(UnityEngine.Color.red);
            remove_button.style.marginBottom            = new StyleLength(0.0);
            remove_button.style.marginLeft              = new StyleLength(0.0);
            remove_button.style.marginTop               = new StyleLength(0.0);
            remove_button.style.marginRight             = new StyleLength(0.0);
            remove_button.style.borderBottomLeftRadius  = new StyleLength(0.0);
            remove_button.style.borderTopLeftRadius     = new StyleLength(0.0);
            remove_button.style.borderTopRightRadius    = new StyleLength(0.0);
            remove_button.style.borderBottomRightRadius = new StyleLength(0.0);
            container.Add(remove_button);
            container.Add(field);
            field.style.flexGrow = new StyleFloat(1.0f);

            view.SetEditorField(container);

            this.items.Add(view);
            Add(view);
            m_OnPropertyChange();
            return(view);
        }