This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 462
/
Copy pathDrawingViewRenderer.wpf.cs
151 lines (135 loc) · 4.18 KB
/
DrawingViewRenderer.wpf.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
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using Xamarin.CommunityToolkit.Sample.WPF;
using Xamarin.CommunityToolkit.UI.Views;
using Xamarin.Forms;
using Xamarin.Forms.Platform.WPF;
[assembly: ExportRenderer(typeof(DrawingView), typeof(DrawingViewRenderer))]
namespace Xamarin.CommunityToolkit.Sample.WPF
{
public class DrawingViewRenderer : ViewRenderer<DrawingView, InkCanvas>
{
InkCanvas? canvas;
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == DrawingView.LinesProperty.PropertyName)
{
canvas!.Strokes.StrokesChanged -= OnStrokesChanged;
canvas.Strokes.Clear();
LoadLines();
canvas.Strokes.StrokesChanged += OnStrokesChanged;
}
}
protected override void OnElementChanged(ElementChangedEventArgs<DrawingView> e)
{
base.OnElementChanged(e);
if (Control == null && Element != null)
{
canvas = new InkCanvas
{
Background = Element.BackgroundColor.ToBrush(),
DefaultDrawingAttributes = new()
{
Color = Element.DefaultLineColor.ToMediaColor(),
Width = Element.DefaultLineWidth,
Height = Element.DefaultLineWidth
}
};
Element.Lines.CollectionChanged += OnCollectionChanged;
SetNativeControl(canvas);
canvas.Strokes.StrokesChanged += OnStrokesChanged;
Control!.PreviewMouseDown += OnPreviewMouseDown;
}
if (e.OldElement != null)
{
canvas!.Strokes.StrokesChanged -= OnStrokesChanged;
Element!.Lines.CollectionChanged -= OnCollectionChanged;
if (Control != null)
Control.PreviewMouseDown -= OnPreviewMouseDown;
}
}
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
canvas!.Strokes.StrokesChanged -= OnStrokesChanged;
canvas.Strokes.Clear();
LoadLines();
canvas.Strokes.StrokesChanged += OnStrokesChanged;
}
void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) => Clear();
void Clear(bool force = false)
{
if (!Element.MultiLineMode || force)
{
canvas!.Strokes.Clear();
Element.Lines.Clear();
}
}
void OnStrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
{
Element.Lines.CollectionChanged -= OnCollectionChanged;
if (e.Added.Count > 0)
{
if (!Element.MultiLineMode)
{
Element.Lines.Clear();
}
var lines = Element.MultiLineMode ? e.Added : new StrokeCollection() { e.Added.First() };
foreach (var line in lines)
{
var points = line.StylusPoints.Select(point => new Point(point.X, point.Y)).ToList();
Element.Lines.Add(new Line()
{
Points = new ObservableCollection<Point>(points),
LineColor = Color.FromRgba(line.DrawingAttributes.Color.R, line.DrawingAttributes.Color.G,
line.DrawingAttributes.Color.B, line.DrawingAttributes.Color.A),
LineWidth = (float)line.DrawingAttributes.Width
});
}
if (Element.Lines.Count > 0)
{
var lastLine = Element.Lines.Last();
if (Element.DrawingLineCompletedCommand?.CanExecute(lastLine) ?? false)
Element.DrawingLineCompletedCommand.Execute(lastLine);
}
if (Element.ClearOnFinish)
{
Element.Lines.CollectionChanged -= OnCollectionChanged;
Clear(true);
canvas!.Strokes.StrokesChanged += OnStrokesChanged;
}
}
Element.Lines.CollectionChanged += OnCollectionChanged;
}
void LoadLines()
{
var lines = Element.MultiLineMode
? Element.Lines
: Element.Lines.Any()
? new ObservableCollection<Line> { Element.Lines.LastOrDefault() }
: new ObservableCollection<Line>();
foreach (var line in lines)
{
var stylusPoints = line.Points.Select(point => new StylusPoint(point.X, point.Y)).ToList();
if (stylusPoints is { Count: > 0 })
{
var stroke = new Stroke(new StylusPointCollection(stylusPoints))
{
DrawingAttributes = new()
{
Color = line.LineColor.ToMediaColor(),
Width = line.LineWidth,
Height = line.LineWidth
}
};
canvas!.Strokes.Add(stroke);
}
}
}
}
}