public void UpdateBinding(AbstractNode new_node) { parent_node = new_node; List <PortView> to_remove = new List <PortView>(); foreach (var view in items) { var new_port = new_node.GetPort(view.target.name); if (new_port != null) { view.target = new_port; } else { to_remove.Add(view); } } foreach (var view in to_remove) { items.Remove(view); view.RemoveFromHierarchy(); } }
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); }