Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Register callback for MouseUpEvent on Toolbar instance when a button in the toolbar is clicked

Discussion in 'UI Toolkit' started by tmhbrts, Jan 21, 2020.

  1. tmhbrts

    tmhbrts

    Joined:
    Dec 24, 2013
    Posts:
    13
    Hi there,

    I am trying to do the following: I added a toolbar (instance name: diagramToolbar) to my EditorWindow using the UnityEditor.UIElements.Toolbar class.

    A Button is added to its contentContainer. I want to register callback for a MouseUpEvent (on both the Toolbar and the Button) in the EditorWindow.

    I try to do this as follows:

    Code (CSharp):
    1. diagramToolbar.RegisterCallback<MouseUpEvent>(HandleToolbarMouseUpEvent, TrickleDown.TrickleDown);
    2.  
    3.  
    with HandleToolbarMouseUpEvent just debugging a simple text:

    Code (CSharp):
    1.  private void HandleToolbarMouseUpEvent(MouseUpEvent _mouseUpEvent)
    2. {
    3.     Debug.Log("you have clicked the toolbar");
    4. }
    5.  
    The Method is only invoked when I click the Toolbar itself, not when I click the Button.
    is there a way to register callback for the MouseUpEvent on the Button, through the Toolbar?

    I was able to register callback for a ChangeEvent on an EnumField in the same Toolbar.
     
  2. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    970
    As far as I can tell, the button will capture the event and will prevent the toolbar from receiving the MouseUpEvent in this situation. You could, however, register and additional MouseUpEvent callback on the button itself.
     
  3. tmhbrts

    tmhbrts

    Joined:
    Dec 24, 2013
    Posts:
    13
    But shouldn't the event travel trough the entire visual element tree during its propagation? As mentioned before I am able to handle ChangeEvents through the toolbar from an EnumField that is in the visual tree of the toolbar.

    Also, is there a way to send a synthesized event to the Toolbar when the button is clicked?

    I really want to subscribe to the toolbar, because I created an MVC-like structure where all my methods are in a controller class where I would rather not be querying the toolbar's visual tree.
     
  4. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    970
    Yes. However, in some circumstances, the control will capture the events to make sure all subsequent mouse events will reach it. This is briefly documented here: https://docs.unity3d.com/2019.3/Documentation/Manual/UIE-Events-Dispatching.html

    This part in particular:
    This is why I think registering an additional callback on the button would be required here.

    Yes, there's a way (see https://docs.unity3d.com/Manual/UIE-Events-Synthesizing.html for details).

    You can sythesize an event and send it to an element with VisualElement.SendEvent. Example:

    Code (CSharp):
    1.     var evt = new Event() { type = EventType.MouseUp, button = 0, mousePosition = new Vector2(100, 30) };
    2.     toolbar.SendEvent(MouseUpEvent.GetPooled(evt));
     
  5. Timboc

    Timboc

    Joined:
    Jun 22, 2015
    Posts:
    234
    In my case I desire to update the contents of a ToolbarMenu on click/open. An exposed event for this would be really nice. In my case adding an additional MouseDownEvent didn't work and I'd have to imitate the look of a ToolbarMenu with a VisualElement manually as far as I can tell. It's dirty but for now I'm using RegisterCallback<MouseCaptureEvent> in case that would be useful for you @tmhbrts