Skip to content

Commit

Permalink
feat: option to disable gestures (josxha#44)
Browse files Browse the repository at this point in the history
- [x] update example app
- [x] web implementation
- [x] android implementation
  • Loading branch information
josxha authored Sep 24, 2024
1 parent 0b8286f commit de370a8
Show file tree
Hide file tree
Showing 21 changed files with 934 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.josxha.maplibre

import CameraChangeReason
import LngLat
import LngLatBounds
import MapCamera
Expand Down Expand Up @@ -71,7 +72,7 @@ class MapLibreMapController(
private var style: Style? = null

init {
val channelSuffix = viewId.toString();
val channelSuffix = viewId.toString()
MapLibreHostApi.setUp(binaryMessenger, this, channelSuffix)
flutterApi = MapLibreFlutterApi(binaryMessenger, channelSuffix)
flutterApi.getOptions { result: Result<MapOptions> ->
Expand Down Expand Up @@ -165,7 +166,7 @@ class MapLibreMapController(
if (bearing != null) camera.bearing(bearing)
val cameraUpdate = CameraUpdateFactory.newCameraPosition(camera.build())
mapLibreMap.moveCamera(cameraUpdate)
callback(Result.success(Unit));
callback(Result.success(Unit))
}

override fun flyTo(
Expand Down Expand Up @@ -198,7 +199,7 @@ class MapLibreMapController(
lat: Double,
callback: (Result<ScreenLocation>) -> Unit
) {
val location = mapLibreMap.projection.toScreenLocation(LatLng(lat, lng));
val location = mapLibreMap.projection.toScreenLocation(LatLng(lat, lng))
callback(Result.success(ScreenLocation(location.x.toDouble(), location.y.toDouble())))
}

Expand Down Expand Up @@ -592,5 +593,22 @@ class MapLibreMapController(
)
mapLibreMap.setLatLngBoundsForCameraTarget(bounds)
}

// gestures
if (options.gestures.rotate != mapOptions.gestures.rotate) {
mapLibreMap.uiSettings.isRotateGesturesEnabled = options.gestures.rotate
}
if (options.gestures.pan != mapOptions.gestures.pan) {
mapLibreMap.uiSettings.isRotateGesturesEnabled = options.gestures.pan
}
if (options.gestures.zoom != mapOptions.gestures.zoom) {
mapLibreMap.uiSettings.isZoomGesturesEnabled = options.gestures.zoom
mapLibreMap.uiSettings.isDoubleTapGesturesEnabled = options.gestures.zoom
mapLibreMap.uiSettings.isScrollGesturesEnabled = options.gestures.zoom
mapLibreMap.uiSettings.isQuickZoomGesturesEnabled = options.gestures.zoom
}
if (options.gestures.tilt != mapOptions.gestures.tilt) {
mapLibreMap.uiSettings.isTiltGesturesEnabled = options.gestures.tilt
}
}
}
66 changes: 57 additions & 9 deletions android/src/main/kotlin/com/github/josxha/maplibre/Pigeon.g.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ data class MapOptions (
/** If the native map should listen to click events. */
val listensOnClick: Boolean,
/** If the native map should listen to long click events. */
val listensOnLongClick: Boolean
val listensOnLongClick: Boolean,
/** The map gestures. */
val gestures: MapGestures
)
{
companion object {
Expand All @@ -141,7 +143,8 @@ data class MapOptions (
val maxTilt = pigeonVar_list[9] as Double
val listensOnClick = pigeonVar_list[10] as Boolean
val listensOnLongClick = pigeonVar_list[11] as Boolean
return MapOptions(style, zoom, tilt, bearing, center, maxBounds, minZoom, maxZoom, minTilt, maxTilt, listensOnClick, listensOnLongClick)
val gestures = pigeonVar_list[12] as MapGestures
return MapOptions(style, zoom, tilt, bearing, center, maxBounds, minZoom, maxZoom, minTilt, maxTilt, listensOnClick, listensOnLongClick, gestures)
}
}
fun toList(): List<Any?> {
Expand All @@ -158,6 +161,42 @@ data class MapOptions (
maxTilt,
listensOnClick,
listensOnLongClick,
gestures,
)
}
}

/**
* Map gestures
*
* Generated class from Pigeon that represents data sent in messages.
*/
data class MapGestures (
/** Rotate the map bearing. */
val rotate: Boolean,
/** Move the center of the map around. */
val pan: Boolean,
/** Zoom the map in and out. */
val zoom: Boolean,
/** Tilt (pitch) the map camera. */
val tilt: Boolean
)
{
companion object {
fun fromList(pigeonVar_list: List<Any?>): MapGestures {
val rotate = pigeonVar_list[0] as Boolean
val pan = pigeonVar_list[1] as Boolean
val zoom = pigeonVar_list[2] as Boolean
val tilt = pigeonVar_list[3] as Boolean
return MapGestures(rotate, pan, zoom, tilt)
}
}
fun toList(): List<Any?> {
return listOf(
rotate,
pan,
zoom,
tilt,
)
}
}
Expand Down Expand Up @@ -302,20 +341,25 @@ private open class PigeonPigeonCodec : StandardMessageCodec() {
}
133.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
LngLat.fromList(it)
MapGestures.fromList(it)
}
}
134.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
ScreenLocation.fromList(it)
LngLat.fromList(it)
}
}
135.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
MapCamera.fromList(it)
ScreenLocation.fromList(it)
}
}
136.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
MapCamera.fromList(it)
}
}
137.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
LngLatBounds.fromList(it)
}
Expand All @@ -341,22 +385,26 @@ private open class PigeonPigeonCodec : StandardMessageCodec() {
stream.write(132)
writeValue(stream, value.toList())
}
is LngLat -> {
is MapGestures -> {
stream.write(133)
writeValue(stream, value.toList())
}
is ScreenLocation -> {
is LngLat -> {
stream.write(134)
writeValue(stream, value.toList())
}
is MapCamera -> {
is ScreenLocation -> {
stream.write(135)
writeValue(stream, value.toList())
}
is LngLatBounds -> {
is MapCamera -> {
stream.write(136)
writeValue(stream, value.toList())
}
is LngLatBounds -> {
stream.write(137)
writeValue(stream, value.toList())
}
else -> super.writeValue(stream, value)
}
}
Expand Down
68 changes: 68 additions & 0 deletions example/lib/gestures_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'package:flutter/material.dart';
import 'package:maplibre/maplibre.dart';

@immutable
class GesturesPage extends StatefulWidget {
const GesturesPage({super.key});

static const location = '/gestures';

@override
State<GesturesPage> createState() => _GesturesPageState();
}

class _GesturesPageState extends State<GesturesPage> {
final _selections = Map.fromEntries(
Gestures.values.map((e) => MapEntry(e, false)),
);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Gestures')),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(8),
child: Wrap(
spacing: 8,
runSpacing: 8,
children: Gestures.values
.map(
(e) => ChoiceChip(
label: Text(e.name),
selected: _selections[e]!,
onSelected: (value) => setState(() {
_selections[e] = !_selections[e]!;
}),
),
)
.toList(growable: false),
),
),
Expanded(
child: MapLibreMap(
options: MapOptions(
center: Position(9.17, 47.68),
zoom: 3,
gestures: MapGestures(
rotate: _selections[Gestures.rotate]!,
pan: _selections[Gestures.pan]!,
zoom: _selections[Gestures.zoom]!,
pitch: _selections[Gestures.tilt]!,
),
),
),
),
],
),
);
}
}

enum Gestures {
rotate,
pan,
zoom,
tilt;
}
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:maplibre_example/animation_page.dart';
import 'package:maplibre_example/annotations_page.dart';
import 'package:maplibre_example/controller_page.dart';
import 'package:maplibre_example/events_page.dart';
import 'package:maplibre_example/gestures_page.dart';
import 'package:maplibre_example/kiosk_page.dart';
import 'package:maplibre_example/layers_circle_page.dart';
import 'package:maplibre_example/layers_fill_extrusion_page.dart';
Expand Down Expand Up @@ -35,6 +36,7 @@ class MyApp extends StatelessWidget {
MenuPage.location: (context) => const MenuPage(),
KioskPage.location: (context) => const KioskPage(),
AnimationPage.location: (context) => const AnimationPage(),
GesturesPage.location: (context) => const GesturesPage(),
EventsPage.location: (context) => const EventsPage(),
StyledMapPage.location: (context) => const StyledMapPage(),
LayersSymbolPage.location: (context) => const LayersSymbolPage(),
Expand Down
6 changes: 6 additions & 0 deletions example/lib/menu_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:maplibre_example/animation_page.dart';
import 'package:maplibre_example/annotations_page.dart';
import 'package:maplibre_example/controller_page.dart';
import 'package:maplibre_example/events_page.dart';
import 'package:maplibre_example/gestures_page.dart';
import 'package:maplibre_example/layers_circle_page.dart';
import 'package:maplibre_example/layers_fill_extrusion_page.dart';
import 'package:maplibre_example/layers_fill_page.dart';
Expand Down Expand Up @@ -48,6 +49,11 @@ class MenuPage extends StatelessWidget {
iconData: Icons.build,
location: ParametersPage.location,
),
ItemCard(
label: 'Gestures',
iconData: Icons.back_hand,
location: GesturesPage.location,
),
ItemCard(
label: 'Events',
iconData: Icons.notifications,
Expand Down
Loading

0 comments on commit de370a8

Please sign in to comment.