Search Unity

DragDrop - How to detect element under cursor? (Droptarget)

Discussion in 'UI Toolkit' started by Kirsche, Mar 9, 2019.

  1. Kirsche

    Kirsche

    Joined:
    Apr 14, 2015
    Posts:
    121
    Hey,

    when I drag and drop a VisualElement onto another, how can I get a reference to the drop target? This is the manipulator (from uDamians youtube video) that's attached to my dragged item:

    Code (CSharp):
    1.     class ItemDragger : MouseManipulator
    2.     {
    3.         private Vector2 startPosition;
    4.         private bool isDragging = false;
    5.  
    6.         public ItemDragger()
    7.         {
    8.             activators.Add(new ManipulatorActivationFilter { button = MouseButton.LeftMouse });
    9.         }
    10.         protected override void RegisterCallbacksOnTarget()
    11.         {
    12.             target.RegisterCallback<MouseDownEvent>(OnMouseDown);
    13.             target.RegisterCallback<MouseMoveEvent>(OnMouseMove);
    14.             target.RegisterCallback<MouseUpEvent>(OnMouseUp);
    15.         }
    16.         protected override void UnregisterCallbacksFromTarget()
    17.         {
    18.             target.UnregisterCallback<MouseDownEvent>(OnMouseDown);
    19.             target.UnregisterCallback<MouseMoveEvent>(OnMouseMove);
    20.             target.UnregisterCallback<MouseUpEvent>(OnMouseUp);
    21.         }
    22.         private void OnMouseDown(MouseDownEvent e)
    23.         {
    24.             if(isDragging)
    25.             {
    26.                 e.StopImmediatePropagation();
    27.                 return;
    28.             }
    29.             if (CanStartManipulation(e))
    30.             {
    31.                 startPosition = e.localMousePosition;
    32.                 isDragging = true;
    33.                 target.CaptureMouse();
    34.                 e.StopPropagation();
    35.             }
    36.         }
    37.         private void OnMouseMove(MouseMoveEvent e)
    38.         {
    39.             if (!isDragging || !target.HasMouseCapture())
    40.                 return;
    41.  
    42.             Vector2 diff = e.localMousePosition - startPosition;
    43.  
    44.             target.style.positionTop = target.layout.y + diff.y;
    45.             target.style.positionLeft = target.layout.x + diff.x;
    46.  
    47.             e.StopPropagation();
    48.         }
    49.         private void OnMouseUp(MouseUpEvent e)
    50.         {
    51.             if (!isDragging || !target.HasMouseCapture() || !CanStopManipulation(e))
    52.                 return;
    53.             isDragging = false;
    54.             target.ReleaseMouse();
    55.             e.StopPropagation();
    56.         }
    57.     }

    I've found classes like DragPerformEvent and DragUpdatedEvent, but I'm not sure on how to use them.
     
  2. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    377
    Hi Kirsche,

    Call PickAll on the panel to get the elements under the mouse:
    mouseEvent.target.panel.PickAll(mouseEvent.mousePosition)


    Also, note that events DragPerformEvent, DragUpdatedEvent, and DragExitedEvent are equivalent to the following IMGUI events: EventType.DragUpdated, EventType.DragPerform, EventType.DragExited.

    Hope this helps
     
    Last edited: Mar 12, 2019
    Kirsche likes this.
  3. Oneiros90

    Oneiros90

    Joined:
    Apr 29, 2014
    Posts:
    78
    Sometimes the
    PickAll
    method returns an empty list, any idea why? The mouse position is valid and I'm always hovering something pickable in my view
     
  4. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    377
    It's hard to tell without an actual example. Is your issue fairly easy to reproduce? If so and if you think it's a buggy behavior, don't hestitate to report it in the Help->Report a bug... menu. This will allow our QA to test locally.
     
    Oneiros90 likes this.