Skip to content

Commit a2bf534

Browse files
authored
fix: correct PolygonLayer.useAltRenderer renderer when Polygons have multiple holes (#1906)
1 parent 0167891 commit a2bf534

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

lib/src/layer/polygon_layer/painter.dart

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ base class _PolygonPainter<R extends Object>
3232
/// This may improve performance: see [polygonLabels] for more information.
3333
final bool drawLabelsLast;
3434

35+
/// See [PolygonLayer.debugAltRenderer]
36+
final bool debugAltRenderer;
37+
3538
/// Create a new [_PolygonPainter] instance.
3639
_PolygonPainter({
3740
required this.polygons,
3841
required this.triangles,
3942
required super.camera,
4043
required this.polygonLabels,
4144
required this.drawLabelsLast,
45+
required this.debugAltRenderer,
4246
required super.hitNotifier,
4347
}) : bounds = camera.visibleBounds;
4448

@@ -125,6 +129,50 @@ base class _PolygonPainter<R extends Object>
125129
}
126130
final vertices = Vertices.raw(VertexMode.triangles, points);
127131
canvas.drawVertices(vertices, BlendMode.src, paint);
132+
133+
if (debugAltRenderer) {
134+
for (int i = 0; i < trianglePoints.length; i += 3) {
135+
canvas.drawCircle(
136+
trianglePoints[i],
137+
5,
138+
Paint()..color = const Color(0x7EFF0000),
139+
);
140+
canvas.drawCircle(
141+
trianglePoints[i + 1],
142+
5,
143+
Paint()..color = const Color(0x7E00FF00),
144+
);
145+
canvas.drawCircle(
146+
trianglePoints[i + 2],
147+
5,
148+
Paint()..color = const Color(0x7E0000FF),
149+
);
150+
151+
final path = Path()
152+
..addPolygon(
153+
[
154+
trianglePoints[i],
155+
trianglePoints[i + 1],
156+
trianglePoints[i + 2],
157+
],
158+
true,
159+
);
160+
161+
canvas.drawPath(
162+
path,
163+
Paint()
164+
..color = const Color(0x7EFFFFFF)
165+
..style = PaintingStyle.fill,
166+
);
167+
168+
canvas.drawPath(
169+
path,
170+
Paint()
171+
..color = const Color(0xFF000000)
172+
..style = PaintingStyle.stroke,
173+
);
174+
}
175+
}
128176
} else {
129177
canvas.drawPath(filledPath, paint);
130178
}
@@ -163,6 +211,25 @@ base class _PolygonPainter<R extends Object>
163211
polygonTriangles != null ? projectedPolygon.holePoints : null,
164212
);
165213

214+
if (debugAltRenderer) {
215+
const offsetsLabelStyle = TextStyle(
216+
color: Color(0xFF000000),
217+
fontSize: 16,
218+
);
219+
220+
for (int i = 0; i < fillOffsets.length; i++) {
221+
TextPainter(
222+
text: TextSpan(
223+
text: i.toString(),
224+
style: offsetsLabelStyle,
225+
),
226+
textDirection: TextDirection.ltr,
227+
)
228+
..layout(maxWidth: 100)
229+
..paint(canvas, fillOffsets[i]);
230+
}
231+
}
232+
166233
// The hash is based on the polygons visual properties. If the hash from
167234
// the current and the previous polygon no longer match, we need to flush
168235
// the batch previous polygons.
@@ -323,6 +390,7 @@ base class _PolygonPainter<R extends Object>
323390
final isSolid = polygon.pattern == const StrokePattern.solid();
324391
final isDashed = polygon.pattern.segments != null;
325392
final isDotted = polygon.pattern.spacingFactor != null;
393+
326394
if (isSolid) {
327395
final SolidPixelHiker hiker = SolidPixelHiker(
328396
offsets: offsets,

lib/src/layer/polygon_layer/polygon_layer.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class PolygonLayer<R extends Object> extends StatefulWidget {
4242
/// above before enabling.
4343
final bool useAltRendering;
4444

45+
/// Whether to overlay a debugging tool when [useAltRendering] is enabled to
46+
/// display triangulation results
47+
final bool debugAltRenderer;
48+
4549
/// Whether to cull polygons and polygon sections that are outside of the
4650
/// viewport
4751
///
@@ -76,6 +80,7 @@ class PolygonLayer<R extends Object> extends StatefulWidget {
7680
super.key,
7781
required this.polygons,
7882
this.useAltRendering = false,
83+
this.debugAltRenderer = false,
7984
this.polygonCulling = true,
8085
this.simplificationTolerance = 0.5,
8186
this.polygonLabels = true,
@@ -175,10 +180,10 @@ class _PolygonLayerState<R extends Object> extends State<PolygonLayer<R>> {
175180
: points.elementAt(ii ~/ 2).y,
176181
growable: false,
177182
),
178-
// Not sure how just this works but it seems to :D
179183
holeIndices: culledPolygon.holePoints.isEmpty
180184
? null
181-
: [culledPolygon.points.length],
185+
: _generateHolesIndices(culledPolygon)
186+
.toList(growable: false),
182187
);
183188
},
184189
growable: false,
@@ -192,13 +197,23 @@ class _PolygonLayerState<R extends Object> extends State<PolygonLayer<R>> {
192197
camera: camera,
193198
polygonLabels: widget.polygonLabels,
194199
drawLabelsLast: widget.drawLabelsLast,
200+
debugAltRenderer: widget.debugAltRenderer,
195201
hitNotifier: widget.hitNotifier,
196202
),
197203
size: Size(camera.size.x, camera.size.y),
198204
),
199205
);
200206
}
201207

208+
Iterable<int> _generateHolesIndices(_ProjectedPolygon<R> polygon) sync* {
209+
var prevValue = polygon.points.length;
210+
yield prevValue;
211+
212+
for (int i = 0; i < polygon.holePoints.length - 1; i++) {
213+
yield prevValue += polygon.holePoints[i].length;
214+
}
215+
}
216+
202217
List<_ProjectedPolygon<R>> _computeZoomLevelSimplification({
203218
required MapCamera camera,
204219
required List<_ProjectedPolygon<R>> polygons,

0 commit comments

Comments
 (0)