Last active
February 20, 2020 16:40
-
-
Save lukpazera/47ff3d3f5cd91c84aeb996eb3c7522b1 to your computer and use it in GitHub Desktop.
A WIP sample on how to measure the distance from a given object in viewport to mouse - in pixels.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is not entirely working as it crashes MODO when an item with this drawing | |
// is selected and transform tool is enable. So something somewhere has to return bad value at some point | |
// and this sample is not handling that. | |
// This code needs to be executed from within item's Draw method. | |
// Drawing is aborted if mouse pointer is more then 120 pixels from the item to be drawn. | |
// The idea is to only draw the item when mouse pointer is close enough to it. | |
#include <lx_vp.hpp> | |
CLxUser_View view(stroke); | |
CLxUser_Matrix itemWorldMatrixRaw; | |
chan.Object(item, "worldMatrix", itemWorldMatrixRaw); | |
CLxMatrix4 itemWorldMatrix(itemWorldMatrixRaw); | |
CLxVector item3dpos = itemWorldMatrix.getTranslation(); | |
CLxUser_View3DportService viewService; | |
int mx, my; | |
int viewIndex = viewService.Mouse(&mx, &my); | |
if (viewIndex >= 0) | |
{ | |
CLxVector mousePos(mx, my, 0.0); | |
double ix, iy; | |
int result = view.ToScreen(item3dpos.v, &ix, &iy); | |
if(result == 0) | |
{ | |
return; // this 3d location is not on screen, bail out. | |
} | |
if (ix < 0.0 || iy < 0.0 || mx < 0 || my < 0) | |
{ | |
return; | |
} | |
CLxVector itemPos(ix, iy, 0.0); // item pos in screen coords | |
CLxVector screenOffset = mousePos - itemPos; | |
double distance = screenOffset.length(); | |
if (distance > 120.0) | |
{ | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment