Merge pull request #8229 from osmandapp/tilt_gesture_opengl

Tilt gesture opengl
This commit is contained in:
max-klaus 2020-01-14 18:57:30 +03:00 committed by GitHub
commit 1dbcf48f89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 111 additions and 12 deletions

View file

@ -2290,6 +2290,7 @@ public class OsmandSettings {
public static final String LAST_KNOWN_MAP_LAT = "last_known_map_lat"; //$NON-NLS-1$
public static final String LAST_KNOWN_MAP_LON = "last_known_map_lon"; //$NON-NLS-1$
public static final String LAST_KNOWN_MAP_ZOOM = "last_known_map_zoom"; //$NON-NLS-1$
public static final String LAST_KNOWN_MAP_ELEVATION = "last_known_map_elevation"; //$NON-NLS-1$
public static final String MAP_LABEL_TO_SHOW = "map_label_to_show"; //$NON-NLS-1$
public static final String MAP_LAT_TO_SHOW = "map_lat_to_show"; //$NON-NLS-1$
@ -2403,6 +2404,14 @@ public class OsmandSettings {
settingsAPI.edit(globalPreferences).putInt(LAST_KNOWN_MAP_ZOOM, zoom).commit();
}
public float getLastKnownMapElevation() {
return settingsAPI.getFloat(globalPreferences, LAST_KNOWN_MAP_ELEVATION, 90);
}
public void setLastKnownMapElevation(float elevation) {
settingsAPI.edit(globalPreferences).putFloat(LAST_KNOWN_MAP_ELEVATION, elevation).commit();
}
public final static String POINT_NAVIGATE_LAT = "point_navigate_lat"; //$NON-NLS-1$
public final static String POINT_NAVIGATE_LON = "point_navigate_lon"; //$NON-NLS-1$
public final static String POINT_NAVIGATE_ROUTE = "point_navigate_route_integer"; //$NON-NLS-1$

View file

@ -474,7 +474,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
OsmAndMapLayersView ml = (OsmAndMapLayersView) findViewById(R.id.MapLayersView);
ml.setVisibility(View.VISIBLE);
atlasMapRendererView.setAzimuth(0);
atlasMapRendererView.setElevationAngle(90);
atlasMapRendererView.setElevationAngle(app.getSettings().getLastKnownMapElevation());
NativeCoreContext.getMapRendererContext().setMapRendererView(atlasMapRendererView);
ml.setMapView(mapView);
mapViewTrackingUtilities.setMapView(mapView);
@ -488,6 +488,12 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
}
}
public void setMapElevation(float angle) {
if (atlasMapRendererView != null) {
atlasMapRendererView.setElevationAngle(angle);
}
}
private void createProgressBarForRouting() {
final ProgressBar pb = (ProgressBar) findViewById(R.id.map_horizontal_progress);
@ -1490,6 +1496,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
}
settings.setLastKnownMapZoom(mapView.getZoom());
settings.setLastKnownMapElevation(mapView.getElevationAngle());
settings.MAP_ACTIVITY_ENABLED.set(false);
app.getResourceManager().interruptRendering();
OsmandPlugin.onMapActivityPause(this);

View file

@ -5,6 +5,8 @@ import android.graphics.PointF;
import android.view.MotionEvent;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.views.corenative.NativeCoreContext;
import net.osmand.util.MapUtils;
import org.apache.commons.logging.Log;
@ -36,6 +38,10 @@ public class MultiTouchSupport {
public void onActionPointerUp();
public void onActionCancel();
public void onChangingViewAngle(float angle);
public void onChangeViewAngleStarted();
}
private boolean multiTouchAPISupported = false;
@ -62,6 +68,14 @@ public class MultiTouchSupport {
return inZoomMode;
}
public boolean isInTiltMode() {
return inTiltMode;
}
private boolean isTiltSupported() {
return ((OsmandApplication) ctx.getApplicationContext()).getSettings().USE_OPENGL_RENDER.get() && NativeCoreContext.isInit();
}
private void initMethods(){
try {
getPointerCount = MotionEvent.class.getMethod("getPointerCount"); //$NON-NLS-1$
@ -76,9 +90,15 @@ public class MultiTouchSupport {
}
private boolean inZoomMode = false;
private boolean inTiltMode = false;
private double zoomStartedDistance = 100;
private double zoomRelative = 1;
private PointF centerPoint = new PointF();
private PointF firstFingerStart = new PointF();
private PointF secondFingerStart = new PointF();
private static final int TILT_X_THRESHOLD_PX = 40;
private static final int TILT_Y_THRESHOLD_PX = 40;
private static final int TILT_DY_THRESHOLD_PX = 40;
public boolean onTouchEvent(MotionEvent event){
if(!isMultiTouchSupported()){
@ -91,10 +111,13 @@ public class MultiTouchSupport {
}
Integer pointCount = (Integer) getPointerCount.invoke(event);
if(pointCount < 2){
if(inZoomMode){
if (inZoomMode) {
listener.onZoomOrRotationEnded(zoomRelative, angleRelative);
inZoomMode = false;
return true;
} else if (inTiltMode) {
inTiltMode = false;
return true;
}
return false;
}
@ -114,27 +137,58 @@ public class MultiTouchSupport {
}
if (actionCode == ACTION_POINTER_DOWN) {
centerPoint = new PointF((x1 + x2) / 2, (y1 + y2) / 2);
firstFingerStart = new PointF(x1, y1);
secondFingerStart = new PointF(x2, y2);
listener.onGestureInit(x1, y1, x2, y2);
listener.onZoomStarted(centerPoint);
zoomStartedDistance = distance;
angleStarted = angle;
inZoomMode = true;
return true;
} else if(actionCode == ACTION_POINTER_UP){
if(inZoomMode){
if (inZoomMode) {
listener.onZoomOrRotationEnded(zoomRelative, angleRelative);
inZoomMode = false;
} else if (inTiltMode) {
inTiltMode = false;
}
return true;
} else if(inZoomMode && actionCode == MotionEvent.ACTION_MOVE){
// Keep zoom center fixed or flexible
centerPoint = new PointF((x1 + x2) / 2, (y1 + y2) / 2);
} else if (actionCode == MotionEvent.ACTION_MOVE) {
if (inZoomMode) {
if(angleDefined) {
angleRelative = MapUtils.unifyRotationTo360(angle - angleStarted);
// Keep zoom center fixed or flexible
centerPoint = new PointF((x1 + x2) / 2, (y1 + y2) / 2);
if (angleDefined) {
angleRelative = MapUtils.unifyRotationTo360(angle - angleStarted);
}
zoomRelative = distance / zoomStartedDistance;
listener.onZoomingOrRotating(zoomRelative, angleRelative);
} else if (inTiltMode) {
float dy2 = secondFingerStart.y - y2;
float viewAngle = dy2 / 8f;
listener.onChangingViewAngle(viewAngle);
} else if (isTiltSupported()) {
float dx1 = Math.abs(firstFingerStart.x - x1);
float dx2 = Math.abs(secondFingerStart.x - x2);
float dy1 = Math.abs(firstFingerStart.y - y1);
float dy2 = Math.abs(secondFingerStart.y - y2);
float startDy = Math.abs(secondFingerStart.y - firstFingerStart.y);
if (dx1 < TILT_X_THRESHOLD_PX && dx2 < TILT_X_THRESHOLD_PX
&& dy1 > TILT_Y_THRESHOLD_PX && dy2 > TILT_Y_THRESHOLD_PX
&& startDy < TILT_Y_THRESHOLD_PX * 6
&& Math.abs(dy2 - dy1) < TILT_DY_THRESHOLD_PX) {
listener.onChangeViewAngleStarted();
inTiltMode = true;
} else if (dx1 > TILT_X_THRESHOLD_PX || dx2 > TILT_X_THRESHOLD_PX
|| Math.abs(dy2 - dy1) > TILT_DY_THRESHOLD_PX
|| Math.abs(dy1 - dy2) > TILT_DY_THRESHOLD_PX) {
angleRelative = 0;
zoomRelative = 0;
inZoomMode = true;
}
} else {
inZoomMode = true;
}
zoomRelative = distance / zoomStartedDistance;
listener.onZoomingOrRotating(zoomRelative, angleRelative);
return true;
}
} catch (Exception e) {

View file

@ -186,6 +186,7 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
private long multiTouchStartTime;
private long multiTouchEndTime;
private boolean wasZoomInMultiTouch;
private float elevationAngle;
public OsmandMapTileView(MapActivity activity, int w, int h) {
this.activity = activity;
@ -257,6 +258,7 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
}
}
};
elevationAngle = settings.getLastKnownMapElevation();
}
public void setView(View view) {
@ -439,6 +441,10 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
return currentViewport.getZoom();
}
public float getElevationAngle() {
return elevationAngle;
}
public double getZoomFractionalPart() {
return currentViewport.getZoomFloatPart();
}
@ -1127,6 +1133,7 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
private LatLon initialCenterLatLon;
private boolean startRotating = false;
private static final float ANGLE_THRESHOLD = 30;
private float initialElevation;
@Override
public void onZoomOrRotationEnded(double relativeToStart, float angleRelative) {
@ -1199,6 +1206,16 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
multiTouch = false;
}
@Override
public void onChangingViewAngle(float angle) {
setElevationAngle(initialElevation - angle);
}
@Override
public void onChangeViewAngleStarted() {
initialElevation = elevationAngle;
}
@Override
public void onZoomStarted(PointF centerPoint) {
initialMultiTouchCenterPoint = centerPoint;
@ -1287,6 +1304,16 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
}
private void setElevationAngle(float angle) {
if (angle < 35f) {
angle = 35f;
} else if (angle > 90f) {
angle = 90f;
}
this.elevationAngle = angle;
((MapActivity) activity).setMapElevation(angle);
}
private boolean isZoomingAllowed(int baseZoom, float dz) {
if (baseZoom > getMaxZoom()) {
return false;
@ -1346,7 +1373,9 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
dragToAnimate(e2.getX() + distanceX, e2.getY() + distanceY, e2.getX(), e2.getY(), true);
if (!multiTouchSupport.isInTiltMode()) {
dragToAnimate(e2.getX() + distanceX, e2.getY() + distanceY, e2.getX(), e2.getY(), true);
}
return true;
}