{
SortByLabelSize sbls = new SortByLabelSize( pb.isHighlightNeighbors() );
Collections.sort( nodes, sbls );
}
DNVNode node = null;
Vector2D screenPosition;
double distance;
double minDistance = Integer.MAX_VALUE;
int nodeI = -1;
int distX = 0; // dist b/w this node and mouse click
int distY = 0;
// Check if user clicked on a solid node label
for( int i = nodes.size() - 1; i >= 0; i-- )
{
node = nodes.get( i );
if( node.isVisible() && (node.isForceLabel() || pb.isShowLabels() ) && node.getProperty( "faded" ) == null )
{
screenPosition = ImageRenderer.transformPosition( globalMinX, globalMaxX, globalMinY, globalMaxY, minX, maxX, minY, maxY, width, height, node.getPosition( true ) );
ImageRenderer.Rectangle boundingRectangle = ImageRenderer.getRectangleBoundingTheLabel( node, screenPosition, null, (int)Math.round(pb.getNodeSize()*node.getRadius()), node.getLabel( pb.isInterpolationLabels() ), pb.isCurvedLabels() || node.isCurvedLabel(), pb.getLabelSize(),
minX, maxX, width / pb.getWidth(), pb.isScaleLabels(), pb.getMaxLabelLength(), pb.getCurvedLabelAngle(), pb.isBoldLabels(), nodes.size() > 1000, false );
if( mouseDownX >= boundingRectangle.left() &&
mouseDownX <= boundingRectangle.right() &&
mouseDownY <= boundingRectangle.bottom() &&
mouseDownY >= boundingRectangle.top() )
{
distX = (int)( mouseDownX - screenPosition.getX() );
distY = (int)( mouseDownY - screenPosition.getY() );
node.setProperty( "distX", "" + distX );
node.setProperty( "distY", "" + distY );
minDistance = 0;
nodeI = i;
break;
}
}
}
if( nodeI == -1 )
{
// loop thru all nodes to find closest node
for( int i = nodes.size() - 1; i >= 0; i-- )
{
node = nodes.get( i );
if( node.isVisible() )
{
screenPosition = ImageRenderer.transformPosition( globalMinX, globalMaxX, globalMinY,
globalMaxY, minX, maxX, minY, maxY, width, height, node.getPosition( true ) );
// find node closest to mouseDown
distX = (int)( mouseDownX - screenPosition.getX() );
distY = (int)( mouseDownY - screenPosition.getY() );
distance = distX * distX + distY * distY;
if( distance < minDistance )
{
node.setProperty( "distX", "" + distX );
node.setProperty( "distY", "" + distY );
minDistance = distance;
nodeI = i;
}
}
}
}
if( nodes.size() > 0 )
{
node = nodes.get( nodeI );
double nodeWidth;
nodeWidth = GraphServlet.getNodeWidth( pb, width, minX, maxX, node.getRadius() ) + selectionBuffer;
// check if selected node is close enough to mouseDown
if( Settings.DEBUG )
System.out.println( "Minimum distance was " + Math.sqrt( minDistance ) );
if( Math.sqrt( minDistance ) >= nodeWidth )
{
// Still no node selected so check nodes with faded labels
for( int i = nodes.size() - 1; i >= 0; i-- )
{
node = nodes.get( i );
if( node.isVisible() && (node.isForceLabel() || pb.isShowLabels() ) && node.getProperty( "faded" ) != null && Float.parseFloat( node.getProperty( "faded" ) ) > 0.1 )
{
screenPosition = ImageRenderer.transformPosition( globalMinX, globalMaxX, globalMinY, globalMaxY, minX, maxX, minY, maxY, width, height, node.getPosition( true ) );
ImageRenderer.Rectangle boundingRectangle = ImageRenderer.getRectangleBoundingTheLabel( node, screenPosition, null, (int)Math.round(pb.getNodeSize()*node.getRadius()), node.getLabel( pb.isInterpolationLabels() ), pb.isCurvedLabels() || node.isCurvedLabel(), pb.getLabelSize(),
minX, maxX, width / pb.getWidth(), pb.isScaleLabels(), pb.getMaxLabelLength(), pb.getCurvedLabelAngle(), pb.isBoldLabels(), nodes.size() > 1000, false );
if( mouseDownX >= boundingRectangle.left() &&
mouseDownX <= boundingRectangle.right() &&
mouseDownY <= boundingRectangle.bottom() &&
mouseDownY >= boundingRectangle.top() )
{
distX = (int)( mouseDownX - screenPosition.getX() );
distY = (int)( mouseDownY - screenPosition.getY() );
node.setProperty( "distX", "" + distX );
node.setProperty( "distY", "" + distY );
minDistance = 0;
nodeI = i;
break;