Skip to content

Commit

Permalink
MAPSIOS-1263 Do not overwrite sea level altitude in gestures (#1997)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksproger authored Jan 29, 2024
1 parent ed4a831 commit d83e2e3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Mapbox welcomes participation and contributions from everyone.
* Fix glitch in chained camera animations.
* Build XCFramework with `SWIFT_SERIALIZE_DEBUGGING_OPTIONS=NO` flag to avoid serialized search paths in Swift modules.
* Fixed a crash that occurs when annotations have duplicate identifiers.
* Expose `MapboxMap.centerAltitudeMode` and ensure correct `centerAltitudeMode` on gesture ending.
* Expose extra configuration methods for `MapboxMap`: `setNorthOrientation(_:)`, `setConstrainMode(_:)` and `setViewportMode(_:)`.
Use them to configure respective map options after creating a map view.
* Expose `MapboxMap.reduceMemoryUse()` which can be used in situations when it is important to keep the memory footprint minimal.
Expand Down
40 changes: 29 additions & 11 deletions Sources/MapboxMaps/Foundation/MapboxMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -820,11 +820,26 @@ public final class MapboxMap: StyleManager {
}
}

/// Get/set the map centerAltitudeMode that defines how the map center point should react to terrain elevation changes.
/// See ``MapCenterAltitudeMode`` for options.
public var centerAltitudeMode: MapCenterAltitudeMode {
get { __map.getCenterAltitudeMode() }
set {
__map.setCenterAltitudeModeFor(newValue)
gestureState.intrinsicMode = newValue
}
}

private struct GestureState {
var count: Int
var intrinsicMode: MapCenterAltitudeMode
}

private lazy var gestureState = GestureState(count: 0, intrinsicMode: centerAltitudeMode)

/// Returns `true` if a gesture is currently in progress.
public var isGestureInProgress: Bool { __map.isGestureInProgress() }

private var gestureCount = 0

/// If implementing a custom gesture, call this method when the gesture begins.
///
/// Tells the map rendering engine that there is currently a gesture in progress. This
Expand All @@ -833,22 +848,25 @@ public final class MapboxMap: StyleManager {
///
/// - Note: Must always be paired with a corresponding call to `endGesture()`
public func beginGesture() {
gestureCount += 1
if gestureCount == 1 {
__map.setGestureInProgressForInProgress(true)
__map.setCenterAltitudeModeFor(.sea)
}
gestureState.count += 1
if gestureState.count == 1 { __map.setGestureInProgressForInProgress(true) }

/// We should set the center altitude mode to ``MapCenterAltitudeMode.sea`` during gestures to avoid bumpiness when the terrain is enabled.
/// It's not necessary to update ``MapCenterAltitudeMode`` if the user explicitly changed altitude to ``MapCenterAltitudeMode.sea`` before the gesture starts.
if centerAltitudeMode != .sea { __map.setCenterAltitudeModeFor(.sea) }
}

/// If implementing a custom gesture, call this method when the gesture ends.
///
/// - Note: Must always be paired with a corresponding call to `beginGesture()`.
public func endGesture() {
assert(gestureCount > 0)
gestureCount -= 1
if gestureCount == 0 {
assert(gestureState.count > 0)
gestureState.count -= 1
if gestureState.count == 0 {
__map.setGestureInProgressForInProgress(false)
__map.setCenterAltitudeModeFor(.terrain)

/// After the gesture end we must ensure to set the ``centerAltitudeMode`` expected be the user.
__map.setCenterAltitudeModeFor(gestureState.intrinsicMode)
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions Tests/MapboxMapsTests/Foundation/MapboxMapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,30 +296,49 @@ final class MapboxMapTests: XCTestCase {

func testBeginAndEndGesture() {
XCTAssertFalse(mapboxMap.__testingMap.isGestureInProgress())
XCTAssertEqual(mapboxMap.__testingMap.getCenterAltitudeMode(), .terrain)

mapboxMap.beginGesture()

XCTAssertTrue(mapboxMap.__testingMap.isGestureInProgress())
XCTAssertEqual(mapboxMap.__testingMap.getCenterAltitudeMode(), .sea)

mapboxMap.centerAltitudeMode = .terrain
mapboxMap.beginGesture()

XCTAssertTrue(mapboxMap.__testingMap.isGestureInProgress())
XCTAssertEqual(mapboxMap.__testingMap.getCenterAltitudeMode(), .sea)

mapboxMap.endGesture()

XCTAssertTrue(mapboxMap.__testingMap.isGestureInProgress())
XCTAssertEqual(mapboxMap.__testingMap.getCenterAltitudeMode(), .sea)

mapboxMap.beginGesture()

XCTAssertTrue(mapboxMap.__testingMap.isGestureInProgress())
XCTAssertEqual(mapboxMap.__testingMap.getCenterAltitudeMode(), .sea)

mapboxMap.endGesture()

XCTAssertTrue(mapboxMap.__testingMap.isGestureInProgress())
XCTAssertEqual(mapboxMap.__testingMap.getCenterAltitudeMode(), .sea)

mapboxMap.endGesture()

XCTAssertFalse(mapboxMap.__testingMap.isGestureInProgress())
XCTAssertEqual(mapboxMap.__testingMap.getCenterAltitudeMode(), .terrain)

mapboxMap.beginGesture()

XCTAssertTrue(mapboxMap.__testingMap.isGestureInProgress())
XCTAssertEqual(mapboxMap.__testingMap.getCenterAltitudeMode(), .sea)

mapboxMap.centerAltitudeMode = .sea
mapboxMap.endGesture()

XCTAssertFalse(mapboxMap.__testingMap.isGestureInProgress())
XCTAssertEqual(mapboxMap.__testingMap.getCenterAltitudeMode(), .sea)
}

func testLoadStyleHandlerIsInvokedExactlyOnce() throws {
Expand Down

0 comments on commit d83e2e3

Please sign in to comment.