代码拉取完成,页面将自动刷新
using DrawTools.Serialize;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace DrawTools
{
/// <summary>
/// 曲线
/// </summary>
public sealed class CurveDrawTool : DrawGeometryBase
{
public CurveDrawTool(DrawingCanvas drawingCanvas) : base(drawingCanvas)
{
this.DrawingToolType = DrawToolType.Curve;
// 准备要处理的事件
this.CanTouchDown = true;
}
#region 鼠标键盘事件
public override Boolean OnTouchLeave(Point point)
{
if (mousePoint.HasValue)
{
points.Add(mousePoint.Value);
mousePoint = null;
}
if (points.Count < 2)
this.drawingCanvas.DeleteVisual(this);
else
{
geometry = geometry.GetWidenedPathGeometry(pen);
Draw();
}
this.drawingCanvas.DeleteWorkingDrawTool(this);
this.IsFinish = true;
this.CanTouchDown = false;
this.CanTouchMove = false;
this.CanTouchLeave = false;
if (this.TouchId == 0 && this.drawingCanvas.IsMouseCaptured)
this.drawingCanvas.ReleaseMouseCapture();
return true;
}
public override Boolean OnTouchDown(Int32 touchId, Point point)
{
this.TouchId = touchId;
if (points.Count == 0)
{
this.drawingCanvas.AddWorkingDrawTool(this);
this.pen = this.drawingCanvas.Pen;
points.Add(point);
geometry = new PathGeometry();
var figure = new PathFigure { StartPoint = point };
pathGeometry.Figures.Add(figure);
this.CanTouchMove = true;
if (this.TouchId != 0 || !this.drawingCanvas.CaptureMouse())
this.CanTouchLeave = true;
this.drawingCanvas.AddVisual(this);
}
else if ((point - points.Last()).Length <= pen.Thickness)
return OnTouchLeave(point);
else if (mousePoint.HasValue)
{
points.Add(mousePoint.Value);
mousePoint = null;
}
return true;
}
public override Boolean OnTouchMove(Point point)
{
var last = points.Last();
if ((point - last).Length <= pen.Thickness)
return true;
var figure = pathGeometry.Figures[0];
var centerX = (last.X + point.X) / 2;
var bezier = new BezierSegment(new Point(centerX, last.Y), new Point(centerX, point.Y), point, true) { IsSmoothJoin = true };
if (mousePoint.HasValue)
figure.Segments[figure.Segments.Count - 1] = bezier;
else
figure.Segments.Add(bezier);
mousePoint = point;
var dc = this.RenderOpen();
dc.DrawGeometry(null, pen, geometry);
dc.Close();
return true;
}
#endregion
#region 序列化
public override DrawGeometrySerializerBase ToSerializer()
{
var serializer = new DrawCurveSerializer
{
Color = ((SolidColorBrush)pen.Brush).Color,
StrokeThickness = pen.Thickness,
Geometry = geometry.ToString()
};
if (geometry.Transform != null)
serializer.Matrix = geometry.Transform.Value;
return serializer;
}
public override void DeserializeFrom(DrawGeometrySerializerBase serializer)
{
this.pen = new Pen(new SolidColorBrush(serializer.Color), serializer.StrokeThickness);
this.geometry = Geometry.Parse(serializer.Geometry).GetFlattenedPathGeometry();
this.geometry.Transform = new TranslateTransform(serializer.Matrix.OffsetX, serializer.Matrix.OffsetY);
this.IsFinish = true;
this.Draw();
}
#endregion
#region 字段
private List<Point> points = new List<Point>();
private Point? mousePoint;
#endregion
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。