How to determine the hit order of graph elements for mouse input
Questions & AnswersSummary
Description
Every ViewMode defines several methods, which determine what to do, when mouse input is being detected. For example mousePressedLeft(), mousePressedRight(), mouseDraggedLeft() and so on. When several graph elements are located at the coordinates, where one of these mouse gestures is being performed, one has to determine, which elements are affected. By default, only one element will be. But which one? Class HitInfo is the one who determines the order, in which these elements will be affected.
Class ViewMode itself provides the method getHitInfo(), which returns an instance of HitInfo. By default a simple HitInfo is used which detemines the following order:
- Port
- Bend
- EdgeLabel
- Edge
- Node
- NodeLabel
One can easily overwrite getHitInfo() to return an own implementation of HitInfo. For example if NodeLabels shall be hit before Edges, the overwritten method may look like this:
//overwrite method from y.view.ViewMode
protected HitInfo getHitInfo( double x, double y )
{
HitInfo hitInfo = new HitInfo(getGraph2D(), x, y, false,
HitInfo.PORT,
HitInfo.BEND,
HitInfo.ELABEL,
HitInfo.NLABEL,
HitInfo.EDGE,
HitInfo.NODE
);
setLastHitInfo(hitInfo);
return hitInfo;
}
Have a look at the according section in the yFiles developers guide.