Several tweaks and improvements to a annotations and other visual aspects of them program. Fixed bug causing minimization crashes.

#implement #refactor #issue[23]
This commit is contained in:
Calum
2017-05-25 01:02:33 +12:00
parent 14a7305a2d
commit 765ea06c3b
9 changed files with 182 additions and 589 deletions
@@ -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;
}
}
+12 -104
View File
@@ -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) {
@@ -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);
+32 -34
View File
@@ -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]);
}
}