Allow visual dragging of items

This commit is contained in:
Tim Young 2017-06-06 07:29:13 -05:00
parent 0d848c6823
commit c9422ab9d7
2 changed files with 45 additions and 0 deletions

View File

@ -94,6 +94,7 @@ namespace EduNetworkBuilder
if ((dest.GetIP & _mask) == NetworkAddress)
return true;
return false;
}
public bool Edit(NetworkDevice FromWhat, Form ParentForm, string message="")

View File

@ -40,6 +40,12 @@ namespace EduNetworkBuilder
private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
public bool NeedsUpdate = false; //Do we do an update next tick
//For mouse-move / dragging
public DateTime LastMoveTime = DateTime.UtcNow;
public bool MouseIsDown = false; //For tracking if we are dragging something
public Image LastBackgroundImage = null;
public Point LastMouseMovePos = new Point(-1, -1);
public BuilderWindow()
{
InitializeComponent();
@ -911,6 +917,9 @@ namespace EduNetworkBuilder
Point CenteredLocation = myNetwork.clickedPosCentered(e.Location);
Point ClickLocation = myNetwork.clickedPos(e.Location);
NetworkDevice ReleasedOn = myNetwork.ItemAtPosition(ClickLocation);
MouseIsDown = false;
LastBackgroundImage = null;
LastMouseMovePos = new Point(-1, -1);
//Do we have something selected that we should add?
TimeSpan duration;
@ -1029,10 +1038,45 @@ namespace EduNetworkBuilder
ClickedLocation = location;
//See if we have clicked on something
ItemClickedOn = myNetwork.ItemAtPosition(location);
MouseIsDown = true;
//Make a duplicate of the old background image.
LastBackgroundImage = new Bitmap(pbNetworkView.BackgroundImage);
}
private void pbNetworkView_MouseMove(object sender, MouseEventArgs e)
{
//Only do this 10 times a second
if (DateTime.UtcNow - LastMoveTime < TimeSpan.FromMilliseconds(100)) return;
if(MouseIsDown && LastBackgroundImage != null && ItemClickedOn != null) //We are trying to drag something
{
//find where we are
Point CenteredLocation = myNetwork.clickedPosCentered(e.Location);
Point MouseLocation = myNetwork.clickedPos(e.Location);
Size itemsize = new Size(myNetwork.itemsize, myNetwork.itemsize);
Rectangle newrec = new Rectangle(CenteredLocation, itemsize);
//erase and Invalidate the old position
if(LastMouseMovePos.X >=0 && LastMouseMovePos.Y>= 0)
{
Rectangle oldrec = new Rectangle(LastMouseMovePos, itemsize);
Graphics.FromImage(pbNetworkView.BackgroundImage).DrawImage(LastBackgroundImage, oldrec, oldrec,GraphicsUnit.Pixel);
myNetwork.Invalidate(oldrec);
}
//temporarily store old pos
Point oldpoint = ItemClickedOn.myLocation();
//set it to the new pos
ItemClickedOn.ChangeLocation(CenteredLocation);
//tell it to draw
ItemClickedOn.Print(pbNetworkView.BackgroundImage,false);
//invalidate
myNetwork.Invalidate(newrec);
//return its old pos.
ItemClickedOn.ChangeLocation(oldpoint);
}
Point location = myNetwork.clickedPos(e.Location);
NetworkDevice MouseOver = myNetwork.ItemAtPosition(location);
MouseHoverOver = MouseOver;