/// <summary> /// Initializes a new instance of the <see cref="ConnectionCreationBehavior"/> class. /// </summary> /// <param name="pParent">The behavior parent view.</param> /// <param name="pWorkingCanvas">The canvas containing the connection line to work with.</param> public ConnectionCreationBehavior(SimpleGraphView pParent, Canvas pWorkingCanvas) : base(pParent) { this.WorkingCanvas = pWorkingCanvas; this.WorkingCanvas.Visibility = Visibility.Collapsed; this.ConnectingLine.Visibility = Visibility.Collapsed; this.mSourceConnector = null; this.mTargetConnector = null; }
/// <summary> /// This method is called when a mouse button up occured on the adorner. /// </summary> /// <param name="pEventArgs">The event arguments</param> protected override void OnMouseUp(MouseButtonEventArgs pEventArgs) { // Release the mouse capture. if (this.IsMouseCaptured) { this.ReleaseMouseCapture(); } // Getting the position. Point lHitPoint = pEventArgs.GetPosition(this); AdornerLayeredCanvas lParentCanvas = this.AdornedElement as AdornerLayeredCanvas; if (lParentCanvas != null) { // Remove the adorner. AdornerLayer lLayer = lParentCanvas.AdornerLayer; if (lLayer != null) { lLayer.Remove(this); } // Hitting the target connector. InputConnector lTargetConnector = lParentCanvas.HitControl <InputConnector>(lHitPoint); if (lTargetConnector != null) { GraphViewModel lGraphViewModel = lParentCanvas.DataContext as GraphViewModel; if (lGraphViewModel != null) { PortViewModel lTargetViewModel = lTargetConnector.ParentPort.Content as PortViewModel; PortViewModel lSourceViewModel = this.mSourceConnector.ParentPort.Content as PortViewModel; if (lTargetViewModel != null && lSourceViewModel.CanBeConnectedTo(lTargetViewModel)) { ConnectionViewModel lConnectionViewModel = new ConnectionViewModel(); lConnectionViewModel.Output = lSourceViewModel; lConnectionViewModel.Input = lTargetViewModel; lGraphViewModel.AddConnection(lConnectionViewModel); } } } } }
/// <summary> /// Delegate called when the mouse isup on the working canvas. /// </summary> /// <param name="pSender">The event sender.</param> /// <param name="pEventArgs">The event arguments.</param> private void OnWorkingCanvasMouseUp(object pSender, MouseButtonEventArgs pEventArgs) { // Release the mouse capture. this.WorkingCanvas.ReleaseMouseCapture(); this.WorkingCanvas.MouseMove -= this.OnWorkingCanvasMouseMove; this.WorkingCanvas.MouseUp -= this.OnWorkingCanvasMouseUp; this.WorkingCanvas.Visibility = Visibility.Collapsed; this.ConnectingLine.Visibility = Visibility.Collapsed; // Trying to create the final connection. if (this.mSourceConnector != null && this.mTargetConnector != null) { // Both defined means the connection can be created. Test have been made in the mouse move event handler. GraphViewModel lGraphViewModel = this.ParentView.DataContext as GraphViewModel; if (lGraphViewModel == null) { return; } PortViewModel lSourceViewModel = this.mSourceConnector.ParentPort.Content as PortViewModel; PortViewModel lTargetViewModel = this.mTargetConnector.ParentPort.Content as PortViewModel; ConnectionViewModel lConnectionViewModel = new ConnectionViewModel(); lConnectionViewModel.Output = lSourceViewModel; lConnectionViewModel.Input = lTargetViewModel; lGraphViewModel.AddConnection(lConnectionViewModel); } // Forget the reference on connectors. this.mSourceConnector = null; this.mTargetConnector = null; }
/// <summary> /// Delegate called when the mouse move on the working canvas. /// </summary> /// <param name="pSender">The event sender.</param> /// <param name="pEventArgs">The event arguments.</param> private void OnWorkingCanvasMouseMove(object pSender, MouseEventArgs pEventArgs) { if (pEventArgs.LeftButton == MouseButtonState.Pressed) { // Create a path according to the source and the end. this.ConnectingLine.To = pEventArgs.GetPosition(this.WorkingCanvas); // Updating the cursor. this.mTargetConnector = this.TryHitInputConnector(); if (this.mTargetConnector != null) { PortViewModel lTargetViewModel = this.mTargetConnector.ParentPort.Content as PortViewModel; PortViewModel lSourceViewModel = this.mSourceConnector.ParentPort.Content as PortViewModel; if (lSourceViewModel.CanBeConnectedTo(lTargetViewModel)) { this.WorkingCanvas.Cursor = Cursors.Cross; } else { this.WorkingCanvas.Cursor = Cursors.No; this.mTargetConnector = null; } } else { this.WorkingCanvas.Cursor = Cursors.Cross; } } }