Add undo and redo actions

This commit is contained in:
Alexander Sytnyk 2017-08-02 16:30:05 +03:00
parent c2c929ad6a
commit 4be8775081
2 changed files with 38 additions and 5 deletions

View file

@ -57,7 +57,7 @@ public class MeasurementToolFragment extends Fragment {
((ImageView) mainView.findViewById(R.id.ruler_icon))
.setImageDrawable(iconsCache.getIcon(R.drawable.ic_action_ruler, R.color.color_myloc_distance));
ImageButton upDownBtn = ((ImageButton) mainView.findViewById(R.id.up_down_button));
final ImageButton upDownBtn = ((ImageButton) mainView.findViewById(R.id.up_down_button));
upDownBtn.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_arrow_up));
upDownBtn.setOnClickListener(new View.OnClickListener() {
@Override
@ -66,21 +66,34 @@ public class MeasurementToolFragment extends Fragment {
}
});
ImageButton undoBtn = ((ImageButton) mainView.findViewById(R.id.undo_point_button));
final ImageButton undoBtn = ((ImageButton) mainView.findViewById(R.id.undo_point_button));
final ImageButton redoBtn = ((ImageButton) mainView.findViewById(R.id.redo_point_button));
undoBtn.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_undo_dark));
undoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (measurementLayer.undoPointOnClick()) {
enable(undoBtn);
} else {
disable(undoBtn);
}
enable(redoBtn);
updateText();
}
});
ImageButton redoBtn = ((ImageButton) mainView.findViewById(R.id.redo_point_button));
redoBtn.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_redo_dark));
redoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (measurementLayer.redoPointOnClick()) {
enable(redoBtn);
} else {
disable(redoBtn);
}
enable(undoBtn);
updateText();
}
});
@ -88,6 +101,8 @@ public class MeasurementToolFragment extends Fragment {
@Override
public void onClick(View view) {
measurementLayer.addPointOnClick();
enable(undoBtn);
disable(redoBtn);
updateText();
}
});
@ -123,6 +138,7 @@ public class MeasurementToolFragment extends Fragment {
return true;
case R.id.action_clear_all:
measurementLayer.clearPoints();
disable(undoBtn, redoBtn);
updateText();
return true;
}

View file

@ -25,6 +25,7 @@ public class MeasurementToolLayer extends OsmandMapLayer {
private OsmandMapTileView view;
private boolean inMeasurementMode;
private LinkedList<WptPt> measurementPoints = new LinkedList<>();
private LinkedList<WptPt> cacheMeasurementPoints;
private Bitmap centerIconDay;
private Bitmap centerIconNight;
private Bitmap pointIcon;
@ -78,6 +79,7 @@ public class MeasurementToolLayer extends OsmandMapLayer {
void clearPoints() {
measurementPoints.clear();
cacheMeasurementPoints.clear();
view.refreshMap();
}
@ -141,9 +143,24 @@ public class MeasurementToolLayer extends OsmandMapLayer {
} else {
measurementPoints.add(pt);
}
cacheMeasurementPoints = new LinkedList<>(measurementPoints);
view.refreshMap();
}
boolean undoPointOnClick() {
measurementPoints.remove(measurementPoints.size() - 1);
WptPt pt = measurementPoints.get(measurementPoints.size() - 1);
view.getAnimatedDraggingThread().startMoving(pt.getLatitude(), pt.getLongitude(), view.getZoom(), true);
return measurementPoints.size() > 0;
}
boolean redoPointOnClick() {
WptPt pt = cacheMeasurementPoints.get(measurementPoints.size());
measurementPoints.add(pt);
view.getAnimatedDraggingThread().startMoving(pt.getLatitude(), pt.getLongitude(), view.getZoom(), true);
return cacheMeasurementPoints.size() > measurementPoints.size();
}
@Override
public void destroyLayer() {