From acbde5aad87ee798bbb3b78bb094b17ce9d66b17 Mon Sep 17 00:00:00 2001 From: Calum Date: Wed, 24 May 2017 03:09:11 +1200 Subject: [PATCH 1/8] Moved boat annotations into their own class. Implemented observer pattern. Observer pattern appears to have caused issues with updating Text objects. Made annotations look nicer. Kinda. #refactor --- src/main/java/seng302/App.java | 1 + .../seng302/controllers/CanvasController.java | 5 +- .../controllers/RaceViewController.java | 1 + .../seng302/fxObjects/BoatAnnotations.java | 142 +++++++++ .../{models => fxObjects}/BoatGroup.java | 236 ++++++++------- .../{models/mark => fxObjects}/MarkGroup.java | 33 ++- .../seng302/{models => fxObjects}/Wake.java | 6 +- src/main/java/seng302/models/Yacht.java | 276 ++++++++++++++++-- .../seng302/models/stream/StreamParser.java | 2 +- 9 files changed, 543 insertions(+), 159 deletions(-) create mode 100644 src/main/java/seng302/fxObjects/BoatAnnotations.java rename src/main/java/seng302/{models => fxObjects}/BoatGroup.java (66%) rename src/main/java/seng302/{models/mark => fxObjects}/MarkGroup.java (75%) rename src/main/java/seng302/{models => fxObjects}/Wake.java (97%) diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index ac264db6..b66376c5 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -64,6 +64,7 @@ public class App extends Application { else{ // sr = new StreamReceiver("localhost", 4949, "RaceStream"); sr = new StreamReceiver("livedata.americascup.com", 4941, "RaceStream"); +// sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941, "RaceStream"); } sr.start(); diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index 0bdeae25..12e29efa 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -15,12 +15,13 @@ import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; import javafx.scene.text.Font; -import seng302.models.BoatGroup; +import seng302.fxObjects.AnnotationsThingThatNeedsABetterName; +import seng302.fxObjects.BoatGroup; import seng302.models.Colors; import seng302.models.Yacht; import seng302.models.mark.GateMark; import seng302.models.mark.Mark; -import seng302.models.mark.MarkGroup; +import seng302.fxObjects.MarkGroup; import seng302.models.mark.MarkType; import seng302.models.mark.SingleMark; import seng302.models.stream.StreamParser; diff --git a/src/main/java/seng302/controllers/RaceViewController.java b/src/main/java/seng302/controllers/RaceViewController.java index 43c04a9b..f2f60ed5 100644 --- a/src/main/java/seng302/controllers/RaceViewController.java +++ b/src/main/java/seng302/controllers/RaceViewController.java @@ -25,6 +25,7 @@ import seng302.controllers.annotations.Annotation; import seng302.controllers.annotations.ImportantAnnotationController; import seng302.controllers.annotations.ImportantAnnotationDelegate; import seng302.controllers.annotations.ImportantAnnotationsState; +import seng302.fxObjects.BoatGroup; import seng302.models.*; import seng302.models.stream.StreamParser; diff --git a/src/main/java/seng302/fxObjects/BoatAnnotations.java b/src/main/java/seng302/fxObjects/BoatAnnotations.java new file mode 100644 index 00000000..d03d9e47 --- /dev/null +++ b/src/main/java/seng302/fxObjects/BoatAnnotations.java @@ -0,0 +1,142 @@ +package seng302.fxObjects; + +import javafx.scene.CacheHint; +import javafx.scene.Group; +import javafx.scene.Node; +import javafx.scene.paint.Color; +import javafx.scene.shape.Polygon; +import javafx.scene.shape.Rectangle; +import javafx.scene.text.Text; +import seng302.models.Yacht; +import seng302.models.stream.StreamParser; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by cir27 on 23/05/17. + */ +public class BoatAnnotations extends Group{ + + private static final double TEAMNAME_X_OFFSET = 18d; + private static final double TEAMNAME_Y_OFFSET = -29d; + private static final double VELOCITY_X_OFFSET = 18d; + private static final double VELOCITY_Y_OFFSET = -17d; + private static final double ESTTIMETONEXTMARK_X_OFFSET = 18d; + private static final double ESTTIMETONEXTMARK_Y_OFFSET = -5d; + private static final double LEGTIME_X_OFFSET = 18d; + private static final double LEGTIME_Y_OFFSET = 7d; + +// private Rectangle background = new Rectangle(); + private Text teamNameObject; + private Text velocityObject; + private Text estTimeToNextMarkObject; + private Text legTimeObject; + private List kids; + + private boolean visible = true; + + BoatAnnotations (Yacht boat, Color theme) { + super.setCache(true); +// background.setX(15d); +// background.setY(-32d); +// background.setWidth(150); +// background.setHeight(55); +// background.setArcHeight(10); +// background.setArcWidth(10); +// background.setFill(new Color(1, 1, 1, 0.75)); +// background.setStroke(theme); +// background.setStrokeWidth(2); +// background.setCache(true); +// background.setCacheHint(CacheHint.SPEED); + + teamNameObject = getTextObject(boat.getShortName(), theme); + teamNameObject.relocate(TEAMNAME_X_OFFSET, TEAMNAME_Y_OFFSET); + + velocityObject = getTextObject("", theme); + velocityObject.relocate(VELOCITY_X_OFFSET, VELOCITY_Y_OFFSET); + //On change listener + boat.getReadOnlyVelocityProperty().addListener((obs, oldVal, newVal) -> + velocityObject.setText(String.format("%.2f m/s", newVal.doubleValue())) + ); + //Invalidation listener + boat.getReadOnlyVelocityProperty().addListener(obs -> + velocityObject.setText("") + ); + + estTimeToNextMarkObject = getTextObject("Next mark: ", theme); + estTimeToNextMarkObject.relocate(ESTTIMETONEXTMARK_X_OFFSET, ESTTIMETONEXTMARK_Y_OFFSET); + boat.getReadOnlyNextMarkProperty().addListener((obs, oldVal, newVal) -> { + DateFormat format = new SimpleDateFormat("mm:ss"); + String timeToNextMark = format + .format(newVal.longValue() - StreamParser.getCurrentTimeLong()); + estTimeToNextMarkObject.setText("Next mark: " + timeToNextMark); + }); + boat.getReadOnlyNextMarkProperty().addListener(obs -> + estTimeToNextMarkObject.setText("Next mark: - ") + ); + + legTimeObject = getTextObject("Last mark: -", theme); + legTimeObject.relocate(LEGTIME_X_OFFSET, LEGTIME_Y_OFFSET); + boat.getReadOnlyMarkRoundingProperty().addListener((obs, oldTime, newTime) -> { + DateFormat format = new SimpleDateFormat("mm:ss"); + String elapsedTime = format + .format(StreamParser.getCurrentTimeLong() - newTime.longValue()); + legTimeObject.setText("Last mark: " + elapsedTime); + }); + boat.getReadOnlyMarkRoundingProperty().addListener(obs -> + legTimeObject.setText("Last mark: - ") + ); + + kids = new ArrayList<>(); +// kids.add(background); + kids.add(velocityObject); + kids.add(teamNameObject); + kids.add(estTimeToNextMarkObject); + kids.add(legTimeObject); + +// super.getChildren().addAll(background, teamNameObject, velocityObject, estTimeToNextMarkObject, legTimeObject); + } + + /** + * Return a text object with caching and a color applied + * + * @param defaultText The default text to display + * @param fill The text fill color + * @return The text object + */ + private Text getTextObject(String defaultText, Color fill) { + Text text = new Text(defaultText); + text.setFill(fill); +// text.setCacheHint(CacheHint.SPEED); + text.setCache(true); + return text; + } + + public void setTeamNameObjectVisible(Boolean visible) { + teamNameObject.setVisible(visible); + } + + public void setVelocityObjectVisible(Boolean visible) { + velocityObject.setVisible(visible); + } + + public void setEstTimeToNextMarkObjectVisible(Boolean visible) { + estTimeToNextMarkObject.setVisible(visible); + } + + public void setLegTimeObjectVisible(Boolean visible) { + legTimeObject.setVisible(visible); + } + + public void toggleVisible() { + visible = !visible; + this.setVisible(visible); + } + + public List getkiddies () { + return kids; + } +} diff --git a/src/main/java/seng302/models/BoatGroup.java b/src/main/java/seng302/fxObjects/BoatGroup.java similarity index 66% rename from src/main/java/seng302/models/BoatGroup.java rename to src/main/java/seng302/fxObjects/BoatGroup.java index 3e97df5e..2d659e84 100644 --- a/src/main/java/seng302/models/BoatGroup.java +++ b/src/main/java/seng302/fxObjects/BoatGroup.java @@ -1,9 +1,10 @@ -package seng302.models; +package seng302.fxObjects; import javafx.event.EventHandler; import javafx.geometry.Point2D; import javafx.scene.CacheHint; import javafx.scene.Group; +import javafx.scene.Node; import javafx.scene.input.MouseDragEvent; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; @@ -12,6 +13,7 @@ import javafx.scene.shape.Line; import javafx.scene.shape.Polygon; import javafx.scene.text.Text; import javafx.scene.transform.Rotate; +import seng302.models.Yacht; import seng302.models.stream.StreamParser; import java.text.DateFormat; @@ -28,14 +30,6 @@ import java.text.SimpleDateFormat; public class BoatGroup extends Group { //Constants for drawing - private static final double TEAMNAME_X_OFFSET = 10d; - private static final double TEAMNAME_Y_OFFSET = -29d; - private static final double VELOCITY_X_OFFSET = 10d; - private static final double VELOCITY_Y_OFFSET = -17d; - private static final double ESTTIMETONEXTMARK_X_OFFSET = 10d; - private static final double ESTTIMETONEXTMARK_Y_OFFSET = -5d; - private static final double LEGTIME_X_OFFSET = 10d; - private static final double LEGTIME_Y_OFFSET = 7d; private static final double BOAT_HEIGHT = 15d; private static final double BOAT_WIDTH = 10d; //Variables for boat logic. @@ -49,16 +43,14 @@ public class BoatGroup extends Group { private Yacht boat; private Group lineGroup = new Group(); private Polygon boatPoly; - private Text teamNameObject; - private Text velocityObject; - private Text estTimeToNextMarkObject; - private Text legTimeObject; private Wake wake; private Double distanceTravelled = 0.0; private Point2D lastPoint; private boolean destinationSet; private Color textColor = Color.RED; + private BoatAnnotations boatAnnotations; + private Boolean isSelected = true; //All boats are initalised as selected /** @@ -89,22 +81,22 @@ public class BoatGroup extends Group { initChildren(color, points); } - /** - * Return a text object with caching and a color applied - * - * @param defaultText The default text to display - * @param fill The text fill color - * @return The text object - */ - private Text getTextObject(String defaultText, Color fill) { - Text text = new Text(defaultText); - - text.setFill(fill); - text.setCacheHint(CacheHint.SPEED); - text.setCache(true); - - return text; - } +// /** +// * Return a text object with caching and a color applied +// * +// * @param defaultText The default text to display +// * @param fill The text fill color +// * @return The text object +// */ +// private Text getTextObject(String defaultText, Color fill) { +// Text text = new Text(defaultText); +// +// text.setFill(fill); +// text.setCacheHint(CacheHint.SPEED); +// text.setCache(true); +// +// return text; +// } /** * Creates the javafx objects that will be the in the group by default. @@ -119,44 +111,51 @@ public class BoatGroup extends Group { boatPoly = new Polygon(points); boatPoly.setFill(color); - boatPoly.setOnMouseEntered(event -> boatPoly.setFill(Color.FLORALWHITE)); - boatPoly.setOnMouseExited(event -> boatPoly.setFill(color)); + boatPoly.setOnMouseEntered(event -> { + boatPoly.setFill(Color.FLORALWHITE); + boatPoly.setStroke(Color.RED); + }); + boatPoly.setOnMouseExited(event -> { + boatPoly.setFill(color); + boatPoly.setStroke(Color.BLACK); + }); boatPoly.setOnMouseClicked(event -> setIsSelected(!isSelected)); boatPoly.setCache(true); boatPoly.setCacheHint(CacheHint.SPEED); - teamNameObject = getTextObject(boat.getShortName(), textColor); - velocityObject = getTextObject(boat.getVelocity().toString(), textColor); +// teamNameObject = getTextObject(boat.getShortName(), textColor); +// velocityObject = getTextObject(boat.getVelocity().toString(), textColor); +// +// teamNameObject.setX(TEAMNAME_X_OFFSET); +// teamNameObject.setY(TEAMNAME_Y_OFFSET); +// teamNameObject.relocate(teamNameObject.getX(), teamNameObject.getY()); +// +// velocityObject.setX(VELOCITY_X_OFFSET); +// velocityObject.setY(VELOCITY_Y_OFFSET); +// velocityObject.relocate(velocityObject.getX(), velocityObject.getY()); +//// +//// updateLastMarkRoundingTime(); +//// updateTimeTillNextMark(); +// +// if (estTimeToNextMarkObject != null) { +// estTimeToNextMarkObject.setX(ESTTIMETONEXTMARK_X_OFFSET); +// estTimeToNextMarkObject.setY(ESTTIMETONEXTMARK_Y_OFFSET); +// estTimeToNextMarkObject +// .relocate(estTimeToNextMarkObject.getX(), estTimeToNextMarkObject.getY()); +// } +// +// if (legTimeObject != null) { +// legTimeObject.setX(LEGTIME_X_OFFSET); +// legTimeObject.setY(LEGTIME_Y_OFFSET); +// legTimeObject.relocate(legTimeObject.getX(), legTimeObject.getY()); +// +// } - teamNameObject.setX(TEAMNAME_X_OFFSET); - teamNameObject.setY(TEAMNAME_Y_OFFSET); - teamNameObject.relocate(teamNameObject.getX(), teamNameObject.getY()); - - velocityObject.setX(VELOCITY_X_OFFSET); - velocityObject.setY(VELOCITY_Y_OFFSET); - velocityObject.relocate(velocityObject.getX(), velocityObject.getY()); - - updateLastMarkRoundingTime(); - updateTimeTillNextMark(); - - if (estTimeToNextMarkObject != null) { - estTimeToNextMarkObject.setX(ESTTIMETONEXTMARK_X_OFFSET); - estTimeToNextMarkObject.setY(ESTTIMETONEXTMARK_Y_OFFSET); - estTimeToNextMarkObject - .relocate(estTimeToNextMarkObject.getX(), estTimeToNextMarkObject.getY()); - } - - if (legTimeObject != null) { - legTimeObject.setX(LEGTIME_X_OFFSET); - legTimeObject.setY(LEGTIME_Y_OFFSET); - legTimeObject.relocate(legTimeObject.getX(), legTimeObject.getY()); - - } + boatAnnotations = new BoatAnnotations(boat, color); wake = new Wake(0, -BOAT_HEIGHT); - super.getChildren() - .addAll(teamNameObject, velocityObject, boatPoly, estTimeToNextMarkObject, - legTimeObject); + super.getChildren().addAll(boatPoly); + super.getChildren().addAll(boatAnnotations.getkiddies()); } /** @@ -181,14 +180,12 @@ public class BoatGroup extends Group { private void moveGroupBy(double dx, double dy) { boatPoly.setLayoutX(boatPoly.getLayoutX() + dx); boatPoly.setLayoutY(boatPoly.getLayoutY() + dy); - teamNameObject.setLayoutX(teamNameObject.getLayoutX() + dx); - teamNameObject.setLayoutY(teamNameObject.getLayoutY() + dy); - velocityObject.setLayoutX(velocityObject.getLayoutX() + dx); - velocityObject.setLayoutY(velocityObject.getLayoutY() + dy); - estTimeToNextMarkObject.setLayoutX(estTimeToNextMarkObject.getLayoutX() + dx); - estTimeToNextMarkObject.setLayoutY(estTimeToNextMarkObject.getLayoutY() + dy); - legTimeObject.setLayoutX(legTimeObject.getLayoutX() + dx); - legTimeObject.setLayoutY(legTimeObject.getLayoutY() + dy); +// boatAnnotations.setLayoutX(boatAnnotations.getLayoutX() + dx); +// boatAnnotations.setLayoutY(boatAnnotations.getLayoutY() + dy); + for (Node node : boatAnnotations.getkiddies()) { + node.setLayoutX(node.getLayoutX() + dx); + node.setLayoutY(node.getLayoutY() + dy); + } wake.setLayoutX(wake.getLayoutX() + dx); wake.setLayoutY(wake.getLayoutY() + dy); } @@ -204,14 +201,14 @@ public class BoatGroup extends Group { rotateTo(rotation); boatPoly.setLayoutX(x); boatPoly.setLayoutY(y); - teamNameObject.setLayoutX(x); - teamNameObject.setLayoutY(y); - velocityObject.setLayoutX(x); - velocityObject.setLayoutY(y); - estTimeToNextMarkObject.setLayoutX(x); - estTimeToNextMarkObject.setLayoutY(y); - legTimeObject.setLayoutX(x); - legTimeObject.setLayoutY(y); +// boatAnnotations.setLayoutX(x); +// boatAnnotations.setLayoutY(y); + int i = 0; + for (Node n : boatAnnotations.getkiddies()) { + n.setLayoutX(x + 10 + i); + n.setLayoutY(y + 10 + i); + i += 10; + } wake.setLayoutX(x); wake.setLayoutY(y); wake.rotate(rotation); @@ -221,41 +218,41 @@ public class BoatGroup extends Group { boatPoly.getTransforms().setAll(new Rotate(rotation)); } - /** - * Updates the time until next mark label, will create a label if one doesn't exist - */ - private void updateTimeTillNextMark() { - if (estTimeToNextMarkObject == null) { - estTimeToNextMarkObject = getTextObject("Next mark: -", textColor); - } - if (boat.getEstimateTimeAtNextMark() != null) { - DateFormat format = new SimpleDateFormat("mm:ss"); - String timeToNextMark = format - .format(boat.getEstimateTimeAtNextMark() - StreamParser.getCurrentTimeLong()); - estTimeToNextMarkObject.setText("Next mark: " + timeToNextMark); - } else { - estTimeToNextMarkObject.setText("Next mark: -"); - } - } +// /** +// * Updates the time until next mark label, will create a label if one doesn't exist +// */ +// private void updateTimeTillNextMark() { +// if (estTimeToNextMarkObject == null) { +// estTimeToNextMarkObject = getTextObject("Next mark: -", textColor); +// } +// if (boat.getEstimateTimeAtNextMark() != null) { +// DateFormat format = new SimpleDateFormat("mm:ss"); +// String timeToNextMark = format +// .format(boat.getEstimateTimeAtNextMark() - StreamParser.getCurrentTimeLong()); +// estTimeToNextMarkObject.setText("Next mark: " + timeToNextMark); +// } else { +// estTimeToNextMarkObject.setText("Next mark: -"); +// } +// } - /** - * Updates the time since last mark rounding, will create a label if one doesn't exist - */ - private void updateLastMarkRoundingTime() { - if (legTimeObject == null) { - legTimeObject = getTextObject("Last mark: -", textColor); - } - - if (boat.getMarkRoundingTime() != null) { - DateFormat format = new SimpleDateFormat("mm:ss"); - String elapsedTime = format - .format(StreamParser.getCurrentTimeLong() - boat.getMarkRoundingTime()); - legTimeObject.setText("Last mark: " + elapsedTime); - } else { - legTimeObject.setText("Last mark: -"); - - } - } +// /** +// * Updates the time since last mark rounding, will create a label if one doesn't exist +// */ +// private void updateLastMarkRoundingTime() { +// if (legTimeObject == null) { +// legTimeObject = getTextObject("Last mark: -", textColor); +// } +// +// if (boat.getMarkRoundingTime() != null) { +// DateFormat format = new SimpleDateFormat("mm:ss"); +// String elapsedTime = format +// .format(StreamParser.getCurrentTimeLong() - boat.getMarkRoundingTime()); +// legTimeObject.setText("Last mark: " + elapsedTime); +// } else { +// legTimeObject.setText("Last mark: -"); +// +// } +// } public void move() { double dx = xIncrement * framesToMove; @@ -298,7 +295,7 @@ public class BoatGroup extends Group { * Calculates the rotational velocity required to reach the rotationalGoal from the * currentRotation. */ - protected Double calculateRotationalVelocity(Double rotationalGoal) { + private Double calculateRotationalVelocity(Double rotationalGoal) { Double rotationalVelocity = 0.0; if (Math.abs(rotationalGoal - lastRotation) > 180) { @@ -344,8 +341,8 @@ public class BoatGroup extends Group { Double rotationalVelocity = calculateRotationalVelocity(rotation); - updateTimeTillNextMark(); - updateLastMarkRoundingTime(); +// updateTimeTillNextMark(); +// updateLastMarkRoundingTime(); if (Math.abs(rotationalVelocity) > 0.075) { rotationalVelocity = 0.0; @@ -353,12 +350,10 @@ public class BoatGroup extends Group { } rotateTo(rotation); + boat.setVelocity(groundSpeed); wake.setRotationalVelocity(rotationalVelocity, groundSpeed); - - velocityObject.setText(String.format("%.2f m/s", groundSpeed)); lastTimeValid = timeValid; isStopped = false; - lastRotation = rotation; } @@ -371,23 +366,24 @@ public class BoatGroup extends Group { setWakeVisible(isSelected); setEstTimeToNextMarkObjectVisible(isSelected); setLegTimeObjectVisible(isSelected); + boatAnnotations.setVisible(isSelected); } public void setTeamNameObjectVisible(Boolean visible) { - teamNameObject.setVisible(visible); + boatAnnotations.setTeamNameObjectVisible(visible); } public void setVelocityObjectVisible(Boolean visible) { - velocityObject.setVisible(visible); + boatAnnotations.setVelocityObjectVisible(visible); } public void setEstTimeToNextMarkObjectVisible(Boolean visible) { - estTimeToNextMarkObject.setVisible(visible); + boatAnnotations.setEstTimeToNextMarkObjectVisible(visible); } public void setLegTimeObjectVisible(Boolean visible) { - legTimeObject.setVisible(visible); + boatAnnotations.setLegTimeObjectVisible(visible); } public void setLineGroupVisible(Boolean visible) { diff --git a/src/main/java/seng302/models/mark/MarkGroup.java b/src/main/java/seng302/fxObjects/MarkGroup.java similarity index 75% rename from src/main/java/seng302/models/mark/MarkGroup.java rename to src/main/java/seng302/fxObjects/MarkGroup.java index c87ae174..464743c9 100644 --- a/src/main/java/seng302/models/mark/MarkGroup.java +++ b/src/main/java/seng302/fxObjects/MarkGroup.java @@ -1,4 +1,4 @@ -package seng302.models.mark; +package seng302.fxObjects; import java.util.ArrayList; import java.util.List; @@ -7,6 +7,10 @@ import javafx.scene.Group; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Line; +import seng302.models.mark.GateMark; +import seng302.models.mark.Mark; +import seng302.models.mark.MarkType; +import seng302.models.mark.SingleMark; /** * Created by CJIRWIN on 26/04/2017. @@ -90,23 +94,28 @@ public class MarkGroup extends Group { { if (mainMark.getMarkType() == MarkType.SINGLE_MARK) { Circle markCircle = (Circle) super.getChildren().get(0); - - markCircle.setCenterX(x); - markCircle.setCenterY(y); + if (Math.abs(markCircle.getCenterX() - x) > 5 || Math.abs(markCircle.getCenterY() - y) > 5) { + markCircle.setCenterX(x); + markCircle.setCenterY(y); + } } else { Circle markCircle1 = (Circle) super.getChildren().get(0); Circle markCircle2 = (Circle) super.getChildren().get(1); Line connectingLine = (Line) super.getChildren().get(2); if (marks.get(0).getId() == raceId) { - markCircle1.setCenterX(x); - markCircle1.setCenterY(y); - connectingLine.setStartX(markCircle1.getCenterX()); - connectingLine.setStartY(markCircle1.getCenterY()); + if (Math.abs(markCircle1.getCenterX() - x) > 5 || Math.abs(markCircle1.getCenterY() - y) > 5) { + markCircle1.setCenterX(x); + markCircle1.setCenterY(y); + connectingLine.setStartX(markCircle1.getCenterX()); + connectingLine.setStartY(markCircle1.getCenterY()); + } } else if (marks.get(1).getId() == raceId) { - markCircle2.setCenterX(x); - markCircle2.setCenterY(y); - connectingLine.setEndX(markCircle2.getCenterX()); - connectingLine.setEndY(markCircle2.getCenterY()); + if (Math.abs(markCircle2.getCenterX() - x) > 5 || Math.abs(markCircle2.getCenterY() - y) > 5) { + markCircle2.setCenterX(x); + markCircle2.setCenterY(y); + connectingLine.setEndX(markCircle2.getCenterX()); + connectingLine.setEndY(markCircle2.getCenterY()); + } } } } diff --git a/src/main/java/seng302/models/Wake.java b/src/main/java/seng302/fxObjects/Wake.java similarity index 97% rename from src/main/java/seng302/models/Wake.java rename to src/main/java/seng302/fxObjects/Wake.java index 886dfba8..706d1b1d 100644 --- a/src/main/java/seng302/models/Wake.java +++ b/src/main/java/seng302/fxObjects/Wake.java @@ -1,4 +1,4 @@ -package seng302.models; +package seng302.fxObjects; import javafx.scene.CacheHint; import javafx.scene.Group; @@ -14,7 +14,7 @@ import javafx.scene.transform.Rotate; class Wake extends Group { //The number of wakes - private int numWakes = 8; + private int numWakes = 6; //The total possible difference between the first wake and the last. Increasing/Decreasing this will make wakes fan out more/less. private final double MAX_DIFF = 75; //Increasing/decreasing this will alter the speed that wakes converge when the heading stop changing. Anything over about 1500 may cause oscillation. @@ -83,7 +83,7 @@ class Wake extends Group { for (Arc arc : arcs) { arc.setRadiusX(rad); arc.setRadiusY(rad); - rad += (20 / numWakes) + (velocity / 2); + rad += (12 / numWakes) + (velocity / 2); } } diff --git a/src/main/java/seng302/models/Yacht.java b/src/main/java/seng302/models/Yacht.java index 8ae5d76c..300c5a1d 100644 --- a/src/main/java/seng302/models/Yacht.java +++ b/src/main/java/seng302/models/Yacht.java @@ -1,9 +1,17 @@ package seng302.models; +import javafx.beans.InvalidationListener; +import javafx.beans.property.*; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.fxml.Initializable; import javafx.scene.paint.Color; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.time.Year; +import java.util.ArrayList; +import java.util.List; /** * Yacht class for the racing boat. @@ -14,7 +22,237 @@ import java.text.SimpleDateFormat; public class Yacht { // Used in boat group private Color colour; - private double velocity; + + private DoubleProperty velocityProperty = new DoubleProperty() { + + private ObservableValue boundValue; + private List changeListeners = new ArrayList<>(); + private List invalidationListeners = new ArrayList<>(); + private double velocity; + + @Override + public void bind(ObservableValue observable) { + boundValue = observable; + } + + @Override + public void unbind() { + boundValue = null; + } + + @Override + public boolean isBound() { + if (boundValue == null) { + return false; + } else { + return true; + } + } + + @Override + public Object getBean() { + return Yacht.this; + } + + @Override + public String getName() { + return "velocity property of " + boatName; + } + + @Override + public double get() { + return velocity; + } + + @Override + public void addListener(ChangeListener listener) { + changeListeners.add(listener); + } + + @Override + public void removeListener(ChangeListener listener) { + changeListeners.remove(listener); + } + + @Override + public void addListener(InvalidationListener listener) { + invalidationListeners.add(listener); + } + + @Override + public void removeListener(InvalidationListener listener) { + invalidationListeners.remove(listener); + } + + @Override + public void set (double newVelocity) { + double oldVelocity = velocity; + velocity = newVelocity; + if (newVelocity >= 0) + for (ChangeListener cl : changeListeners) { + cl.changed(this, oldVelocity, newVelocity); + } + else + for (InvalidationListener il : invalidationListeners) { + il.invalidated(this); + } + if (isBound()) + boundValue.notify(); + } + }; + private LongProperty timeAtNextProperty = new LongProperty() { + + private ObservableValue boundValue; + private List changeListeners = new ArrayList<>(); + private List invalidationListeners = new ArrayList<>(); + private long estimate; + + @Override + public void bind(ObservableValue observable) { + boundValue = observable; + } + + @Override + public void unbind() { + boundValue = null; + } + + @Override + public boolean isBound() { + if (boundValue == null) { + return false; + } else { + return true; + } + } + + @Override + public Object getBean() { + return Yacht.this; + } + + @Override + public String getName() { + return "estimated time to next mark property of " + boatName; + } + + @Override + public long get() { + return estimate; + } + + @Override + public void addListener(ChangeListener listener) { + changeListeners.add(listener); + } + + @Override + public void removeListener(ChangeListener listener) { + changeListeners.remove(listener); + } + + @Override + public void addListener(InvalidationListener listener) { + invalidationListeners.add(listener); + } + + @Override + public void removeListener(InvalidationListener listener) { + invalidationListeners.remove(listener); + } + + @Override + public void set (long newEstimate) { + long oldEstimate = estimate; + estimate = newEstimate; + if (newEstimate >= 0) + for (ChangeListener cl : changeListeners) { + cl.changed(this, oldEstimate, newEstimate); + } + else + for (InvalidationListener il : invalidationListeners) { + il.invalidated(this); + } + if (isBound()) + boundValue.notify(); + } + }; + private LongProperty markRoundingTimeProperty = new LongProperty() { + private ObservableValue boundValue; + private List changeListeners = new ArrayList<>(); + private List invalidationListeners = new ArrayList<>(); + private long roundingTime; + + @Override + public void bind(ObservableValue observable) { + boundValue = observable; + } + + @Override + public void unbind() { + boundValue = null; + } + + @Override + public boolean isBound() { + if (boundValue == null) { + return false; + } else { + return true; + } + } + + @Override + public Object getBean() { + return Yacht.this; + } + + @Override + public String getName() { + return "time from last mark property of " + boatName; + } + + @Override + public long get() { + return roundingTime; + } + + @Override + public void addListener(ChangeListener listener) { + changeListeners.add(listener); + } + + @Override + public void removeListener(ChangeListener listener) { + changeListeners.remove(listener); + } + + @Override + public void addListener(InvalidationListener listener) { + invalidationListeners.add(listener); + } + + @Override + public void removeListener(InvalidationListener listener) { + invalidationListeners.remove(listener); + } + + @Override + public void set (long newTime) { + long oldTime = newTime; + roundingTime = newTime; + if (newTime >= 0) + for (ChangeListener cl : changeListeners) { + cl.changed(this, oldTime, newTime); + } + else + for (InvalidationListener il : invalidationListeners) { + il.invalidated(this); + } + if (isBound()) + boundValue.notify(); + } + }; private String boatType; private Integer sourceID; @@ -27,11 +265,8 @@ public class Yacht { private Integer legNumber; private Integer penaltiesAwarded; private Integer penaltiesServed; - private Long estimateTimeAtNextMark; private Long estimateTimeAtFinish; private String position; - // Mark rounding - private Long markRoundingTime; /** * Used in EventTest and RaceTest. @@ -51,7 +286,7 @@ public class Yacht { */ public Yacht(String boatName, double boatVelocity, String shortName, int id) { this.boatName = boatName; - this.velocity = boatVelocity; + this.velocityProperty.set(boatVelocity); this.shortName = shortName; this.sourceID = id; } @@ -116,14 +351,8 @@ public class Yacht { this.penaltiesServed = penaltiesServed; } - public Long getEstimateTimeAtNextMark() { -// DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); -// return format.format(estimateTimeAtNextMark); - return estimateTimeAtNextMark; - } - public void setEstimateTimeAtNextMark(Long estimateTimeAtNextMark) { - this.estimateTimeAtNextMark = estimateTimeAtNextMark; + timeAtNextProperty.set(estimateTimeAtNextMark); } public String getEstimateTimeAtFinish() { @@ -151,24 +380,29 @@ public class Yacht { this.colour = colour; } - public Double getVelocity() { - return velocity; - } - public void setVelocity(double velocity) { - this.velocity = velocity; + velocityProperty.set(velocity); } - public Long getMarkRoundingTime() { - return markRoundingTime; - } public void setMarkRoundingTime(Long markRoundingTime) { - this.markRoundingTime = markRoundingTime; + markRoundingTimeProperty.set(markRoundingTime); } @Override public String toString() { return boatName; } + + public ReadOnlyDoubleProperty getReadOnlyVelocityProperty () { + return velocityProperty; + } + + public ReadOnlyLongProperty getReadOnlyNextMarkProperty() { + return timeAtNextProperty; + } + + public ReadOnlyLongProperty getReadOnlyMarkRoundingProperty() { + return markRoundingTimeProperty; + } } diff --git a/src/main/java/seng302/models/stream/StreamParser.java b/src/main/java/seng302/models/stream/StreamParser.java index f800379c..9e8c5387 100644 --- a/src/main/java/seng302/models/stream/StreamParser.java +++ b/src/main/java/seng302/models/stream/StreamParser.java @@ -149,7 +149,7 @@ public class StreamParser extends Thread{ } } catch (NullPointerException e){ - System.out.println("Error parsing packet"); + System.out.println("Error parsing packet of type " + packet.getType()); } } From d22d758757edc8989fceef27afc5cf39afef8164 Mon Sep 17 00:00:00 2001 From: Calum Date: Wed, 24 May 2017 03:23:02 +1200 Subject: [PATCH 2/8] Fix issues caused by not updating the time since last mark value frequently. BoatAnnotations now has an update() function that must be called somewhat regularly. --- .../seng302/controllers/CanvasController.java | 1 - .../seng302/fxObjects/BoatAnnotations.java | 64 ++++++++----------- .../java/seng302/fxObjects/BoatGroup.java | 32 +++++----- src/main/java/seng302/fxObjects/Wake.java | 4 +- 4 files changed, 43 insertions(+), 58 deletions(-) diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index 12e29efa..fbd93c8a 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -15,7 +15,6 @@ import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; import javafx.scene.text.Font; -import seng302.fxObjects.AnnotationsThingThatNeedsABetterName; import seng302.fxObjects.BoatGroup; import seng302.models.Colors; import seng302.models.Yacht; diff --git a/src/main/java/seng302/fxObjects/BoatAnnotations.java b/src/main/java/seng302/fxObjects/BoatAnnotations.java index d03d9e47..c43a13d9 100644 --- a/src/main/java/seng302/fxObjects/BoatAnnotations.java +++ b/src/main/java/seng302/fxObjects/BoatAnnotations.java @@ -2,9 +2,7 @@ package seng302.fxObjects; import javafx.scene.CacheHint; import javafx.scene.Group; -import javafx.scene.Node; import javafx.scene.paint.Color; -import javafx.scene.shape.Polygon; import javafx.scene.shape.Rectangle; import javafx.scene.text.Text; import seng302.models.Yacht; @@ -12,8 +10,6 @@ import seng302.models.stream.StreamParser; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.List; /** * Created by cir27 on 23/05/17. @@ -29,28 +25,26 @@ public class BoatAnnotations extends Group{ private static final double LEGTIME_X_OFFSET = 18d; private static final double LEGTIME_Y_OFFSET = 7d; -// private Rectangle background = new Rectangle(); + private Rectangle background = new Rectangle(); private Text teamNameObject; private Text velocityObject; private Text estTimeToNextMarkObject; private Text legTimeObject; - private List kids; - - private boolean visible = true; + private Long lastMarkTime; BoatAnnotations (Yacht boat, Color theme) { super.setCache(true); -// background.setX(15d); -// background.setY(-32d); -// background.setWidth(150); -// background.setHeight(55); -// background.setArcHeight(10); -// background.setArcWidth(10); -// background.setFill(new Color(1, 1, 1, 0.75)); -// background.setStroke(theme); -// background.setStrokeWidth(2); -// background.setCache(true); -// background.setCacheHint(CacheHint.SPEED); + background.setX(15d); + background.setY(-32d); + background.setWidth(150); + background.setHeight(55); + background.setArcHeight(10); + background.setArcWidth(10); + background.setFill(new Color(1, 1, 1, 0.25)); + background.setStroke(theme); + background.setStrokeWidth(2); + background.setCache(true); + background.setCacheHint(CacheHint.SPEED); teamNameObject = getTextObject(boat.getShortName(), theme); teamNameObject.relocate(TEAMNAME_X_OFFSET, TEAMNAME_Y_OFFSET); @@ -81,23 +75,13 @@ public class BoatAnnotations extends Group{ legTimeObject = getTextObject("Last mark: -", theme); legTimeObject.relocate(LEGTIME_X_OFFSET, LEGTIME_Y_OFFSET); boat.getReadOnlyMarkRoundingProperty().addListener((obs, oldTime, newTime) -> { - DateFormat format = new SimpleDateFormat("mm:ss"); - String elapsedTime = format - .format(StreamParser.getCurrentTimeLong() - newTime.longValue()); - legTimeObject.setText("Last mark: " + elapsedTime); + lastMarkTime = newTime.longValue(); }); boat.getReadOnlyMarkRoundingProperty().addListener(obs -> legTimeObject.setText("Last mark: - ") ); - kids = new ArrayList<>(); -// kids.add(background); - kids.add(velocityObject); - kids.add(teamNameObject); - kids.add(estTimeToNextMarkObject); - kids.add(legTimeObject); - -// super.getChildren().addAll(background, teamNameObject, velocityObject, estTimeToNextMarkObject, legTimeObject); + super.getChildren().addAll(background, teamNameObject, velocityObject, estTimeToNextMarkObject, legTimeObject); } /** @@ -110,7 +94,7 @@ public class BoatAnnotations extends Group{ private Text getTextObject(String defaultText, Color fill) { Text text = new Text(defaultText); text.setFill(fill); -// text.setCacheHint(CacheHint.SPEED); + text.setCacheHint(CacheHint.SPEED); text.setCache(true); return text; } @@ -131,12 +115,14 @@ public class BoatAnnotations extends Group{ legTimeObject.setVisible(visible); } - public void toggleVisible() { - visible = !visible; - this.setVisible(visible); - } - - public List getkiddies () { - return kids; + public void update () { + if (lastMarkTime != null) { + DateFormat format = new SimpleDateFormat("mm:ss"); + String elapsedTime = format + .format(StreamParser.getCurrentTimeLong() - lastMarkTime); + legTimeObject.setText("Last mark: " + elapsedTime); + }else { + legTimeObject.setText("Last mark: - "); + } } } diff --git a/src/main/java/seng302/fxObjects/BoatGroup.java b/src/main/java/seng302/fxObjects/BoatGroup.java index 2d659e84..a7b3535a 100644 --- a/src/main/java/seng302/fxObjects/BoatGroup.java +++ b/src/main/java/seng302/fxObjects/BoatGroup.java @@ -154,8 +154,7 @@ public class BoatGroup extends Group { boatAnnotations = new BoatAnnotations(boat, color); wake = new Wake(0, -BOAT_HEIGHT); - super.getChildren().addAll(boatPoly); - super.getChildren().addAll(boatAnnotations.getkiddies()); + super.getChildren().addAll(boatPoly, boatAnnotations); } /** @@ -180,12 +179,12 @@ public class BoatGroup extends Group { private void moveGroupBy(double dx, double dy) { boatPoly.setLayoutX(boatPoly.getLayoutX() + dx); boatPoly.setLayoutY(boatPoly.getLayoutY() + dy); -// boatAnnotations.setLayoutX(boatAnnotations.getLayoutX() + dx); -// boatAnnotations.setLayoutY(boatAnnotations.getLayoutY() + dy); - for (Node node : boatAnnotations.getkiddies()) { - node.setLayoutX(node.getLayoutX() + dx); - node.setLayoutY(node.getLayoutY() + dy); - } + boatAnnotations.setLayoutX(boatAnnotations.getLayoutX() + dx); + boatAnnotations.setLayoutY(boatAnnotations.getLayoutY() + dy); +// for (Node node : boatAnnotations.getkiddies()) { +// node.setLayoutX(node.getLayoutX() + dx); +// node.setLayoutY(node.getLayoutY() + dy); +// } wake.setLayoutX(wake.getLayoutX() + dx); wake.setLayoutY(wake.getLayoutY() + dy); } @@ -201,14 +200,14 @@ public class BoatGroup extends Group { rotateTo(rotation); boatPoly.setLayoutX(x); boatPoly.setLayoutY(y); -// boatAnnotations.setLayoutX(x); -// boatAnnotations.setLayoutY(y); - int i = 0; - for (Node n : boatAnnotations.getkiddies()) { - n.setLayoutX(x + 10 + i); - n.setLayoutY(y + 10 + i); - i += 10; - } + boatAnnotations.setLayoutX(x); + boatAnnotations.setLayoutY(y); +// int i = 0; +// for (Node n : boatAnnotations.getkiddies()) { +// n.setLayoutX(x + 10 + i); +// n.setLayoutY(y + 10 + i); +// i += 10; +// } wake.setLayoutX(x); wake.setLayoutY(y); wake.rotate(rotation); @@ -355,6 +354,7 @@ public class BoatGroup extends Group { lastTimeValid = timeValid; isStopped = false; lastRotation = rotation; + boatAnnotations.update(); } diff --git a/src/main/java/seng302/fxObjects/Wake.java b/src/main/java/seng302/fxObjects/Wake.java index 706d1b1d..dbeef2ee 100644 --- a/src/main/java/seng302/fxObjects/Wake.java +++ b/src/main/java/seng302/fxObjects/Wake.java @@ -14,7 +14,7 @@ import javafx.scene.transform.Rotate; class Wake extends Group { //The number of wakes - private int numWakes = 6; + private int numWakes = 8; //The total possible difference between the first wake and the last. Increasing/Decreasing this will make wakes fan out more/less. private final double MAX_DIFF = 75; //Increasing/decreasing this will alter the speed that wakes converge when the heading stop changing. Anything over about 1500 may cause oscillation. @@ -83,7 +83,7 @@ class Wake extends Group { for (Arc arc : arcs) { arc.setRadiusX(rad); arc.setRadiusY(rad); - rad += (12 / numWakes) + (velocity / 2); + rad += (10 / numWakes) + (velocity / 2); } } From 14a7305a2dff17f5c63e801662c58e469006ba16 Mon Sep 17 00:00:00 2001 From: Calum Date: Wed, 24 May 2017 17:17:06 +1200 Subject: [PATCH 3/8] Made FPS marker not draw on canvas. Have to revert the use of the observer pattern since there is no time to change how and when data is passed. --- .../seng302/controllers/CanvasController.java | 20 ++++---- .../seng302/fxObjects/BoatAnnotations.java | 9 +++- .../java/seng302/fxObjects/BoatGroup.java | 50 +++---------------- src/main/java/seng302/fxObjects/Wake.java | 2 +- .../seng302/models/stream/StreamParser.java | 2 + 5 files changed, 27 insertions(+), 56 deletions(-) diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index fbd93c8a..b349c111 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -14,7 +14,7 @@ import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; -import javafx.scene.text.Font; +import javafx.scene.text.Text; import seng302.fxObjects.BoatGroup; import seng302.models.Colors; import seng302.models.Yacht; @@ -64,6 +64,7 @@ public class CanvasController { private List markGroups = new ArrayList<>(); private List boatGroups = new ArrayList<>(); + private Text FPSdisplay = new Text(); //FRAME RATE private Double frameRate = 60.0; @@ -102,6 +103,10 @@ public class CanvasController { gc.fillRect(0,0, CANVAS_WIDTH, CANVAS_HEIGHT); gc.restore(); fitMarksToCanvas(); + FPSdisplay.setLayoutX(5); + FPSdisplay.setLayoutY(20); + FPSdisplay.setStrokeWidth(2); + group.getChildren().add(FPSdisplay); // TODO: 1/05/17 wmu16 - Change this call to now draw the marks as from the xml @@ -315,17 +320,10 @@ public class CanvasController { private void drawFps(int fps){ if (raceViewController.isDisplayFps()){ - gc.clearRect(5,5,50,20); - gc.setFill(Color.SKYBLUE); - gc.fillRect(4,4,51,21); - gc.setFill(Color.BLACK); - gc.setFont(new Font(14)); - gc.setLineWidth(3); - gc.fillText(fps + " FPS", 5, 20); + FPSdisplay.setVisible(true); + FPSdisplay.setText(String.format("%d FPS", fps)); } else { - gc.clearRect(5,5,50,20); - gc.setFill(Color.SKYBLUE); - gc.fillRect(4,4,51,21); + FPSdisplay.setVisible(false); } } diff --git a/src/main/java/seng302/fxObjects/BoatAnnotations.java b/src/main/java/seng302/fxObjects/BoatAnnotations.java index c43a13d9..460e3912 100644 --- a/src/main/java/seng302/fxObjects/BoatAnnotations.java +++ b/src/main/java/seng302/fxObjects/BoatAnnotations.java @@ -32,6 +32,13 @@ public class BoatAnnotations extends Group{ private Text legTimeObject; private Long lastMarkTime; + public enum Annotations { + TEAM_NAME, + VELOCITY_OBJECT, + TTNEXT, + LEG_TIME, + } + BoatAnnotations (Yacht boat, Color theme) { super.setCache(true); background.setX(15d); @@ -40,7 +47,7 @@ public class BoatAnnotations extends Group{ background.setHeight(55); background.setArcHeight(10); background.setArcWidth(10); - background.setFill(new Color(1, 1, 1, 0.25)); + background.setFill(new Color(1, 1, 1, 0.35)); background.setStroke(theme); background.setStrokeWidth(2); background.setCache(true); diff --git a/src/main/java/seng302/fxObjects/BoatGroup.java b/src/main/java/seng302/fxObjects/BoatGroup.java index a7b3535a..98241045 100644 --- a/src/main/java/seng302/fxObjects/BoatGroup.java +++ b/src/main/java/seng302/fxObjects/BoatGroup.java @@ -48,6 +48,8 @@ public class BoatGroup extends Group { private Point2D lastPoint; private boolean destinationSet; private Color textColor = Color.RED; + private double rotationalVelocity; + private double rotation; private BoatAnnotations boatAnnotations; @@ -122,35 +124,6 @@ public class BoatGroup extends Group { boatPoly.setOnMouseClicked(event -> setIsSelected(!isSelected)); boatPoly.setCache(true); boatPoly.setCacheHint(CacheHint.SPEED); - -// teamNameObject = getTextObject(boat.getShortName(), textColor); -// velocityObject = getTextObject(boat.getVelocity().toString(), textColor); -// -// teamNameObject.setX(TEAMNAME_X_OFFSET); -// teamNameObject.setY(TEAMNAME_Y_OFFSET); -// teamNameObject.relocate(teamNameObject.getX(), teamNameObject.getY()); -// -// velocityObject.setX(VELOCITY_X_OFFSET); -// velocityObject.setY(VELOCITY_Y_OFFSET); -// velocityObject.relocate(velocityObject.getX(), velocityObject.getY()); -//// -//// updateLastMarkRoundingTime(); -//// updateTimeTillNextMark(); -// -// if (estTimeToNextMarkObject != null) { -// estTimeToNextMarkObject.setX(ESTTIMETONEXTMARK_X_OFFSET); -// estTimeToNextMarkObject.setY(ESTTIMETONEXTMARK_Y_OFFSET); -// estTimeToNextMarkObject -// .relocate(estTimeToNextMarkObject.getX(), estTimeToNextMarkObject.getY()); -// } -// -// if (legTimeObject != null) { -// legTimeObject.setX(LEGTIME_X_OFFSET); -// legTimeObject.setY(LEGTIME_Y_OFFSET); -// legTimeObject.relocate(legTimeObject.getX(), legTimeObject.getY()); -// -// } - boatAnnotations = new BoatAnnotations(boat, color); wake = new Wake(0, -BOAT_HEIGHT); @@ -181,10 +154,6 @@ public class BoatGroup extends Group { boatPoly.setLayoutY(boatPoly.getLayoutY() + dy); boatAnnotations.setLayoutX(boatAnnotations.getLayoutX() + dx); boatAnnotations.setLayoutY(boatAnnotations.getLayoutY() + dy); -// for (Node node : boatAnnotations.getkiddies()) { -// node.setLayoutX(node.getLayoutX() + dx); -// node.setLayoutY(node.getLayoutY() + dy); -// } wake.setLayoutX(wake.getLayoutX() + dx); wake.setLayoutY(wake.getLayoutY() + dy); } @@ -214,6 +183,7 @@ public class BoatGroup extends Group { } private void rotateTo(double rotation) { + this.rotation = rotation; boatPoly.getTransforms().setAll(new Rotate(rotation)); } @@ -286,7 +256,7 @@ public class BoatGroup extends Group { lastPoint = new Point2D(boatPoly.getLayoutX(), boatPoly.getLayoutY()); } } - + rotateTo(rotation + rotationalVelocity * 1000 / 60); wake.updatePosition(1000 / 60); } @@ -295,7 +265,7 @@ public class BoatGroup extends Group { * currentRotation. */ private Double calculateRotationalVelocity(Double rotationalGoal) { - Double rotationalVelocity = 0.0; + Double rotationalVelocity; if (Math.abs(rotationalGoal - lastRotation) > 180) { if (rotationalGoal - lastRotation >= 0.0) { @@ -306,12 +276,6 @@ public class BoatGroup extends Group { } else { rotationalVelocity = (rotationalGoal - lastRotation) / 200; } - - //Sometimes the rotation is too large to be realistic. In that case just do it instantly. - if (Math.abs(rotationalVelocity) > 1) { - rotationalVelocity = 0.0; - } - return rotationalVelocity; } @@ -338,7 +302,7 @@ public class BoatGroup extends Group { destinationSet = true; - Double rotationalVelocity = calculateRotationalVelocity(rotation); + rotationalVelocity = calculateRotationalVelocity(rotation); // updateTimeTillNextMark(); // updateLastMarkRoundingTime(); @@ -348,7 +312,7 @@ public class BoatGroup extends Group { wake.rotate(rotation); } - rotateTo(rotation); + //rotateTo(rotation); boat.setVelocity(groundSpeed); wake.setRotationalVelocity(rotationalVelocity, groundSpeed); lastTimeValid = timeValid; diff --git a/src/main/java/seng302/fxObjects/Wake.java b/src/main/java/seng302/fxObjects/Wake.java index dbeef2ee..dfa7486f 100644 --- a/src/main/java/seng302/fxObjects/Wake.java +++ b/src/main/java/seng302/fxObjects/Wake.java @@ -40,7 +40,7 @@ class Wake extends Group { //Default triangle is -110 deg out of phase with a default wake and has angle of 40 deg. arc = new Arc(0, 0, 0, 0, -110, 40); arc.setCache(true); - arc.setCacheHint(CacheHint.SPEED); + arc.setCacheHint(CacheHint.SCALE_AND_ROTATE); arc.setType(ArcType.OPEN); arc.setStroke(new Color(0.18, 0.7, 1.0, 1.0 + (-0.99 / numWakes * i))); arc.setStrokeWidth(3.0); diff --git a/src/main/java/seng302/models/stream/StreamParser.java b/src/main/java/seng302/models/stream/StreamParser.java index 9e8c5387..506e5a09 100644 --- a/src/main/java/seng302/models/stream/StreamParser.java +++ b/src/main/java/seng302/models/stream/StreamParser.java @@ -394,6 +394,8 @@ public class StreamParser extends Thread{ //type 1 is a racing yacht and type 3 is a mark, needed for updating positions of the mark and boat if (deviceType == 1){ + Yacht boat = boats.get((int) boatId); + boat.setVelocity(groundSpeed); BoatPositionPacket boatPacket = new BoatPositionPacket(boatId, timeValid, lat, lon, heading, groundSpeed); //add a new priority que to the boatPositions HashMap From 765ea06c3b53bb3a2500c5b7e53448f79a1388d3 Mon Sep 17 00:00:00 2001 From: Calum Date: Thu, 25 May 2017 01:02:33 +1200 Subject: [PATCH 4/8] Several tweaks and improvements to a annotations and other visual aspects of them program. Fixed bug causing minimization crashes. #implement #refactor #issue[23] --- src/main/java/seng302/App.java | 4 +- .../seng302/controllers/CanvasController.java | 86 +++--- .../controllers/RaceViewController.java | 65 +---- .../seng302/fxObjects/BoatAnnotations.java | 136 +++++---- .../java/seng302/fxObjects/BoatGroup.java | 116 +------- .../java/seng302/fxObjects/MarkGroup.java | 1 + src/main/java/seng302/fxObjects/Wake.java | 66 +++-- .../java/seng302/models/TimelineInfo.java | 31 -- src/main/java/seng302/models/Yacht.java | 266 ++---------------- 9 files changed, 182 insertions(+), 589 deletions(-) delete mode 100644 src/main/java/seng302/models/TimelineInfo.java diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index b66376c5..57a5e1e6 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -63,8 +63,8 @@ public class App extends Application { //Change the StreamReceiver in this else block to change the default data source. else{ // sr = new StreamReceiver("localhost", 4949, "RaceStream"); - sr = new StreamReceiver("livedata.americascup.com", 4941, "RaceStream"); -// sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941, "RaceStream"); +// sr = new StreamReceiver("livedata.americascup.com", 4941, "RaceStream"); + sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941, "RaceStream"); } sr.start(); diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index b349c111..13340429 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -14,6 +14,7 @@ import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; +import javafx.scene.shape.Polygon; import javafx.scene.text.Text; import seng302.fxObjects.BoatGroup; import seng302.models.Colors; @@ -65,6 +66,7 @@ public class CanvasController { private List markGroups = new ArrayList<>(); private List boatGroups = new ArrayList<>(); private Text FPSdisplay = new Text(); + private Polygon raceBorder = new Polygon(); //FRAME RATE private Double frameRate = 60.0; @@ -107,6 +109,7 @@ public class CanvasController { FPSdisplay.setLayoutY(20); FPSdisplay.setStrokeWidth(2); group.getChildren().add(FPSdisplay); + group.getChildren().add(raceBorder); // TODO: 1/05/17 wmu16 - Change this call to now draw the marks as from the xml @@ -114,29 +117,34 @@ public class CanvasController { initializeMarks(); timer = new AnimationTimer() { + private long lastTime = 0; + @Override public void handle(long now) { - //fps stuff - long oldFrameTime = frameTimes[frameTimeIndex] ; - frameTimes[frameTimeIndex] = now ; - frameTimeIndex = (frameTimeIndex + 1) % frameTimes.length ; - if (frameTimeIndex == 0) { - arrayFilled = true ; - } - long elapsedNanos; - if (arrayFilled) { - elapsedNanos = now - oldFrameTime ; - long elapsedNanosPerFrame = elapsedNanos / frameTimes.length ; - frameRate = 1_000_000_000.0 / elapsedNanosPerFrame ; - drawFps(frameRate.intValue()); - } - - // TODO: 1/05/17 cir27 - Make the RaceObjects update on the actual delay. - elapsedNanos = 1000 / 60; - updateGroups(); - if (StreamParser.isRaceFinished()) { - this.stop(); + if (lastTime == 0) { + lastTime = now; + } else { + if (now - lastTime >= (1e8 / 60)) { //Fix for framerate going above 60 when minimized + long oldFrameTime = frameTimes[frameTimeIndex] ; + frameTimes[frameTimeIndex] = now ; + frameTimeIndex = (frameTimeIndex + 1) % frameTimes.length ; + if (frameTimeIndex == 0) { + arrayFilled = true ; + } + long elapsedNanos; + if (arrayFilled) { + elapsedNanos = now - oldFrameTime ; + long elapsedNanosPerFrame = elapsedNanos / frameTimes.length ; + frameRate = 1_000_000_000.0 / elapsedNanosPerFrame ; + drawFps(frameRate.intValue()); + } + updateGroups(frameRate); + if (StreamParser.isRaceFinished()) { + this.stop(); + } + lastTime = now; + } } } }; @@ -153,37 +161,19 @@ public class CanvasController { private void addRaceBorder() { XMLParser.RaceXMLObject raceXMLObject = StreamParser.getXmlObject().getRaceXML(); ArrayList courseLimits = raceXMLObject.getCourseLimit(); - gc.setStroke(Color.DARKBLUE); - gc.setLineWidth(3); - double[] xBoundaryPoints = new double[courseLimits.size()]; - double[] yBoundaryPoints = new double[courseLimits.size()]; - for (int i = 0; i < courseLimits.size() - 1; i++) { - Limit thisPoint1 = courseLimits.get(i); - SingleMark thisMark1 = new SingleMark("", thisPoint1.getLat(), thisPoint1.getLng(), thisPoint1.getSeqID()); - Limit thisPoint2 = courseLimits.get(i+1); - SingleMark thisMark2 = new SingleMark("", thisPoint2.getLat(), thisPoint2.getLng(), thisPoint2.getSeqID()); - Point2D borderPoint1 = findScaledXY(thisMark1); - Point2D borderPoint2 = findScaledXY(thisMark2); - gc.strokeLine(borderPoint1.getX(), borderPoint1.getY(), - borderPoint2.getX(), borderPoint2.getY()); - xBoundaryPoints[i] = borderPoint1.getX(); - yBoundaryPoints[i] = borderPoint1.getY(); + raceBorder.setStroke(new Color(0.0f, 0.0f, 0.74509807f, 1)); + raceBorder.setStrokeWidth(3); + raceBorder.setFill(new Color(0,0,0,0)); + List boundaryPoints = new ArrayList<>(); + for (Limit limit : courseLimits) { + Point2D location = findScaledXY(limit.getLat(), limit.getLng()); + boundaryPoints.add(location.getX()); + boundaryPoints.add(location.getY()); } - Limit thisPoint1 = courseLimits.get(courseLimits.size()-1); - SingleMark thisMark1 = new SingleMark("", thisPoint1.getLat(), thisPoint1.getLng(), thisPoint1.getSeqID()); - Limit thisPoint2 = courseLimits.get(0); - SingleMark thisMark2 = new SingleMark("", thisPoint2.getLat(), thisPoint2.getLng(), thisPoint2.getSeqID()); - Point2D borderPoint1 = findScaledXY(thisMark1); - Point2D borderPoint2 = findScaledXY(thisMark2); - gc.strokeLine(borderPoint1.getX(), borderPoint1.getY(), - borderPoint2.getX(), borderPoint2.getY()); - xBoundaryPoints[courseLimits.size()-1] = borderPoint1.getX(); - yBoundaryPoints[courseLimits.size()-1] = borderPoint1.getY(); - gc.setFill(Color.LIGHTBLUE); - gc.fillPolygon(xBoundaryPoints,yBoundaryPoints,yBoundaryPoints.length); + raceBorder.getPoints().setAll(boundaryPoints); } - private void updateGroups(){ + private void updateGroups(double frameRate){ for (BoatGroup boatGroup : boatGroups) { // some raceObjects will have multiple ID's (for instance gate marks) //checking if the current "ID" has any updates associated with it diff --git a/src/main/java/seng302/controllers/RaceViewController.java b/src/main/java/seng302/controllers/RaceViewController.java index f2f60ed5..9c55f9a2 100644 --- a/src/main/java/seng302/controllers/RaceViewController.java +++ b/src/main/java/seng302/controllers/RaceViewController.java @@ -327,76 +327,31 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel return displayFps; } - /** - * Display the important annotations for a specific BoatGroup - * @param bg The boat group to set the annotations for - */ - private void setBoatGroupImportantAnnotations(BoatGroup bg) { - if (importantAnnotations.getAnnotationState(Annotation.NAME)) { - bg.setTeamNameObjectVisible(true); - } else { - bg.setTeamNameObjectVisible(false); - } - - if (importantAnnotations.getAnnotationState(Annotation.SPEED)) { - bg.setVelocityObjectVisible(true); - } else { - bg.setVelocityObjectVisible(false); - } - - if (importantAnnotations.getAnnotationState(Annotation.TRACK)) { - bg.setLineGroupVisible(true); - } else { - bg.setLineGroupVisible(false); - } - - if (importantAnnotations.getAnnotationState(Annotation.WAKE)) { - bg.setWakeVisible(true); - } else { - bg.setWakeVisible(false); - } - //TODO fix boat annotations with new boatgroup - if (importantAnnotations.getAnnotationState(Annotation.ESTTIMETONEXTMARK)) { - bg.setEstTimeToNextMarkObjectVisible(true); - } else { - bg.setEstTimeToNextMarkObjectVisible(false); - } - - if (importantAnnotations.getAnnotationState(Annotation.LEGTIME)) { - bg.setLegTimeObjectVisible(true); - } else { - bg.setLegTimeObjectVisible(false); - } - } - private void setAnnotations(Integer annotationLevel) { switch (annotationLevel) { // No Annotations case 0: for (BoatGroup bg : includedCanvasController.getBoatGroups()) { - bg.setTeamNameObjectVisible(false); - bg.setVelocityObjectVisible(false); - bg.setEstTimeToNextMarkObjectVisible(false); - bg.setLegTimeObjectVisible(false); - bg.setLineGroupVisible(false); - bg.setWakeVisible(false); + bg.setVisibility(false, false, false, false, false, false); } break; // Important Annotations case 1: for (BoatGroup bg : includedCanvasController.getBoatGroups()) { - setBoatGroupImportantAnnotations(bg); + bg.setVisibility( + importantAnnotations.getAnnotationState(Annotation.NAME), + importantAnnotations.getAnnotationState(Annotation.SPEED), + importantAnnotations.getAnnotationState(Annotation.ESTTIMETONEXTMARK), + importantAnnotations.getAnnotationState(Annotation.LEGTIME), + importantAnnotations.getAnnotationState(Annotation.TRACK), + importantAnnotations.getAnnotationState(Annotation.WAKE) + ); } break; // All Annotations case 2: for (BoatGroup bg : includedCanvasController.getBoatGroups()) { - bg.setTeamNameObjectVisible(true); - bg.setVelocityObjectVisible(true); - bg.setEstTimeToNextMarkObjectVisible(true); - bg.setLegTimeObjectVisible(true); - bg.setLineGroupVisible(true); - bg.setWakeVisible(true); + bg.setVisibility(true, true, true, true, true, true); } break; } diff --git a/src/main/java/seng302/fxObjects/BoatAnnotations.java b/src/main/java/seng302/fxObjects/BoatAnnotations.java index 460e3912..90ec3fb6 100644 --- a/src/main/java/seng302/fxObjects/BoatAnnotations.java +++ b/src/main/java/seng302/fxObjects/BoatAnnotations.java @@ -5,6 +5,7 @@ import javafx.scene.Group; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.text.Text; +import seng302.controllers.annotations.Annotation; import seng302.models.Yacht; import seng302.models.stream.StreamParser; @@ -12,81 +13,56 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; /** - * Created by cir27 on 23/05/17. + * Collection of annotations for boats. */ -public class BoatAnnotations extends Group{ +class BoatAnnotations extends Group{ - private static final double TEAMNAME_X_OFFSET = 18d; - private static final double TEAMNAME_Y_OFFSET = -29d; - private static final double VELOCITY_X_OFFSET = 18d; - private static final double VELOCITY_Y_OFFSET = -17d; - private static final double ESTTIMETONEXTMARK_X_OFFSET = 18d; - private static final double ESTTIMETONEXTMARK_Y_OFFSET = -5d; - private static final double LEGTIME_X_OFFSET = 18d; - private static final double LEGTIME_Y_OFFSET = 7d; + //Text offset constants + private static final double X_OFFSET_TEXT = 18d; + private static final double Y_OFFSET_TEXT_INIT = -29d; + private static final double Y_OFFSET_PER_TEXT = 12d; + //Background constants + private static final double TEXT_BUFFER = 3; + private static final double BACKGROUND_X = X_OFFSET_TEXT - TEXT_BUFFER; + private static final double BACKGROUND_Y = Y_OFFSET_TEXT_INIT - TEXT_BUFFER; + private static final double BACKGROUND_H_PER_TEXT = 9.5d; + private static final double BACKGROUND_W = 125d; + private static final double BACKGROUND_ARC_SIZE = 10; private Rectangle background = new Rectangle(); private Text teamNameObject; private Text velocityObject; private Text estTimeToNextMarkObject; private Text legTimeObject; - private Long lastMarkTime; - public enum Annotations { - TEAM_NAME, - VELOCITY_OBJECT, - TTNEXT, - LEG_TIME, - } + private Yacht boat; BoatAnnotations (Yacht boat, Color theme) { super.setCache(true); - background.setX(15d); - background.setY(-32d); - background.setWidth(150); - background.setHeight(55); - background.setArcHeight(10); - background.setArcWidth(10); - background.setFill(new Color(1, 1, 1, 0.35)); + this.boat = boat; + background.setX(BACKGROUND_X); + background.setY(BACKGROUND_Y); + background.setWidth(BACKGROUND_W); + background.setHeight(Math.abs(BACKGROUND_X) + TEXT_BUFFER + BACKGROUND_H_PER_TEXT * 4); + background.setArcHeight(BACKGROUND_ARC_SIZE); + background.setArcWidth(BACKGROUND_ARC_SIZE); + background.setFill(new Color(1, 1, 1, 0.5)); background.setStroke(theme); background.setStrokeWidth(2); background.setCache(true); background.setCacheHint(CacheHint.SPEED); teamNameObject = getTextObject(boat.getShortName(), theme); - teamNameObject.relocate(TEAMNAME_X_OFFSET, TEAMNAME_Y_OFFSET); + teamNameObject.relocate(X_OFFSET_TEXT, Y_OFFSET_TEXT_INIT + Y_OFFSET_PER_TEXT); - velocityObject = getTextObject("", theme); - velocityObject.relocate(VELOCITY_X_OFFSET, VELOCITY_Y_OFFSET); - //On change listener - boat.getReadOnlyVelocityProperty().addListener((obs, oldVal, newVal) -> - velocityObject.setText(String.format("%.2f m/s", newVal.doubleValue())) - ); - //Invalidation listener - boat.getReadOnlyVelocityProperty().addListener(obs -> - velocityObject.setText("") - ); + velocityObject = getTextObject("0 m/s", theme); + velocityObject.relocate(X_OFFSET_TEXT, Y_OFFSET_TEXT_INIT + Y_OFFSET_PER_TEXT * 2); estTimeToNextMarkObject = getTextObject("Next mark: ", theme); - estTimeToNextMarkObject.relocate(ESTTIMETONEXTMARK_X_OFFSET, ESTTIMETONEXTMARK_Y_OFFSET); - boat.getReadOnlyNextMarkProperty().addListener((obs, oldVal, newVal) -> { - DateFormat format = new SimpleDateFormat("mm:ss"); - String timeToNextMark = format - .format(newVal.longValue() - StreamParser.getCurrentTimeLong()); - estTimeToNextMarkObject.setText("Next mark: " + timeToNextMark); - }); - boat.getReadOnlyNextMarkProperty().addListener(obs -> - estTimeToNextMarkObject.setText("Next mark: - ") - ); + estTimeToNextMarkObject.relocate(X_OFFSET_TEXT, Y_OFFSET_TEXT_INIT + Y_OFFSET_PER_TEXT * 3); legTimeObject = getTextObject("Last mark: -", theme); - legTimeObject.relocate(LEGTIME_X_OFFSET, LEGTIME_Y_OFFSET); - boat.getReadOnlyMarkRoundingProperty().addListener((obs, oldTime, newTime) -> { - lastMarkTime = newTime.longValue(); - }); - boat.getReadOnlyMarkRoundingProperty().addListener(obs -> - legTimeObject.setText("Last mark: - ") - ); + legTimeObject.relocate(X_OFFSET_TEXT, Y_OFFSET_TEXT_INIT + Y_OFFSET_PER_TEXT * 4); super.getChildren().addAll(background, teamNameObject, velocityObject, estTimeToNextMarkObject, legTimeObject); } @@ -106,30 +82,52 @@ public class BoatAnnotations extends Group{ return text; } - public void setTeamNameObjectVisible(Boolean visible) { - teamNameObject.setVisible(visible); - } + void update () { + velocityObject.setText(String.format(String.format("%.2f m/s", boat.getVelocity()))); - public void setVelocityObjectVisible(Boolean visible) { - velocityObject.setVisible(visible); - } + if (boat.getTimeTillNext() != null) { + DateFormat format = new SimpleDateFormat("mm:ss"); + String timeToNextMark = format + .format(boat.getTimeTillNext() - StreamParser.getCurrentTimeLong()); + estTimeToNextMarkObject.setText("Next mark: " + timeToNextMark); + } else { + estTimeToNextMarkObject.setText("Next mark: -"); + } - public void setEstTimeToNextMarkObjectVisible(Boolean visible) { - estTimeToNextMarkObject.setVisible(visible); - } - - public void setLegTimeObjectVisible(Boolean visible) { - legTimeObject.setVisible(visible); - } - - public void update () { - if (lastMarkTime != null) { + if (boat.getMarkRoundTime() != null) { DateFormat format = new SimpleDateFormat("mm:ss"); String elapsedTime = format - .format(StreamParser.getCurrentTimeLong() - lastMarkTime); + .format(StreamParser.getCurrentTimeLong() - boat.getMarkRoundTime()); legTimeObject.setText("Last mark: " + elapsedTime); }else { legTimeObject.setText("Last mark: - "); } } + + void setVisibile (boolean nameVisibility, boolean speedVisibility, + boolean estTimeVisibility, boolean lastMarkVisibility) { + int totalVisible = 0; + totalVisible = updateVisibility(nameVisibility, teamNameObject, totalVisible); + totalVisible = updateVisibility(speedVisibility, velocityObject, totalVisible); + totalVisible = updateVisibility(estTimeVisibility, estTimeToNextMarkObject, totalVisible); + totalVisible = updateVisibility(lastMarkVisibility, legTimeObject, totalVisible); + if (totalVisible != 0) { + background.setVisible(true); + background.setHeight(Math.abs(BACKGROUND_X) + TEXT_BUFFER + BACKGROUND_H_PER_TEXT * totalVisible); + } else { + background.setVisible(false); + } + } + + private int updateVisibility (boolean visibility, Text text, int totalVisible) { + if (visibility){ + totalVisible ++; + text.setVisible(true); + text.setLayoutX(X_OFFSET_TEXT); + text.setLayoutY(Y_OFFSET_TEXT_INIT + Y_OFFSET_PER_TEXT * totalVisible); + } else { + text.setVisible(false); + } + return totalVisible; + } } diff --git a/src/main/java/seng302/fxObjects/BoatGroup.java b/src/main/java/seng302/fxObjects/BoatGroup.java index 98241045..3efc9a7e 100644 --- a/src/main/java/seng302/fxObjects/BoatGroup.java +++ b/src/main/java/seng302/fxObjects/BoatGroup.java @@ -47,13 +47,9 @@ public class BoatGroup extends Group { private Double distanceTravelled = 0.0; private Point2D lastPoint; private boolean destinationSet; - private Color textColor = Color.RED; - private double rotationalVelocity; - private double rotation; + private BoatAnnotations boatAnnotations;; - private BoatAnnotations boatAnnotations; - - private Boolean isSelected = true; //All boats are initalised as selected + private Boolean isSelected = true; //All boats are initialised as selected /** * Creates a BoatGroup with the default triangular boat polygon. @@ -63,9 +59,9 @@ public class BoatGroup extends Group { * @param color The colour of the boat polygon and the trailing line. */ public BoatGroup(Yacht boat, Color color) { + destinationSet = false; this.boat = boat; initChildren(color); - this.textColor = color; } /** @@ -79,27 +75,11 @@ public class BoatGroup extends Group { * polygon. */ public BoatGroup(Yacht boat, Color color, double... points) { + destinationSet = false; this.boat = boat; initChildren(color, points); } -// /** -// * Return a text object with caching and a color applied -// * -// * @param defaultText The default text to display -// * @param fill The text fill color -// * @return The text object -// */ -// private Text getTextObject(String defaultText, Color fill) { -// Text text = new Text(defaultText); -// -// text.setFill(fill); -// text.setCacheHint(CacheHint.SPEED); -// text.setCache(true); -// -// return text; -// } - /** * Creates the javafx objects that will be the in the group by default. * @@ -108,9 +88,6 @@ public class BoatGroup extends Group { * polygon. */ private void initChildren(Color color, double... points) { - textColor = color; - destinationSet = false; - boatPoly = new Polygon(points); boatPoly.setFill(color); boatPoly.setOnMouseEntered(event -> { @@ -125,7 +102,6 @@ public class BoatGroup extends Group { boatPoly.setCache(true); boatPoly.setCacheHint(CacheHint.SPEED); boatAnnotations = new BoatAnnotations(boat, color); - wake = new Wake(0, -BOAT_HEIGHT); super.getChildren().addAll(boatPoly, boatAnnotations); } @@ -171,58 +147,16 @@ public class BoatGroup extends Group { boatPoly.setLayoutY(y); boatAnnotations.setLayoutX(x); boatAnnotations.setLayoutY(y); -// int i = 0; -// for (Node n : boatAnnotations.getkiddies()) { -// n.setLayoutX(x + 10 + i); -// n.setLayoutY(y + 10 + i); -// i += 10; -// } + wake.setLayoutX(x); wake.setLayoutY(y); wake.rotate(rotation); } private void rotateTo(double rotation) { - this.rotation = rotation; boatPoly.getTransforms().setAll(new Rotate(rotation)); } -// /** -// * Updates the time until next mark label, will create a label if one doesn't exist -// */ -// private void updateTimeTillNextMark() { -// if (estTimeToNextMarkObject == null) { -// estTimeToNextMarkObject = getTextObject("Next mark: -", textColor); -// } -// if (boat.getEstimateTimeAtNextMark() != null) { -// DateFormat format = new SimpleDateFormat("mm:ss"); -// String timeToNextMark = format -// .format(boat.getEstimateTimeAtNextMark() - StreamParser.getCurrentTimeLong()); -// estTimeToNextMarkObject.setText("Next mark: " + timeToNextMark); -// } else { -// estTimeToNextMarkObject.setText("Next mark: -"); -// } -// } - -// /** -// * Updates the time since last mark rounding, will create a label if one doesn't exist -// */ -// private void updateLastMarkRoundingTime() { -// if (legTimeObject == null) { -// legTimeObject = getTextObject("Last mark: -", textColor); -// } -// -// if (boat.getMarkRoundingTime() != null) { -// DateFormat format = new SimpleDateFormat("mm:ss"); -// String elapsedTime = format -// .format(StreamParser.getCurrentTimeLong() - boat.getMarkRoundingTime()); -// legTimeObject.setText("Last mark: " + elapsedTime); -// } else { -// legTimeObject.setText("Last mark: -"); -// -// } -// } - public void move() { double dx = xIncrement * framesToMove; double dy = yIncrement * framesToMove; @@ -256,8 +190,7 @@ public class BoatGroup extends Group { lastPoint = new Point2D(boatPoly.getLayoutX(), boatPoly.getLayoutY()); } } - rotateTo(rotation + rotationalVelocity * 1000 / 60); - wake.updatePosition(1000 / 60); + wake.updatePosition(); } /** @@ -302,19 +235,9 @@ public class BoatGroup extends Group { destinationSet = true; - rotationalVelocity = calculateRotationalVelocity(rotation); - -// updateTimeTillNextMark(); -// updateLastMarkRoundingTime(); - - if (Math.abs(rotationalVelocity) > 0.075) { - rotationalVelocity = 0.0; - wake.rotate(rotation); - } - - //rotateTo(rotation); + rotateTo(rotation); + wake.setRotation(rotation, groundSpeed); boat.setVelocity(groundSpeed); - wake.setRotationalVelocity(rotationalVelocity, groundSpeed); lastTimeValid = timeValid; isStopped = false; lastRotation = rotation; @@ -324,30 +247,15 @@ public class BoatGroup extends Group { public void setIsSelected(Boolean isSelected) { this.isSelected = isSelected; - setTeamNameObjectVisible(isSelected); - setVelocityObjectVisible(isSelected); setLineGroupVisible(isSelected); setWakeVisible(isSelected); - setEstTimeToNextMarkObjectVisible(isSelected); - setLegTimeObjectVisible(isSelected); boatAnnotations.setVisible(isSelected); } - - public void setTeamNameObjectVisible(Boolean visible) { - boatAnnotations.setTeamNameObjectVisible(visible); - } - - public void setVelocityObjectVisible(Boolean visible) { - boatAnnotations.setVelocityObjectVisible(visible); - } - - public void setEstTimeToNextMarkObjectVisible(Boolean visible) { - boatAnnotations.setEstTimeToNextMarkObjectVisible(visible); - } - - public void setLegTimeObjectVisible(Boolean visible) { - boatAnnotations.setLegTimeObjectVisible(visible); + public void setVisibility (boolean teamName, boolean velocity, boolean estTime, boolean legTime, boolean trail, boolean wake) { + boatAnnotations.setVisibile(teamName, velocity, estTime, legTime); + this.wake.setVisible(wake); + this.lineGroup.setVisible(trail); } public void setLineGroupVisible(Boolean visible) { diff --git a/src/main/java/seng302/fxObjects/MarkGroup.java b/src/main/java/seng302/fxObjects/MarkGroup.java index 464743c9..5e02ad4c 100644 --- a/src/main/java/seng302/fxObjects/MarkGroup.java +++ b/src/main/java/seng302/fxObjects/MarkGroup.java @@ -94,6 +94,7 @@ public class MarkGroup extends Group { { if (mainMark.getMarkType() == MarkType.SINGLE_MARK) { Circle markCircle = (Circle) super.getChildren().get(0); + //One of the test streams produced frequent, jittery movements. Added this as a fix. if (Math.abs(markCircle.getCenterX() - x) > 5 || Math.abs(markCircle.getCenterY() - y) > 5) { markCircle.setCenterX(x); markCircle.setCenterY(y); diff --git a/src/main/java/seng302/fxObjects/Wake.java b/src/main/java/seng302/fxObjects/Wake.java index dfa7486f..41647893 100644 --- a/src/main/java/seng302/fxObjects/Wake.java +++ b/src/main/java/seng302/fxObjects/Wake.java @@ -7,6 +7,7 @@ import javafx.scene.shape.Arc; import javafx.scene.shape.ArcType; import javafx.scene.shape.StrokeLineCap; import javafx.scene.transform.Rotate; +import javafx.scene.transform.Scale; /** * A group containing objects used to represent wakes onscreen. Contains functionality for their animation. @@ -18,13 +19,12 @@ class Wake extends Group { //The total possible difference between the first wake and the last. Increasing/Decreasing this will make wakes fan out more/less. private final double MAX_DIFF = 75; //Increasing/decreasing this will alter the speed that wakes converge when the heading stop changing. Anything over about 1500 may cause oscillation. - private final int UNIFICATION_SPEED = 750; + private final int UNIFICATION_SPEED = 45; private Arc[] arcs = new Arc[numWakes]; private double[] rotationalVelocities = new double[numWakes]; private double[] rotations = new double[numWakes]; - private double baseRad; /** * Create a wake at the given location. @@ -40,62 +40,60 @@ class Wake extends Group { //Default triangle is -110 deg out of phase with a default wake and has angle of 40 deg. arc = new Arc(0, 0, 0, 0, -110, 40); arc.setCache(true); - arc.setCacheHint(CacheHint.SCALE_AND_ROTATE); + arc.setCacheHint(CacheHint.ROTATE); arc.setType(ArcType.OPEN); arc.setStroke(new Color(0.18, 0.7, 1.0, 1.0 + (-0.99 / numWakes * i))); arc.setStrokeWidth(3.0); arc.setStrokeLineCap(StrokeLineCap.ROUND); arc.setFill(new Color(0.0, 0.0, 0.0, 0.0)); - baseRad = (20 / numWakes); arcs[i] = arc; + arc.getTransforms().setAll( + new Rotate(1) + ); } super.getChildren().addAll(arcs); } - /** - * Sets the rotationalVelocity of each arc. - * - * @param rotationalVelocity The rotationalVelocity the wake should move at. - * @param velocity The real world velocity of the boat in m/s. - */ - void setRotationalVelocity(double rotationalVelocity, double velocity) { - rotationalVelocities[0] = rotationalVelocity; - for (int i = 1; i < numWakes; i++) { - double wakeSeparationRad = Math.toRadians(rotations[i - 1] - rotations[i]); - double shortestDistance = Math.atan2( - Math.sin(wakeSeparationRad), - Math.cos(wakeSeparationRad) - ); - double distDeg = Math.toDegrees(shortestDistance); + void setRotation (double rotation, double velocity) { + if (Math.abs(rotations[0] - rotation) > 20) { + rotate(rotation); + } else { + rotations[0] = rotation; + ((Rotate) arcs[0].getTransforms().get(0)).setAngle(rotation); + for (int i = 1; i < numWakes; i++) { + double wakeSeparationRad = Math.toRadians(rotations[i - 1] - rotations[i]); + double shortestDistance = Math.atan2( + Math.sin(wakeSeparationRad), + Math.cos(wakeSeparationRad) + ); + double distDeg = Math.toDegrees(shortestDistance); + if (rotationalVelocities[i - 1] < 0.01 && rotationalVelocities[i - 1] > -0.01) { + rotationalVelocities[i] = distDeg / UNIFICATION_SPEED * 2 * Math.log(Math.abs(distDeg) + 1) / Math.log(MAX_DIFF / numWakes); - if (rotationalVelocities[i - 1] < 0.01 && rotationalVelocities[i - 1] > -0.01) { - rotationalVelocities[i] = distDeg / UNIFICATION_SPEED * Math.log(Math.abs(distDeg) + 1) / Math.log(MAX_DIFF / numWakes); - - } else { - if (distDeg < (MAX_DIFF / numWakes)) - rotationalVelocities[i] = rotationalVelocities[i - 1] * Math.log(Math.abs(distDeg) + 1) / Math.log(MAX_DIFF / numWakes); - else - rotationalVelocities[i] = rotationalVelocities[i - 1]; + } else { + if (distDeg < (MAX_DIFF / numWakes)) { + rotationalVelocities[i] = distDeg / UNIFICATION_SPEED * Math.log(Math.abs(distDeg) + 1) / Math.log(MAX_DIFF / numWakes); + } else + rotationalVelocities[i] = rotationalVelocities[i - 1]; + } } } - double rad = baseRad + velocity; + double rad = (12 / numWakes) + velocity; for (Arc arc : arcs) { arc.setRadiusX(rad); arc.setRadiusY(rad); - rad += (10 / numWakes) + (velocity / 2); + rad += (12 / numWakes) + (velocity / 2); } } /** * Arcs rotate based on the distance they would have travelled over the supplied time interval. - * - * @param timeInterval the time interval, in microseconds, that the wake should move. */ - void updatePosition(long timeInterval) { + void updatePosition() { for (int i = 0; i < numWakes; i++) { - rotations[i] = rotations[i] + rotationalVelocities[i] * timeInterval; - arcs[i].getTransforms().setAll(new Rotate(rotations[i])); + rotations[i] = rotations[i] + rotationalVelocities[i]; + ((Rotate) arcs[i].getTransforms().get(0)).setAngle(rotations[i]); } } diff --git a/src/main/java/seng302/models/TimelineInfo.java b/src/main/java/seng302/models/TimelineInfo.java deleted file mode 100644 index 867ce67b..00000000 --- a/src/main/java/seng302/models/TimelineInfo.java +++ /dev/null @@ -1,31 +0,0 @@ -package seng302.models; - -import javafx.animation.Timeline; -import javafx.beans.property.DoubleProperty; - - -/** - * Created by zyt10 on 17/03/17. - * this class is literally just to associate a timeline with a DoubleProperty x and y - */ -public class TimelineInfo { - private Timeline timeline; - private DoubleProperty x; - private DoubleProperty y; - - public TimelineInfo(Timeline timeline, DoubleProperty x, DoubleProperty y) { - this.timeline = timeline; - this.x = x; - this.y = y; - } - - public Timeline getTimeline() { - return timeline; - } - public DoubleProperty getX() { - return x; - } - public DoubleProperty getY() { - return y; - } -} diff --git a/src/main/java/seng302/models/Yacht.java b/src/main/java/seng302/models/Yacht.java index 300c5a1d..90e24739 100644 --- a/src/main/java/seng302/models/Yacht.java +++ b/src/main/java/seng302/models/Yacht.java @@ -23,237 +23,6 @@ public class Yacht { // Used in boat group private Color colour; - private DoubleProperty velocityProperty = new DoubleProperty() { - - private ObservableValue boundValue; - private List changeListeners = new ArrayList<>(); - private List invalidationListeners = new ArrayList<>(); - private double velocity; - - @Override - public void bind(ObservableValue observable) { - boundValue = observable; - } - - @Override - public void unbind() { - boundValue = null; - } - - @Override - public boolean isBound() { - if (boundValue == null) { - return false; - } else { - return true; - } - } - - @Override - public Object getBean() { - return Yacht.this; - } - - @Override - public String getName() { - return "velocity property of " + boatName; - } - - @Override - public double get() { - return velocity; - } - - @Override - public void addListener(ChangeListener listener) { - changeListeners.add(listener); - } - - @Override - public void removeListener(ChangeListener listener) { - changeListeners.remove(listener); - } - - @Override - public void addListener(InvalidationListener listener) { - invalidationListeners.add(listener); - } - - @Override - public void removeListener(InvalidationListener listener) { - invalidationListeners.remove(listener); - } - - @Override - public void set (double newVelocity) { - double oldVelocity = velocity; - velocity = newVelocity; - if (newVelocity >= 0) - for (ChangeListener cl : changeListeners) { - cl.changed(this, oldVelocity, newVelocity); - } - else - for (InvalidationListener il : invalidationListeners) { - il.invalidated(this); - } - if (isBound()) - boundValue.notify(); - } - }; - private LongProperty timeAtNextProperty = new LongProperty() { - - private ObservableValue boundValue; - private List changeListeners = new ArrayList<>(); - private List invalidationListeners = new ArrayList<>(); - private long estimate; - - @Override - public void bind(ObservableValue observable) { - boundValue = observable; - } - - @Override - public void unbind() { - boundValue = null; - } - - @Override - public boolean isBound() { - if (boundValue == null) { - return false; - } else { - return true; - } - } - - @Override - public Object getBean() { - return Yacht.this; - } - - @Override - public String getName() { - return "estimated time to next mark property of " + boatName; - } - - @Override - public long get() { - return estimate; - } - - @Override - public void addListener(ChangeListener listener) { - changeListeners.add(listener); - } - - @Override - public void removeListener(ChangeListener listener) { - changeListeners.remove(listener); - } - - @Override - public void addListener(InvalidationListener listener) { - invalidationListeners.add(listener); - } - - @Override - public void removeListener(InvalidationListener listener) { - invalidationListeners.remove(listener); - } - - @Override - public void set (long newEstimate) { - long oldEstimate = estimate; - estimate = newEstimate; - if (newEstimate >= 0) - for (ChangeListener cl : changeListeners) { - cl.changed(this, oldEstimate, newEstimate); - } - else - for (InvalidationListener il : invalidationListeners) { - il.invalidated(this); - } - if (isBound()) - boundValue.notify(); - } - }; - private LongProperty markRoundingTimeProperty = new LongProperty() { - private ObservableValue boundValue; - private List changeListeners = new ArrayList<>(); - private List invalidationListeners = new ArrayList<>(); - private long roundingTime; - - @Override - public void bind(ObservableValue observable) { - boundValue = observable; - } - - @Override - public void unbind() { - boundValue = null; - } - - @Override - public boolean isBound() { - if (boundValue == null) { - return false; - } else { - return true; - } - } - - @Override - public Object getBean() { - return Yacht.this; - } - - @Override - public String getName() { - return "time from last mark property of " + boatName; - } - - @Override - public long get() { - return roundingTime; - } - - @Override - public void addListener(ChangeListener listener) { - changeListeners.add(listener); - } - - @Override - public void removeListener(ChangeListener listener) { - changeListeners.remove(listener); - } - - @Override - public void addListener(InvalidationListener listener) { - invalidationListeners.add(listener); - } - - @Override - public void removeListener(InvalidationListener listener) { - invalidationListeners.remove(listener); - } - - @Override - public void set (long newTime) { - long oldTime = newTime; - roundingTime = newTime; - if (newTime >= 0) - for (ChangeListener cl : changeListeners) { - cl.changed(this, oldTime, newTime); - } - else - for (InvalidationListener il : invalidationListeners) { - il.invalidated(this); - } - if (isBound()) - boundValue.notify(); - } - }; - private String boatType; private Integer sourceID; private String hullID; //matches HullNum in the XML spec. @@ -267,6 +36,10 @@ public class Yacht { private Integer penaltiesServed; private Long estimateTimeAtFinish; private String position; + private double velocity; + private Long timeTillNext; + private Long markRoundTime; + /** * Used in EventTest and RaceTest. @@ -286,7 +59,7 @@ public class Yacht { */ public Yacht(String boatName, double boatVelocity, String shortName, int id) { this.boatName = boatName; - this.velocityProperty.set(boatVelocity); + this.velocity = boatVelocity; this.shortName = shortName; this.sourceID = id; } @@ -352,7 +125,7 @@ public class Yacht { } public void setEstimateTimeAtNextMark(Long estimateTimeAtNextMark) { - timeAtNextProperty.set(estimateTimeAtNextMark); + timeTillNext = estimateTimeAtNextMark; } public String getEstimateTimeAtFinish() { @@ -381,12 +154,24 @@ public class Yacht { } public void setVelocity(double velocity) { - velocityProperty.set(velocity); + this.velocity = velocity; } public void setMarkRoundingTime(Long markRoundingTime) { - markRoundingTimeProperty.set(markRoundingTime); + this.markRoundTime = markRoundingTime; + } + + public double getVelocity() { + return velocity; + } + + public Long getTimeTillNext() { + return timeTillNext; + } + + public Long getMarkRoundTime() { + return markRoundTime; } @Override @@ -394,15 +179,4 @@ public class Yacht { return boatName; } - public ReadOnlyDoubleProperty getReadOnlyVelocityProperty () { - return velocityProperty; - } - - public ReadOnlyLongProperty getReadOnlyNextMarkProperty() { - return timeAtNextProperty; - } - - public ReadOnlyLongProperty getReadOnlyMarkRoundingProperty() { - return markRoundingTimeProperty; - } } From 3085125f3eaf14423432f535e0d4463f062cef7a Mon Sep 17 00:00:00 2001 From: Calum Date: Thu, 25 May 2017 01:21:40 +1200 Subject: [PATCH 5/8] Merged with develop --- src/main/java/seng302/App.java | 4 +- .../seng302/controllers/CanvasController.java | 76 ++++++------------- .../seng302/controllers/RaceController.java | 0 .../controllers/RaceViewController.java | 44 ----------- .../ImportantAnnotationController.java | 6 +- .../seng302/fxObjects/BoatAnnotations.java | 1 - src/main/java/seng302/models/Yacht.java | 9 --- 7 files changed, 27 insertions(+), 113 deletions(-) delete mode 100644 src/main/java/seng302/controllers/RaceController.java diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index 64cf5e2c..93dcd998 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -64,9 +64,9 @@ public class App extends Application { } //Change the StreamReceiver in this else block to change the default data source. else{ -// sr = new StreamReceiver("localhost", 4949, "RaceStream"); + sr = new StreamReceiver("localhost", 4949, "RaceStream"); // sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941, "RaceStream"); - sr = new StreamReceiver("livedata.americascup.com", 4941, "RaceStream"); +// sr = new StreamReceiver("livedata.americascup.com", 4941, "RaceStream"); } sr.start(); diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index daa9a4fa..644477ba 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -22,7 +22,6 @@ import seng302.models.mark.MarkType; import seng302.models.mark.SingleMark; import seng302.models.map.Boundary; import seng302.models.map.CanvasMap; -import seng302.models.mark.*; import seng302.models.stream.StreamParser; import seng302.models.stream.XMLParser; import seng302.models.stream.XMLParser.RaceXMLObject.Limit; @@ -132,61 +131,36 @@ public class CanvasController { initializeBoats(); initializeMarks(); timer = new AnimationTimer() { - - private int UPDATE_FPM_PERIOD = 50; // update FPM label every 50 frames - private int updateFPMCounter = 100; - private long lastTime = 0; @Override public void handle(long now) { - //fps stuff - long oldFrameTime = frameTimes[frameTimeIndex] ; - frameTimes[frameTimeIndex] = now ; - frameTimeIndex = (frameTimeIndex + 1) % frameTimes.length ; - if (frameTimeIndex == 0) { - arrayFilled = true ; - } - long elapsedNanos; - if (arrayFilled) { - elapsedNanos = now - oldFrameTime ; - long elapsedNanosPerFrame = elapsedNanos / frameTimes.length ; - frameRate = 1_000_000_000.0 / elapsedNanosPerFrame ; - if (updateFPMCounter++ > UPDATE_FPM_PERIOD) { - updateFPMCounter = 0; - drawFps(frameRate.intValue()); + if (lastTime == 0) { + lastTime = now; + } else { + if (now - lastTime >= (1e8 / 60)) { //Fix for framerate going above 60 when minimized + long oldFrameTime = frameTimes[frameTimeIndex]; + frameTimes[frameTimeIndex] = now; + frameTimeIndex = (frameTimeIndex + 1) % frameTimes.length; + if (frameTimeIndex == 0) { + arrayFilled = true; + } + long elapsedNanos; + if (arrayFilled) { + elapsedNanos = now - oldFrameTime; + long elapsedNanosPerFrame = elapsedNanos / frameTimes.length; + frameRate = 1_000_000_000.0 / elapsedNanosPerFrame; + drawFps(frameRate.intValue()); + } + updateGroups(); + if (StreamParser.isRaceFinished()) { + this.stop(); + } + lastTime = now; + } } - raceViewController.updateSparkLine(); - } - - // TODO: 1/05/17 cir27 - Make the RaceObjects update on the actual delay. - elapsedNanos = 1000 / 60; - updateGroups(); if (StreamParser.isRaceFinished()) { this.stop(); - if (lastTime == 0) { - lastTime = now; - } else { - if (now - lastTime >= (1e8 / 60)) { //Fix for framerate going above 60 when minimized - long oldFrameTime = frameTimes[frameTimeIndex] ; - frameTimes[frameTimeIndex] = now ; - frameTimeIndex = (frameTimeIndex + 1) % frameTimes.length ; - if (frameTimeIndex == 0) { - arrayFilled = true ; - } - long elapsedNanos; - if (arrayFilled) { - elapsedNanos = now - oldFrameTime ; - long elapsedNanosPerFrame = elapsedNanos / frameTimes.length ; - frameRate = 1_000_000_000.0 / elapsedNanosPerFrame ; - drawFps(frameRate.intValue()); - } - updateGroups(frameRate); - if (StreamParser.isRaceFinished()) { - this.stop(); - } - lastTime = now; - } } } }; @@ -239,7 +213,7 @@ public class CanvasController { raceBorder.getPoints().setAll(boundaryPoints); } - private void updateGroups(double frameRate){ + private void updateGroups(){ for (BoatGroup boatGroup : boatGroups) { // some raceObjects will have multiple ID's (for instance gate marks) //checking if the current "ID" has any updates associated with it @@ -262,8 +236,6 @@ public class CanvasController { private void checkForCourseChanges() { if (StreamParser.isNewRaceXmlReceived()){ - gc.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); - drawGoogleMap(); addRaceBorder(); } } diff --git a/src/main/java/seng302/controllers/RaceController.java b/src/main/java/seng302/controllers/RaceController.java deleted file mode 100644 index e69de29b..00000000 diff --git a/src/main/java/seng302/controllers/RaceViewController.java b/src/main/java/seng302/controllers/RaceViewController.java index 57793426..bbf07322 100644 --- a/src/main/java/seng302/controllers/RaceViewController.java +++ b/src/main/java/seng302/controllers/RaceViewController.java @@ -6,7 +6,6 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; -import javafx.geometry.Side; import javafx.scene.Scene; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; @@ -442,49 +441,6 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel return displayFps; } - /** - * Display the important annotations for a specific BoatGroup - * - * @param bg The boat group to set the annotations for - */ - private void setBoatGroupImportantAnnotations(BoatGroup bg) { - if (importantAnnotations.getAnnotationState(Annotation.NAME)) { - bg.setTeamNameObjectVisible(true); - } else { - bg.setTeamNameObjectVisible(false); - } - - if (importantAnnotations.getAnnotationState(Annotation.SPEED)) { - bg.setVelocityObjectVisible(true); - } else { - bg.setVelocityObjectVisible(false); - } - - if (importantAnnotations.getAnnotationState(Annotation.TRACK)) { - bg.setLineGroupVisible(true); - } else { - bg.setLineGroupVisible(false); - } - - if (importantAnnotations.getAnnotationState(Annotation.WAKE)) { - bg.setWakeVisible(true); - } else { - bg.setWakeVisible(false); - } - //TODO fix boat annotations with new boatgroup - if (importantAnnotations.getAnnotationState(Annotation.ESTTIMETONEXTMARK)) { - bg.setEstTimeToNextMarkObjectVisible(true); - } else { - bg.setEstTimeToNextMarkObjectVisible(false); - } - - if (importantAnnotations.getAnnotationState(Annotation.LEGTIME)) { - bg.setLegTimeObjectVisible(true); - } else { - bg.setLegTimeObjectVisible(false); - } - } - private void setAnnotations(Integer annotationLevel) { switch (annotationLevel) { // No Annotations diff --git a/src/main/java/seng302/controllers/annotations/ImportantAnnotationController.java b/src/main/java/seng302/controllers/annotations/ImportantAnnotationController.java index 8dfa8df7..b91f5ca1 100644 --- a/src/main/java/seng302/controllers/annotations/ImportantAnnotationController.java +++ b/src/main/java/seng302/controllers/annotations/ImportantAnnotationController.java @@ -6,12 +6,8 @@ import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; -import seng302.controllers.RaceViewController; -import seng302.controllers.annotations.Annotation; -import java.net.URL; -import java.util.HashMap; -import java.util.Map; +import java.net.URL;; import java.util.ResourceBundle; public class ImportantAnnotationController implements Initializable { diff --git a/src/main/java/seng302/fxObjects/BoatAnnotations.java b/src/main/java/seng302/fxObjects/BoatAnnotations.java index 90ec3fb6..cbff71b9 100644 --- a/src/main/java/seng302/fxObjects/BoatAnnotations.java +++ b/src/main/java/seng302/fxObjects/BoatAnnotations.java @@ -5,7 +5,6 @@ import javafx.scene.Group; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.text.Text; -import seng302.controllers.annotations.Annotation; import seng302.models.Yacht; import seng302.models.stream.StreamParser; diff --git a/src/main/java/seng302/models/Yacht.java b/src/main/java/seng302/models/Yacht.java index fbbf9849..3e14f6a9 100644 --- a/src/main/java/seng302/models/Yacht.java +++ b/src/main/java/seng302/models/Yacht.java @@ -1,19 +1,10 @@ package seng302.models; -import javafx.beans.InvalidationListener; -import javafx.beans.property.*; -import javafx.beans.value.ChangeListener; -import javafx.beans.value.ObservableValue; -import javafx.fxml.Initializable; - import javafx.scene.paint.Color; import seng302.controllers.RaceViewController; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.time.Year; -import java.util.ArrayList; -import java.util.List; /** * Yacht class for the racing boat. From 0cd2867ac09749b97e337d1eb48519bc56364f30 Mon Sep 17 00:00:00 2001 From: Calum Date: Thu, 25 May 2017 10:30:01 +1200 Subject: [PATCH 6/8] Made annotations more readable when overlapping. --- src/main/java/seng302/App.java | 4 ++-- src/main/java/seng302/fxObjects/BoatAnnotations.java | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index a169bd55..a3d5f49f 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -63,9 +63,9 @@ public class App extends Application { } //Change the StreamReceiver in this else block to change the default data source. else{ - sr = new StreamReceiver("localhost", 4949, "RaceStream"); +// sr = new StreamReceiver("localhost", 4949, "RaceStream"); // sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941, "RaceStream"); -// sr = new StreamReceiver("livedata.americascup.com", 4941, "RaceStream"); + sr = new StreamReceiver("livedata.americascup.com", 4941, "RaceStream"); } sr.start(); diff --git a/src/main/java/seng302/fxObjects/BoatAnnotations.java b/src/main/java/seng302/fxObjects/BoatAnnotations.java index cbff71b9..b8f2a031 100644 --- a/src/main/java/seng302/fxObjects/BoatAnnotations.java +++ b/src/main/java/seng302/fxObjects/BoatAnnotations.java @@ -45,7 +45,7 @@ class BoatAnnotations extends Group{ background.setHeight(Math.abs(BACKGROUND_X) + TEXT_BUFFER + BACKGROUND_H_PER_TEXT * 4); background.setArcHeight(BACKGROUND_ARC_SIZE); background.setArcWidth(BACKGROUND_ARC_SIZE); - background.setFill(new Color(1, 1, 1, 0.5)); + background.setFill(new Color(1, 1, 1, 0.75)); background.setStroke(theme); background.setStrokeWidth(2); background.setCache(true); @@ -76,6 +76,7 @@ class BoatAnnotations extends Group{ private Text getTextObject(String defaultText, Color fill) { Text text = new Text(defaultText); text.setFill(fill); + text.setStrokeWidth(2); text.setCacheHint(CacheHint.SPEED); text.setCache(true); return text; From 65223ceaaf5c7365ce7c5eb7d92eade94d1156b6 Mon Sep 17 00:00:00 2001 From: Calum Date: Thu, 25 May 2017 16:29:27 +1200 Subject: [PATCH 7/8] shifted annotation layers. Merged with dev. --- src/main/java/seng302/App.java | 3 -- .../seng302/controllers/CanvasController.java | 31 ++++++++---- .../controllers/RaceViewController.java | 3 +- .../seng302/fxObjects/BoatAnnotations.java | 2 +- .../java/seng302/fxObjects/BoatGroup.java | 48 +++++++------------ src/main/java/seng302/fxObjects/Wake.java | 6 +-- src/main/java/seng302/models/Yacht.java | 3 -- 7 files changed, 43 insertions(+), 53 deletions(-) diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index 64cf7291..b3c9439f 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -29,9 +29,6 @@ public class App extends Application { StreamReceiver.noMoreBytes(); System.exit(0); }); - - - } public static void main(String[] args) { diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index 0f62c5a0..c3ba9c12 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -12,7 +12,9 @@ import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; import javafx.scene.text.Text; +import seng302.fxObjects.BoatAnnotations; import seng302.fxObjects.BoatGroup; +import seng302.fxObjects.Wake; import seng302.models.Colors; import seng302.models.Yacht; import seng302.models.mark.GateMark; @@ -120,13 +122,12 @@ public class CanvasController { FPSdisplay.setStrokeWidth(2); group.getChildren().add(FPSdisplay); group.getChildren().add(raceBorder); - - - // TODO: 1/05/17 wmu16 - Change this call to now draw the marks as from the xml - initializeBoats(); initializeMarks(); + initializeBoats(); + timer = new AnimationTimer() { private long lastTime = 0; + private int FPSCount = 30; @Override public void handle(long now) { @@ -145,7 +146,11 @@ public class CanvasController { elapsedNanos = now - oldFrameTime; long elapsedNanosPerFrame = elapsedNanos / frameTimes.length; frameRate = 1_000_000_000.0 / elapsedNanosPerFrame; - drawFps(frameRate.intValue()); + if (FPSCount-- == 0) { + FPSCount = 30; + drawFps(frameRate.intValue()); + } + raceViewController.updateSparkLine(); } updateGroups(); if (StreamParser.isRaceFinished()) { @@ -243,7 +248,9 @@ public class CanvasController { BoatPositionPacket positionPacket = movementQueue.take(); Point2D p2d = findScaledXY(positionPacket.getLat(), positionPacket.getLon()); double heading = 360.0 / 0xffff * positionPacket.getHeading(); - boatGroup.setDestination(p2d.getX(), p2d.getY(), heading, positionPacket.getGroundSpeed(), positionPacket.getTimeValid(), frameRate, boatGroup.getRaceId()); + boatGroup.setDestination( + p2d.getX(), p2d.getY(), heading, positionPacket.getGroundSpeed(), + positionPacket.getTimeValid(), frameRate); } catch (InterruptedException e){ e.printStackTrace(); } @@ -269,7 +276,9 @@ public class CanvasController { */ private void initializeBoats() { Map boats = StreamParser.getBoats(); - Group boatAnnotations = new Group(); + Group wakes = new Group(); + Group trails = new Group(); + Group annotations = new Group(); ArrayList participants = StreamParser.getXmlObject().getRaceXML().getParticipants(); ArrayList participantIDs = new ArrayList<>(); @@ -282,10 +291,14 @@ public class CanvasController { boat.setColour(Colors.getColor()); BoatGroup boatGroup = new BoatGroup(boat, boat.getColour()); boatGroups.add(boatGroup); - boatAnnotations.getChildren().add(boatGroup.getLowPriorityAnnotations()); + trails.getChildren().add(boatGroup.getTrail()); + wakes.getChildren().add(boatGroup.getWake()); + annotations.getChildren().add(boatGroup.getAnnotations()); } } - group.getChildren().add(boatAnnotations); + group.getChildren().addAll(trails); + group.getChildren().addAll(wakes); + group.getChildren().addAll(annotations); group.getChildren().addAll(boatGroups); } diff --git a/src/main/java/seng302/controllers/RaceViewController.java b/src/main/java/seng302/controllers/RaceViewController.java index d121adad..7aef03bb 100644 --- a/src/main/java/seng302/controllers/RaceViewController.java +++ b/src/main/java/seng302/controllers/RaceViewController.java @@ -7,7 +7,6 @@ import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.geometry.Point2D; -import javafx.geometry.Side; import javafx.scene.Scene; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; @@ -34,10 +33,10 @@ import seng302.controllers.annotations.ImportantAnnotationController; import seng302.controllers.annotations.ImportantAnnotationDelegate; import seng302.controllers.annotations.ImportantAnnotationsState; import seng302.fxObjects.BoatGroup; +import seng302.fxObjects.MarkGroup; import seng302.models.*; import seng302.models.mark.GateMark; import seng302.models.mark.Mark; -import seng302.models.mark.MarkGroup; import seng302.models.mark.SingleMark; import seng302.models.stream.StreamParser; import seng302.models.stream.XMLParser; diff --git a/src/main/java/seng302/fxObjects/BoatAnnotations.java b/src/main/java/seng302/fxObjects/BoatAnnotations.java index b8f2a031..fbba2257 100644 --- a/src/main/java/seng302/fxObjects/BoatAnnotations.java +++ b/src/main/java/seng302/fxObjects/BoatAnnotations.java @@ -14,7 +14,7 @@ import java.text.SimpleDateFormat; /** * Collection of annotations for boats. */ -class BoatAnnotations extends Group{ +public class BoatAnnotations extends Group{ //Text offset constants private static final double X_OFFSET_TEXT = 18d; diff --git a/src/main/java/seng302/fxObjects/BoatGroup.java b/src/main/java/seng302/fxObjects/BoatGroup.java index 5121a603..b1c96002 100644 --- a/src/main/java/seng302/fxObjects/BoatGroup.java +++ b/src/main/java/seng302/fxObjects/BoatGroup.java @@ -51,7 +51,7 @@ public class BoatGroup extends Group { private Double distanceTravelled = 0.0; private Point2D lastPoint; private boolean destinationSet; - private BoatAnnotations boatAnnotations;; + private BoatAnnotations boatAnnotations; private Boolean isSelected = true; //All boats are initialised as selected @@ -106,6 +106,10 @@ public class BoatGroup extends Group { boatPoly.setCache(true); boatPoly.setCacheHint(CacheHint.SPEED); boatAnnotations = new BoatAnnotations(boat, color); + + leftLayLine = new Line(); + rightLayline = new Line(); + wake = new Wake(0, -BOAT_HEIGHT); super.getChildren().addAll(boatPoly, boatAnnotations); } @@ -196,25 +200,6 @@ public class BoatGroup extends Group { wake.updatePosition(); } - /** - * Calculates the rotational velocity required to reach the rotationalGoal from the - * currentRotation. - */ - private Double calculateRotationalVelocity(Double rotationalGoal) { - Double rotationalVelocity; - - if (Math.abs(rotationalGoal - lastRotation) > 180) { - if (rotationalGoal - lastRotation >= 0.0) { - rotationalVelocity = ((rotationalGoal - lastRotation) - 360) / 200; - } else { - rotationalVelocity = (360 + (rotationalGoal - lastRotation)) / 200; - } - } else { - rotationalVelocity = (rotationalGoal - lastRotation) / 200; - } - return rotationalVelocity; - } - /** * Sets the destination of the boat and the headng it should have once it reaches * @@ -224,7 +209,7 @@ public class BoatGroup extends Group { * @param timeValid the time the position values are valid for */ public void setDestination(double newXValue, double newYValue, double rotation, - double groundSpeed, long timeValid, double frameRate, long id) { + double groundSpeed, long timeValid, double frameRate) { if (lastTimeValid == 0) { lastTimeValid = timeValid - 200; moveTo(newXValue, newYValue, rotation); @@ -338,19 +323,17 @@ public class BoatGroup extends Group { return boat.getSourceID(); } - /** - * Due to javaFX limitations annotations associated with a boat that you want to appear below - * all boats in the Z-axis need to be pulled out of the BoatGroup and added to the parent group - * of the BoatGroups. This function returns these annotations as a group. - * - * @return A group containing low priority annotations. - */ - public Group getLowPriorityAnnotations() { - Group group = new Group(); - group.getChildren().addAll(wake, lineGroup); - return group; + public Wake getWake () { + return wake; } + public Group getTrail() { + return lineGroup; + } + + public BoatAnnotations getAnnotations() { + return boatAnnotations; + } public Double getBoatLayoutX() { return boatPoly.getLayoutX(); @@ -369,4 +352,5 @@ public class BoatGroup extends Group { public String toString() { return boat.toString(); } + } \ No newline at end of file diff --git a/src/main/java/seng302/fxObjects/Wake.java b/src/main/java/seng302/fxObjects/Wake.java index 41647893..8f8cc8aa 100644 --- a/src/main/java/seng302/fxObjects/Wake.java +++ b/src/main/java/seng302/fxObjects/Wake.java @@ -12,7 +12,7 @@ import javafx.scene.transform.Scale; /** * A group containing objects used to represent wakes onscreen. Contains functionality for their animation. */ -class Wake extends Group { +public class Wake extends Group { //The number of wakes private int numWakes = 8; @@ -79,11 +79,11 @@ class Wake extends Group { } } - double rad = (12 / numWakes) + velocity; + double rad = (14 / numWakes) + velocity; for (Arc arc : arcs) { arc.setRadiusX(rad); arc.setRadiusY(rad); - rad += (12 / numWakes) + (velocity / 2); + rad += (14 / numWakes) + (velocity / 2.5); } } diff --git a/src/main/java/seng302/models/Yacht.java b/src/main/java/seng302/models/Yacht.java index 5381cfe8..c11c7407 100644 --- a/src/main/java/seng302/models/Yacht.java +++ b/src/main/java/seng302/models/Yacht.java @@ -6,8 +6,6 @@ import seng302.controllers.RaceViewController; import java.text.DateFormat; import java.text.SimpleDateFormat; -import seng302.models.stream.StreamParser; -import seng302.models.stream.XMLParser.RaceXMLObject.Corner; /** * Yacht class for the racing boat. @@ -38,7 +36,6 @@ public class Yacht { private Long markRoundTime; // Mark rounding - private Long markRoundingTime; private Mark lastMarkRounded; private Mark nextMark; From c1aa1d8eaead4667a65483b3d7ac021624193e1e Mon Sep 17 00:00:00 2001 From: Calum Date: Thu, 25 May 2017 16:32:25 +1200 Subject: [PATCH 8/8] merged with dev --- src/main/java/seng302/fxObjects/BoatGroup.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/seng302/fxObjects/BoatGroup.java b/src/main/java/seng302/fxObjects/BoatGroup.java index b1c96002..d5ab53b2 100644 --- a/src/main/java/seng302/fxObjects/BoatGroup.java +++ b/src/main/java/seng302/fxObjects/BoatGroup.java @@ -323,7 +323,7 @@ public class BoatGroup extends Group { return boat.getSourceID(); } - public Wake getWake () { + public Group getWake () { return wake; } @@ -331,7 +331,7 @@ public class BoatGroup extends Group { return lineGroup; } - public BoatAnnotations getAnnotations() { + public Group getAnnotations() { return boatAnnotations; }