Marker class can now store and show multiple arrows sequentially

#implement #story[1118]
This commit is contained in:
Calum
2017-08-16 14:51:05 +12:00
parent ac47e9d88a
commit 3542c646f8
2 changed files with 64 additions and 32 deletions
@@ -294,7 +294,7 @@ public class GameView extends Pane {
*/ */
private void makeAndBindMarker(Mark observableMark, Paint colour) { private void makeAndBindMarker(Mark observableMark, Paint colour) {
Marker marker = new Marker(colour); Marker marker = new Marker(colour);
marker.constructArrows(MarkArrowFactory.RoundingSide.PORT, ThreadLocalRandom.current().nextDouble(91, 180), ThreadLocalRandom.current().nextDouble(1, 90)); marker.addArrows(MarkArrowFactory.RoundingSide.PORT, ThreadLocalRandom.current().nextDouble(91, 180), ThreadLocalRandom.current().nextDouble(1, 90));
markerObjects.put(observableMark, marker); markerObjects.put(observableMark, marker);
observableMark.addPositionListener((mark, lat, lon) -> { observableMark.addPositionListener((mark, lat, lon) -> {
Point2D p2d = findScaledXY(lat, lon); Point2D p2d = findScaledXY(lat, lon);
@@ -650,11 +650,11 @@ public class GameView extends Pane {
boatObjects.get(playerYacht).setAsPlayer(); boatObjects.get(playerYacht).setAsPlayer();
CompoundMark currentMark = course.get(playerYacht.getLegNumber()); CompoundMark currentMark = course.get(playerYacht.getLegNumber());
for (Mark mark : currentMark.getMarks()) { for (Mark mark : currentMark.getMarks()) {
markerObjects.get(mark).showExitArrow(); markerObjects.get(mark).showNextExitArrow();
} }
CompoundMark destination = course.get(playerYacht.getLegNumber() + 1); CompoundMark destination = course.get(playerYacht.getLegNumber() + 1);
for (Mark mark : destination.getMarks()) { for (Mark mark : destination.getMarks()) {
markerObjects.get(mark).showEnterArrow(); markerObjects.get(mark).showNextEnterArrow();
} }
annotations.get(playerYacht).addAnnotation( annotations.get(playerYacht).addAnnotation(
"velocity", "velocity",
@@ -673,16 +673,16 @@ public class GameView extends Pane {
private void updateMarkArrows (ClientYacht yacht, CompoundMark compoundMark, int legNumber) { private void updateMarkArrows (ClientYacht yacht, CompoundMark compoundMark, int legNumber) {
//Only show arrows for this and next leg. //Only show arrows for this and next leg.
for (Mark mark : compoundMark.getMarks()) { for (Mark mark : compoundMark.getMarks()) {
markerObjects.get(mark).showExitArrow(); markerObjects.get(mark).showNextExitArrow();
} }
CompoundMark nextMark = course.get(legNumber); CompoundMark nextMark = course.get(legNumber);
for (Mark mark : nextMark.getMarks()) { for (Mark mark : nextMark.getMarks()) {
markerObjects.get(mark).showEnterArrow(); markerObjects.get(mark).showNextEnterArrow();
} }
if (legNumber - 2 >= 0) { if (legNumber - 2 >= 0) {
CompoundMark lastMark = course.get(Math.max(0, legNumber - 2)); CompoundMark lastMark = course.get(Math.max(0, legNumber - 2));
for (Mark mark : lastMark.getMarks()) { for (Mark mark : lastMark.getMarks()) {
markerObjects.get(mark).hideAllArows(); markerObjects.get(mark).hideAllArrows();
} }
} }
} }
@@ -1,5 +1,7 @@
package seng302.visualiser.fxObjects; package seng302.visualiser.fxObjects;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.scene.Group; import javafx.scene.Group;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
@@ -7,54 +9,84 @@ import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle; import javafx.scene.shape.Circle;
/** /**
* Visual object for a mark. * Visual object for a mark. Contains a coloured circle and any specified arrows.
*/ */
public class Marker extends Group { public class Marker extends Group {
Circle mark = new Circle(); private Circle mark = new Circle();
Paint colour = Color.BLACK; private Paint colour = Color.BLACK;
Group enterArrow; private List<Group> enterArrows = new ArrayList<>();
Group exitArrow; private List<Group> exitArrows = new ArrayList<>();
private int enterArrowIndex = 0;
private int exitArrowIndex = 0;
/**
* Creates a new Marker containing only a circle. The default colour is black.
*/
public Marker() { public Marker() {
mark.setRadius(5); mark.setRadius(5);
mark.setCenterX(0); mark.setCenterX(0);
mark.setCenterY(0); mark.setCenterY(0);
Platform.runLater(() -> this.getChildren().add(mark)); Platform.runLater(() -> this.getChildren().addAll(mark, new Group())); //Empty group placeholder or arrows.
} }
/**
* Creates a new Marker containing only a circle of the given colour.
* @param colour the desired colour for the marker.
*/
public Marker(Paint colour) { public Marker(Paint colour) {
this(); this();
this.colour = colour; this.colour = colour;
mark.setFill(colour); mark.setFill(colour);
} }
public void constructArrows(MarkArrowFactory.RoundingSide roundingSide, double entryAngle, double exitAngle) { /**
enterArrow = MarkArrowFactory.constructEntryArrow(roundingSide, entryAngle, exitAngle, colour); * Adds an exit and entry arrow pair to the mark. Arrows are hidden and shown in the order they
exitArrow = MarkArrowFactory.constructExitArrow(roundingSide, exitAngle, colour); * are created by calling showNextEnterArrow() or showNextExitArrow()
* @param roundingSide the side the marker will be from the perspective of the arrow.
* @param entryAngle The angle the arrow will point towards a marker
* @param exitAngle The angle the arrow wil point from the marker.
*/
public void addArrows(MarkArrowFactory.RoundingSide roundingSide, double entryAngle,
double exitAngle) {
enterArrows.add(
MarkArrowFactory.constructEntryArrow(roundingSide, entryAngle, exitAngle, colour)
);
exitArrows.add(
MarkArrowFactory.constructExitArrow(roundingSide, exitAngle, colour)
);
} }
public void showEnterArrow () { /**
if (!this.getChildren().contains(enterArrow)) { * Shows the next EnterArrow. Does nothing if there are no more enter arrows. Other arrows become hidden.
*/
public void showNextEnterArrow() {
showArrow(enterArrows, enterArrowIndex);
enterArrowIndex++;
}
/**
* Shows the next ExitArrow. Does nothing if there are no more enter arrows. Other arrows become hidden.
*/
public void showNextExitArrow() {
showArrow(exitArrows, exitArrowIndex);
exitArrowIndex++;
}
private void showArrow(List<Group> arrowList, int arrowListIndex) {
if (arrowListIndex < arrowList.size()) {
Platform.runLater(() -> { Platform.runLater(() -> {
this.getChildren().remove(exitArrow); this.getChildren().remove(1);
this.getChildren().add(enterArrow); this.getChildren().add(arrowList.get(arrowListIndex));
}); });
} }
} }
public void showExitArrow () { /**
if (!this.getChildren().contains(exitArrow)) { * Hides all arrows.
Platform.runLater(() -> { */
this.getChildren().remove(enterArrow); public void hideAllArrows() {
this.getChildren().add(exitArrow); Platform.runLater(() -> this.getChildren().setAll(mark));
});
}
}
public void hideAllArows () {
Platform.runLater(() -> {
this.getChildren().removeAll(enterArrow, exitArrow);
});
} }
} }