From 3542c646f86234e1f28e7f43bbf4bfebbbd24a75 Mon Sep 17 00:00:00 2001 From: Calum Date: Wed, 16 Aug 2017 14:51:05 +1200 Subject: [PATCH] Marker class can now store and show multiple arrows sequentially #implement #story[1118] --- .../java/seng302/visualiser/GameView.java | 12 +-- .../seng302/visualiser/fxObjects/Marker.java | 84 +++++++++++++------ 2 files changed, 64 insertions(+), 32 deletions(-) diff --git a/src/main/java/seng302/visualiser/GameView.java b/src/main/java/seng302/visualiser/GameView.java index 75a3dac7..c9e32563 100644 --- a/src/main/java/seng302/visualiser/GameView.java +++ b/src/main/java/seng302/visualiser/GameView.java @@ -294,7 +294,7 @@ public class GameView extends Pane { */ private void makeAndBindMarker(Mark observableMark, Paint 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); observableMark.addPositionListener((mark, lat, lon) -> { Point2D p2d = findScaledXY(lat, lon); @@ -650,11 +650,11 @@ public class GameView extends Pane { boatObjects.get(playerYacht).setAsPlayer(); CompoundMark currentMark = course.get(playerYacht.getLegNumber()); for (Mark mark : currentMark.getMarks()) { - markerObjects.get(mark).showExitArrow(); + markerObjects.get(mark).showNextExitArrow(); } CompoundMark destination = course.get(playerYacht.getLegNumber() + 1); for (Mark mark : destination.getMarks()) { - markerObjects.get(mark).showEnterArrow(); + markerObjects.get(mark).showNextEnterArrow(); } annotations.get(playerYacht).addAnnotation( "velocity", @@ -673,16 +673,16 @@ public class GameView extends Pane { private void updateMarkArrows (ClientYacht yacht, CompoundMark compoundMark, int legNumber) { //Only show arrows for this and next leg. for (Mark mark : compoundMark.getMarks()) { - markerObjects.get(mark).showExitArrow(); + markerObjects.get(mark).showNextExitArrow(); } CompoundMark nextMark = course.get(legNumber); for (Mark mark : nextMark.getMarks()) { - markerObjects.get(mark).showEnterArrow(); + markerObjects.get(mark).showNextEnterArrow(); } if (legNumber - 2 >= 0) { CompoundMark lastMark = course.get(Math.max(0, legNumber - 2)); for (Mark mark : lastMark.getMarks()) { - markerObjects.get(mark).hideAllArows(); + markerObjects.get(mark).hideAllArrows(); } } } diff --git a/src/main/java/seng302/visualiser/fxObjects/Marker.java b/src/main/java/seng302/visualiser/fxObjects/Marker.java index ead4681f..73340fe3 100644 --- a/src/main/java/seng302/visualiser/fxObjects/Marker.java +++ b/src/main/java/seng302/visualiser/fxObjects/Marker.java @@ -1,5 +1,7 @@ package seng302.visualiser.fxObjects; +import java.util.ArrayList; +import java.util.List; import javafx.application.Platform; import javafx.scene.Group; import javafx.scene.paint.Color; @@ -7,54 +9,84 @@ import javafx.scene.paint.Paint; 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 { - Circle mark = new Circle(); - Paint colour = Color.BLACK; - Group enterArrow; - Group exitArrow; + private Circle mark = new Circle(); + private Paint colour = Color.BLACK; + private List enterArrows = new ArrayList<>(); + private List 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() { mark.setRadius(5); mark.setCenterX(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) { this(); this.colour = colour; mark.setFill(colour); } - public void constructArrows(MarkArrowFactory.RoundingSide roundingSide, double entryAngle, double exitAngle) { - enterArrow = MarkArrowFactory.constructEntryArrow(roundingSide, entryAngle, exitAngle, colour); - exitArrow = MarkArrowFactory.constructExitArrow(roundingSide, exitAngle, colour); + /** + * Adds an exit and entry arrow pair to the mark. Arrows are hidden and shown in the order they + * 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 arrowList, int arrowListIndex) { + if (arrowListIndex < arrowList.size()) { Platform.runLater(() -> { - this.getChildren().remove(exitArrow); - this.getChildren().add(enterArrow); + this.getChildren().remove(1); + this.getChildren().add(arrowList.get(arrowListIndex)); }); } } - public void showExitArrow () { - if (!this.getChildren().contains(exitArrow)) { - Platform.runLater(() -> { - this.getChildren().remove(enterArrow); - this.getChildren().add(exitArrow); - }); - } - } - - public void hideAllArows () { - Platform.runLater(() -> { - this.getChildren().removeAll(enterArrow, exitArrow); - }); + /** + * Hides all arrows. + */ + public void hideAllArrows() { + Platform.runLater(() -> this.getChildren().setAll(mark)); } } \ No newline at end of file