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

Rework animated marker layer #285

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
5 changes: 1 addition & 4 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,7 @@ class HomeScreen extends View<HomeViewModel> with PromptHandler {
viewModel.uploadQueue.length;
return OsmElementLayer(
elements: viewModel.elements,
currentZoom: viewModel.mapZoomRound,
onOsmElementTap: viewModel.onElementTap,
selectedElement: viewModel.selectedElement,
uploadQueue: viewModel.uploadQueue,
// onSelect: viewModel.onElementTap,
);
},
),
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/completed_area_layer/completed_area_layer.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:latlong2/latlong.dart' hide Path;

import '/widgets/loading_area_layer/map_layer.dart';
import '../map_layer.dart';
import '/widgets/animated_path.dart';


Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/loading_area_layer/loading_area_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:latlong2/latlong.dart';

import 'ripple_indicator.dart';
import 'map_layer.dart';
import '../map_layer.dart';

/// Layer to show ripple animations for geo circles and automatically remove them when expired.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
Expand Down Expand Up @@ -197,6 +199,8 @@ class RenderMapLayer extends RenderBox
mapCamera.nonRotatedSize.y,
);

final occupancyList = <Rect>[];

var child = firstChild;
while (child != null) {
final childParentData = child.parentData! as _MapLayerParentData;
Expand All @@ -205,11 +209,42 @@ class RenderMapLayer extends RenderBox
// only render child if bounds are inside the viewport
if (viewport.overlaps(childRect)) {
context.paintChild(child, relativePixelPosition + offset);

if (childParentData.collider != null) {
final pos = _computeRelativeOffset(childParentData.offset & childParentData.collider!, childParentData.align!);

_handleCollision(pos, childParentData.collider!, occupancyList);
}
}
child = childParentData.nextSibling;
}
}

Offset _computeRelativeOffset(Rect rect, Alignment align) {
var globalPixelPosition = rect.topLeft;
// apply rotation
if (mapCamera.rotation != 0.0) {
globalPixelPosition = mapCamera.rotatePoint(
_pixelMapCenter,
globalPixelPosition.toPoint(),
counterRotation: false,
).toOffset();
}
// apply alignment
return globalPixelPosition - align.alongSize(rect.size) - _nonRotatedPixelOrigin;
}

void _handleCollision(Offset offset, BoxCollider collider, List<Rect> occupancyList) {
final rect = offset & collider;
if (occupancyList.any((box) => box.overlaps(rect))) {
collider.reportCollision(true, postFrame: true);
}
else {
occupancyList.add(rect);
collider.reportCollision(false, postFrame: true);
}
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
Expand All @@ -232,11 +267,14 @@ class MapLayerPositioned extends ParentDataWidget<_MapLayerParentData> {

final Alignment align;

final BoxCollider? collider;

const MapLayerPositioned({
required this.position,
required super.child,
this.align = Alignment.center,
this.size,
this.collider,
super.key,
});

Expand All @@ -263,6 +301,11 @@ class MapLayerPositioned extends ParentDataWidget<_MapLayerParentData> {
parentData.align = align;
targetParent.markNeedsPaint();
}

if (parentData.collider != collider) {
parentData.collider = collider;
targetParent.markNeedsPaint();
}
}

@override
Expand All @@ -283,4 +326,41 @@ class _MapLayerParentData extends ContainerBoxParentData<RenderBox> {
Size? size;

Alignment? align;

BoxCollider? collider;
}











class BoxCollider extends Size with ChangeNotifier implements ValueListenable<bool> {
bool _collision = false;

BoxCollider(super.width, super.height);

// ignore: avoid_positional_boolean_parameters
void reportCollision(bool collision, { bool postFrame = false }) {
if (collision != _collision) {
_collision = collision;

if (postFrame) {
SchedulerBinding.instance.addPostFrameCallback(
(_) => notifyListeners(),
);
}
else {
notifyListeners();
}
}
}

@override
bool get value => _collision;
}
Loading
Loading