From f7b82d38c936b7cf670cda3292ca020fc3a787c3 Mon Sep 17 00:00:00 2001 From: Tim Young Date: Fri, 16 Feb 2018 17:24:45 +0000 Subject: [PATCH] recognize if we click on a link --- EduNetworkBuilder/Network.cs | 15 +++++++++++ EduNetworkBuilder/NetworkBuilder.cs | 4 +++ EduNetworkBuilder/NetworkLink.cs | 41 +++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/EduNetworkBuilder/Network.cs b/EduNetworkBuilder/Network.cs index a5f3f5a..6318834 100644 --- a/EduNetworkBuilder/Network.cs +++ b/EduNetworkBuilder/Network.cs @@ -501,6 +501,21 @@ namespace EduNetworkBuilder } return null; } + + public NetworkLink LinkAtPosition(Point NetworkLocation) + { + NetworkLink tLink; + foreach (NetworkComponent tItem in NetComponents) + { + if (tItem is NetworkLink) + { + tLink = (NetworkLink)tItem; + if (tLink.AtLocation(NetworkLocation)) + return tLink; + } + } + return null; + } public List DevicesInRectangle(Rectangle area) { NetworkDevice tDevice; diff --git a/EduNetworkBuilder/NetworkBuilder.cs b/EduNetworkBuilder/NetworkBuilder.cs index 8ff6420..a565074 100644 --- a/EduNetworkBuilder/NetworkBuilder.cs +++ b/EduNetworkBuilder/NetworkBuilder.cs @@ -1233,6 +1233,10 @@ namespace EduNetworkBuilder Point CenteredLocation = myNetwork.clickedPosCentered(e.Location); Point ClickLocation = myNetwork.clickedPos(e.Location); NetworkDevice ReleasedOn = myNetwork.ItemAtPosition(ClickLocation); + NetworkLink ReleasedOnLink = null; + if(ReleasedOn == null) + ReleasedOnLink = myNetwork.LinkAtPosition(ClickLocation); + LastBackgroundImage = null; pbNetworkView.Image = null; //erase old highlight area LastMouseMovePos = new Point(-1, -1); diff --git a/EduNetworkBuilder/NetworkLink.cs b/EduNetworkBuilder/NetworkLink.cs index 55f41d1..a91fe89 100644 --- a/EduNetworkBuilder/NetworkLink.cs +++ b/EduNetworkBuilder/NetworkLink.cs @@ -320,6 +320,47 @@ namespace EduNetworkBuilder return usedLinks; } + public bool AtLocation(Point NetworkPoint) + { + Network myNet = NB.GetNetwork(); + NetworkDevice Src = myNet.HostMatchingHostNicID(SrcNic); + NetworkDevice Dst = myNet.HostMatchingHostNicID(DstNic); + int Slop = 10; + int AutoWin = Slop * 2; + //Draw a line between them + if (Src == null || Dst == null) + return false; + + Point sPoint = Src.GetCenter(); + Point dPoint = Dst.GetCenter(); + Rectangle SlopRec = new Rectangle( + Math.Min(sPoint.X, dPoint.X) - Slop, + Math.Min(sPoint.Y, dPoint.Y) - Slop, + Math.Abs(sPoint.X - dPoint.X) + (Slop * 2), + Math.Abs(sPoint.Y - dPoint.Y) + (Slop * 2)); + Rectangle ActualRec = new Rectangle( + Math.Min(sPoint.X, dPoint.X), + Math.Min(sPoint.Y, dPoint.Y), + Math.Abs(sPoint.X - dPoint.X), + Math.Abs(sPoint.Y - dPoint.Y)); + if (SlopRec.Contains(NetworkPoint)) + { + if (ActualRec.Width < AutoWin || ActualRec.Height < AutoWin) return true; //we are close enough + //We are inside it. Now to figure out if we are somewhere along the line + Rectangle smallrec = new Rectangle(Math.Min(sPoint.X, dPoint.X), + Math.Min(sPoint.Y, NetworkPoint.Y), + Math.Abs(sPoint.X - NetworkPoint.X), + Math.Abs(sPoint.Y - NetworkPoint.Y)); + double deltax = smallrec.Width / (double)ActualRec.Width; + double deltay = smallrec.Height / (double)ActualRec.Height; + double TrueDelta = Math.Abs(Math.Abs(deltax) - Math.Abs(deltay)); + if (TrueDelta > .15) return false; + return true; + } + return false; + } + + /// /// Have the packet traverse the network ///