Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct PolygonLayer.useAltRenderer renderer when Polygons have multiple holes #1906

Merged
merged 4 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -22,13 +22,17 @@ base class _PolygonPainter<R extends Object>
/// Whether to draw labels last and thus over all the polygons
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 @@ -113,6 +117,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 @@ -151,6 +199,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 @@ -293,6 +360,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
26 changes: 22 additions & 4 deletions lib/src/layer/polygon_layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ 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
///
/// Ignored when not in debug mode.
final bool debugAltRenderer;

/// Whether to cull polygons and polygon sections that are outside of the
/// viewport
///
Expand Down Expand Up @@ -76,15 +82,17 @@ class PolygonLayer<R extends Object> extends StatefulWidget {
super.key,
required this.polygons,
this.useAltRendering = false,
bool debugAltRenderer = false,
this.polygonCulling = true,
this.simplificationTolerance = 0.5,
this.polygonLabels = true,
this.drawLabelsLast = false,
this.hitNotifier,
}) : assert(
}) : assert(
simplificationTolerance >= 0,
'simplificationTolerance cannot be negative: $simplificationTolerance',
);
),
debugAltRenderer = kDebugMode && debugAltRenderer;
JaffaKetchup marked this conversation as resolved.
Show resolved Hide resolved

@override
State<PolygonLayer<R>> createState() => _PolygonLayerState<R>();
Expand Down Expand Up @@ -175,10 +183,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 +200,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
Loading