Skip to content

Commit

Permalink
Merge branch 'javafx' of github.com:Cardshifter/Cardshifter into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Sep 18, 2014
2 parents ef41ed2 + 1920f1f commit 828fc06
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.Group;
Expand Down Expand Up @@ -80,7 +83,7 @@ public void acceptIPAndPort(String ipAddress, int port) {
this.ipAddress = ipAddress;
this.port = port;
}
public void connectToGame() {
public boolean connectToGame() {
// this is called on the object from the Game launcher before the scene is displayed
try {
this.socket = new Socket(this.ipAddress, this.port);
Expand All @@ -90,14 +93,12 @@ public void connectToGame() {
mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
new Thread(this::listen).start();
} catch (Exception e) {
e.printStackTrace();
}

try {
new Thread(this::play).start();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Connection Failed");
return false;
}

new Thread(this::play).start();
return true;
}
private void play() {
// this method only runs once at the start
Expand Down Expand Up @@ -144,6 +145,9 @@ private void listen() {
messages.offer(message);
Platform.runLater(() -> this.processMessageFromServer(message));
}
} catch (SocketException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -216,7 +220,7 @@ private void processPlayerMessage(PlayerMessage message) {
private void processPlayerMessageForPlayer(PlayerMessage message, Pane statBox, Map playerMap) {
statBox.getChildren().clear();
Map<String, Integer> sortedMap = new TreeMap<>(message.getProperties());
playerMap = sortedMap;
playerMap.putAll(sortedMap);
for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
String key = entry.getKey();
statBox.getChildren().add(new Label(key));
Expand Down Expand Up @@ -296,7 +300,7 @@ private void processUseableActionMessage(UseableActionMessage message) {
double paneHeight = actionBox.getHeight();
double paneWidth = actionBox.getWidth();

int maxActions = 5;
int maxActions = 8;
double actionWidth = paneWidth / maxActions;

ActionButton actionButton = new ActionButton(message, this, actionWidth, paneHeight);
Expand All @@ -312,10 +316,8 @@ private void processUpdateMessage(UpdateMessage message) {
}
private void processUpdateMessageForPlayer(Pane statBox, UpdateMessage message, Map playerMap) {
String key = (String)message.getKey();
System.out.println(String.format("The old value = %d", playerMap.get(key)));
Integer value = (Integer)message.getValue();
playerMap.put(key, value);
System.out.println(String.format("new map value = %d", playerMap.get(key)));

this.repaintStatBox(statBox, playerMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,70 +12,52 @@
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

//This class just loads the FXML document which initializes its DocumentController

public class GameClientLauncherController implements Initializable {

@FXML
private TextField ipAddressBox;

@FXML
private TextField portBox;

@FXML
private Button connectButton;

@FXML
private Label errorMessage;
@FXML private TextField ipAddressBox;
@FXML private TextField portBox;
@FXML private Button connectButton;
@FXML private Label errorMessage;
@FXML private AnchorPane anchorPane;

private String getCharactersFromTextField(TextField textField) {
return textField.getCharacters().toString();
}

private void buttonClick(ActionEvent event) {
//Get values from the TextFields
String ipAddressValue = this.getCharactersFromTextField(ipAddressBox);
int portValue = Integer.parseInt(this.getCharactersFromTextField(portBox));

//Attempt to make a connection
try {
//Send a test to the server, to make sure that it is valid

//if it is valid
errorMessage.setText("Success!");
this.closeWithSuccess(event);
this.switchToMainGameWindow(ipAddressValue, portValue);
} catch (Exception e) {
String message = e.getMessage();
errorMessage.setText(message);
}
this.switchToMainGameWindow(ipAddressValue, portValue);
}

private void closeWithSuccess(ActionEvent event) {
Node source = (Node)event.getSource();
private void closeWithSuccess() {
Node source = anchorPane;
Stage stage = (Stage)source.getScene().getWindow();
stage.close();
}

private void switchToMainGameWindow(String ipAddress, int port) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("ClientDocument.fxml"));

Parent root = (Parent)loader.load();

GameClientController controller = loader.<GameClientController>getController();
controller.acceptIPAndPort(ipAddress, port);
controller.connectToGame();

Scene scene = new Scene(root);

Stage gameStage = new Stage();

gameStage.setScene(scene);
//stage.centerOnScreen();
gameStage.show();
if (controller.connectToGame()) {
errorMessage.setText("Success!");
this.closeWithSuccess();

Scene scene = new Scene(root);
Stage gameStage = new Stage();
gameStage.setScene(scene);
gameStage.show();
} else {
errorMessage.setText("Connection Failed!");
}
}
catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -84,7 +66,6 @@ private void switchToMainGameWindow(String ipAddress, int port) {

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
connectButton.setOnAction(this::buttonClick);
ipAddressBox.setText("127.0.0.1");
portBox.setText("4242");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.cardshifter.client.GameClientLauncherController">
<AnchorPane fx:id="anchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.cardshifter.client.GameClientLauncherController">
<children>
<AnchorPane layoutX="182.0" layoutY="25.0" prefHeight="102.0" prefWidth="200.0">
<children>
Expand Down

0 comments on commit 828fc06

Please sign in to comment.