/// <summary> /// Update the attributes of the rectangles associated to the wire /// </summary> private void Redraw() { SortNodes(); // Positions of vertices Node[] vertices = new Node[4]; vertices[0] = ExtremitiesNodes[0]; if (ExtremitiesNodes[0].Position.Y < ExtremitiesNodes[1].Position.Y) { vertices[1] = BoardGrid.Magnetize(new Point((ExtremitiesNodes[0].Position.X + ExtremitiesNodes[1].Position.X) / 2, ExtremitiesNodes[0].Position.Y)); } else { vertices[1] = BoardGrid.Magnetize(new Point((ExtremitiesNodes[0].Position.X + ExtremitiesNodes[1].Position.X) / 2, ExtremitiesNodes[1].Position.Y)); } vertices[2] = BoardGrid.Magnetize(new Point((ExtremitiesNodes[0].Position.X + ExtremitiesNodes[1].Position.X) / 2, ExtremitiesNodes[1].Position.Y)); vertices[3] = ExtremitiesNodes[1]; // Set the rectangles position and size for (int k = 0; k < 3; k++) { Canvas.SetLeft(Rectangles[k], vertices[k].Position.X - Thickness / 2); Canvas.SetTop(Rectangles[k], vertices[k].Position.Y - Thickness / 2); if (k == 1) // The middle rectangle is the vertical one { Rectangles[k].Height = Math.Abs(vertices[0].Position.Y - vertices[2].Position.Y) + Thickness; // Size Rectangles[k].Width = Thickness; } else // The two other rectangles are horizontal { Rectangles[k].Height = Thickness; // Size Rectangles[k].Width = Math.Abs(vertices[k + 1].Position.X - vertices[k].Position.X) + Thickness; } } }
/// <summary> /// Called when the mouse moves /// </summary> public void DragOver(Point mousePos) { Node newNode = BoardGrid.Magnetize(mousePos + MouseOffset); // Move the wire if the new node is inside the canvas and different from the former node if (!(newNode.Position.X < -BoardGrid.GridThickness || newNode.Position.Y < -BoardGrid.GridThickness || newNode.Position.X > BoardGrid.ActualWidth + BoardGrid.GridThickness || newNode.Position.Y > BoardGrid.ActualHeight + BoardGrid.GridThickness)) { if (newNode != DraggingNode) { DraggingNode = newNode; Wire.Nodes = new List <Node>() { StaticNode, DraggingNode }; // Move the wire } } }
/// <summary> /// Connect all anchors to the nearest nodes /// </summary> private void ConnectToNodes() { ClearNodes(); // Clear previous connections foreach (Vector anchor in Anchors) { Node node = BoardGrid.Magnetize(ImagePosition + anchor); // The nearest node Vector nodeRelativePosition = node.Position - ImagePosition; // Node position relative to the image Directions direction = new Directions(); // Direction of the component relative to the node try { if (Math.Abs(ImageSize.X - nodeRelativePosition.X) < Properties.Settings.Default.GridThickness) // The grid thickness is used as an error threshold { direction = Directions.Left; } else if (Math.Abs(ImageSize.Y - nodeRelativePosition.Y) < Properties.Settings.Default.GridThickness) { direction = Directions.Up; } else if (Math.Abs(nodeRelativePosition.Y) < Properties.Settings.Default.GridThickness) { direction = Directions.Down; } else if (Math.Abs(nodeRelativePosition.X) < Properties.Settings.Default.GridThickness) { direction = Directions.Right; } else { throw new System.ApplicationException("Can't determine anchor position relatively to the node."); } } catch (System.ApplicationException e) { ((MainWindow)Application.Current.MainWindow).LogError(e); // Write error to log and close the processus } ConnectedNodes.Add(node); node.ConnectedElements.Add(this, direction); } }