3 Star 4 Fork 3

DLGCY_Clone/DrawTools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
CurveDrawTool.cs 4.23 KB
一键复制 编辑 原始数据 按行查看 历史
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
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/DLGCY_Clone/DrawTools.git
git@gitee.com:DLGCY_Clone/DrawTools.git
DLGCY_Clone
DrawTools
DrawTools
main

搜索帮助