You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.0 KiB

package visualiser.Controllers;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.SplitPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import mock.app.Event;
import mock.exceptions.EventConstructionException;
import visualiser.model.View3D;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Controller for Hosting a game.
*/
public class HostController extends Controller {
// @FXML
// TextField gameNameField;
//
// @FXML
// TextField hostNameField;
@FXML
private ImageView imageView;
@FXML
AnchorPane hostWrapper;
@FXML
AnchorPane imagePane;
@FXML
SplitPane splitPane;
@FXML
AnchorPane specPane;
@FXML
GridPane playerContainer;
@FXML
private Pane playerPane;
@FXML
private Pane playerPane4;
private Event game;
private View3D fancyStuff;
@Override
public void initialize(URL location, ResourceBundle resources) {
fancyStuff = new View3D();
playerPane4.getChildren().add(fancyStuff);
//centering the 3d object
fancyStuff.layoutXProperty().bind(playerPane4.widthProperty().subtract(fancyStuff.widthProperty()).divide(2));
fancyStuff.layoutYProperty().bind(playerPane4.heightProperty().subtract(fancyStuff.heightProperty()).divide(2));
//playerContainer.add(fancyStuff, 0,0);
fancyStuff.spinBox();
}
/**
* Hosts a game
* @throws IOException if socket cannot be connected to
*/
public void hostGamePressed() throws IOException{
try {
this.game = new Event(false);
connectSocket("localhost", 4942);
} catch (EventConstructionException e) {
Logger.getGlobal().log(Level.SEVERE, "Could not create Event.", e);
throw new RuntimeException(e);
}
}
public void endEvent() throws IOException {
game.endEvent();
}
/**
* Connect to a socket
* @param address address of the server
* @param port port that the server is run off
*/
public void connectSocket(String address, int port) {
try{
Socket socket = new Socket(address, port);
hostWrapper.setVisible(false);
parent.enterLobby(socket, true);
} catch (IOException e) { /* Never reached */ }
}
public AnchorPane startWrapper(){
return hostWrapper;
}
/**
* Hosts a game.
*/
public void hostGame(){
splitPane.setResizableWithParent(specPane, false);
splitPane.lookupAll(".split-pane-divider").stream().forEach(div -> div.setMouseTransparent(true));
imageView.fitWidthProperty().bind(imagePane.widthProperty());
imageView.fitHeightProperty().bind(imagePane.heightProperty());
hostWrapper.setVisible(true);
}
/**
* Menu button pressed. Prompt alert then return to menu
*/
public void menuBtnPressed(){
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Quitting race");
alert.setContentText("Do you wish to quit the race?");
alert.setHeaderText("You are about to quit the race");
Optional<ButtonType> result = alert.showAndWait();
if(result.get() == ButtonType.OK){
hostWrapper.setVisible(false);
parent.enterTitle();
}
}
/**
* Start button pressed. Currently only prints out start
*/
public void startBtnPressed(){
System.out.println("Should start the race. This button is only visible for the host");
}
public void joinSpecPressed(){
System.out.println("Spectator list pressed. Joining spectators");
}
public void joinRacePressed(){
System.out.println("Empty race user pane pressed. Joining racers");
}
}