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

Enable linear easing on map camera #1281

Merged
merged 8 commits into from
Apr 18, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ public static CameraStop fromReadableMap(Context context, @NonNull ReadableMap r
case CameraMode.FLIGHT:
stop.setMode(CameraMode.FLIGHT);
break;
case CameraMode.LINEAR:
stop.setMode(CameraMode.LINEAR);
break;
case CameraMode.NONE:
stop.setMode(CameraMode.NONE);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ public void onFinish() {

if (mCameraMode == CameraMode.FLIGHT) {
map.animateCamera(mCameraUpdate, duration, callback);
} else if (mCameraMode == CameraMode.LINEAR) {
map.easeCamera(mCameraUpdate, duration, false, callback);
} else if (mCameraMode == CameraMode.EASE) {
map.easeCamera(mCameraUpdate, duration, callback);
map.easeCamera(mCameraUpdate, duration, true, callback);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public void onFinish() {
};

if (isAnimated) {
mMapView.easeCamera(cameraUpdate, USER_LOCATION_CAMERA_MOVE_DURATION, callback);
mMapView.easeCamera(cameraUpdate, USER_LOCATION_CAMERA_MOVE_DURATION, true, callback);
} else {
mMapView.moveCamera(cameraUpdate, callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

public class CameraMode {

@IntDef({ FLIGHT, EASE, NONE })
@IntDef({ FLIGHT, EASE, LINEAR, NONE })
@Retention(RetentionPolicy.SOURCE)
public @interface Mode {}

public static final int FLIGHT = 1;
public static final int EASE = 2;
public static final int NONE = 3;
public static final int LINEAR = 3;
public static final int NONE = 4;
}
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ public void moveCamera(CameraUpdate cameraUpdate) {
mMap.moveCamera(cameraUpdate);
}

public void easeCamera(CameraUpdate cameraUpdate, int duration, MapboxMap.CancelableCallback callback) {
mMap.easeCamera(cameraUpdate, duration, callback);
public void easeCamera(CameraUpdate cameraUpdate, int duration, boolean easingInterpolator, MapboxMap.CancelableCallback callback) {
mMap.easeCamera(cameraUpdate, duration, easingInterpolator, callback);
}

public void easeCamera(CameraUpdate cameraUpdate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public Map<String, Object> getConstants() {
Map<String, Integer> cameraModes = new HashMap<>();
cameraModes.put("Flight", CameraMode.FLIGHT);
cameraModes.put("Ease", CameraMode.EASE);
cameraModes.put("Linear", CameraMode.LINEAR);
cameraModes.put("None", CameraMode.NONE);

// style source constants
Expand Down
2 changes: 1 addition & 1 deletion docs/Camera.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| Prop | Type | Default | Required | Description |
| ---- | :--: | :-----: | :------: | :----------: |
| animationDuration | `number` | `2000` | `false` | The duration a camera update takes (in ms) |
| animationMode | `enum` | `'easeTo'` | `false` | The animationstyle when the camara updates. One of; `flyTo`, `easeTo`, `moveTo` |
| animationMode | `enum` | `'easeTo'` | `false` | The animationstyle when the camara updates. One of; `flyTo`, `easeTo`, `linearTo`, `moveTo` |
| defaultSettings | `shape` | `none` | `false` | Default view settings applied on camera |
| &nbsp;&nbsp;centerCoordinate | `array` | `none` | `false` | Center coordinate on map [lng, lat] |
| &nbsp;&nbsp;heading | `number` | `none` | `false` | Heading on map |
Expand Down
2 changes: 1 addition & 1 deletion docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@
"required": false,
"type": "enum",
"default": "'easeTo'",
"description": "The animationstyle when the camara updates. One of; `flyTo`, `easeTo`, `moveTo`"
"description": "The animationstyle when the camara updates. One of; `flyTo`, `easeTo`, `linearTo`, `moveTo`"
},
{
"name": "defaultSettings",
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ export interface MapViewProps extends ViewProps {

export interface CameraProps extends CameraSettings, ViewProps {
animationDuration?: number;
animationMode?: 'flyTo' | 'easeTo' | 'moveTo';
animationMode?: 'flyTo' | 'easeTo' | 'linearTo' | 'moveTo';
defaultSettings?: CameraSettings;
minZoomLevel?: number;
maxZoomLevel?: number;
Expand Down Expand Up @@ -516,7 +516,7 @@ export interface CameraSettings {
};
zoomLevel?: number;
animationDuration?: number;
animationMode?: 'flyTo' | 'easeTo' | 'moveTo';
animationMode?: 'flyTo' | 'easeTo' | 'linearTo' | 'moveTo';
stops?: CameraSettings[];
}

Expand Down
1 change: 1 addition & 0 deletions ios/RCTMGL/CameraMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

extern int const RCT_MAPBOX_CAMERA_MODE_FLIGHT;
extern int const RCT_MAPBOX_CAMERA_MODE_EASE;
extern int const RCT_MAPBOX_CAMERA_MODE_LINEAR;
extern int const RCT_MAPBOX_CAMERA_MODE_NONE;

@end
3 changes: 2 additions & 1 deletion ios/RCTMGL/CameraMode.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ @implementation CameraMode

int const RCT_MAPBOX_CAMERA_MODE_FLIGHT = 1;
int const RCT_MAPBOX_CAMERA_MODE_EASE = 2;
int const RCT_MAPBOX_CAMERA_MODE_NONE = 3;
int const RCT_MAPBOX_CAMERA_MODE_LINEAR = 3;
int const RCT_MAPBOX_CAMERA_MODE_NONE = 4;

@end
2 changes: 2 additions & 0 deletions ios/RCTMGL/CameraStop.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ - (void)setMode:(NSNumber *)mode
_mode = [NSNumber numberWithInt:modeInt];
} else if (modeInt == RCT_MAPBOX_CAMERA_MODE_NONE) {
_mode = [NSNumber numberWithInt:modeInt];
} else if (modeInt == RCT_MAPBOX_CAMERA_MODE_LINEAR) {
_mode = [NSNumber numberWithInt:modeInt];
} else {
_mode = [NSNumber numberWithInt:RCT_MAPBOX_CAMERA_MODE_EASE];
}
Expand Down
11 changes: 7 additions & 4 deletions ios/RCTMGL/CameraUpdateItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ - (void)execute:(RCTMGLMapView *)mapView withCompletionHandler:(void (^)(void))c
if (_cameraStop.mode == [NSNumber numberWithInt:RCT_MAPBOX_CAMERA_MODE_FLIGHT]) {
[self _flyToCamera:mapView withCompletionHandler:completionHandler];
} else if (_cameraStop.mode == [NSNumber numberWithInt:RCT_MAPBOX_CAMERA_MODE_EASE]) {
[self _moveCamera:mapView animated:YES withCompletionHandler:completionHandler];
[self _moveCamera:mapView animated:YES ease:YES withCompletionHandler:completionHandler];
} else if (_cameraStop.mode == [NSNumber numberWithInt:RCT_MAPBOX_CAMERA_MODE_LINEAR]) {
[self _moveCamera:mapView animated:YES ease:NO withCompletionHandler:completionHandler];
} else if ([self _areBoundsValid:_cameraStop.bounds]) {
[self _fitBoundsCamera:mapView withCompletionHandler:completionHandler];
} else {
[self _moveCamera:mapView animated:NO withCompletionHandler:completionHandler];
[self _moveCamera:mapView animated:NO ease:NO withCompletionHandler:completionHandler];
}
}

Expand All @@ -51,16 +53,17 @@ - (void)_flyToCamera:(RCTMGLMapView*)mapView withCompletionHandler:(void (^)(voi
}
}

- (void)_moveCamera:(RCTMGLMapView*)mapView animated:(BOOL)animated withCompletionHandler:(void (^)(void))completionHandler
- (void)_moveCamera:(RCTMGLMapView*)mapView animated:(BOOL)animated ease:(BOOL)ease withCompletionHandler:(void (^)(void))completionHandler
{
if ([self _hasCenterCoordAndZoom]) {
[self _centerCoordWithZoomCamera:mapView animated:animated withCompletionHandler:completionHandler];
} else {
RCTMGLCameraWithPadding *nextCamera = [self _makeCamera:mapView];
NSString *easeFunctionName = ease ? kCAMediaTimingFunctionEaseInEaseOut : kCAMediaTimingFunctionLinear;

[mapView setCamera:nextCamera.camera
withDuration:animated ? _cameraStop.duration : 0
animationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
animationTimingFunction:[CAMediaTimingFunction functionWithName:easeFunctionName]
edgePadding:nextCamera.boundsPadding
completionHandler:completionHandler];
}
Expand Down
1 change: 1 addition & 0 deletions ios/RCTMGL/MGLModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ + (BOOL)requiresMainQueueSetup
NSMutableDictionary *cameraModes = [[NSMutableDictionary alloc] init];
[cameraModes setObject:[NSNumber numberWithInt:RCT_MAPBOX_CAMERA_MODE_FLIGHT] forKey:@"Flight"];
[cameraModes setObject:[NSNumber numberWithInt:RCT_MAPBOX_CAMERA_MODE_EASE] forKey:@"Ease"];
[cameraModes setObject:[NSNumber numberWithInt:RCT_MAPBOX_CAMERA_MODE_LINEAR] forKey:@"Linear"];
[cameraModes setObject:[NSNumber numberWithInt:RCT_MAPBOX_CAMERA_MODE_NONE] forKey:@"None"];

// style sources
Expand Down
7 changes: 5 additions & 2 deletions javascript/components/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ class Camera extends React.Component {
animationDuration: PropTypes.number,

/**
* The animationstyle when the camara updates. One of; `flyTo`, `easeTo`, `moveTo`
* The animationstyle when the camara updates. One of; `flyTo`, `easeTo`, `linearTo`, `moveTo`
*/
animationMode: PropTypes.oneOf(['flyTo', 'easeTo', 'moveTo']),
animationMode: PropTypes.oneOf(['flyTo', 'easeTo', 'linearTo', 'moveTo']),

/**
* Default view settings applied on camera
Expand Down Expand Up @@ -161,6 +161,7 @@ class Camera extends React.Component {
Flight: 'flyTo',
Move: 'moveTo',
Ease: 'easeTo',
Linear: 'linearTo',
};

UNSAFE_componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -503,6 +504,8 @@ class Camera extends React.Component {
return MapboxGL.CameraModes.Flight;
case Camera.Mode.Move:
return MapboxGL.CameraModes.None;
case Camera.Mode.Linear:
return MapboxGL.CameraModes.Linear;
default:
return MapboxGL.CameraModes.Ease;
}
Expand Down