/// <summary> /// Manages addition and removal of objects from the selection /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void selection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset) { throw new Exception("To properly clean the selection, please use RemoveAll() instead."); } // call the select method on elements added to the collection if (e.NewItems != null) { foreach (ISelectable n in e.NewItems) { //n.Select(); //UIElement el = (UIElement)n; //double left = Canvas.GetLeft(el); //double right = Canvas.GetRight(el); //double top = Canvas.GetTop(el); //double bottom = Canvas.GetBottom(el); //MVVM: now storing element coordinates on models double left = n.X; double right = n.X + n.Width; double top = n.Y; double bottom = n.Y + n.Height; // Calculate the offset deltas and determine for which sides // of the Canvas to adjust the offsets. bool modLeft = false; bool modTop = false; double hOffset = ResolveOffset(left, right, out modLeft); double vOffset = ResolveOffset(top, bottom, out modTop); OffsetData os = new OffsetData(hOffset, vOffset, modLeft, modTop, n); offsets.Add(os); } } if (e.OldItems != null) { // call the deselect method on elements removed from the collection foreach (ISelectable n in e.OldItems) { //(n as ISelectable).Deselect(); // remove the corresponding offsetdata object // for the element being removed List <OffsetData> toRemove = offsets.Where(od => od.Node == n).ToList(); foreach (OffsetData od in toRemove) { offsets.Remove(od); } } } }
protected override void OnMouseUp(MouseButtonEventArgs e) { base.OnMouseUp(e); this.isDragInProgress = false; // recalculate the offsets for all items in // the selection. int count = 0; foreach (ISelectable sel in DynamoSelection.Instance.Selection) { var n = sel as ILocatable; if (n == null) { continue; } //UIElement el = (UIElement)n; //double left = Canvas.GetLeft(el); //double right = Canvas.GetRight(el); //double top = Canvas.GetTop(el); //double bottom = Canvas.GetBottom(el); //MVVM: now storing element coordinates on models double left = n.X; double right = n.X + n.Width; double top = n.Y; double bottom = n.Y + n.Height; // Calculate the offset deltas and determine for which sides // of the Canvas to adjust the offsets. bool modLeft = false; bool modTop = false; double hOffset = ResolveOffset(left, right, out modLeft); double vOffset = ResolveOffset(top, bottom, out modTop); if (offsets.Count > count) { OffsetData os = offsets[count]; os.ModifyLeftOffset = modLeft; os.ModifyTopOffset = modTop; os.OriginalHorizontalOffset = hOffset; os.OriginalVerticalOffset = vOffset; } count++; } }
/// <summary> /// Manages addition and removal of objects from the selection /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void selection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset) { throw new Exception("To properly clean the selection, please use RemoveAll() instead."); } // call the select method on elements added to the collection if (e.NewItems != null) { foreach (ISelectable sel in e.NewItems) { var n = sel as ILocatable; if (n == null) continue; //n.Select(); //UIElement el = (UIElement)n; //double left = Canvas.GetLeft(el); //double right = Canvas.GetRight(el); //double top = Canvas.GetTop(el); //double bottom = Canvas.GetBottom(el); //MVVM: now storing element coordinates on models double left = n.X; double right = n.X + n.Width; double top = n.Y; double bottom = n.Y + n.Height; // Calculate the offset deltas and determine for which sides // of the Canvas to adjust the offsets. bool modLeft = false; bool modTop = false; double hOffset = ResolveOffset(left, right, out modLeft); double vOffset = ResolveOffset(top, bottom, out modTop); OffsetData os = new OffsetData(hOffset, vOffset, modLeft, modTop, n); offsets.Add(os); } } if (e.OldItems != null) { // call the deselect method on elements removed from the collection foreach (ISelectable n in e.OldItems) { //(n as ISelectable).Deselect(); // remove the corresponding offsetdata object // for the element being removed List<OffsetData> toRemove = offsets.Where(od => od.Node == n).ToList(); foreach (OffsetData od in toRemove) { offsets.Remove(od); } } } }
protected override void OnPreviewMouseMove(MouseEventArgs e) { base.OnPreviewMouseMove(e); if (DynamoSelection.Instance.Selection.Count == 0 || !this.isDragInProgress) { return; } // Get the position of the mouse cursor, relative to the Canvas. Point cursorLocation = e.GetPosition(this); #region Calculate Offsets int count = 0; foreach (ISelectable sel in DynamoSelection.Instance.Selection) { OffsetData od = offsets[count]; // Determine the horizontal offset. if (od.ModifyLeftOffset) { od.NewHorizontalOffset = od.OriginalHorizontalOffset + (cursorLocation.X - this.origCursorLocation.X); } else { od.NewHorizontalOffset = od.OriginalHorizontalOffset - (cursorLocation.X - this.origCursorLocation.X); } // Determine the vertical offset. if (od.ModifyTopOffset) { od.NewVerticalOffset = od.OriginalVerticalOffset + (cursorLocation.Y - this.origCursorLocation.Y); } else { od.NewVerticalOffset = od.OriginalVerticalOffset - (cursorLocation.Y - this.origCursorLocation.Y); } //Debug.WriteLine(string.Format("New h:{0} v:{1}", od.NewHorizontalOffset, od.NewVerticalOffset)); count++; } #endregion // Calculate Offsets if (!this.AllowDragOutOfView) { #region Verify Drag Element Location count = 0; foreach (ISelectable sel in DynamoSelection.Instance.Selection) { OffsetData od = offsets[count]; // Get the bounding rect of the drag element. Rect elemRect = this.CalculateDragElementRect(sel, od.NewHorizontalOffset, od.NewVerticalOffset, od.ModifyLeftOffset, od.ModifyTopOffset); // If the element is being dragged out of the viewable area, // determine the ideal rect location, so that the element is // within the edge(s) of the canvas. // bool leftAlign = elemRect.Left < 0; bool rightAlign = elemRect.Right > this.ActualWidth; if (leftAlign) { od.NewHorizontalOffset = od.ModifyLeftOffset ? 0 : this.ActualWidth - elemRect.Width; } else if (rightAlign) { od.NewHorizontalOffset = od.ModifyLeftOffset ? this.ActualWidth - elemRect.Width : 0; } bool topAlign = elemRect.Top < 0; bool bottomAlign = elemRect.Bottom > this.ActualHeight; if (topAlign) { od.NewVerticalOffset = od.ModifyTopOffset ? 0 : this.ActualHeight - elemRect.Height; } else if (bottomAlign) { od.NewVerticalOffset = od.ModifyTopOffset ? this.ActualHeight - elemRect.Height : 0; } count++; } #endregion // Verify Drag Element Location } #region Move Drag Element count = 0; this.Dispatcher.Invoke(new Action( delegate { foreach (ISelectable el in DynamoSelection.Instance.Selection) { OffsetData od = offsets[count]; if (od.ModifyLeftOffset) { //Canvas.SetLeft(el, od.NewHorizontalOffset); el.X = od.NewHorizontalOffset; } else { //Canvas.SetRight(el, od.NewHorizontalOffset); el.X = od.NewHorizontalOffset + el.Width; } if (od.ModifyTopOffset) { //Canvas.SetTop(el, od.NewVerticalOffset); el.Y = od.NewVerticalOffset; } else { //Canvas.SetBottom(el, od.NewVerticalOffset); el.Y = od.NewHorizontalOffset + el.Height; } count++; } } ), DispatcherPriority.Render, null); #endregion // Move Drag Element }
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { if (ignoreClick) { ignoreClick = false; return; } if (!isConnecting) { base.OnMouseLeftButtonDown(e); this.isDragInProgress = false; // Cache the mouse cursor location. this.origCursorLocation = e.GetPosition(this); if (this.elementsBeingDragged.Count == 0) return; foreach (UIElement el in elementsBeingDragged) { // Get the element's offsets from the four sides of the Canvas. double left = Canvas.GetLeft(el); double right = Canvas.GetRight(el); double top = Canvas.GetTop(el); double bottom = Canvas.GetBottom(el); // Calculate the offset deltas and determine for which sides // of the Canvas to adjust the offsets. bool modLeft = false; bool modTop = false; double hOffset = ResolveOffset(left, right, out modLeft); double vOffset = ResolveOffset(top, bottom, out modTop); OffsetData os = new OffsetData(hOffset, vOffset, modLeft, modTop); offsets.Add(os); } e.Handled = true; this.isDragInProgress = true; } }