/// <summary> /// Нажатие кнопки мыши инструмента трансформации /// </summary> private void toolMouseDown(object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left) { this.mousePos = Mouse.GetPosition(this.window); if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) // надатие мыши при нажатой кнопке shift { this.border.SetValue(Canvas.LeftProperty, this.mousePos.Value.X); this.border.SetValue(Canvas.TopProperty, this.mousePos.Value.Y); this.border.Width = 0; this.border.Height = 0; this.border.Visibility = Visibility.Visible; this.selectShapes(null); Mouse.OverrideCursor = null; } else if (e.ClickCount > 1 && this.Selection.Count == 1) // двойной щелчок по линии { Polyline line = this.Selection.First() as Polyline; if (line != null) { Point topleft = e.GetPosition(this.canvas); topleft.Offset(-2, -2); // немного расширяем область для более комфортного нажатия на линию Point bottomright = new Point(topleft.X + 5, topleft.Y + 5); for (int i = line.Points.Count - 1; i >= 1; i--) { if (ShapesHelper.CheckLineLineIntersection(topleft, bottomright, line.Points[i - 1], line.Points[i])) { line.Points.Insert(i, topleft); this.dots.SetSource(line); break; } } } } else { var s = this.GetCanvasHoveredElement(); if (s != null) // выделение 1 фигуры { if (s != this.dotsControl) // Если не точка транфсормации { if (!this.Selection.Contains(s)) { this.selectShape(s as Shape); } this.selectedDot = null; Mouse.OverrideCursor = Cursors.SizeAll; } } else // Щелчок по пустой области { this.selectShapes(null); Mouse.OverrideCursor = Cursors.ScrollAll; } } } }
/// <summary> /// Удаляет все фигуры /// </summary> public void ClearAll() { this.selectShape(null); this.selectedDot = null; this.lastShape = null; var shapes = this.canvas.Children.OfType <Shape>(); while (shapes.Count() > 0) { this.canvas.Children.Remove(shapes.First()); } }
/// <summary> /// Перемещение кнопки мыши инструмента трансформации /// </summary> private void toolMouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed && this.mousePos != null) { Point lastPos = Mouse.GetPosition(this.window); if (lastPos == this.mousePos.Value) { return; } if (this.border.Visibility == Visibility.Visible) // Выделение { this.toolSelection(lastPos); } else if (this.selectedDot != null) // Трансформация { if (this.selectedDot.Parent.Source is Polyline) // перемещеине узлов линий { this.toolPolylineTransform(lastPos); } else if (this.selectedDot.Parent.Source is Path) //трансформация 4х угольника { Path path = this.selectedDot.Parent.Source as Path; if (this.selectedDot.RectPoint == RectPoints.Center) // поворот { this.toolRotateRect(lastPos, path); } else // изменение размера { this.toolResizeRect(lastPos, path); } } // обновляем положеие точек после трансформации + сохраняем выделенной такую же точку что и в старой коллекции int olddot = this.selectedDot.DotID; this.dots.SetSource(this.selectedDot.Parent.Source); if (this.selectedDot != this.dots.Dots[olddot]) { this.selectedDot = this.dots.Dots[olddot]; } } else if (this.Selection.Count > 0) // Перемещение фигур { this.toolMoveShapes(lastPos); } else if (this.canvas.IsMouseOver) // перемещение рабочей области рисования { this.toolMoveCanvas(lastPos); } } }
/// <summary> /// Отпускание кнопки мыши инструмента трансформации /// </summary> private void toolMouseUp(object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left) { if (this.border.Visibility == Visibility.Visible) { this.border.Visibility = Visibility.Hidden; } this.mousePos = null; this.selectedDot = null; Mouse.OverrideCursor = null; } }
/// <summary> /// Нажатие кнопки мыши на точке трансформации /// </summary> private void dotsControl_MouseDown(object sender, MouseButtonEventArgs e) { var ht = VisualTreeHelper.HitTest(this.dotsControl, Mouse.GetPosition(this.dotsControl)); if (ht != null) { this.selectedDot = (ht.VisualHit as Rectangle).Tag as DrawToolDot; if (this.selectedDot.Parent.Source is Path && this.selectedDot.RectPoint == RectPoints.Center) { Mouse.OverrideCursor = Cursors.ScrollWE; return; } } Mouse.OverrideCursor = null; }
/// <summary> /// Отпускание кнопки мыши на точке трансформации /// </summary> private void dotsControl_MouseUp(object sender, MouseButtonEventArgs e) { if ((e == null || e.ChangedButton == MouseButton.Left) && this.selectedDot != null) { var intersectedDots = this.dots.Dots .Where(d => Math.Abs(d.DotID - this.selectedDot.DotID) == 1 && DrawToolDot.IsDotsIntersect(this.selectedDot, d)); if (this.selectedDot.Parent.Source is Polyline) { if (intersectedDots.Count() > 0) { Polyline line = this.selectedDot.Parent.Source as Polyline; if (intersectedDots.Count() == line.Points.Count - 1) { this.canvas.Children.Remove(line); this.selectShape(null); } else { int i = 0; foreach (var dot in intersectedDots.OrderBy(d => d.DotID)) { line.Points.RemoveAt(dot.DotID + i); i--; } this.dots.SetSource(line); } } } else if (this.dots.Dots.Count(d => d.Point == this.selectedDot.Point) == 9) // удаляем фигуру если все 9 точек СОВПАДАЮТ { this.canvas.Children.Remove(this.selectedDot.Parent.Source); this.selectShape(null); } this.mousePos = null; this.selectedDot = null; } Mouse.OverrideCursor = null; }
/// <summary> /// Проверяет 2 точки на пересечение. Точки считаются пересекающимеся когда их прямоугольники пересекаются /// </summary> public static bool IsDotsIntersect(DrawToolDot d1, DrawToolDot d2) { double size = d1.Parent.DotSize; return(Math.Abs(d1.X - d2.X) <= size && Math.Abs(d1.Y - d2.Y) <= size); }