Search Unity

Is there an easy way to customize the context menu of a GraphView's BlackboardField?

Discussion in 'UI Toolkit' started by Kerihobo, Nov 16, 2020.

  1. Kerihobo

    Kerihobo

    Joined:
    Jun 26, 2013
    Posts:
    65
    Hi, I've been exploring the GraphView and am stuck with customizing the context menu for a VisualElement held within a Blackboard.

    170259-graphex.png

    I have a vague understanding of how to register the callbacks and add manipulators to things but I seem to have hit a dead-end on customizing the context menu that appears when I right-click on one of my blackboard properties. I would like to add **Remove** or **Delete** to the context menu, as currently, only Rename is an option.

    How would I do that?

    Bonus question: Despite setting my blackboardFields' capabilities to selectable, deletable, drag/droppable etc, I can't seem to pick them up to rearrange or drag them to the graph itself. Are capabilities not the correct way to make them draggable?

    This is how I'm adding these property fields in the first place:

    Code (CSharp):
    1. VisualElement container = new VisualElement();
    2.     BlackboardField blackboardField = new BlackboardField {
    3.         text = newProperty.name
    4.     ,   typeText = "string"
    5.     };
    6.  
    7.     container.Add(blackboardField);
    8.  
    9.     TextField textPropertyValue = new TextField("Value") {
    10.         value = localPropertyValue
    11.     };
    12.  
    13.     textPropertyValue.RegisterValueChangedCallback(e => {
    14.         int changingPropertyIndex = exposedProperties.FindIndex(x => x.name == newProperty.name);
    15.         exposedProperties[changingPropertyIndex].value = e.newValue;
    16.     });
    17.  
    18.     var blackboardValueRow = new BlackboardRow(blackboardField, textPropertyValue);
    19.     container.Add(blackboardValueRow);
    20.  
    21.     StyleSheet st = Resources.Load<StyleSheet>("StyleSheets/Blackboard");
    22.     container.styleSheets.Add(st);
    23.  
    24.     blackboard.Add(container);
    Am I doin it wrong? :S
     
  2. patrickf

    patrickf

    Unity Technologies

    Joined:
    Oct 24, 2016
    Posts:
    57
    Hi!
    Looking at the BlackboardField code, my impression is you would have to register an additional callback for the ContextualMenuEvent on your BlackboardField elements.

    Code (CSharp):
    1. blackboardField.RegisterCallback<ContextualMenuPopulateEvent>(MyMenuPopulateCB);
    2.  
    3. ...
    4.  
    5. void MyMenuPopulateCB(ContextualMenuPopulateEvent evt)
    6. {
    7.     evt.menu.AppendAction("MyItem", MyAction), DropdownMenuAction.AlwaysEnabled);
    8. }
    9.  
    Regarding drag and drop of fields, the capabilities are already set so you should not need to modify them. Reordering of variables in the blackboard should already work. To drop fields in the graph view, the graph view needs to have event handlers for DragUpdatedEvent and DragPerformEvent. For example:

    Code (CSharp):
    1. void OnDragUpdatedEvent(DragUpdatedEvent e)
    2. {
    3.     if (DragAndDrop.GetGenericData("DragSelection") is List<ISelectable> selection && (selection.OfType<BlackboardField>().Count() >= 0))
    4.     {
    5.         DragAndDrop.visualMode = e.actionKey ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Move;
    6.     }
    7. }
    8.  
    9. void OnDragPerformEvent(DragPerformEvent e)
    10. {
    11.     var selection = DragAndDrop.GetGenericData("DragSelection") as List<ISelectable>;
    12.     IEnumerable<BlackboardField> fields = selection.OfType<BlackboardField>();
    13.     foreach (BlackboardField field in fields)
    14.     {
    15.         // Create a node
    16.     }
    17. }
     
    romi-fauzi and gooby429 like this.
  3. Kerihobo

    Kerihobo

    Joined:
    Jun 26, 2013
    Posts:
    65
    Thank you very much! I will give this a try ^^