-
Notifications
You must be signed in to change notification settings - Fork 1
/
MapInkManager.cs
265 lines (256 loc) · 10.4 KB
/
MapInkManager.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
/* -----------------------------------------------
* Copyright © 2013 Esri Inc. All Rights Reserved.
* ----------------------------------------------- */
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Data.Json;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
namespace ESRI.PrototypeLab.ProximityMap {
public class MapInkManager : BindableBase {
//
// Properties
//
public List<MapInkLine> Lines { get; private set; }
public bool IsEnabled { get; set; }
private readonly Map _map = null;
private readonly Canvas _canvas = null;
//
// Constructor
//
public MapInkManager(Map map, Canvas canvas) {
this._map = map;
this._canvas = canvas;
this.Lines = new List<MapInkLine>();
this.IsEnabled = false;
this._map.ExtentChanged += (s, e) => {
this.Render();
};
this._map.PointerPressed += (s, e) => {
this.AddPoint(e);
};
this._map.PointerMoved += (s, e) => {
if (!e.Pointer.IsInContact) { return; }
this.AddPoint(e);
if (this.IsEnabled) {
this.Render();
}
};
this._map.PointerReleased += (s, e) => {
this.CloseLine(e);
if (this.IsEnabled) {
this.Render();
}
};
}
//
// EVENTS
//
public event EventHandler<MapInkLineEventArgs> LineChanged;
public event EventHandler<MapInkLineEventArgs> LineClosed;
//
// METHODS
//
protected void OnLineChanged(MapInkLineEventArgs e) {
if (this.LineChanged != null) {
this.LineChanged(this, e);
}
}
protected void OnLineClosed(MapInkLineEventArgs e) {
if (this.LineClosed != null) {
this.LineClosed(this, e);
}
}
private void AddPoint(PointerRoutedEventArgs e) {
if (!this.IsEnabled) { return; }
MapInkLine line = this.Lines.FirstOrDefault(l => l.Id == e.Pointer.PointerId && !l.IsClosed);
if (line == null) {
line = new MapInkLine(e.Pointer.PointerId, this.InkColor);
this.Lines.Add(line);
}
line.Points.Add(this._map.ScreenToMap(e.GetCurrentPoint(this._map).Position));
this.OnLineChanged(new MapInkLineEventArgs(line));
}
private void CloseLine(PointerRoutedEventArgs e) {
MapInkLine line = this.Lines.FirstOrDefault(l => l.Id == e.Pointer.PointerId && !l.IsClosed);
if (line != null) {
line.Close();
this.OnLineClosed(new MapInkLineEventArgs(line));
}
}
public void Render() {
const double FLARE_LEN = 200d;
const double MAX_WIDTH = 30d;
const double MIN_WIDTH = 10d;
this._canvas.Children.Clear();
foreach (var line in this.Lines) {
if (line.Points.Count < 2) { continue; }
if (line.IsClosed) {
PathSegmentCollection pathSegments = new PathSegmentCollection();
for (int i = 1; i < line.Points.Count - 2; i = i + 2) {
Point point1 = this._map.MapToScreen(line.Points[i]);
Point point2 = this._map.MapToScreen(line.Points[i + 1]);
QuadraticBezierSegment bezier = new QuadraticBezierSegment() {
Point1 = point1,
Point2 = point2
};
pathSegments.Add(bezier);
}
PathFigure pathFigure = new PathFigure() {
StartPoint = this._map.MapToScreen(line.Points[0]),
Segments = pathSegments
};
PathFigureCollection pathFigures = new PathFigureCollection();
pathFigures.Add(pathFigure);
PathGeometry pathGeometry = new PathGeometry() {
Figures = pathFigures
};
Path p = new Path() {
Stroke = new SolidColorBrush() {
Color = line.Color
},
StrokeStartLineCap = PenLineCap.Round,
StrokeEndLineCap = PenLineCap.Round,
StrokeLineJoin = PenLineJoin.Round,
StrokeThickness = MIN_WIDTH,
IsHitTestVisible = false,
Data = pathGeometry
};
this._canvas.Children.Add(p);
}
else {
Point? curr = null;
Point? prev = null;
double len = 0;
foreach (var point in line.Points.Reverse<MapPoint>()) {
curr = this._map.MapToScreen(point);
if (prev != null) {
len += Math.Sqrt(Math.Pow(prev.Value.X - curr.Value.X, 2) + Math.Pow(prev.Value.Y - curr.Value.Y, 2));
double size = MIN_WIDTH;
if (len <= FLARE_LEN) {
size = len * (MIN_WIDTH - MAX_WIDTH) / FLARE_LEN + MAX_WIDTH;
}
this._canvas.Children.Add(
new Line() {
X1 = curr.Value.X,
Y1 = curr.Value.Y,
X2 = prev.Value.X,
Y2 = prev.Value.Y,
Stroke = new SolidColorBrush() {
Color = Colors.Orange
},
StrokeStartLineCap = PenLineCap.Round,
StrokeEndLineCap = PenLineCap.Round,
StrokeLineJoin = PenLineJoin.Round,
StrokeThickness = size,
IsHitTestVisible = false
}
);
}
prev = curr;
}
}
}
}
public static readonly DependencyProperty InkColorProperty = DependencyProperty.Register(
"InkColor",
typeof(Color),
typeof(MapInkManager),
new PropertyMetadata(Colors.Red)
);
public Color InkColor {
get { return (Color)this.GetValue(MapInkManager.InkColorProperty); }
set {
this.SetValue(MapInkManager.InkColorProperty, value);
this.OnPropertyChanged("InkColor");
}
}
}
public class MapInkLine {
//
// PROPERTIES
//
public Guid UniqueId { get; private set; }
public uint Id { get; private set; }
public List<MapPoint> Points { get; private set; }
public bool IsClosed { get; private set; }
public Color Color { get; private set; }
//
// CONSTRUCTOR
//
public MapInkLine(uint id, Color color) {
this.UniqueId = Guid.NewGuid();
this.Id = id;
this.Points = new List<MapPoint>();
this.IsClosed = false;
this.Color = color;
}
//
// METHODS
//
public void Close() {
this.IsClosed = true;
}
public string ToJson() {
JsonArray j = new JsonArray();
foreach (var point in this.Points) {
JsonObject c = new JsonObject();
c["X"] = JsonValue.CreateNumberValue(point.X);
c["Y"] = JsonValue.CreateNumberValue(point.Y);
j.Add(c);
}
JsonObject color = new JsonObject();
color["A"] = JsonValue.CreateNumberValue(this.Color.A);
color["R"] = JsonValue.CreateNumberValue(this.Color.R);
color["G"] = JsonValue.CreateNumberValue(this.Color.G);
color["B"] = JsonValue.CreateNumberValue(this.Color.B);
JsonObject jsonObject = new JsonObject();
jsonObject["UniqueId"] = JsonValue.CreateStringValue(this.UniqueId.ToString());
jsonObject["Id"] = JsonValue.CreateNumberValue(this.Id);
jsonObject["IsClosed"] = JsonValue.CreateBooleanValue(this.IsClosed);
jsonObject["Points"] = j;
jsonObject["Color"] = color;
return jsonObject.Stringify();
}
public static MapInkLine FromJson(string json) {
JsonValue jsonValue = JsonValue.Parse(json);
Guid guid = Guid.Parse(jsonValue.GetObject().GetNamedString("UniqueId"));
uint id = (uint)jsonValue.GetObject().GetNamedNumber("Id");
bool isclosed = jsonValue.GetObject().GetNamedBoolean("IsClosed");
JsonObject color = jsonValue.GetObject()["Color"].GetObject();
Color c = new Color() {
A = (byte)color.GetNamedNumber("A"),
R = (byte)color.GetNamedNumber("R"),
G = (byte)color.GetNamedNumber("G"),
B = (byte)color.GetNamedNumber("B")
};
var x = jsonValue.GetObject().GetNamedArray("Points").Select(
p => {
return new MapPoint() {
X = p.GetObject().GetNamedNumber("X"),
Y = p.GetObject().GetNamedNumber("Y")
};
}
);
return new MapInkLine(id, c) {
UniqueId = guid,
IsClosed = isclosed,
Points = x.ToList()
};
}
}
public class MapInkLineEventArgs : EventArgs {
public MapInkLine MapInkLine { get; private set; }
public MapInkLineEventArgs(MapInkLine line) {
this.MapInkLine = line;
}
}
}