Skip to content

Commit

Permalink
fix: correct PolygonLayer.useAltRenderer renderer when Polygons h…
Browse files Browse the repository at this point in the history
…ave multiple holes (fleaflet#1906)
  • Loading branch information
JaffaKetchup authored Jun 8, 2024
1 parent 0167891 commit a2bf534
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
68 changes: 68 additions & 0 deletions lib/src/layer/polygon_layer/painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ base class _PolygonPainter<R extends Object>
/// This may improve performance: see [polygonLabels] for more information.
final bool drawLabelsLast;

/// See [PolygonLayer.debugAltRenderer]
final bool debugAltRenderer;

/// Create a new [_PolygonPainter] instance.
_PolygonPainter({
required this.polygons,
required this.triangles,
required super.camera,
required this.polygonLabels,
required this.drawLabelsLast,
required this.debugAltRenderer,
required super.hitNotifier,
}) : bounds = camera.visibleBounds;

Expand Down Expand Up @@ -125,6 +129,50 @@ base class _PolygonPainter<R extends Object>
}
final vertices = Vertices.raw(VertexMode.triangles, points);
canvas.drawVertices(vertices, BlendMode.src, paint);

if (debugAltRenderer) {
for (int i = 0; i < trianglePoints.length; i += 3) {
canvas.drawCircle(
trianglePoints[i],
5,
Paint()..color = const Color(0x7EFF0000),
);
canvas.drawCircle(
trianglePoints[i + 1],
5,
Paint()..color = const Color(0x7E00FF00),
);
canvas.drawCircle(
trianglePoints[i + 2],
5,
Paint()..color = const Color(0x7E0000FF),
);

final path = Path()
..addPolygon(
[
trianglePoints[i],
trianglePoints[i + 1],
trianglePoints[i + 2],
],
true,
);

canvas.drawPath(
path,
Paint()
..color = const Color(0x7EFFFFFF)
..style = PaintingStyle.fill,
);

canvas.drawPath(
path,
Paint()
..color = const Color(0xFF000000)
..style = PaintingStyle.stroke,
);
}
}
} else {
canvas.drawPath(filledPath, paint);
}
Expand Down Expand Up @@ -163,6 +211,25 @@ base class _PolygonPainter<R extends Object>
polygonTriangles != null ? projectedPolygon.holePoints : null,
);

if (debugAltRenderer) {
const offsetsLabelStyle = TextStyle(
color: Color(0xFF000000),
fontSize: 16,
);

for (int i = 0; i < fillOffsets.length; i++) {
TextPainter(
text: TextSpan(
text: i.toString(),
style: offsetsLabelStyle,
),
textDirection: TextDirection.ltr,
)
..layout(maxWidth: 100)
..paint(canvas, fillOffsets[i]);
}
}

// The hash is based on the polygons visual properties. If the hash from
// the current and the previous polygon no longer match, we need to flush
// the batch previous polygons.
Expand Down Expand Up @@ -323,6 +390,7 @@ base class _PolygonPainter<R extends Object>
final isSolid = polygon.pattern == const StrokePattern.solid();
final isDashed = polygon.pattern.segments != null;
final isDotted = polygon.pattern.spacingFactor != null;

if (isSolid) {
final SolidPixelHiker hiker = SolidPixelHiker(
offsets: offsets,
Expand Down
19 changes: 17 additions & 2 deletions lib/src/layer/polygon_layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class PolygonLayer<R extends Object> extends StatefulWidget {
/// above before enabling.
final bool useAltRendering;

/// Whether to overlay a debugging tool when [useAltRendering] is enabled to
/// display triangulation results
final bool debugAltRenderer;

/// Whether to cull polygons and polygon sections that are outside of the
/// viewport
///
Expand Down Expand Up @@ -76,6 +80,7 @@ class PolygonLayer<R extends Object> extends StatefulWidget {
super.key,
required this.polygons,
this.useAltRendering = false,
this.debugAltRenderer = false,
this.polygonCulling = true,
this.simplificationTolerance = 0.5,
this.polygonLabels = true,
Expand Down Expand Up @@ -175,10 +180,10 @@ class _PolygonLayerState<R extends Object> extends State<PolygonLayer<R>> {
: points.elementAt(ii ~/ 2).y,
growable: false,
),
// Not sure how just this works but it seems to :D
holeIndices: culledPolygon.holePoints.isEmpty
? null
: [culledPolygon.points.length],
: _generateHolesIndices(culledPolygon)
.toList(growable: false),
);
},
growable: false,
Expand All @@ -192,13 +197,23 @@ class _PolygonLayerState<R extends Object> extends State<PolygonLayer<R>> {
camera: camera,
polygonLabels: widget.polygonLabels,
drawLabelsLast: widget.drawLabelsLast,
debugAltRenderer: widget.debugAltRenderer,
hitNotifier: widget.hitNotifier,
),
size: Size(camera.size.x, camera.size.y),
),
);
}

Iterable<int> _generateHolesIndices(_ProjectedPolygon<R> polygon) sync* {
var prevValue = polygon.points.length;
yield prevValue;

for (int i = 0; i < polygon.holePoints.length - 1; i++) {
yield prevValue += polygon.holePoints[i].length;
}
}

List<_ProjectedPolygon<R>> _computeZoomLevelSimplification({
required MapCamera camera,
required List<_ProjectedPolygon<R>> polygons,
Expand Down

0 comments on commit a2bf534

Please sign in to comment.