-
-
Notifications
You must be signed in to change notification settings - Fork 898
Expand file tree
/
Copy pathpainter.dart
More file actions
299 lines (251 loc) · 9.31 KB
/
painter.dart
File metadata and controls
299 lines (251 loc) · 9.31 KB
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
part of 'polyline_layer.dart';
/// The [CustomPainter] used to draw [Polyline]s for the [PolylineLayer].
// TODO: We should consider exposing this publicly, as with [CirclePainter] -
// but the projected objects are private at the moment.
class _PolylinePainter<R extends Object> extends CustomPainter
with HitDetectablePainter<R, _ProjectedPolyline<R>>, FeatureLayerUtils {
final List<_ProjectedPolyline<R>> polylines;
final double minimumHitbox;
@override
final MapCamera camera;
@override
final LayerHitNotifier<R>? hitNotifier;
/// Create a new [_PolylinePainter] instance
_PolylinePainter({
required this.polylines,
required this.minimumHitbox,
required this.camera,
required this.hitNotifier,
}) {
_helper = OffsetHelper(camera: camera);
}
late final OffsetHelper _helper;
@override
bool elementHitTest(
_ProjectedPolyline<R> projectedPolyline, {
required Offset point,
required LatLng coordinate,
}) {
final polyline = projectedPolyline.polyline;
// TODO: We should check the bounding box here, for efficiency
// However, we need to account for:
// * map rotation
// * extended bbox that accounts for `minimumHitbox`
//
// if (!polyline.boundingBox.contains(touch)) {
// continue;
// }
WorldWorkControl checkIfHit(double shift) {
final (offsets, _) = _helper.getOffsetsXY(
points: projectedPolyline.points,
shift: shift,
);
final strokeWidth = polyline.useStrokeWidthInMeter
? metersToScreenPixels(
projectedPolyline.polyline.points.first,
polyline.strokeWidth,
)
: polyline.strokeWidth;
if (!areOffsetsVisible(offsets, strokeWidth)) {
return WorldWorkControl.invisible;
}
final hittableDistance = math.max(
strokeWidth / 2 + polyline.borderStrokeWidth / 2,
minimumHitbox,
);
for (int i = 0; i < offsets.length - 1; i++) {
final o1 = offsets[i];
final o2 = offsets[i + 1];
final distanceSq =
getSqSegDist(point.dx, point.dy, o1.dx, o1.dy, o2.dx, o2.dy);
if (distanceSq <= hittableDistance * hittableDistance) {
return WorldWorkControl.hit;
}
}
return WorldWorkControl.visible;
}
return workAcrossWorlds(checkIfHit);
}
@override
Iterable<_ProjectedPolyline<R>> get elements => polylines;
@override
void paint(Canvas canvas, Size size) {
super.paint(canvas, size);
var path = ui.Path();
var borderPath = ui.Path();
var filterPath = ui.Path();
var paint = Paint();
var needsLayerSaving = false;
Paint? borderPaint;
Paint? filterPaint;
int? lastHash;
void drawPaths() {
final hasBorder = borderPaint != null && filterPaint != null;
if (hasBorder) {
if (needsLayerSaving) {
canvas.saveLayer(viewportRect, Paint());
}
canvas.drawPath(borderPath, borderPaint!);
borderPath = ui.Path();
borderPaint = null;
if (needsLayerSaving) {
canvas.drawPath(filterPath, filterPaint!);
filterPath = ui.Path();
filterPaint = null;
canvas.restore();
}
}
canvas.drawPath(path, paint);
path = ui.Path();
paint = Paint();
}
for (final projectedPolyline in polylines) {
final polyline = projectedPolyline.polyline;
if (polyline.points.isEmpty) {
continue;
}
/// Draws on a "single-world"
WorldWorkControl drawIfVisible(double shift) {
final (offsets, _) = _helper.getOffsetsXY(
points: projectedPolyline.points,
shift: shift,
);
late final double strokeWidth;
if (polyline.useStrokeWidthInMeter) {
strokeWidth = metersToScreenPixels(
projectedPolyline.polyline.points.first,
polyline.strokeWidth,
);
} else {
strokeWidth = polyline.strokeWidth;
}
if (!areOffsetsVisible(offsets, strokeWidth)) {
return WorldWorkControl.invisible;
}
final hash = polyline.renderHashCode;
if (needsLayerSaving || (lastHash != null && lastHash != hash)) {
drawPaths();
}
lastHash = hash;
needsLayerSaving = polyline.color.a < 1 ||
(polyline.gradientColors?.any((c) => c.a < 1) ?? false);
// strokeWidth, or strokeWidth + borderWidth if relevant.
double largestStrokeWidth = strokeWidth;
final isSolid = polyline.pattern == const StrokePattern.solid();
final isDashed = polyline.pattern.segments != null;
final isDotted = polyline.pattern.spacingFactor != null;
paint = Paint()
..strokeWidth = strokeWidth
..strokeCap = polyline.strokeCap
..strokeJoin = polyline.strokeJoin
..style = isDotted ? PaintingStyle.fill : PaintingStyle.stroke
..blendMode = BlendMode.srcOver;
if (polyline.gradientColors == null) {
paint.color = polyline.color;
} else {
polyline.gradientColors!.isNotEmpty
? paint.shader = _paintGradient(polyline, offsets)
: paint.color = polyline.color;
}
if (polyline.borderStrokeWidth > 0.0) {
// Outlined lines are drawn by drawing a thicker path underneath, then
// stenciling the middle (in case the line fill is transparent), and
// finally drawing the line fill.
largestStrokeWidth = strokeWidth + polyline.borderStrokeWidth;
borderPaint = Paint()
..color = polyline.borderColor
..strokeWidth = strokeWidth + polyline.borderStrokeWidth
..strokeCap = polyline.strokeCap
..strokeJoin = polyline.strokeJoin
..style = isDotted ? PaintingStyle.fill : PaintingStyle.stroke
..blendMode = BlendMode.srcOver;
filterPaint = Paint()
..color = polyline.borderColor.withAlpha(255)
..strokeWidth = strokeWidth
..strokeCap = polyline.strokeCap
..strokeJoin = polyline.strokeJoin
..style = isDotted ? PaintingStyle.fill : PaintingStyle.stroke
..blendMode = BlendMode.dstOut;
}
final radius = paint.strokeWidth / 2;
final borderRadius = (borderPaint?.strokeWidth ?? 0) / 2;
final List<ui.Path> paths = [];
if (borderPaint != null && filterPaint != null) {
paths.add(borderPath);
paths.add(filterPath);
}
paths.add(path);
if (isSolid) {
final SolidPixelHiker hiker = SolidPixelHiker(
offsets: offsets,
closePath: false,
canvasSize: size,
strokeWidth: largestStrokeWidth,
);
hiker.addAllVisibleSegments(paths);
} else if (isDotted) {
final DottedPixelHiker hiker = DottedPixelHiker(
offsets: offsets,
stepLength: strokeWidth * polyline.pattern.spacingFactor!,
patternFit: polyline.pattern.patternFit!,
closePath: false,
canvasSize: size,
strokeWidth: largestStrokeWidth,
);
final List<double> radii = [];
if (borderPaint != null && filterPaint != null) {
radii.add(borderRadius);
radii.add(radius);
}
radii.add(radius);
for (final visibleDot in hiker.getAllVisibleDots()) {
for (int i = 0; i < paths.length; i++) {
paths[i].addOval(
Rect.fromCircle(center: visibleDot, radius: radii[i]));
}
}
} else if (isDashed) {
final DashedPixelHiker hiker = DashedPixelHiker(
offsets: offsets,
segmentValues: polyline.pattern.segments!,
patternFit: polyline.pattern.patternFit!,
closePath: false,
canvasSize: size,
strokeWidth: largestStrokeWidth,
);
for (final visibleSegment in hiker.getAllVisibleSegments()) {
for (final path in paths) {
path.moveTo(visibleSegment.begin.dx, visibleSegment.begin.dy);
path.lineTo(visibleSegment.end.dx, visibleSegment.end.dy);
}
}
}
return WorldWorkControl.visible;
}
workAcrossWorlds(drawIfVisible);
}
drawPaths();
}
ui.Gradient _paintGradient(Polyline polyline, List<Offset> offsets) =>
ui.Gradient.linear(offsets.first, offsets.last, polyline.gradientColors!,
_getColorsStop(polyline));
List<double>? _getColorsStop(Polyline polyline) =>
(polyline.colorsStop != null &&
polyline.colorsStop!.length == polyline.gradientColors!.length)
? polyline.colorsStop
: _calculateColorsStop(polyline);
List<double> _calculateColorsStop(Polyline polyline) {
final colorsStopInterval = 1.0 / polyline.gradientColors!.length;
return polyline.gradientColors!
.map((gradientColor) =>
polyline.gradientColors!.indexOf(gradientColor) *
colorsStopInterval)
.toList();
}
@override
bool shouldRepaint(_PolylinePainter<R> oldDelegate) =>
polylines != oldDelegate.polylines ||
camera != oldDelegate.camera ||
hitNotifier != oldDelegate.hitNotifier ||
minimumHitbox != oldDelegate.minimumHitbox;
}