Fxml refactored, partway through refactoring controllers (app does not run) #story[463]

This commit is contained in:
Peter
2017-03-29 12:58:49 +13:00
parent 1497858cc0
commit 4a6978ff79
5 changed files with 202 additions and 65 deletions
@@ -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<Boat, TimelineInfo> 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<Integer> 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<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
annotationCheck = !annotationCheck;
}
});
toggleFps.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> 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
*/
@@ -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");
}
}
@@ -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<Boat> 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<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
displayAnnotations = !displayAnnotations;
}
});
toggleFps.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> 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<Boat>() {
@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();
}
}
+1 -1
View File
@@ -4,7 +4,7 @@
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:id="contentPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.controllers.MasterViewController">
<AnchorPane fx:id="contentPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.controllers.Controller">
<children>
<!--<fx:include source="RaceView.fxml" fx:id="raceView"/>-->
</children>