mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-03-03 00:00:57 +08:00
支持画笔及痕迹擦除,为白板做准备
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Media3D;
|
||||
|
||||
namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
public class DirectLineDrawingDesignerItemViewModel : DrawingDesignerItemViewModelBase
|
||||
{
|
||||
public DirectLineDrawingDesignerItemViewModel()
|
||||
{
|
||||
}
|
||||
|
||||
public DirectLineDrawingDesignerItemViewModel(IDiagramViewModel root) : base(root)
|
||||
{
|
||||
}
|
||||
|
||||
public DirectLineDrawingDesignerItemViewModel(IDiagramViewModel root, Point startPoint, bool erasable) : base(root, DrawMode.DirectLine, startPoint, erasable)
|
||||
{
|
||||
}
|
||||
|
||||
public DirectLineDrawingDesignerItemViewModel(IDiagramViewModel root, List<Point> points, bool erasable) : base(root, DrawMode.DirectLine, points, erasable)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool OnMouseMove(IInputElement sender, MouseEventArgs e)
|
||||
{
|
||||
var point = e.GetPosition(sender);
|
||||
if (Points == null || Points.Count == 0)//没有起始点
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((Points.LastOrDefault() - point).Length < ColorViewModel.LineWidth)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//按着LeftShift自动封闭曲线
|
||||
if (Keyboard.IsKeyDown(Key.LeftShift) == true)
|
||||
{
|
||||
point = Points[0];
|
||||
}
|
||||
|
||||
if (Geometry is PathGeometry geometry)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
geometry = new PathGeometry();
|
||||
var figure = new PathFigure { StartPoint = Points[0] };
|
||||
geometry.Figures.Add(figure);
|
||||
}
|
||||
|
||||
LineSegment arc = new LineSegment(point, true);
|
||||
|
||||
var preLine = geometry.Figures[0].Segments.LastOrDefault();
|
||||
if (preLine != _lastLine)
|
||||
{
|
||||
geometry.Figures[0].Segments.Remove(preLine);
|
||||
}
|
||||
geometry.Figures[0].Segments.Add(arc);
|
||||
Geometry = geometry;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private LineSegment _lastLine;
|
||||
public override bool OnMouseDown(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
//左键添加点
|
||||
if (e.ChangedButton == MouseButton.Left)
|
||||
{
|
||||
var point = e.GetPosition(sender);
|
||||
if (Points == null || Points.Count == 0)//没有起始点
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((Points.LastOrDefault() - point).Length < ColorViewModel.LineWidth)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
//按着LeftShift自动封闭曲线
|
||||
if (Keyboard.IsKeyDown(Key.LeftShift) == true)
|
||||
{
|
||||
point = Points[0];
|
||||
}
|
||||
Points.Add(point);
|
||||
|
||||
if (Geometry is PathGeometry geometry)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
geometry = new PathGeometry();
|
||||
var figure = new PathFigure { StartPoint = Points[0] };
|
||||
geometry.Figures.Add(figure);
|
||||
}
|
||||
|
||||
var preLine = geometry.Figures[0].Segments.LastOrDefault();
|
||||
if (preLine != _lastLine)
|
||||
{
|
||||
geometry.Figures[0].Segments.Remove(preLine);
|
||||
}
|
||||
|
||||
_lastLine = new LineSegment(point, true);
|
||||
geometry.Figures[0].Segments.Add(_lastLine);
|
||||
Geometry = geometry;
|
||||
|
||||
IsFinish = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool OnMouseUp(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (e.ChangedButton == MouseButton.Left && Keyboard.IsKeyDown(Key.LeftShift) == false)//按着LeftShift自动封闭曲线
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Geometry is PathGeometry geometry)
|
||||
{
|
||||
var preLine = geometry.Figures[0].Segments.LastOrDefault();
|
||||
if (preLine != _lastLine)
|
||||
{
|
||||
geometry.Figures[0].Segments.Remove(preLine);
|
||||
}
|
||||
|
||||
_lastLine = null;
|
||||
Geometry = geometry;
|
||||
}
|
||||
return base.OnMouseUp(sender, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Media3D;
|
||||
using System.Windows.Shapes;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
public class DrawingDesignerItemViewModelBase : DesignerItemViewModelBase
|
||||
{
|
||||
public DrawingDesignerItemViewModelBase() : this(null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public DrawingDesignerItemViewModelBase(IDiagramViewModel root) : base(root)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public DrawingDesignerItemViewModelBase(IDiagramViewModel root, DrawMode drawMode, Point startPoint, bool erasable) : this(root, drawMode, new List<Point> { startPoint }, erasable)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public DrawingDesignerItemViewModelBase(IDiagramViewModel root, DrawMode drawMode, List<Point> points, bool erasable) : base(root)
|
||||
{
|
||||
DrawMode = drawMode;
|
||||
Points = points;
|
||||
Erasable = erasable;
|
||||
}
|
||||
|
||||
public virtual bool OnMouseMove(IInputElement sender, MouseEventArgs e)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool OnMouseDown(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool OnMouseUp(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (IsFinish)
|
||||
{
|
||||
UpdateLocation();
|
||||
|
||||
Geometry.Transform = new TranslateTransform(0 - Left, 0 - Top);
|
||||
|
||||
if (Erasable)
|
||||
{
|
||||
var aPen = new Pen(ColorObject.ToBrush(ColorViewModel.LineColor), ColorViewModel.LineWidth);
|
||||
Geometry = Geometry.GetWidenedPathGeometry(aPen); //可擦除,需要把Geometry转成几何图像,所以不能有填充色
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool Erase(Geometry erase)
|
||||
{
|
||||
if (Erasable)
|
||||
{
|
||||
erase.Transform = new TranslateTransform(0 - Left, 0 - Top);
|
||||
Geometry = Geometry.Combine(Geometry, erase, GeometryCombineMode.Exclude, null);
|
||||
|
||||
if (Geometry.IsEmpty())
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
//var area = erase.GetArea();
|
||||
//if (area > 10)
|
||||
//{
|
||||
return true;
|
||||
//}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Erasable
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
private Geometry _geometry;
|
||||
public Geometry Geometry
|
||||
{
|
||||
get
|
||||
{
|
||||
return _geometry;
|
||||
}
|
||||
set
|
||||
{
|
||||
SetProperty(ref _geometry, value);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsFinish
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
private List<Point> _points;
|
||||
public List<Point> Points
|
||||
{
|
||||
get
|
||||
{
|
||||
return _points;
|
||||
}
|
||||
set
|
||||
{
|
||||
SetProperty(ref _points, value);
|
||||
}
|
||||
}
|
||||
public DrawMode DrawMode
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public void UpdateLocation()
|
||||
{
|
||||
ItemWidth = Geometry.Bounds.Width + ColorViewModel.LineWidth;
|
||||
ItemHeight = Geometry.Bounds.Height + ColorViewModel.LineWidth;
|
||||
Left = Geometry.Bounds.Left;
|
||||
Top = Geometry.Bounds.Top;
|
||||
}
|
||||
|
||||
protected override void Init(IDiagramViewModel root, bool initNew)
|
||||
{
|
||||
base.Init(root, initNew);
|
||||
IsHitTestVisible = false;
|
||||
}
|
||||
|
||||
protected override void InitNew()
|
||||
{
|
||||
ClearConnectors();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
public class EllipseDrawingDesignerItemViewModel : DrawingDesignerItemViewModelBase
|
||||
{
|
||||
public EllipseDrawingDesignerItemViewModel()
|
||||
{
|
||||
}
|
||||
|
||||
public EllipseDrawingDesignerItemViewModel(IDiagramViewModel root) : base(root)
|
||||
{
|
||||
}
|
||||
|
||||
public EllipseDrawingDesignerItemViewModel(IDiagramViewModel root, Point startPoint, bool erasable) : base(root, DrawMode.Rectangle, startPoint, erasable)
|
||||
{
|
||||
}
|
||||
|
||||
public EllipseDrawingDesignerItemViewModel(IDiagramViewModel root, List<Point> points, bool erasable) : base(root, DrawMode.Rectangle, points, erasable)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool OnMouseMove(IInputElement sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.LeftButton == MouseButtonState.Pressed)
|
||||
{
|
||||
var point = e.GetPosition(sender);
|
||||
if (Points == null || Points.Count == 0)//没有起始点
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((Points[0] - point).Length < ColorViewModel.LineWidth)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Keyboard.IsKeyDown(Key.LeftShift))//圆形
|
||||
{
|
||||
var len =Math.Abs(point.X - Points[0].X);//按X轴放大
|
||||
point = new Point(Points[0].X + (point.X > Points[0].X ? len : -len), Points[0].Y + (point.Y > Points[0].Y ? len : -len));
|
||||
}
|
||||
|
||||
if (Points.Count == 2)
|
||||
{
|
||||
Points[1] = point;
|
||||
}
|
||||
else
|
||||
{
|
||||
Points.Add(point);
|
||||
}
|
||||
Geometry = new EllipseGeometry(new Rect(Points[0], Points[1]));
|
||||
IsFinish = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool OnMouseDown(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool OnMouseUp(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
return base.OnMouseUp(sender, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using SvgPathProperties;
|
||||
|
||||
namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
public class EraserDrawingDesignerItemViewModel : DrawingDesignerItemViewModelBase
|
||||
{
|
||||
public EraserDrawingDesignerItemViewModel()
|
||||
{
|
||||
}
|
||||
|
||||
public EraserDrawingDesignerItemViewModel(IDiagramViewModel root) : base(root)
|
||||
{
|
||||
}
|
||||
|
||||
public EraserDrawingDesignerItemViewModel(IDiagramViewModel root, Point startPoint) : base(root, DrawMode.Eraser, startPoint, true)
|
||||
{
|
||||
}
|
||||
|
||||
public EraserDrawingDesignerItemViewModel(IDiagramViewModel root, List<Point> points) : base(root, DrawMode.Eraser, points, true)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool OnMouseMove(IInputElement sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.LeftButton == MouseButtonState.Pressed)
|
||||
{
|
||||
var point = e.GetPosition(sender);
|
||||
if (Points == null || Points.Count == 0)//没有起始点
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if ((Points[0] - point).Length < ColorViewModel.LineWidth)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
PathGeometry geometry;
|
||||
PathFigure figure;
|
||||
if (Keyboard.IsKeyDown(Key.LeftShift) && Geometry != null)//预览模式,一并擦除
|
||||
{
|
||||
geometry = (PathGeometry)Geometry;
|
||||
figure = geometry.Figures[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
geometry = new PathGeometry();
|
||||
figure = new PathFigure { IsClosed = true, IsFilled = true };
|
||||
geometry.Figures.Add(figure);
|
||||
}
|
||||
|
||||
var radius = ColorViewModel.LineWidth / 2;
|
||||
var positiveX = radius * (point.X > Points[0].X ? 1 : -1);
|
||||
var positiveY = radius * (point.Y > Points[0].Y ? 1 : -1);
|
||||
|
||||
figure.StartPoint = new Point( Points[0].X - positiveX, Points[0].Y - positiveY);
|
||||
|
||||
var line = new LineSegment(new Point( Points[0].X + positiveX, Points[0].Y - positiveY), false);
|
||||
figure.Segments.Add(line);
|
||||
|
||||
line = new LineSegment(new Point(point.X + positiveX, point.Y - positiveY), false);
|
||||
figure.Segments.Add(line);
|
||||
|
||||
line = new LineSegment(new Point(point.X + positiveX, point.Y + positiveY), false);
|
||||
figure.Segments.Add(line);
|
||||
|
||||
line = new LineSegment(new Point(point.X - positiveX, point.Y + positiveY), false);
|
||||
figure.Segments.Add(line);
|
||||
|
||||
line = new LineSegment(new Point( Points[0].X - positiveX, Points[0].Y + positiveY), false);
|
||||
figure.Segments.Add(line);
|
||||
|
||||
Points[0] = point;
|
||||
Geometry = geometry;
|
||||
if (Keyboard.IsKeyDown(Key.LeftShift))//预览模式,一并擦除
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Erase(Geometry);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool OnMouseDown(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool OnMouseUp(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (Geometry != null)
|
||||
{
|
||||
Erase(Geometry);
|
||||
}
|
||||
return base.OnMouseUp(sender, e);
|
||||
}
|
||||
|
||||
private new bool Erase(Geometry geometry)
|
||||
{
|
||||
var empty = true;
|
||||
List<DrawingDesignerItemViewModelBase> deleteDrawGeometries = new List<DrawingDesignerItemViewModelBase>();
|
||||
foreach (var g in this.Root.Items.OfType<DrawingDesignerItemViewModelBase>())
|
||||
{
|
||||
if (g.Erase(geometry))
|
||||
deleteDrawGeometries.Add(g);
|
||||
else
|
||||
empty = false;
|
||||
}
|
||||
|
||||
foreach (var g in deleteDrawGeometries)
|
||||
{
|
||||
this.Root.Remove(g);
|
||||
}
|
||||
|
||||
return empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
public class LineDrawingDesignerItemViewModel : DrawingDesignerItemViewModelBase
|
||||
{
|
||||
public LineDrawingDesignerItemViewModel()
|
||||
{
|
||||
}
|
||||
|
||||
public LineDrawingDesignerItemViewModel(IDiagramViewModel root) : base(root)
|
||||
{
|
||||
}
|
||||
|
||||
public LineDrawingDesignerItemViewModel(IDiagramViewModel root, Point startPoint, bool erasable) : base(root, DrawMode.Line, startPoint, erasable)
|
||||
{
|
||||
}
|
||||
|
||||
public LineDrawingDesignerItemViewModel(IDiagramViewModel root, List<Point> points, bool erasable) : base(root, DrawMode.Line, points, erasable)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override bool OnMouseMove(IInputElement sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.LeftButton == MouseButtonState.Pressed)
|
||||
{
|
||||
var point = e.GetPosition(sender);
|
||||
if (Points == null || Points.Count == 0)//没有起始点
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((Points[0] - point).Length < ColorViewModel.LineWidth)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Points.Count == 2)
|
||||
{
|
||||
Points[1] = point;
|
||||
}
|
||||
else
|
||||
{
|
||||
Points.Add(point);
|
||||
}
|
||||
|
||||
Geometry = new LineGeometry(Points[0], Points[1]);
|
||||
IsFinish = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool OnMouseDown(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool OnMouseUp(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
return base.OnMouseUp(sender, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
public class PolygonDrawingDesignerItemViewModel : DrawingDesignerItemViewModelBase
|
||||
{
|
||||
public PolygonDrawingDesignerItemViewModel()
|
||||
{
|
||||
}
|
||||
|
||||
public PolygonDrawingDesignerItemViewModel(IDiagramViewModel root) : base(root)
|
||||
{
|
||||
}
|
||||
|
||||
public PolygonDrawingDesignerItemViewModel(IDiagramViewModel root, Point startPoint, bool erasable) : base(root, DrawMode.Line, startPoint, erasable)
|
||||
{
|
||||
}
|
||||
|
||||
public PolygonDrawingDesignerItemViewModel(IDiagramViewModel root, List<Point> points, bool erasable) : base(root, DrawMode.Line, points, erasable)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override bool OnMouseMove(IInputElement sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.LeftButton == MouseButtonState.Pressed)
|
||||
{
|
||||
var point = e.GetPosition(sender);
|
||||
if (Points == null || Points.Count == 0)//没有起始点
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((Points.LastOrDefault() - point).Length < ColorViewModel.LineWidth)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Points.Add(point);
|
||||
|
||||
if (Geometry is PathGeometry geometry)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
geometry = new PathGeometry();
|
||||
var figure = new PathFigure { StartPoint = Points[0] };
|
||||
figure.IsClosed = true;
|
||||
geometry.Figures.Add(figure);
|
||||
}
|
||||
|
||||
LineSegment arc = new LineSegment(point, true) { IsSmoothJoin = true };
|
||||
geometry.Figures[0].Segments.Add(arc);
|
||||
Geometry = geometry;
|
||||
|
||||
IsFinish = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool OnMouseDown(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool OnMouseUp(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
return base.OnMouseUp(sender, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
public class PolylineDrawingDesignerItemViewModel : DrawingDesignerItemViewModelBase
|
||||
{
|
||||
public PolylineDrawingDesignerItemViewModel()
|
||||
{
|
||||
}
|
||||
|
||||
public PolylineDrawingDesignerItemViewModel(IDiagramViewModel root) : base(root)
|
||||
{
|
||||
}
|
||||
|
||||
public PolylineDrawingDesignerItemViewModel(IDiagramViewModel root, Point startPoint, bool erasable) : base(root, DrawMode.Line, startPoint, erasable)
|
||||
{
|
||||
}
|
||||
|
||||
public PolylineDrawingDesignerItemViewModel(IDiagramViewModel root, List<Point> points, bool erasable) : base(root, DrawMode.Line, points, erasable)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override bool OnMouseMove(IInputElement sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.LeftButton == MouseButtonState.Pressed)
|
||||
{
|
||||
var point = e.GetPosition(sender);
|
||||
if (Points == null || Points.Count == 0)//没有起始点
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((Points.LastOrDefault() - point).Length < ColorViewModel.LineWidth)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Points.Add(point);
|
||||
|
||||
if (Geometry is PathGeometry geometry)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
geometry = new PathGeometry();
|
||||
var figure = new PathFigure { StartPoint = Points[0] };
|
||||
geometry.Figures.Add(figure);
|
||||
}
|
||||
|
||||
LineSegment arc = new LineSegment(point, true);// { IsSmoothJoin = true };
|
||||
geometry.Figures[0].Segments.Add(arc);
|
||||
Geometry = geometry;
|
||||
|
||||
IsFinish = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool OnMouseDown(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool OnMouseUp(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
return base.OnMouseUp(sender, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
public class RectangleDrawingDesignerItemViewModel : DrawingDesignerItemViewModelBase
|
||||
{
|
||||
public RectangleDrawingDesignerItemViewModel()
|
||||
{
|
||||
}
|
||||
|
||||
public RectangleDrawingDesignerItemViewModel(IDiagramViewModel root) : base(root)
|
||||
{
|
||||
}
|
||||
|
||||
public RectangleDrawingDesignerItemViewModel(IDiagramViewModel root, Point startPoint, bool erasable) : base(root, DrawMode.Rectangle, startPoint, erasable)
|
||||
{
|
||||
}
|
||||
|
||||
public RectangleDrawingDesignerItemViewModel(IDiagramViewModel root, List<Point> points, bool erasable) : base(root, DrawMode.Rectangle, points, erasable)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool OnMouseMove(IInputElement sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.LeftButton == MouseButtonState.Pressed)
|
||||
{
|
||||
var point = e.GetPosition(sender);
|
||||
if (Points == null || Points.Count == 0)//没有起始点
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((Points[0] - point).Length < ColorViewModel.LineWidth)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Keyboard.IsKeyDown(Key.LeftShift))//正方形
|
||||
{
|
||||
var len = Math.Abs(point.X - Points[0].X);//按X轴放大
|
||||
point = new Point(Points[0].X + (point.X > Points[0].X ? len : -len), Points[0].Y + (point.Y > Points[0].Y ? len : -len));
|
||||
}
|
||||
|
||||
if (Points.Count == 2)
|
||||
{
|
||||
Points[1] = point;
|
||||
}
|
||||
else
|
||||
{
|
||||
Points.Add(point);
|
||||
}
|
||||
Geometry = new RectangleGeometry(new Rect(Points[0], Points[1]));
|
||||
IsFinish = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool OnMouseDown(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool OnMouseUp(IInputElement sender, MouseButtonEventArgs e)
|
||||
{
|
||||
return base.OnMouseUp(sender, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user