-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathPencilCanvas.cs
351 lines (273 loc) · 9.87 KB
/
PencilCanvas.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Geometry;
using Microsoft.Graphics.Canvas.UI;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
namespace Telegram.Controls
{
[TemplatePart(Name = "Canvas", Type = typeof(CanvasControl))]
public partial class PencilCanvas : Control
{
private static readonly Color? ERASING_STROKE = null;
private static readonly float ERASING_STROKE_THICKNESS = 20;
private CanvasRenderTarget _renderTarget;
private ConcurrentDictionary<uint, SmoothPathBuilder> _builders;
private List<SmoothPathBuilder> _drawing;
private List<SmoothPathBuilder> _strokes = new List<SmoothPathBuilder>();
private readonly List<SmoothPathBuilder> _history = new List<SmoothPathBuilder>();
private PencilCanvasMode _mode;
private bool _needToCreateSizeDependentResources;
private bool _needToRedrawInkSurface;
private CanvasControl _canvas;
public PencilCanvas()
{
DefaultStyleKey = typeof(PencilCanvas);
}
protected override void OnApplyTemplate()
{
_builders = new ConcurrentDictionary<uint, SmoothPathBuilder>();
_canvas = (CanvasControl)GetTemplateChild("Canvas");
_canvas.SizeChanged += OnSizeChanged;
_canvas.CreateResources += OnCreateResources;
_canvas.Draw += OnDraw;
_canvas.PointerPressed += OnPointerPressed;
_canvas.PointerMoved += OnPointerMoved;
_canvas.PointerReleased += OnPointerReleased;
base.OnApplyTemplate();
}
public ICanvasResourceCreatorWithDpi Creator => _canvas;
public PencilCanvasMode Mode
{
get => _mode;
set => _mode = value;
}
public event EventHandler StrokesChanged;
public Color Stroke { get; set; }
public float StrokeThickness { get; set; }
public IReadOnlyList<SmoothPathBuilder> Strokes
{
get => _drawing?.ToList();
set
{
if (value == null)
{
return;
}
_drawing = value.ToList();
_strokes = value?.ToList() ?? new List<SmoothPathBuilder>();
_history.Clear();
_canvas?.Invalidate();
}
}
public bool CanUndo => _strokes.Count > 0;
public bool CanRedo => _history.Count > 0;
public void Undo()
{
var last = _strokes.LastOrDefault();
if (last == null)
{
return;
}
_strokes.Remove(last);
_history.Insert(0, last);
_needToRedrawInkSurface = true;
_canvas.Invalidate();
StrokesChanged?.Invoke(this, EventArgs.Empty);
}
public void Redo()
{
var first = _history.FirstOrDefault();
if (first == null)
{
return;
}
_history.Remove(first);
_strokes.Add(first);
_needToRedrawInkSurface = true;
_canvas.Invalidate();
StrokesChanged?.Invoke(this, EventArgs.Empty);
}
public void SaveState()
{
_drawing = _strokes.ToList();
}
public void RestoreState()
{
_strokes = _drawing?.ToList() ?? new List<SmoothPathBuilder>();
_history.Clear();
_canvas.Invalidate();
}
public void Invalidate()
{
_canvas?.Invalidate();
}
#region Resources
private void OnCreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
{
CreateSizeDependentResources();
}
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
_needToCreateSizeDependentResources = true;
_canvas.Invalidate();
}
private void CreateSizeDependentResources()
{
_renderTarget = new CanvasRenderTarget(_canvas, _canvas.Size);
_needToCreateSizeDependentResources = false;
_needToRedrawInkSurface = true;
}
#endregion
#region Drawing
private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
_canvas.CapturePointer(e.Pointer);
var point = e.GetCurrentPoint(_canvas);
var erasing = _mode == PencilCanvasMode.Eraser || point.Properties.IsEraser;
_builders[point.PointerId] = new SmoothPathBuilder(point.Position.ToVector2() / _canvas.Size.ToVector2())
{
Stroke = erasing ? ERASING_STROKE : Stroke,
StrokeThickness = erasing ? ERASING_STROKE_THICKNESS : StrokeThickness
};
}
private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
{
if (_builders.TryGetValue(e.Pointer.PointerId, out SmoothPathBuilder _builder))
{
_builder.MoveTo(e.GetCurrentPoint(_canvas).Position.ToVector2() / _canvas.Size.ToVector2());
_canvas.Invalidate();
}
}
private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
{
_canvas.ReleasePointerCapture(e.Pointer);
if (_builders.TryRemove(e.Pointer.PointerId, out SmoothPathBuilder builder))
{
using (var session = _renderTarget.CreateDrawingSession())
{
DrawPath(session, builder, _renderTarget.Size.ToVector2());
}
_strokes.Add(builder);
_history.Clear();
StrokesChanged?.Invoke(this, EventArgs.Empty);
}
}
private void OnDraw(CanvasControl sender, CanvasDrawEventArgs args)
{
if (_needToCreateSizeDependentResources)
{
CreateSizeDependentResources();
}
if (_needToRedrawInkSurface)
{
using (var canvas = _renderTarget.CreateDrawingSession())
{
canvas.Clear(Colors.Transparent);
foreach (var builder in _strokes)
{
DrawPath(canvas, builder, _renderTarget.Size.ToVector2());
}
}
}
args.DrawingSession.DrawImage(_renderTarget);
foreach (var builder in _builders.Values)
{
DrawPath(args.DrawingSession, builder, sender.Size.ToVector2());
}
}
public static void DrawPath(CanvasDrawingSession canvas, SmoothPathBuilder builder, Vector2 canvasSize)
{
var geometry = builder.ToGeometry(canvas, canvasSize);
var style = new CanvasStrokeStyle();
style.StartCap = CanvasCapStyle.Round;
style.EndCap = CanvasCapStyle.Round;
style.LineJoin = CanvasLineJoin.Round;
canvas.Blend = builder.Stroke == null ? CanvasBlend.Copy : CanvasBlend.SourceOver;
canvas.DrawGeometry(geometry, builder.Stroke ?? Colors.Transparent, builder.StrokeThickness, style);
}
#endregion
}
public enum PencilCanvasMode
{
Stroke,
Eraser
}
public sealed partial class SmoothPathBuilder
{
private List<Vector2> _controlPoints;
private List<Vector2> _path;
private Vector2 _beginPoint;
public SmoothPathBuilder(Vector2 beginPoint)
{
_beginPoint = beginPoint;
_controlPoints = new List<Vector2>();
_path = new List<Vector2>();
}
public Color? Stroke { get; set; }
public float StrokeThickness { get; set; }
public Vector2 BeginPoint
{
get => _beginPoint;
set => _beginPoint = value;
}
public List<Vector2> Path
{
get => _path;
set => _path = value;
}
public void MoveTo(Vector2 point)
{
if (_controlPoints.Count < 4)
{
_controlPoints.Add(point);
return;
}
var endPoint = new Vector2(
(_controlPoints[2].X + point.X) / 2,
(_controlPoints[2].Y + point.Y) / 2);
_path.Add(_controlPoints[1]);
_path.Add(_controlPoints[2]);
_path.Add(endPoint);
_controlPoints = new List<Vector2> { endPoint, point };
}
public void EndFigure(Vector2 point)
{
if (_controlPoints.Count > 1)
{
for (int i = 0; i < _controlPoints.Count; i++)
{
MoveTo(point);
}
}
}
public CanvasGeometry ToGeometry(ICanvasResourceCreator resourceCreator, Vector2 canvasSize)
{
var multiplier = canvasSize;
using var builder = new CanvasPathBuilder(resourceCreator);
builder.BeginFigure(_beginPoint * multiplier);
for (int i = 0; i < _path.Count; i += 3)
{
builder.AddCubicBezier(
_path[i] * multiplier,
_path[i + 1] * multiplier,
_path[i + 2] * multiplier);
}
builder.EndFigure(CanvasFigureLoop.Open);
return CanvasGeometry.CreatePath(builder);
}
}
}