forked from fleaflet/flutter_map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgestures.dart
811 lines (682 loc) · 25.2 KB
/
gestures.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
import 'dart:async';
import 'dart:math' as math;
import 'dart:math';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/gestures/interactive_flag.dart';
import 'package:flutter_map/src/gestures/latlng_tween.dart';
import 'package:flutter_map/src/map/map.dart';
import 'package:latlong2/latlong.dart';
import 'package:positioned_tap_detector_2/positioned_tap_detector_2.dart';
abstract class MapGestureMixin extends State<FlutterMap>
with TickerProviderStateMixin {
static const double _kMinFlingVelocity = 800.0;
var _dragMode = false;
var _gestureWinner = MultiFingerGesture.none;
var _pointerCounter = 0;
void savePointer(PointerEvent event) => ++_pointerCounter;
void removePointer(PointerEvent event) => --_pointerCounter;
void onPointerSignal(PointerSignalEvent pointerSignal) {
// Handle mouse scroll events if the enableScrollWheel parameter is enabled
if (pointerSignal is PointerScrollEvent &&
mapState.options.enableScrollWheel) {
if (pointerSignal.scrollDelta.dy != 0) {
_debounce(() {
// Check whether the mouse is scrolled down and calculate new zoom level
final delta = pointerSignal.scrollDelta.dy > 0 ? -0.25 : 0.25;
print(pointerSignal.scrollDelta.dy == 0);
final zoom = delta > 0
? min(mapState.options.maxZoom ?? 19, mapState.zoom + delta)
: max(mapState.options.minZoom ?? 0, mapState.zoom + delta);
print('zoom: $zoom');
final newZoom = mapState.fitZoomToBounds(zoom);
// Move the map to the new zoom level
mapState.move(mapState.center, newZoom,
source: MapEventSource.custom);
});
}
}
}
Map timeouts = {};
void _debounce(void Function() callback) {
if (timeouts.containsKey(callback)) {
timeouts[callback].cancel();
}
final timer = Timer(const Duration(milliseconds: 150), () {
callback();
});
timeouts[callback] = timer;
}
var _rotationStarted = false;
var _pinchZoomStarted = false;
var _pinchMoveStarted = false;
var _dragStarted = false;
var _flingAnimationStarted = false;
// Helps to reset ScaleUpdateDetails.scale back to 1.0 when a multi finger
// gesture wins
late double _scaleCorrector;
late double _lastRotation;
late double _lastScale;
late Offset _lastFocalLocal;
late LatLng _mapCenterStart;
late double _mapZoomStart;
late Offset _focalStartLocal;
late final AnimationController _flingController;
late Animation<Offset> _flingAnimation;
late final AnimationController _doubleTapController;
late Animation _doubleTapZoomAnimation;
late Animation _doubleTapCenterAnimation;
StreamSubscription<MapEvent>? _mapControllerAnimationInterruption;
int _tapUpCounter = 0;
Timer? _doubleTapHoldMaxDelay;
@override
FlutterMap get widget;
MapState get mapState;
MapController get mapController;
MapOptions get options;
@override
void initState() {
super.initState();
_flingController = AnimationController(vsync: this)
..addListener(_handleFlingAnimation)
..addStatusListener(_flingAnimationStatusListener);
_doubleTapController =
AnimationController(vsync: this, duration: Duration(milliseconds: 200))
..addListener(_handleDoubleTapZoomAnimation)
..addStatusListener(_doubleTapZoomStatusListener);
}
@override
void didUpdateWidget(FlutterMap oldWidget) {
super.didUpdateWidget(oldWidget);
final oldFlags = oldWidget.options.interactiveFlags;
final flags = options.interactiveFlags;
final oldGestures =
_getMultiFingerGestureFlags(mapOptions: oldWidget.options);
final gestures = _getMultiFingerGestureFlags();
if (flags != oldFlags || gestures != oldGestures) {
var emitMapEventMoveEnd = false;
if (!InteractiveFlag.hasFlag(flags, InteractiveFlag.flingAnimation)) {
closeFlingAnimationController(MapEventSource.interactiveFlagsChanged);
}
if (!InteractiveFlag.hasFlag(flags, InteractiveFlag.doubleTapZoom)) {
closeDoubleTapController(MapEventSource.interactiveFlagsChanged);
}
if (_rotationStarted &&
!(InteractiveFlag.hasFlag(flags, InteractiveFlag.rotate) &&
MultiFingerGesture.hasFlag(
gestures, MultiFingerGesture.rotate))) {
_rotationStarted = false;
if (_gestureWinner == MultiFingerGesture.rotate) {
_gestureWinner = MultiFingerGesture.none;
}
mapState.emitMapEvent(
MapEventRotateEnd(
center: mapState.center,
zoom: mapState.zoom,
source: MapEventSource.interactiveFlagsChanged,
),
);
}
if (_pinchZoomStarted &&
!(InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchZoom) &&
MultiFingerGesture.hasFlag(
gestures, MultiFingerGesture.pinchZoom))) {
_pinchZoomStarted = false;
emitMapEventMoveEnd = true;
if (_gestureWinner == MultiFingerGesture.pinchZoom) {
_gestureWinner = MultiFingerGesture.none;
}
}
if (_pinchMoveStarted &&
!(InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchMove) &&
MultiFingerGesture.hasFlag(
gestures, MultiFingerGesture.pinchMove))) {
_pinchMoveStarted = false;
emitMapEventMoveEnd = true;
if (_gestureWinner == MultiFingerGesture.pinchMove) {
_gestureWinner = MultiFingerGesture.none;
}
}
if (_dragStarted &&
!InteractiveFlag.hasFlag(flags, InteractiveFlag.drag)) {
_dragStarted = false;
emitMapEventMoveEnd = true;
}
if (emitMapEventMoveEnd) {
mapState.emitMapEvent(
MapEventRotateEnd(
center: mapState.center,
zoom: mapState.zoom,
source: MapEventSource.interactiveFlagsChanged,
),
);
}
}
}
void _yieldMultiFingerGestureWinner(
int gestureWinner, bool resetStartVariables) {
_gestureWinner = gestureWinner;
if (resetStartVariables) {
// note: here we could reset to current values instead of last values
_scaleCorrector = 1.0 - _lastScale;
}
}
int _getMultiFingerGestureFlags(
{int? gestureWinner, MapOptions? mapOptions}) {
gestureWinner ??= _gestureWinner;
mapOptions ??= options;
if (mapOptions.enableMultiFingerGestureRace) {
if (gestureWinner == MultiFingerGesture.pinchZoom) {
return mapOptions.pinchZoomWinGestures;
} else if (gestureWinner == MultiFingerGesture.rotate) {
return mapOptions.rotationWinGestures;
} else if (gestureWinner == MultiFingerGesture.pinchMove) {
return mapOptions.pinchMoveWinGestures;
}
return MultiFingerGesture.none;
} else {
return MultiFingerGesture.all;
}
}
void closeFlingAnimationController(MapEventSource source) {
_flingAnimationStarted = false;
if (_flingController.isAnimating) {
_flingController.stop();
_stopListeningForAnimationInterruptions();
mapState.emitMapEvent(
MapEventFlingAnimationEnd(
center: mapState.center, zoom: mapState.zoom, source: source),
);
}
}
void closeDoubleTapController(MapEventSource source) {
if (_doubleTapController.isAnimating) {
_doubleTapController.stop();
_stopListeningForAnimationInterruptions();
mapState.emitMapEvent(
MapEventDoubleTapZoomEnd(
center: mapState.center, zoom: mapState.zoom, source: source),
);
}
}
void handleScaleStart(ScaleStartDetails details) {
_dragMode = _pointerCounter == 1;
final eventSource = _dragMode
? MapEventSource.dragStart
: MapEventSource.multiFingerGestureStart;
closeFlingAnimationController(eventSource);
closeDoubleTapController(eventSource);
_gestureWinner = MultiFingerGesture.none;
_mapZoomStart = mapState.zoom;
_mapCenterStart = mapState.center;
_focalStartLocal = _lastFocalLocal = details.localFocalPoint;
_dragStarted = false;
_pinchZoomStarted = false;
_pinchMoveStarted = false;
_rotationStarted = false;
_lastRotation = 0.0;
_scaleCorrector = 0.0;
_lastScale = 1.0;
}
void handleScaleUpdate(ScaleUpdateDetails details) {
if (_tapUpCounter == 1) {
_handleDoubleTapHold(details);
return;
}
final eventSource =
_dragMode ? MapEventSource.onDrag : MapEventSource.onMultiFinger;
final flags = options.interactiveFlags;
final focalOffset = details.localFocalPoint;
final currentRotation = radianToDeg(details.rotation);
if (_dragMode) {
if (InteractiveFlag.hasFlag(flags, InteractiveFlag.drag)) {
if (!_dragStarted) {
// We could emit start event at [handleScaleStart], however it is
// possible drag will be disabled during ongoing drag then
// [didUpdateWidget] will emit MapEventMoveEnd and if drag is enabled
// again then this will emit the start event again.
_dragStarted = true;
mapState.emitMapEvent(
MapEventMoveStart(
center: mapState.center,
zoom: mapState.zoom,
source: eventSource,
),
);
}
final oldCenterPt = mapState.project(mapState.center, mapState.zoom);
var localDistanceOffset = _rotateOffset(_lastFocalLocal - focalOffset);
final newCenterPt = oldCenterPt + _offsetToPoint(localDistanceOffset);
final newCenter = mapState.unproject(newCenterPt, mapState.zoom);
mapState.move(
newCenter,
mapState.zoom,
hasGesture: true,
source: eventSource,
);
}
} else {
final hasIntPinchMove =
InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchMove);
final hasIntPinchZoom =
InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchZoom);
final hasIntRotate =
InteractiveFlag.hasFlag(flags, InteractiveFlag.rotate);
if (hasIntPinchMove || hasIntPinchZoom || hasIntRotate) {
final hasGestureRace = options.enableMultiFingerGestureRace;
if (hasGestureRace && _gestureWinner == MultiFingerGesture.none) {
if (hasIntPinchZoom &&
(_getZoomForScale(_mapZoomStart, details.scale) - _mapZoomStart)
.abs() >=
options.pinchZoomThreshold) {
if (options.debugMultiFingerGestureWinner) {
print('Multi Finger Gesture winner: Pinch Zoom');
}
_yieldMultiFingerGestureWinner(MultiFingerGesture.pinchZoom, true);
} else if (hasIntRotate &&
currentRotation.abs() >= options.rotationThreshold) {
if (options.debugMultiFingerGestureWinner) {
print('Multi Finger Gesture winner: Rotate');
}
_yieldMultiFingerGestureWinner(MultiFingerGesture.rotate, true);
} else if (hasIntPinchMove &&
(_focalStartLocal - focalOffset).distance >=
options.pinchMoveThreshold) {
if (options.debugMultiFingerGestureWinner) {
print('Multi Finger Gesture winner: Pinch Move');
}
_yieldMultiFingerGestureWinner(MultiFingerGesture.pinchMove, true);
}
}
if (!hasGestureRace || _gestureWinner != MultiFingerGesture.none) {
final gestures = _getMultiFingerGestureFlags();
final hasGesturePinchMove = MultiFingerGesture.hasFlag(
gestures, MultiFingerGesture.pinchMove);
final hasGesturePinchZoom = MultiFingerGesture.hasFlag(
gestures, MultiFingerGesture.pinchZoom);
final hasGestureRotate =
MultiFingerGesture.hasFlag(gestures, MultiFingerGesture.rotate);
final hasMove = hasIntPinchMove && hasGesturePinchMove;
final hasZoom = hasIntPinchZoom && hasGesturePinchZoom;
final hasRotate = hasIntRotate && hasGestureRotate;
var mapMoved = false;
var mapRotated = false;
if (hasMove || hasZoom) {
double newZoom;
if (hasZoom) {
newZoom = _getZoomForScale(
_mapZoomStart, details.scale + _scaleCorrector);
if (!_pinchZoomStarted) {
if (newZoom != _mapZoomStart) {
_pinchZoomStarted = true;
if (!_pinchMoveStarted) {
// emit MoveStart event only if pinchMove hasn't started
mapState.emitMapEvent(
MapEventMoveStart(
center: mapState.center,
zoom: mapState.zoom,
source: eventSource,
),
);
}
}
}
} else {
newZoom = mapState.zoom;
}
LatLng newCenter;
if (hasMove) {
if (!_pinchMoveStarted && _lastFocalLocal != focalOffset) {
_pinchMoveStarted = true;
if (!_pinchZoomStarted) {
// emit MoveStart event only if pinchZoom hasn't started
mapState.emitMapEvent(
MapEventMoveStart(
center: mapState.center,
zoom: mapState.zoom,
source: eventSource,
),
);
}
}
if (_pinchMoveStarted) {
final oldCenterPt = mapState.project(mapState.center, newZoom);
final localDistanceOffset =
_rotateOffset(_lastFocalLocal - focalOffset);
final newCenterPt =
oldCenterPt + _offsetToPoint(localDistanceOffset);
newCenter = mapState.unproject(newCenterPt, newZoom);
} else {
newCenter = mapState.center;
}
} else {
newCenter = mapState.center;
}
if (_pinchZoomStarted || _pinchMoveStarted) {
mapMoved = mapState.move(
newCenter,
newZoom,
hasGesture: true,
callOnMoveSink: false,
source: eventSource,
);
}
}
if (hasRotate) {
if (!_rotationStarted && currentRotation != 0.0) {
_rotationStarted = true;
mapState.emitMapEvent(
MapEventRotateStart(
center: mapState.center,
zoom: mapState.zoom,
source: eventSource,
),
);
}
if (_rotationStarted) {
final rotationDiff = currentRotation - _lastRotation;
mapRotated = mapState.rotate(
mapState.rotation + rotationDiff,
hasGesture: true,
callOnMoveSink: false,
source: eventSource,
);
}
}
if (mapMoved || mapRotated) {
mapState.rebuildLayers();
}
}
}
}
_lastRotation = currentRotation;
_lastScale = details.scale;
_lastFocalLocal = focalOffset;
}
void handleScaleEnd(ScaleEndDetails details) {
_resetDoubleTapHold();
if (!options.allowPanning) {
return;
}
final eventSource =
_dragMode ? MapEventSource.dragEnd : MapEventSource.multiFingerEnd;
if (_rotationStarted) {
_rotationStarted = false;
mapState.emitMapEvent(
MapEventRotateEnd(
center: mapState.center,
zoom: mapState.zoom,
source: eventSource,
),
);
}
if (_dragStarted || _pinchZoomStarted || _pinchMoveStarted) {
_dragStarted = _pinchZoomStarted = _pinchMoveStarted = false;
mapState.emitMapEvent(
MapEventMoveEnd(
center: mapState.center,
zoom: mapState.zoom,
source: eventSource,
),
);
}
var hasFling = InteractiveFlag.hasFlag(
options.interactiveFlags, InteractiveFlag.flingAnimation);
var magnitude = details.velocity.pixelsPerSecond.distance;
if (magnitude < _kMinFlingVelocity || !hasFling) {
if (hasFling) {
mapState.emitMapEvent(
MapEventFlingAnimationNotStarted(
center: mapState.center,
zoom: mapState.zoom,
source: eventSource,
),
);
}
return;
}
var direction = details.velocity.pixelsPerSecond / magnitude;
var distance = (Offset.zero &
Size(mapState.originalSize!.x as double,
mapState.originalSize!.y as double))
.shortestSide;
var _flingOffset = _focalStartLocal - _lastFocalLocal;
_flingAnimation = Tween<Offset>(
begin: _flingOffset,
end: _flingOffset - direction * distance,
).animate(_flingController);
_flingController
..value = 0.0
..fling(
velocity: magnitude / 1000.0,
springDescription: SpringDescription.withDampingRatio(
mass: 1.0,
stiffness: 1000.0,
ratio: 5.0,
));
}
void handleTap(TapPosition position) {
closeFlingAnimationController(MapEventSource.tap);
closeDoubleTapController(MapEventSource.tap);
final latlng = _offsetToCrs(position.relative!);
if (options.onTap != null) {
// emit the event
options.onTap!(position, latlng);
}
mapState.emitMapEvent(
MapEventTap(
tapPosition: latlng,
center: mapState.center,
zoom: mapState.zoom,
source: MapEventSource.tap,
),
);
}
void handleLongPress(TapPosition position) {
_resetDoubleTapHold();
closeFlingAnimationController(MapEventSource.longPress);
closeDoubleTapController(MapEventSource.longPress);
final latlng = _offsetToCrs(position.relative!);
if (options.onLongPress != null) {
// emit the event
options.onLongPress!(position, latlng);
}
mapState.emitMapEvent(
MapEventLongPress(
tapPosition: latlng,
center: mapState.center,
zoom: mapState.zoom,
source: MapEventSource.longPress,
),
);
}
LatLng _offsetToCrs(Offset offset) {
final focalStartPt = mapState.project(mapState.center, mapState.zoom);
final point = (_offsetToPoint(offset) - (mapState.originalSize! / 2.0))
.rotate(mapState.rotationRad);
var newCenterPt = focalStartPt + point;
return mapState.unproject(newCenterPt, mapState.zoom);
}
void handleDoubleTap(TapPosition tapPosition) {
_resetDoubleTapHold();
if (!options.allowPanning) {
return;
}
closeFlingAnimationController(MapEventSource.doubleTap);
closeDoubleTapController(MapEventSource.doubleTap);
if (InteractiveFlag.hasFlag(
options.interactiveFlags, InteractiveFlag.doubleTapZoom)) {
final centerPos = _pointToOffset(mapState.originalSize!) / 2.0;
final newZoom = _getZoomForScale(mapState.zoom, 2.0);
final focalDelta = _getDoubleTapFocalDelta(
centerPos, tapPosition.relative!, newZoom - mapState.zoom);
final newCenter = _offsetToCrs(centerPos + focalDelta);
_startDoubleTapAnimation(newZoom, newCenter);
}
}
Offset _getDoubleTapFocalDelta(
Offset centerPos, Offset tapPos, double zoomDiff) {
final tapDelta = tapPos - centerPos;
final zoomScale = 1 / math.pow(2, zoomDiff);
// The map center offset within which double-tap won't cause zooming to
// previously invisible area
final maxDelta = centerPos * (1 - zoomScale);
final tappedOutExtent =
tapDelta.dx.abs() > maxDelta.dx || tapDelta.dy.abs() > maxDelta.dy;
return tappedOutExtent
? _projectDeltaOnBounds(tapDelta, maxDelta)
: tapDelta;
}
Offset _projectDeltaOnBounds(Offset delta, Offset maxDelta) {
final weightX = delta.dx.abs() / maxDelta.dx;
final weightY = delta.dy.abs() / maxDelta.dy;
return delta / math.max(weightX, weightY);
}
void _startDoubleTapAnimation(double newZoom, LatLng newCenter) {
_doubleTapZoomAnimation = Tween<double>(begin: mapState.zoom, end: newZoom)
.chain(CurveTween(curve: Curves.fastOutSlowIn))
.animate(_doubleTapController);
_doubleTapCenterAnimation =
LatLngTween(begin: mapState.center, end: newCenter)
.chain(CurveTween(curve: Curves.fastOutSlowIn))
.animate(_doubleTapController);
_doubleTapController.forward(from: 0.0);
}
void _doubleTapZoomStatusListener(AnimationStatus status) {
if (status == AnimationStatus.forward) {
mapState.emitMapEvent(
MapEventDoubleTapZoomStart(
center: mapState.center,
zoom: mapState.zoom,
source: MapEventSource.doubleTapZoomAnimationController),
);
_startListeningForAnimationInterruptions();
} else if (status == AnimationStatus.completed) {
_stopListeningForAnimationInterruptions();
mapState.emitMapEvent(
MapEventDoubleTapZoomEnd(
center: mapState.center,
zoom: mapState.zoom,
source: MapEventSource.doubleTapZoomAnimationController),
);
}
}
void _handleDoubleTapZoomAnimation() {
mapState.move(
_doubleTapCenterAnimation.value,
_doubleTapZoomAnimation.value,
hasGesture: true,
source: MapEventSource.doubleTapZoomAnimationController,
);
}
void handleOnTapUp(TapUpDetails details) {
_doubleTapHoldMaxDelay?.cancel();
if (++_tapUpCounter == 1) {
_doubleTapHoldMaxDelay =
Timer(const Duration(milliseconds: 350), _resetDoubleTapHold);
}
}
void _handleDoubleTapHold(ScaleUpdateDetails details) {
_doubleTapHoldMaxDelay?.cancel();
var flags = options.interactiveFlags;
// TODO: is this pinchZoom? never seen this fired
if (InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchZoom)) {
final zoom = mapState.zoom;
final focalOffset = details.localFocalPoint;
final verticalOffset = (_focalStartLocal - focalOffset).dy;
final newZoom = _mapZoomStart - verticalOffset / 360 * zoom;
final min = options.minZoom ?? 0.0;
final max = options.maxZoom ?? double.infinity;
final actualZoom = math.max(min, math.min(max, newZoom));
mapState.move(
mapState.center,
actualZoom,
hasGesture: true,
source: MapEventSource.doubleTapHold,
);
}
}
void _resetDoubleTapHold() {
_doubleTapHoldMaxDelay?.cancel();
_tapUpCounter = 0;
}
void _flingAnimationStatusListener(AnimationStatus status) {
if (status == AnimationStatus.completed) {
_flingAnimationStarted = false;
_stopListeningForAnimationInterruptions();
mapState.emitMapEvent(
MapEventFlingAnimationEnd(
center: mapState.center,
zoom: mapState.zoom,
source: MapEventSource.flingAnimationController),
);
}
}
void _handleFlingAnimation() {
if (!_flingAnimationStarted) {
_flingAnimationStarted = true;
mapState.emitMapEvent(
MapEventFlingAnimationStart(
center: mapState.center,
zoom: mapState.zoom,
source: MapEventSource.flingAnimationController),
);
_startListeningForAnimationInterruptions();
}
var newCenterPoint = mapState.project(_mapCenterStart) +
_offsetToPoint(_flingAnimation.value).rotate(mapState.rotationRad);
var newCenter = mapState.unproject(newCenterPoint);
mapState.move(
newCenter,
mapState.zoom,
hasGesture: true,
source: MapEventSource.flingAnimationController,
);
}
void _startListeningForAnimationInterruptions() {
if (_mapControllerAnimationInterruption != null) return;
// cancel map animation controllers on map controller move events
_mapControllerAnimationInterruption = mapController.mapEventStream.where(
(event) => event.source == MapEventSource.mapController &&
event is MapEventMove
).listen(_handleAnimationInterruptions);
}
void _stopListeningForAnimationInterruptions() {
_mapControllerAnimationInterruption?.cancel();
_mapControllerAnimationInterruption = null;
}
void _handleAnimationInterruptions(MapEvent event) {
closeDoubleTapController(event.source);
closeFlingAnimationController(event.source);
}
CustomPoint _offsetToPoint(Offset offset) {
return CustomPoint(offset.dx, offset.dy);
}
Offset _pointToOffset(CustomPoint point) {
return Offset(point.x.toDouble(), point.y.toDouble());
}
double _getZoomForScale(double startZoom, double scale) {
var resultZoom =
scale == 1.0 ? startZoom : startZoom + math.log(scale) / math.ln2;
return mapState.fitZoomToBounds(resultZoom);
}
Offset _rotateOffset(Offset offset) {
final radians = mapState.rotationRad;
if (radians != 0.0) {
final cos = math.cos(radians);
final sin = math.sin(radians);
final nx = (cos * offset.dx) + (sin * offset.dy);
final ny = (cos * offset.dy) - (sin * offset.dx);
return Offset(nx, ny);
}
return offset;
}
@override
void dispose() {
_flingController.dispose();
_doubleTapController.dispose();
super.dispose();
}
}