Search Unity

Question Vector API and pointer events

Discussion in 'UI Toolkit' started by Destruxion, Nov 30, 2022.

  1. Destruxion

    Destruxion

    Joined:
    Jun 5, 2019
    Posts:
    37
    Hey,

    I started to dive into UI Toolkit some days ago and struggle to receive pointer events for a custom visual element that draws a spline using the Vector API (Painter2D.Stroke in a generateVisualContent callback).

    The element draws just fine using "style.position = Position.Absolute" and world spline points, though the debugger shows a width and height of zero for this element, so I guess the mesh vertex stream is not evaluated to compute bounds.

    Can custom mesh shapes like a vector API spline receive pointer/mouse events?
    Can we set the local bounds somehow to match the actual mesh shape?

    Using IMGUI we can get the closest point to a bezier curve and thus make splines clickable. How would I do this using a visual element?
     

    Attached Files:

  2. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    571
    Ui Toolkit use the bounding box to filter the event and see what related element they could be relating to.

    No the mesh itself does not impact the bounding box, as we assume people will be good citizen and only draw where they are supposed to :). Also, the performance hit of checking the meshes at every modification would be noticeable. I am personally thinking about exposing an option in the debugger to validate this on demand, at some point in the future.

    If the element is on the root, the size of the root should cover the whole window, so you could in theory just add top, bottom, left, right =0 so that the element stretch the full size.
     
  3. Destruxion

    Destruxion

    Joined:
    Jun 5, 2019
    Posts:
    37
    So in theory, I could adjust the style to match the region where the spline mesh is drawn and will at least receive events when that rect is clicked?

    In case that works, does the vector API or any related API support something like "closest point on path"? If not, is there any way to perform the required computations and get at least something to compute a ray-mesh intersection? I am not even sure how to access data generated by the vector API.
     
  4. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    571
    Yes, but you could also set it to be the maximum and never change the layout after. If you need more precise picking it may be worth doing a tighter bounding box.




    During the picking algorithm, the element's bounding box is used to do a quick AABB check with the pointer, then there is a VisualElement.ContainsPoint callback to implement if you want more precise picking. This is used for the hover style for example. You will have to provide your own distance method if you want to go this way but we will not be able to select the "closes" only the topmost that contains the point.
     
    Destruxion likes this.