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. Dismiss Notice

Bug Updating VE Transform Resetting height/width

Discussion in 'UI Toolkit' started by DukeGrimm, Feb 4, 2021.

  1. DukeGrimm

    DukeGrimm

    Joined:
    Sep 29, 2012
    Posts:
    61
    I'm working on some logic for a VisualElement to behave like a OS window pane. I'm working on Resizing the pane. I can detect when clicking near an edge for resize action. I can update the pane size by mouse position difference. Works great for resizing from right and bottom sides of pane.

    I'm working on the logic for resizing from the left and top and I've hit a snag. I presume to resize from the left and top I need to do two steps.
    -Resize Box
    -Then move to new position

    This should make the box smaller and update it's position in the top left to make it appear to be shrinking from the top left. However, the behavior of the code is odd. It seems to ignore the code that resizes the Visual Element if the event also triggers a transform. The sequence of the events appears to have no bearings (position update first then resize or vice versa). It always seems to reset to it's default size.

    I'm using a MouseManipulator that was a basis for resizing VisualElements modified my purposes. Here is the example code from the OnMouseMove.

    Is this perhaps a bug or is there something I need to do, to get this to work?

    Code (CSharp):
    1.         protected void OnMouseMove(MouseMoveEvent e)
    2.         {
    3.             if (!m_Active || !target.HasMouseCapture())
    4.                 return;
    5.  
    6.             Vector2 diff = e.localMousePosition - m_Start;//Use For calculating resize
    7.             Vector2 posDiff = e.mousePosition - m_StartMPos;//Use for position information
    8.  
    9.             if (m_topLeftorBottomRight)//Are we click in top left, then also move Pane position
    10.                 target.parent.transform.position += (Vector3)posDiff;
    11.             //Resize the Pane
    12.             target.parent.style.height = target.parent.layout.height + diff.y;
    13.             target.parent.style.width = target.parent.layout.width + diff.x;
    14.  
    15.             m_Start = e.localMousePosition;//Update Previous mouse positions so the difference doesn't become exponetial.
    16.             m_StartMPos = e.mousePosition;//Update Previous mouse positions so the difference doesn't become exponetial.
    17.  
    18.             e.StopPropagation();
    19.         }
     
  2. DukeGrimm

    DukeGrimm

    Joined:
    Sep 29, 2012
    Posts:
    61
    This may be a bug. I did notice an issue with my code where I should be subtracting the Diff when resizing at the top left. I fixed that, however I'm still having the issue were I can't seem to set .transform.position in the same frame/eventCall as changing the styles; doing so seems to reset the styles.

    I can get some of the behavior I want if I use a toggling bool so that the transform only happens every other event. So it does appear to be an issue were something with the execution of changing the transform rec is undoing any changes in the styles.