How to determine the hit order of graph elements for mouse input

Questions & Answers

Summary

This article shows how to determine the order in which coincident graph elements are hit, when - for example - clicking on them.

This article was written for an older version. It is only online as a reference for customers using this old version. The information it contains is probably out of date.

The latest information can be found in the yFiles documentation

Description

In version 2.6 of yFiles for Java the HitInfo class and the default renderer were enhanced. The hit info returned by a ViewMode instance now corresponds to the rendering order of the corresponding view by default. See Rendering Order and HitInfo in the yFiles for Java Developer's Guide.

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;
  }
It is necessary to call method setLastHitInfo().
Overwriting method getHitInfo() will affect all mouse gestures of class ViewMode. If you only want to change the order of specific mouse gestures, you'll have to overwrite the according methods themselves.

Have a look at the according section in the yFiles developers guide.

Categories this article belongs to:
yFiles for Java > yFiles Viewer > Displaying and Editing Graphs > View Implementations
Applies to:
yFiles for Java 2: 2.0, 2.1, 2.2, 2.3, 2.4, 2.5
Keywords:
mouse - input - ViewMode - order - getHitInfo - HitInfo - mousePressed