diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index c8a8797f..e9ce5d58 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -35,56 +35,32 @@ import java.util.*; * Modified by Haoming Yin (hyi25) on 20/3/2017. */ public class CanvasController { + @FXML - private AnchorPane contentAnchorPane; - @FXML - private Text windArrowText, windDirectionText; - @FXML - private Pane raceTimer; - @FXML - private BoatPositionController teamPositionsController; - @FXML - private CheckBox toggleAnnotation, toggleFps; + private AnchorPane canvasPane; private ResizableCanvas canvas; - private Race race; private GraphicsContext gc; private HashMap timelineInfos; - - private AnchorPane raceResults; - - private final double ORIGIN_LAT = 32.321504; - private final double ORIGIN_LON = -64.857063; - private Animation.Status raceStatus = Animation.Status.PAUSED; - private final int SCALE = 16000; - - private boolean annotationCheck = true; - private boolean displayFps = true; - ///test private HashSet headingTest = new HashSet<>(); - /** - * Initialize the controller - */ public void initialize() { canvas = new ResizableCanvas(); - contentAnchorPane.getChildren().add(canvas); + canvasPane.getChildren().add(canvas); // Bind canvas size to stack pane size. canvas.widthProperty().bind( - contentAnchorPane.widthProperty()); + canvasPane.widthProperty()); canvas.heightProperty().bind( - contentAnchorPane.heightProperty()); + canvasPane.heightProperty()); gc = canvas.getGraphicsContext2D(); - RaceController raceController = new RaceController(); - raceController.initializeRace(); - race = raceController.getRace(); - timelineInfos = new HashMap<>(); +// +// timelineInfos = new HashMap<>(); // overriding the handle so that it can clean canvas and redraw boats and course marks AnimationTimer timer = new AnimationTimer() { @@ -154,18 +130,7 @@ public class CanvasController { loadRaceResultView(); }); - toggleAnnotation.selectedProperty().addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observable, Boolean oldValue, Boolean newValue) { - annotationCheck = !annotationCheck; - } - }); - toggleFps.selectedProperty().addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observable, Boolean oldValue, Boolean newValue) { - displayFps = !displayFps; - } - }); + //set wind direction!!!!!!! can't find another place to put my code --haoming double windDirection = new ConfigParser("/config.xml").getWindDirection(); @@ -224,26 +189,6 @@ public class CanvasController { } } - /** - * Load the race timer - */ - private void loadTimerView(){ - FXMLLoader loader = new FXMLLoader(getClass().getResource("/raceTimer.fxml")); - loader.setController(new RaceTimerController(race)); - - try{ - raceTimer.getChildren().clear(); - raceTimer.getChildren().removeAll(); - raceTimer.getChildren().addAll((Pane) loader.load()); - } - catch(javafx.fxml.LoadException e){ - System.out.println(e); - } - catch(IOException e){ - System.out.println(e); - } - } - /** * Play each boats timeline */ diff --git a/src/main/java/seng302/controllers/Controller.java b/src/main/java/seng302/controllers/Controller.java new file mode 100644 index 00000000..22f4dfcb --- /dev/null +++ b/src/main/java/seng302/controllers/Controller.java @@ -0,0 +1,38 @@ +package seng302.controllers; + +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.fxml.Initializable; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.Pane; + +import java.io.IOException; +import java.net.URL; +import java.util.ResourceBundle; + +/** + * Created by michaelrausch on 21/03/17. + */ +public class Controller implements Initializable { + @FXML + private AnchorPane contentPane; + + private void setContentPane(String jfxUrl){ + try{ + contentPane.getChildren().removeAll(); + contentPane.getChildren().clear(); + contentPane.getChildren().addAll((Pane) FXMLLoader.load(getClass().getResource(jfxUrl))); + } + catch(javafx.fxml.LoadException e){ + System.err.println(e.getCause()); + } + catch(IOException e){ + System.err.println(e); + } + } + + @Override + public void initialize(URL location, ResourceBundle resources) { + setContentPane("/RaceView.fxml"); + } +} diff --git a/src/main/java/seng302/controllers/RaceViewController.java b/src/main/java/seng302/controllers/RaceViewController.java new file mode 100644 index 00000000..0b57d849 --- /dev/null +++ b/src/main/java/seng302/controllers/RaceViewController.java @@ -0,0 +1,155 @@ +package seng302.controllers; + +import javafx.animation.KeyFrame; +import javafx.animation.Timeline; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.fxml.FXML; +import javafx.scene.control.CheckBox; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.VBox; +import javafx.scene.text.Text; +import javafx.util.Duration; +import seng302.models.Boat; +import seng302.models.Event; +import seng302.models.Race; + +import java.net.URL; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.ResourceBundle; + +/** + * Created by ptg19 on 29/03/17. + */ +public class RaceViewController { + @FXML + private VBox positionVbox; + @FXML + private CheckBox toggleAnnotation, toggleFps; + @FXML + private Text timerLabel; + @FXML + private AnchorPane contentAnchorPane; + @FXML + private Text windArrowText, windDirectionText; + + private boolean displayAnnotations; + private boolean displayFps; + private Timeline timeline; + private Race race; + private ArrayList boatOrder = new ArrayList<>(); + + private final double ORIGIN_LAT = 32.321504; + private final double ORIGIN_LON = -64.857063; + private final int SCALE = 16000; + +// /** +// * Controller to control the race timer +// * @param race the race the timer is timing +// */ +// public RaceTimerController(Race race){ +// this.race = race; +// } + + public void initialize() { + RaceController raceController = new RaceController(); + raceController.initializeRace(); + race = raceController.getRace(); + + initializeTimer(); + initializeSettings(); + + + + } + + private void initializeSettings(){ + displayAnnotations = true; + displayFps = true; + + toggleAnnotation.selectedProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, Boolean oldValue, Boolean newValue) { + displayAnnotations = !displayAnnotations; + } + }); + toggleFps.selectedProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, Boolean oldValue, Boolean newValue) { + displayFps = !displayFps; + } + }); + } + + private void initializeTimer(){ + timeline = new Timeline(); + timeline.setCycleCount(Timeline.INDEFINITE); + // Run timer update every second + timeline.getKeyFrames().add( + new KeyFrame(Duration.seconds(1), + event -> { + // Stop timer if race is finished + if (this.race.isRaceFinished()) { + this.timeline.stop(); + } else { + timerLabel.setText(convertTimeToMinutesSeconds(race.getRaceTime())); + this.race.incrementRaceTime(); + } + }) + ); + + // Start the timer + timeline.playFromStart(); + } + + public void handleEvent(Event event) { + Boat boat = event.getBoat(); + boatOrder.remove(boat); + boat.setMarkLastPast(event.getMarkPosInRace()); + boatOrder.add(boat); + boatOrder.sort(new Comparator() { + @Override + public int compare(Boat b1, Boat b2) { + return b2.getMarkLastPast() - b1.getMarkLastPast(); + } + }); + showOrder(); + } + + private void showOrder() { + positionVbox.getChildren().clear(); + positionVbox.getChildren().removeAll(); + + for (Boat boat : boatOrder) { + positionVbox.getChildren().add(new Text(boat.getShortName() + " " + boat.getSpeedInKnots() + " Knots")); + } + } + + /** + * Convert seconds to a string of the format mm:ss + * + * @param time the time in seconds + * @return a formatted string + */ + public String convertTimeToMinutesSeconds(int time) { + if (time < 0) { + return String.format("-%02d:%02d", (time * -1) / 60, (time * -1) % 60); + } + return String.format("%02d:%02d", time / 60, time % 60); + } + + /** + * Stop the race timer + */ + public void stop() { + timeline.stop(); + } + + /** + * Start the race timer + */ + public void start() { + timeline.play(); + } +} \ No newline at end of file diff --git a/src/main/resources/MainView.fxml b/src/main/resources/MainView.fxml index 9c1ea80f..ac0b944e 100644 --- a/src/main/resources/MainView.fxml +++ b/src/main/resources/MainView.fxml @@ -4,7 +4,7 @@ - + diff --git a/src/test/java/seng302/TestRaceTimer.java b/src/test/java/seng302/TestRaceTimer.java index cd51db3b..138cd7ad 100644 --- a/src/test/java/seng302/TestRaceTimer.java +++ b/src/test/java/seng302/TestRaceTimer.java @@ -1,7 +1,6 @@ package seng302; import org.junit.Test; -import seng302.controllers.RaceTimerController; import seng302.models.Race; import static org.junit.Assert.assertTrue;