Nevron Forum

Automatically activate CreateConnectionTool when mouse it is 'near' a port

https://www.nevron.com/Forum/Topic4681.aspx

By Luciano Cavallero - Tuesday, December 28, 2010

Hi All,

I need to realize this simple behaviour in my Nevron Diagram Winform application but I don't know how to do it
(I write this topic after I have not found any in the forum):

My app start with the Selector Tool enabled: I would like that when the mouse cursors is 'near' a any port of a 2D shape the view automatically
activate the Connector Creato Tool (so i can start to create connection without manual selecting the right tool from the toolbar) then and go back to the Selector Tool if the mouse cursor go away from the port location.

I'm able to activate the Connector Tool, but I don't know to raise and 'event' to signal me that I'm 'near' a port (is this is the right way)

Anyone can suggest me if it is possible to solve my proble and how?

Thanks
Luciano Cavallero

By Nevron Support - Thursday, December 30, 2010

Hi Luciano,

Following is a code sample that implements similar behavior:

...
view.MouseMove += new MouseEventHandler(view_MouseMove);
...


void view_MouseMove(object sender, MouseEventArgs e)
{
   // create connector tool already activated - do nothing
   if (view.Controller.Tools.GetToolByName(NDWFR.ToolCreateConnector).IsActive)
      return;

   // determine whether there is a port neard the current mouse position.
   // this implementation considers near to be 10 pixels
   NPointF mouseInClient = new NPointF(e.X, e.Y);
   bool hasPortNearMouse = false;
   NRectangleF window = view.Window;

   foreach (NPort port in document.Descendants(Nevron.Diagram.Filters.NFilters.TypeNPort, -1))
   {
      NPointF portInClient = view.SceneToDevice.TransformPoint(port.Location);
      if (window.Contains(portInClient) == false)
         continue;

      if (NGeometry2D.PointsDistance(portInClient, mouseInClient) < 10)
      {
         hasPortNearMouse = true;
         break;
      }
   }

   // enable the create connector tool or the pointer tool
   if (hasPortNearMouse)
   {
      view.Controller.Tools.SingleEnableTool(NDWFR.ToolCreateConnector);
   }
   else
   {
      string[] toolNames = new string[] {   
                               NDWFR.ToolCreateGuideline,
                               NDWFR.ToolHandle,
                               NDWFR.ToolMove,
                               NDWFR.ToolSelector,
                               NDWFR.ToolContextMenu,
                               NDWFR.ToolKeyboard,
                               NDWFR.ToolInplaceEdit,
                               NDWFR.ToolMouseEventDelegator
                            };

      view.Controller.Tools.SingleEnableTools(toolNames);
   }
}

By Luciano Cavallero - Monday, January 3, 2011

Hi Support Team

your code is exaclty what I need.
It work correclty.

Thank you very much!!!!!

Best regards,

Luciano Cavallero
By Wilhelm Vortisch - Wednesday, February 2, 2011

Hi Luciano,

the solution is very usefull, but in which assambly you have found "NGeometry2D"?

I use the last Version of Nevron components coming with Nevron Diagram for .Net 2010 Volume 1.

Regards

Wilhelm
By Nevron Support - Wednesday, February 2, 2011

Hi Wilhelm,

"NGeometry2D" is a static class located in the namespace "Nevron.GraphicsCore" of the assembly "Nevron.Presentation.dll".

By Wilhelm Vortisch - Thursday, February 3, 2011

Hi Nevron Support,

in assembly "Nevron.Presentation.dll" I can't find the static class "NGeometry2D" located in the namespace "Nevron.GraphicsCore". (I have searched in the whole assembly, too ....)

I have installed the latest version "Nevron .NET Vision 2010 Vol.1 for Visual Studio 2010 (build version: 11.1.17.12)" from your web site downloaded yesterday.

By the way: In which way differs the "sub version" (or "builds") of the main "Nevron .NET Vision 2010 Vol.1 for Visual Studio 2010"?
And where can I determine the real version of used Nevron lib? (in "software" tab in the control center of windows xp I can only read about the main version "10.1.0000" ...)

Regards

Wilhelm
By Nevron Support - Friday, February 4, 2011

Hi Wilhelm,

It appears that the obfuscator we use has renamed the "NGeometry2D" class in the release build. We’ll fix this for the next release. Until then, here’s the source code of the method you need:

 

/// <summary>

/// Obtains the distance between the specified points.

/// </summary>

/// <param name="pt1"></param>

/// <param name="pt2"></param>

/// <returns></returns>

public static float PointsDistance(NPointF pt1, NPointF pt2)

{

      float dx = pt1.X - pt2.X;

      float dy = pt1.Y - pt2.Y;

      return (float)Math.Sqrt(dx * dx + dy * dy);

}

By Wilhelm Vortisch - Monday, February 7, 2011

Hi Nevron Support,

thanks for the workaround, this works for the moment (until the next release ...).

Regards

Wilhelm