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

Support desktop (click and drag) rotation #1592

Merged
merged 5 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 60 additions & 2 deletions lib/src/gestures/flutter_map_interactive_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import 'dart:async';
import 'dart:math' as math;

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_map/src/gestures/interactive_flag.dart';
import 'package:flutter_map/src/gestures/latlng_tween.dart';
import 'package:flutter_map/src/gestures/map_events.dart';
Expand All @@ -14,6 +15,17 @@ import 'package:flutter_map/src/misc/point.dart';
import 'package:flutter_map/src/misc/private/positioned_tap_detector_2.dart';
import 'package:latlong2/latlong.dart';

extension on LogicalKeyboardKey {
bool get isControlKey {
return switch (this) {
LogicalKeyboardKey.control => true,
LogicalKeyboardKey.controlLeft => true,
LogicalKeyboardKey.controlRight => true,
_ => false,
};
}
}

/// Applies interactions (gestures/scroll/taps etc) to the current [MapCamera]
/// via the internal [controller].
class FlutterMapInteractiveViewer extends StatefulWidget {
Expand Down Expand Up @@ -80,6 +92,12 @@ class FlutterMapInteractiveViewerState
late Animation<double> _doubleTapZoomAnimation;
late Animation<LatLng> _doubleTapCenterAnimation;

final ctrlPressedNotifier = ValueNotifier<bool>(false);
Offset? initialPointerDownPos;
double cursorRotation = 0;
double clickDegrees = 0;
double dragDegrees = 0;

int _tapUpCounter = 0;
Timer? _doubleTapHoldMaxDelay;

Expand All @@ -100,6 +118,8 @@ class FlutterMapInteractiveViewerState
_doubleTapController
..addListener(_handleDoubleTapZoomAnimation)
..addStatusListener(_doubleTapZoomStatusListener);

ServicesBinding.instance.keyboard.addHandler(keyHandler);
}

void _onMapStateChange() {
Expand All @@ -121,10 +141,19 @@ class FlutterMapInteractiveViewerState
widget.controller.removeListener(_onMapStateChange);
_flingController.dispose();
_doubleTapController.dispose();

ctrlPressedNotifier.dispose();
ServicesBinding.instance.keyboard.removeHandler(keyHandler);
super.dispose();
}

bool keyHandler(KeyEvent event) {
if (event.logicalKey.isControlKey) {
ctrlPressedNotifier.value =
event is KeyDownEvent || event is KeyRepeatEvent;
}
return false;
}

void updateGestures(
InteractionOptions oldOptions,
InteractionOptions newOptions,
Expand Down Expand Up @@ -253,11 +282,15 @@ class FlutterMapInteractiveViewerState

@override
Widget build(BuildContext context) {
clickDegrees = 0;
dragDegrees = 0;

return Listener(
onPointerDown: _onPointerDown,
onPointerUp: _onPointerUp,
onPointerCancel: _onPointerCancel,
onPointerHover: _onPointerHover,
onPointerMove: _onPointerMove,
onPointerSignal: _onPointerSignal,
child: PositionedTapDetector2(
controller: _positionedTapController,
Expand All @@ -283,6 +316,8 @@ class FlutterMapInteractiveViewerState

void _onPointerDown(PointerDownEvent event) {
++_pointerCounter;
clickDegrees =
getCursorRotationDegrees(event.localPosition, context) - cursorRotation;

if (_options.onPointerDown != null) {
final latlng = _camera.offsetToCrs(event.localPosition);
Expand All @@ -292,6 +327,7 @@ class FlutterMapInteractiveViewerState

void _onPointerUp(PointerUpEvent event) {
--_pointerCounter;
cursorRotation = dragDegrees;

if (_options.onPointerUp != null) {
final latlng = _camera.offsetToCrs(event.localPosition);
Expand All @@ -315,6 +351,18 @@ class FlutterMapInteractiveViewerState
}
}

void _onPointerMove(PointerMoveEvent event) {
if (!ctrlPressedNotifier.value) return;

widget.controller.rotate(
dragDegrees =
getCursorRotationDegrees(event.localPosition, context) - clickDegrees,
hasGesture: true,
source: MapEventSource.cursorRotation,
id: null,
);
}

void _onPointerSignal(PointerSignalEvent pointerSignal) {
// Handle mouse scroll events if the enableScrollWheel parameter is enabled
if (pointerSignal is PointerScrollEvent &&
Expand Down Expand Up @@ -366,6 +414,14 @@ class FlutterMapInteractiveViewerState
}
}

// Thanks to https://stackoverflow.com/questions/48916517/javascript-click-and-drag-to-rotate
double getCursorRotationDegrees(Offset offset, BuildContext context) =>
round((math.atan2(offset.dx - MediaQuery.sizeOf(context).width / 2,
offset.dy - MediaQuery.sizeOf(context).height / 2) *
(180 / pi) *
-1) +
100);
JaffaKetchup marked this conversation as resolved.
Show resolved Hide resolved

void _closeFlingAnimationController(MapEventSource source) {
_flingAnimationStarted = false;
if (_flingController.isAnimating) {
Expand Down Expand Up @@ -432,6 +488,8 @@ class FlutterMapInteractiveViewerState
}

void _handleScaleDragUpdate(ScaleUpdateDetails details) {
if (ctrlPressedNotifier.value) return;

const eventSource = MapEventSource.onDrag;

if (InteractiveFlag.hasDrag(_interactionOptions.flags)) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/gestures/map_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum MapEventSource {
custom,
scrollWheel,
nonRotatedSizeChange,
cursorRotation,
}

/// Base event class which is emitted by MapController instance, the event
Expand Down