[ all classes ]
[ it.polimi.ingsw.view.GUI ]
Coverage Summary for Class: InitialPageController (it.polimi.ingsw.view.GUI)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
InitialPageController | 0% (0/ 1) | 0% (0/ 19) | 0% (0/ 153) |
1 package it.polimi.ingsw.view.GUI;
2
3 import javafx.animation.FadeTransition;
4 import javafx.application.Platform;
5 import javafx.beans.property.DoubleProperty;
6 import javafx.beans.property.SimpleDoubleProperty;
7 import javafx.collections.FXCollections;
8 import javafx.collections.ObservableList;
9 import javafx.fxml.FXML;
10 import javafx.scene.control.Button;
11 import javafx.scene.control.ChoiceBox;
12 import javafx.scene.control.Label;
13 import javafx.scene.control.TextField;
14 import javafx.scene.image.Image;
15 import javafx.scene.image.ImageView;
16 import javafx.scene.layout.Pane;
17 import javafx.util.Duration;
18
19 import java.net.URL;
20 import java.util.ResourceBundle;
21
22 public class InitialPageController implements Controller {
23
24 ObservableList<String> gameModes = FXCollections.observableArrayList("2 players", "3 players");
25 private static MainController controller = new MainController();
26 private Boolean state = false;
27 private DoubleProperty height = new SimpleDoubleProperty(720);
28 private DoubleProperty width = new SimpleDoubleProperty(1280);
29 private static String IP = null;
30 private static int PORT = 0;
31
32 @FXML
33 Pane pane;
34 @FXML
35 private ResourceBundle resources;
36 @FXML
37 public ImageView backGround, cloud;
38 @FXML
39 private URL location;
40 @FXML
41 private Label message, alert, usernameAlert;
42 @FXML
43 private TextField ip, port, username;
44 @FXML
45 private Button connect, sendMode, sendUsername, quit;
46 @FXML
47 private ChoiceBox<String> modes;
48
49 public static void setController(MainController controller) {
50 InitialPageController.controller = controller;
51 }
52
53 /**
54 * Setup the connection for player
55 */
56 @FXML
57 void setConnection() {
58 alert.setVisible(false);
59 state = false;
60 IP = ip.getText();
61 PORT = Integer.parseInt(port.getText());
62 state = controller.setConnection(IP, PORT);
63 if (state)
64 changeScene();
65 else {
66 alert.setText("Wrong IP/Port");
67 alert.setVisible(true);
68 }
69 }
70
71 /**
72 * Send the chosen mode to server
73 */
74 @FXML
75 void sendMode() {
76 if (modes.getValue() == "2 players")
77 controller.setMode("TWO");
78 else
79 controller.setMode("THREE");
80 modes.setVisible(false);
81 sendMode.setVisible(false);
82 username.setVisible(true);
83 sendUsername.setVisible(true);
84
85 }
86
87 /**
88 * Send username to server
89 */
90 @FXML
91 void sendUsername() {
92 if (username.getText().trim().equals("")) {
93 usernameAlert.setText("Username not available!");
94 usernameAlert.setVisible(true);
95 username.clear();
96 return;
97 }
98 state = controller.sendUsername(username.getText().trim());
99 if (state) {
100 username.setVisible(false);
101 sendUsername.setVisible(false);
102 message.setVisible(true);
103 usernameAlert.setVisible(false);
104 } else {
105 usernameAlert.setText("Username not available!");
106 usernameAlert.setVisible(true);
107 }
108 }
109
110 private void changeScene() {
111 ip.setVisible(false);
112 port.setVisible(false);
113 alert.setVisible(false);
114 connect.setVisible(false);
115 modes.setValue("2 players");
116 modes.setItems(gameModes);
117 modes.setVisible(true);
118 sendMode.setVisible(true);
119 quit.setOnAction(e -> {
120 controller.closeConnection();
121 initialize();
122 });
123 }
124
125 @FXML
126 private void initialize() {
127 changePage(true);
128 port.setText("9090");
129 ip.setVisible(true);
130 port.setVisible(true);
131 alert.setVisible(false);
132 connect.setVisible(true);
133 modes.setVisible(false);
134 sendMode.setVisible(false);
135 username.setVisible(false);
136 usernameAlert.setVisible(false);
137 sendUsername.setVisible(false);
138 message.setVisible(false);
139 cloud.setVisible(false);
140 cloud.setDisable(true);
141 cloud.setImage(new Image(ImageEnum.getUrl("CLOUD")));
142 if (IP != null && PORT != 0) {
143 ip.setText(IP);
144 port.setText(String.valueOf(PORT));
145 }
146 quit.setOnAction(e -> {
147 Platform.exit();
148 });
149 port.setOnKeyPressed(e -> {
150 if (e.getCode().toString().equals("ENTER")) {
151 setConnection();
152 }
153 });
154 ip.setOnKeyPressed(e -> {
155 if (e.getCode().toString().equals("ENTER")) {
156 setConnection();
157 }
158 });
159 username.setOnKeyPressed(e -> {
160 if (e.getCode().toString().equals("ENTER")) {
161 sendUsername();
162 }
163 });
164 modes.setOnKeyPressed(e -> {
165 if (e.getCode().toString().equals("ENTER")) {
166 sendMode();
167 }
168 });
169 setUpDimension();
170 }
171
172 private void setUpDimension() {
173 pane.prefHeightProperty().bind(height);
174 pane.prefWidthProperty().bind(width);
175 cloud.fitWidthProperty().bind(width);
176 cloud.fitHeightProperty().bind(height);
177 backGround.fitWidthProperty().bind(width);
178 backGround.fitHeightProperty().bind(height);
179 ip.layoutXProperty().bind(width.subtract(150).divide(2));
180 ip.layoutYProperty().bind(height.multiply(0.7));
181 alert.layoutXProperty().bind(width.subtract(150).divide(2).add(155));
182 alert.layoutYProperty().bind(height.multiply(0.7).add(5));
183 port.layoutXProperty().bind(width.subtract(150).divide(2));
184 port.layoutYProperty().bind(height.multiply(0.7).add(40));
185 connect.layoutXProperty().bind(width.subtract(150).divide(2));
186 connect.layoutYProperty().bind(height.multiply(0.7).add(80));
187 quit.layoutXProperty().bind(width.subtract(150).divide(2).add(105));
188 quit.layoutYProperty().bind(height.multiply(0.7).add(80));
189 modes.layoutXProperty().bind(width.subtract(150).divide(2));
190 modes.layoutYProperty().bind(height.multiply(0.7).add(40));
191 sendMode.layoutXProperty().bind(width.subtract(150).divide(2));
192 sendMode.layoutYProperty().bind(height.multiply(0.7).add(80));
193 username.layoutXProperty().bind(width.subtract(150).divide(2));
194 username.layoutYProperty().bind(height.multiply(0.7).add(40));
195 usernameAlert.layoutXProperty().bind(width.subtract(150).divide(2).add(155));
196 usernameAlert.layoutYProperty().bind(height.multiply(0.7).add(45));
197 sendUsername.layoutXProperty().bind(width.subtract(150).divide(2));
198 sendUsername.layoutYProperty().bind(height.multiply(0.7).add(80));
199 message.layoutXProperty().bind(width.subtract(150).divide(2));
200 message.layoutYProperty().bind(height.multiply(0.7).add(40));
201 }
202
203 /**
204 * Set width
205 *
206 * @param width width
207 */
208 @Override
209 public void setDimension(double width, double height) {
210 if (width * 720 / 1280 < height) {
211 pane.setLayoutY((height - (width * 720 / 1280)) / 2);
212 pane.setLayoutX(0);
213 this.height.set(width * 720 / 1280);
214 this.width.set(width);
215 } else {
216 pane.setLayoutX((width - (height * 1280 / 720)) / 2);
217 pane.setLayoutY(0);
218 this.width.set(height * 1280 / 720);
219 this.height.set(height);
220 }
221 }
222
223
224 /**
225 * Change view
226 *
227 * @param state if it's allowed to change view
228 */
229 @Override
230 public void changePage(Boolean state) {
231 cloud.setVisible(true);
232 FadeTransition fade = new FadeTransition();
233 fade.setDuration(Duration.millis(1000));
234 if (!state) {
235 fade.setFromValue(0);
236 fade.setToValue(10);
237 fade.setOnFinished(e -> {
238 controller.changeScene();
239 });
240 } else {
241 fade.setToValue(0);
242 fade.setFromValue(10);
243 }
244 fade.setCycleCount(1);
245 fade.setAutoReverse(false);
246 fade.setNode(cloud);
247 fade.play();
248
249 }
250
251 /**
252 * Reload the board for all players
253 */
254 @Override
255 public void reSet() {
256
257 }
258 }