Refactor drawing segments

This commit is contained in:
PavelRatushny 2017-08-23 18:57:39 +03:00
parent a9724b0d14
commit ec0d724f4b
3 changed files with 47 additions and 18 deletions

View file

@ -167,13 +167,21 @@ public class MeasurementEditingContext {
after = new TrkSegment();
}
public void recreateSegments(int position) {
public void recreateSegments(int position, boolean addAfter, boolean addBefore) {
before = new TrkSegment();
before.points.addAll(measurementPoints.subList(0, position));
if (addAfter) {
before.points.addAll(measurementPoints.subList(0, position + 1));
} else {
before.points.addAll(measurementPoints.subList(0, position));
}
addBeforeRenders();
after = new TrkSegment();
if (position != measurementPoints.size() - 1) {
after.points.addAll(measurementPoints.subList(position + 1, measurementPoints.size()));
if (addBefore) {
after.points.addAll(measurementPoints.subList(position, measurementPoints.size()));
} else {
after.points.addAll(measurementPoints.subList(position + 1, measurementPoints.size()));
}
addAfterRenders();
}
}

View file

@ -602,6 +602,7 @@ public class MeasurementToolFragment extends Fragment {
public void moveOnClick() {
if (measurementLayer != null) {
measurementLayer.enterMovingPointMode();
editingCtx.recreateSegments(measurementLayer.getSelectedPointPos(), false, false);
}
switchMovePointMode(true);
}
@ -618,6 +619,7 @@ public class MeasurementToolFragment extends Fragment {
public void addPointAfterOnClick() {
if (measurementLayer != null) {
positionToAddPoint = measurementLayer.getSelectedPointPos() + 1;
editingCtx.recreateSegments(measurementLayer.getSelectedPointPos(), true, false);
measurementLayer.enterAddingPointAfterMode();
}
switchAddPointAfterMode(true);
@ -627,6 +629,7 @@ public class MeasurementToolFragment extends Fragment {
public void addPointBeforeOnClick() {
if (measurementLayer != null) {
positionToAddPoint = measurementLayer.getSelectedPointPos();
editingCtx.recreateSegments(measurementLayer.getSelectedPointPos(), false, true);
measurementLayer.enterAddingPointBeforeMode();
}
switchAddPointBeforeMode(true);
@ -873,12 +876,13 @@ public class MeasurementToolFragment extends Fragment {
measurementToolLayer.refreshMap();
}
clearSelection();
editingCtx.recreateSegments();
}
private void addPointAfter() {
MeasurementToolLayer measurementLayer = getMeasurementLayer();
if (measurementLayer != null && positionToAddPoint != -1) {
if (addPointToPosition(positionToAddPoint)) {
if (addPointToPosition(positionToAddPoint, true, false)) {
selectedPointPos += 1;
selectedCachedPoint = new WptPt(editingCtx.getPoints().get(selectedPointPos));
measurementLayer.setSelectedPointPos(selectedPointPos);
@ -898,6 +902,7 @@ public class MeasurementToolFragment extends Fragment {
}
clearSelection();
positionToAddPoint = -1;
editingCtx.recreateSegments();
}
private void cancelAddPointAfterMode() {
@ -909,12 +914,13 @@ public class MeasurementToolFragment extends Fragment {
}
clearSelection();
positionToAddPoint = -1;
editingCtx.recreateSegments();
}
private void addPointBefore() {
MeasurementToolLayer measurementLayer = getMeasurementLayer();
if (measurementLayer != null && positionToAddPoint != -1) {
if (addPointToPosition(positionToAddPoint)) {
if (addPointToPosition(positionToAddPoint, false, true)) {
selectedCachedPoint = new WptPt(editingCtx.getPoints().get(selectedPointPos));
measurementLayer.setSelectedPointPos(selectedPointPos);
measurementLayer.setSelectedCachedPoint(selectedCachedPoint);
@ -932,6 +938,7 @@ public class MeasurementToolFragment extends Fragment {
}
clearSelection();
positionToAddPoint = -1;
editingCtx.recreateSegments();
}
private void cancelAddPointBeforeMode() {
@ -943,6 +950,7 @@ public class MeasurementToolFragment extends Fragment {
}
clearSelection();
positionToAddPoint = -1;
editingCtx.recreateSegments();
}
private void clearSelection() {
@ -1038,12 +1046,12 @@ public class MeasurementToolFragment extends Fragment {
}
}
private boolean addPointToPosition(int position) {
private boolean addPointToPosition(int position, boolean addAfter, boolean addBefore) {
boolean added = false;
MeasurementToolLayer measurementLayer = getMeasurementLayer();
if (measurementLayer != null) {
added = commandManager.execute(new AddPointCommand(measurementLayer, position));
editingCtx.recreateSegments(position);
editingCtx.recreateSegments(position, addAfter, addBefore);
doAddOrMovePointCommonStuff();
}
return added;

View file

@ -199,7 +199,6 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
lowestDistance = distToPoint;
selectedCachedPoint = new WptPt(pt);
selectedPointPos = i;
editingCtx.recreateSegments(selectedPointPos);
}
}
}
@ -251,20 +250,34 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL
List<WptPt> drawPoints = editingCtx.getPoints();
if (drawPoints.size() > 0) {
TrkSegment before = editingCtx.getBeforeTrkSegmentLine();
TrkSegment after = editingCtx.getAfterTrkSegmentLine();
if (before.points.size() > 0 || after.points.size() > 0) {
path.reset();
tx.reset();
ty.reset();
WptPt pt = drawPoints.get(drawPoints.size() - 1);
int locX = tb.getPixXFromLonNoRot(pt.lon);
int locY = tb.getPixYFromLatNoRot(pt.lat);
path.moveTo(locX, locY);
tx.add(locX);
ty.add(locY);
path.lineTo(tb.getCenterPixelX(), tb.getCenterPixelY());
tx.add(tb.getCenterPixelX());
ty.add(tb.getCenterPixelY());
if (before.points.size() > 0) {
WptPt pt = before.points.get(before.points.size() - 1);
int locX = tb.getPixXFromLonNoRot(pt.lon);
int locY = tb.getPixYFromLatNoRot(pt.lat);
path.moveTo(locX, locY);
tx.add(locX);
ty.add(locY);
path.lineTo(tb.getCenterPixelX(), tb.getCenterPixelY());
tx.add(tb.getCenterPixelX());
ty.add(tb.getCenterPixelY());
}
if (after.points.size() > 0) {
WptPt pt = after.points.get(0);
int locX = tb.getPixXFromLonNoRot(pt.lon);
int locY = tb.getPixYFromLatNoRot(pt.lat);
path.lineTo(locX, locY);
tx.add(locX);
ty.add(locY);
}
calculatePath(tb, tx, ty, path);
canvas.drawPath(path, lineAttrs.paint);
}