Search Unity

Mouse event handling of Handles is off

Discussion in 'Immediate Mode GUI (IMGUI)' started by pdinklag, Dec 26, 2018.

  1. pdinklag

    pdinklag

    Joined:
    Jan 24, 2017
    Posts:
    154
    For editing custom assets, I am rendering a little preview scene using a PreviewRenderUtility in an EditorWindow and I'd like it support handles for convenient position editing.

    This is a rough outline of my code (which is ultimately called during the window's OnGUI):
    Code (CSharp):
    1. // begin preview
    2. _previewRenderUtility.BeginPreview(rect, GUIStyle.none);
    3.  
    4. /* ... render the scene ... */
    5.  
    6. // do handles
    7. Handles.SetCamera(_previewRenderUtility.camera);
    8. editPos = Handles.PositionHandle(editPos, Quaternion.identity);
    9.  
    10. // finalize preview
    11. _previewRenderUtility.EndAndDrawPreview(rect);
    rect is the rectangle of the GUI the preview is using.

    The position handle is rendered at the correct position and looks fine. However, it's processing mouse input in a wrong way: the detected mouse position is off. In order to select the handle, I have to move the mouse somewhere above and left of it.

    So there seems to be some discrepancy between Handles' screen-to-world position conversion and the preview's world-to-screen rendering, with the latter being correct. I feel like I need to tell Handles what part of the screen I'm using, but the variant of Handles.SetCamera that takes a rectangle is not documented and I couldn't get it to work properly.

    Does anybody know what I need to do here?
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Quick and dirty solution: modify mousePosition in Event.current to account for the discrepancy.
     
  3. pdinklag

    pdinklag

    Joined:
    Jan 24, 2017
    Posts:
    154
    I tried that, but that's not as quick as it seems because perspective is involved, and I believe getting this right is harder than a clean solution...