Skip to content

Commit

Permalink
Remove dangerous Point extensions which were used to cover up deeper …
Browse files Browse the repository at this point in the history
…issues.
  • Loading branch information
ignatz committed Nov 2, 2023
1 parent c89e959 commit 36f3658
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 63 deletions.
3 changes: 1 addition & 2 deletions lib/src/layer/marker_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter/widgets.dart';
import 'package:flutter_map/src/layer/general/mobile_layer_transformer.dart';
import 'package:flutter_map/src/map/camera/camera.dart';
import 'package:flutter_map/src/misc/bounds.dart';
import 'package:flutter_map/src/misc/point_extensions.dart';
import 'package:latlong2/latlong.dart';

/// A container for a [child] widget located at a geographic coordinate [point]
Expand Down Expand Up @@ -126,7 +125,7 @@ class MarkerLayer extends StatelessWidget {
)) continue;

// Apply map camera to marker position
final pos = pxPoint.subtract(map.pixelOrigin);
final pos = pxPoint - map.pixelOrigin;

yield Positioned(
key: m.key,
Expand Down
14 changes: 6 additions & 8 deletions lib/src/layer/overlay_image_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class OverlayImage extends BaseOverlayImage {
}) {
// northWest is not necessarily upperLeft depending on projection
final bounds = Bounds<double>(
camera.project(this.bounds.northWest).subtract(camera.pixelOrigin),
camera.project(this.bounds.southEast).subtract(camera.pixelOrigin),
camera.project(this.bounds.northWest) - camera.pixelOrigin,
camera.project(this.bounds.southEast) - camera.pixelOrigin,
);

return Positioned(
Expand Down Expand Up @@ -111,12 +111,10 @@ class RotatedOverlayImage extends BaseOverlayImage {
required Image child,
required MapCamera camera,
}) {
final pxTopLeft =
camera.project(topLeftCorner).subtract(camera.pixelOrigin);
final pxTopLeft = camera.project(topLeftCorner) - camera.pixelOrigin;
final pxBottomRight =
camera.project(bottomRightCorner).subtract(camera.pixelOrigin);
final pxBottomLeft =
camera.project(bottomLeftCorner).subtract(camera.pixelOrigin);
camera.project(bottomRightCorner) - camera.pixelOrigin;
final pxBottomLeft = camera.project(bottomLeftCorner) - camera.pixelOrigin;

/// calculate pixel coordinate of top-right corner by calculating the
/// vector from bottom-left to top-left and adding it to bottom-right
Expand All @@ -129,7 +127,7 @@ class RotatedOverlayImage extends BaseOverlayImage {

final vectorX = (pxTopRight - pxTopLeft) / bounds.size.x;
final vectorY = (pxBottomLeft - pxTopLeft) / bounds.size.y;
final offset = pxTopLeft.subtract(bounds.topLeft);
final offset = pxTopLeft - bounds.topLeft;

final a = vectorX.x;
final b = vectorX.y;
Expand Down
1 change: 0 additions & 1 deletion lib/src/layer/polygon_layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:flutter_map/src/geo/latlng_bounds.dart';
import 'package:flutter_map/src/layer/general/mobile_layer_transformer.dart';
import 'package:flutter_map/src/layer/polygon_layer/label.dart';
import 'package:flutter_map/src/map/camera/camera.dart';
import 'package:flutter_map/src/misc/bounds.dart';
import 'package:flutter_map/src/misc/point_extensions.dart';
import 'package:latlong2/latlong.dart' hide Path; // conflict with Path from UI

Expand Down
5 changes: 2 additions & 3 deletions lib/src/layer/tile_layer/tile_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import 'package:flutter_map/src/layer/tile_layer/tile_update_transformer.dart';
import 'package:flutter_map/src/map/camera/camera.dart';
import 'package:flutter_map/src/map/controller/map_controller.dart';
import 'package:flutter_map/src/misc/bounds.dart';
import 'package:flutter_map/src/misc/point_extensions.dart';
import 'package:http/retry.dart';
import 'package:logger/logger.dart';

Expand Down Expand Up @@ -533,8 +532,8 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
);

final currentPixelOrigin = Point<double>(
map.pixelOrigin.x.toDouble(),
map.pixelOrigin.y.toDouble(),
map.pixelOrigin.x,
map.pixelOrigin.y,
);

_tileScaleCalculator.clearCacheUnlessZoomMatches(map.zoom);
Expand Down
5 changes: 2 additions & 3 deletions lib/src/layer/tile_layer/wms_tile_layer_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ class WMSTileLayerOptions {
}

String getUrl(TileCoordinates coords, int tileSize, bool retinaMode) {
final tileSizePoint = Point(tileSize, tileSize);
final nwPoint = coords.scaleBy(tileSizePoint);
final sePoint = nwPoint + tileSizePoint;
final nwPoint = coords * tileSize;
final sePoint = nwPoint + Point<int>(tileSize, tileSize);
final nwCoords = crs.pointToLatLng(nwPoint, coords.z.toDouble());
final seCoords = crs.pointToLatLng(sePoint, coords.z.toDouble());
final nw = crs.projection.project(nwCoords);
Expand Down
5 changes: 2 additions & 3 deletions lib/src/map/camera/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ class MapCamera {
/// This will convert a latLng to a position that we could use with a widget
/// outside of FlutterMap layer space. Eg using a Positioned Widget.
Point<double> latLngToScreenPoint(LatLng latLng) {
final nonRotatedPixelOrigin =
(project(center, zoom) - nonRotatedSize / 2.0).round();
final nonRotatedPixelOrigin = project(center, zoom) - nonRotatedSize / 2.0;

var point = crs.latLngToPoint(latLng, zoom);

Expand All @@ -292,7 +291,7 @@ class MapCamera {
point = rotatePoint(mapCenter, point, counterRotation: false);
}

return point.subtract(nonRotatedPixelOrigin);
return point - nonRotatedPixelOrigin;
}

LatLng pointToLatLng(Point localPoint) {
Expand Down
44 changes: 1 addition & 43 deletions lib/src/misc/point_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,54 +67,12 @@ extension PointExtension<T extends num> on Point<T> {
/// Point<int>(1, 2).subtract(1.5) would cause a runtime error because the
/// resulting x/y values are doubles and the return value is a Point<int> since
/// the method returns Point<T>.
///
/// Note that division methods (unscaleBy and the / operator) are defined on
/// Point<T extends num> with a Point<double> return argument because division
/// always returns a double.
extension DoublePointExtension on Point<double> {
/// Subtract [other] from this Point.
@Deprecated('camera.pixelOrigin is now a Point<double>. Prefer operator-.')
Point<double> subtract(Point<num> other) {
return Point(x - other.x, y - other.y);
}

/// Add [other] to this Point.
Point<double> add(Point<num> other) {
return Point(x + other.x, y + other.y);
}

/// Create a new [Point] where [x] and [y] values are scaled by the respective
/// values in [other].
Point<double> scaleBy(Point<num> other) {
return Point<double>(x * other.x, y * other.y);
}
}

/// This extension contains methods which, if defined on Point<T extends num>,
/// could cause a runtime error when called on a Point<int> with a non-int
/// argument. An example:
///
/// Point<int>(1, 2).subtract(1.5) would cause a runtime error because the
/// resulting x/y values are doubles and the return value is a Point<int> since
/// the method returns Point<T>.
///
/// The methods in this extension only take Point<int> arguments to prevent
/// this.
extension IntegerPointExtension on Point<int> {
/// Subtract [other] from this Point.
Point<int> subtract(Point<int> other) {
return Point(x - other.x, y - other.y);
}

/// Add [other] to this Point.
Point<int> add(Point<int> other) {
return Point(x + other.x, y + other.y);
}

/// Create a new [Point] where [x] and [y] values are scaled by the respective
/// values in [other].
Point<int> scaleBy(Point<int> other) {
return Point<int>(x * other.x, y * other.y);
}
}

extension OffsetToPointExtension on Offset {
Expand Down

0 comments on commit 36f3658

Please sign in to comment.