Fix moving object

This commit is contained in:
Alexey Kulish 2016-05-07 10:58:13 +03:00
parent 598cf9385a
commit f5717b63ed

View file

@ -18,8 +18,8 @@ import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.FrameLayout.LayoutParams; import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView; import android.widget.ImageView;
import net.osmand.CallbackWithObject; import net.osmand.CallbackWithObject;
import net.osmand.PlatformUtil;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.PointDescription; import net.osmand.data.PointDescription;
import net.osmand.data.RotatedTileBox; import net.osmand.data.RotatedTileBox;
@ -30,15 +30,13 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.other.MapMultiSelectionMenu; import net.osmand.plus.mapcontextmenu.other.MapMultiSelectionMenu;
import org.apache.commons.logging.Log;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class ContextMenuLayer extends OsmandMapLayer { public class ContextMenuLayer extends OsmandMapLayer {
private static final Log LOG = PlatformUtil.getLog(ContextMenuLayer.class); //private static final Log LOG = PlatformUtil.getLog(ContextMenuLayer.class);
public static final int VIBRATE_SHORT = 100; public static final int VIBRATE_SHORT = 100;
private OsmandMapTileView view; private OsmandMapTileView view;
@ -59,6 +57,7 @@ public class ContextMenuLayer extends OsmandMapLayer {
private final MoveMarkerBottomSheetHelper mMoveMarkerBottomSheetHelper; private final MoveMarkerBottomSheetHelper mMoveMarkerBottomSheetHelper;
private boolean mInChangeMarkerPositionMode; private boolean mInChangeMarkerPositionMode;
private IContextMenuProvider selectedObjectContextMenuProvider;
public ContextMenuLayer(MapActivity activity) { public ContextMenuLayer(MapActivity activity) {
this.activity = activity; this.activity = activity;
@ -108,11 +107,11 @@ public class ContextMenuLayer extends OsmandMapLayer {
} }
if (mInChangeMarkerPositionMode) { if (mInChangeMarkerPositionMode) {
// TODO draw marker if selection was only marker? if (menu.getObject() == null) {
// int x = previousMarkerX; canvas.translate(box.getPixWidth() / 2 - contextMarker.getWidth() / 2, box.getPixHeight() / 2 - contextMarker.getHeight());
// int y = previousMarkerY; contextMarker.draw(canvas);
// canvas.translate(x - contextMarker.getWidth() / 2, y - contextMarker.getHeight()); }
// contextMarker.draw(canvas); mMoveMarkerBottomSheetHelper.onDraw(box);
} else if (menu.isActive()) { } else if (menu.isActive()) {
LatLon latLon = menu.getLatLon(); LatLon latLon = menu.getLatLon();
int x = (int) box.getPixXFromLatLon(latLon.getLatitude(), latLon.getLongitude()); int x = (int) box.getPixXFromLatLon(latLon.getLatitude(), latLon.getLongitude());
@ -152,12 +151,14 @@ public class ContextMenuLayer extends OsmandMapLayer {
return false; return false;
} }
if (pressedContextMarker(tileBox, point.x, point.y)) { if (pressedContextMarker(tileBox, point.x, point.y)) {
Object obj = menu.getObject() ; Object obj = menu.getObject();
if (obj != null && isObjectMoveable(obj)) { if (isObjectMoveable(obj)) {
Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE); Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(VIBRATE_SHORT); vibrator.vibrate(VIBRATE_SHORT);
activity.getContextMenu().hide(); menu.updateMapCenter(null);
menu.hide();
LatLon ll = menu.getLatLon(); LatLon ll = menu.getLatLon();
RotatedTileBox rb = new RotatedTileBox(tileBox); RotatedTileBox rb = new RotatedTileBox(tileBox);
rb.setCenterLocation(0.5f, 0.5f); rb.setCenterLocation(0.5f, 0.5f);
@ -177,41 +178,39 @@ public class ContextMenuLayer extends OsmandMapLayer {
view.refreshMap(); view.refreshMap();
return true; return true;
} }
public PointF getMoveableCenterPoint(RotatedTileBox tb) { public PointF getMoveableCenterPoint(RotatedTileBox tb) {
return new PointF(tb.getPixWidth() / 2, tb.getPixHeight() / 2); return new PointF(tb.getPixWidth() / 2, tb.getPixHeight() / 2);
} }
public Object getMoveableObject() { public Object getMoveableObject() {
return mInChangeMarkerPositionMode ? menu.getObject() : null; return mInChangeMarkerPositionMode ? menu.getObject() : null;
} }
public boolean isInChangeMarkerPositionMode() { public boolean isInChangeMarkerPositionMode() {
return mInChangeMarkerPositionMode; return mInChangeMarkerPositionMode;
} }
public boolean isObjectMoveable(Object o) { public boolean isObjectMoveable(Object o) {
for (OsmandMapLayer lt : view.getLayers()) { if (o == null) {
if (lt instanceof ContextMenuLayer.IMoveObjectProvider) { return true;
final IMoveObjectProvider l = (ContextMenuLayer.IMoveObjectProvider) lt; } else if (selectedObjectContextMenuProvider != null
if(l.isObjectMoveable(o)) { && selectedObjectContextMenuProvider instanceof ContextMenuLayer.IMoveObjectProvider) {
return true; final IMoveObjectProvider l = (ContextMenuLayer.IMoveObjectProvider) selectedObjectContextMenuProvider;
} if (l.isObjectMoveable(o)) {
return true;
} }
} }
return false; return false;
} }
public void applyMovedObject(Object o, LatLon position) { public void applyMovedObject(Object o, LatLon position) {
for (OsmandMapLayer lt : view.getLayers()) { if (selectedObjectContextMenuProvider != null
if (lt instanceof ContextMenuLayer.IMoveObjectProvider) { && selectedObjectContextMenuProvider instanceof ContextMenuLayer.IMoveObjectProvider) {
final IMoveObjectProvider l = (ContextMenuLayer.IMoveObjectProvider) lt; final IMoveObjectProvider l = (ContextMenuLayer.IMoveObjectProvider) selectedObjectContextMenuProvider;
if(l.isObjectMoveable(o)) { if (l.isObjectMoveable(o)) {
l.applyNewObjectPosition(o, position); l.applyNewObjectPosition(o, position);
return;
}
} }
} }
} }
@ -220,14 +219,20 @@ public class ContextMenuLayer extends OsmandMapLayer {
if (!mInChangeMarkerPositionMode) { if (!mInChangeMarkerPositionMode) {
throw new IllegalStateException("Not in change marker position mode"); throw new IllegalStateException("Not in change marker position mode");
} }
Object obj = getMoveableObject();
quitMovingMarker();
RotatedTileBox tileBox = activity.getMapView().getCurrentRotatedTileBox(); RotatedTileBox tileBox = activity.getMapView().getCurrentRotatedTileBox();
PointF newMarkerPosition = getMoveableCenterPoint(tileBox); PointF newMarkerPosition = getMoveableCenterPoint(tileBox);
LatLon ll = tileBox.getLatLonFromPixel(newMarkerPosition.x, newMarkerPosition.y); LatLon ll = tileBox.getLatLonFromPixel(newMarkerPosition.x, newMarkerPosition.y);
Object obj = getMoveableObject();
applyMovedObject(obj, ll); applyMovedObject(obj, ll);
// TODO pass object properly quitMovingMarker();
showContextMenu(newMarkerPosition, tileBox, true);
PointDescription pointDescription = null;
if (selectedObjectContextMenuProvider != null) {
pointDescription = selectedObjectContextMenuProvider.getObjectName(obj);
}
menu.show(ll, pointDescription, obj);
view.refreshMap(); view.refreshMap();
} }
@ -236,7 +241,7 @@ public class ContextMenuLayer extends OsmandMapLayer {
mark(View.VISIBLE, R.id.map_ruler_layout, mark(View.VISIBLE, R.id.map_ruler_layout,
R.id.map_left_widgets_panel, R.id.map_right_widgets_panel, R.id.map_center_info); R.id.map_left_widgets_panel, R.id.map_right_widgets_panel, R.id.map_center_info);
} }
private void enterMovingMode() { private void enterMovingMode() {
mInChangeMarkerPositionMode = true; mInChangeMarkerPositionMode = true;
mMoveMarkerBottomSheetHelper.show(menu.getLeftIcon()); mMoveMarkerBottomSheetHelper.show(menu.getLeftIcon());
@ -245,9 +250,9 @@ public class ContextMenuLayer extends OsmandMapLayer {
} }
private void mark(int status, int... widgets) { private void mark(int status, int... widgets) {
for(int i = 0; i < widgets.length ; i++) { for (int widget : widgets) {
View v = activity.findViewById(widgets[i]); View v = activity.findViewById(widget);
if(v != null) { if (v != null) {
v.setVisibility(status); v.setVisibility(status);
} }
} }
@ -269,29 +274,30 @@ public class ContextMenuLayer extends OsmandMapLayer {
Map<Object, IContextMenuProvider> selectedObjects = selectObjectsForContextMenu(tileBox, point, false); Map<Object, IContextMenuProvider> selectedObjects = selectObjectsForContextMenu(tileBox, point, false);
if (selectedObjects.size() == 1) { if (selectedObjects.size() == 1) {
Object selectedObj = selectedObjects.keySet().iterator().next(); Object selectedObj = selectedObjects.keySet().iterator().next();
IContextMenuProvider contextObject = selectedObjects.get(selectedObj); selectedObjectContextMenuProvider = selectedObjects.get(selectedObj);
LatLon latLon = null; LatLon latLon = null;
PointDescription pointDescription = null; PointDescription pointDescription = null;
if (contextObject != null) { if (selectedObjectContextMenuProvider != null) {
latLon = contextObject.getObjectLocation(selectedObj); latLon = selectedObjectContextMenuProvider.getObjectLocation(selectedObj);
pointDescription = contextObject.getObjectName(selectedObj); pointDescription = selectedObjectContextMenuProvider.getObjectName(selectedObj);
} }
if (latLon == null) { if (latLon == null) {
latLon = getLatLon(point, tileBox); latLon = getLatLon(point, tileBox);
} }
hideVisibleMenues(); hideVisibleMenues();
activity.getMapViewTrackingUtilities().locationChanged(latLon.getLatitude(), latLon.getLongitude(), this); activity.getMapViewTrackingUtilities().setMapLinkedToLocation(false);
menu.show(latLon, pointDescription, selectedObj); menu.show(latLon, pointDescription, selectedObj);
return true; return true;
} else if (selectedObjects.size() > 1) { } else if (selectedObjects.size() > 1) {
selectedObjectContextMenuProvider = null;
showContextMenuForSelectedObjects(getLatLon(point, tileBox), selectedObjects); showContextMenuForSelectedObjects(getLatLon(point, tileBox), selectedObjects);
return true; return true;
} else if (showUnknownLocation) { } else if (showUnknownLocation) {
hideVisibleMenues(); hideVisibleMenues();
LatLon latLon = getLatLon(point, tileBox); LatLon latLon = getLatLon(point, tileBox);
activity.getMapViewTrackingUtilities().locationChanged(latLon.getLatitude(), latLon.getLongitude(), this); activity.getMapViewTrackingUtilities().setMapLinkedToLocation(false);
menu.show(latLon, null, null); menu.show(latLon, null, null);
return true; return true;
} }
@ -308,9 +314,6 @@ public class ContextMenuLayer extends OsmandMapLayer {
} }
public boolean disableSingleTap() { public boolean disableSingleTap() {
if (mInChangeMarkerPositionMode) {
return true;
}
boolean res = false; boolean res = false;
for (OsmandMapLayer lt : view.getLayers()) { for (OsmandMapLayer lt : view.getLayers()) {
if (lt instanceof ContextMenuLayer.IContextMenuProvider) { if (lt instanceof ContextMenuLayer.IContextMenuProvider) {
@ -398,6 +401,10 @@ public class ContextMenuLayer extends OsmandMapLayer {
@Override @Override
public boolean onSingleTap(PointF point, RotatedTileBox tileBox) { public boolean onSingleTap(PointF point, RotatedTileBox tileBox) {
if (mInChangeMarkerPositionMode) {
return true;
}
if (pressedContextMarker(tileBox, point.x, point.y)) { if (pressedContextMarker(tileBox, point.x, point.y)) {
hideVisibleMenues(); hideVisibleMenues();
menu.show(); menu.show();
@ -449,9 +456,11 @@ public class ContextMenuLayer extends OsmandMapLayer {
switch (event.getAction()) { switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
selectObjectsForContextMenu(tileBox, new PointF(event.getX(), event.getY()), true); if (!mInChangeMarkerPositionMode) {
if (pressedLatLonFull.size() > 0 || pressedLatLonSmall.size() > 0) { selectObjectsForContextMenu(tileBox, new PointF(event.getX(), event.getY()), true);
view.refreshMap(); if (pressedLatLonFull.size() > 0 || pressedLatLonSmall.size() > 0) {
view.refreshMap();
}
} }
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
@ -478,17 +487,17 @@ public class ContextMenuLayer extends OsmandMapLayer {
boolean disableLongPressOnMap(); boolean disableLongPressOnMap();
boolean isObjectClickable(Object o); boolean isObjectClickable(Object o);
} }
public interface IMoveObjectProvider { public interface IMoveObjectProvider {
boolean isObjectMoveable(Object o); boolean isObjectMoveable(Object o);
boolean applyNewObjectPosition(Object o, LatLon position); boolean applyNewObjectPosition(Object o, LatLon position);
} }
public interface IContextMenuProviderSelection { public interface IContextMenuProviderSelection {