package luj.komandor;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
/*import org.lwjgl.LWJGLException;
import org.lwjgl.input.Controller;
import org.lwjgl.input.Controllers;*/
/**
*
* @author MichaĆ
*/
public class LujKomandor extends Application {
private TextArea console;
private Button connectButton;
private ComboBox gamepadSelect;
private LinkedList<Text> statusIndicators;
private LujSocket lujSocket;
private Thread socketThread;
private Thread sensorsUpdaterThread;
@Override
public void start(Stage primaryStage) {
lujSocket = new LujSocket();
BorderPane bpane = new BorderPane();
bpane.setTop(addTopHBox());
//bpane.setRight(addRightVBox());
bpane.setCenter(addCenterHBox());
bpane.setBottom(addBottomVBox());
Scene scene = new Scene(bpane);
primaryStage.setScene(scene);
primaryStage.setTitle("Luj Komandor");
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
try {
if(socketThread != null)
socketThread.join();
if(sensorsUpdaterThread != null)
sensorsUpdaterThread.join();
} catch (InterruptedException ex) {
Logger.getLogger(LujKomandor.class.getName()).log(Level.SEVERE, null, ex);
}
//Platform.exit();
//System.exit(0);
}
});
primaryStage.show();
console.appendText("Program started.\n");
}
private HBox addTopHBox() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10);
hbox.setStyle(
"-fx-background-color:\n"
+ "linear-gradient(#e5eaeb 0%, #bac4c9 100%)\n");
VBox textVBox = new VBox();
Text welcome = new Text("Luj Komandor");
welcome.setFont(Font.font("Arial", 24));
Text info = new Text("Luj Szperacz control application");
info.setFont(Font.font("Arial", 11));
textVBox.getChildren().addAll(welcome, info);
ImageView imageTop = new ImageView(new Image(getClass().getResourceAsStream("/images/koprobo.png")));
hbox.getChildren().addAll(imageTop, textVBox);
return hbox;
}
private VBox addBottomVBox() {
VBox vbox = new VBox();
vbox.setStyle("-fx-background-color: #bac4c9;");
console = new TextArea();
Text caption = new Text("Console:");
vbox.getChildren().addAll(caption, console);
return vbox;
}
private HBox addCenterHBox() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10);
hbox.setStyle("-fx-background-color: #bac4c9;");
// SETTINGS
VBox settings = new VBox();
settings.setSpacing(5);
Text settingsTitle = new Text("Settings");
Text settingsGamepad = new Text("Gamepad settings");
Text settingsConnection = new Text("Connection settings");
settingsTitle.setFont(Font.font("Arial", 18));
settingsGamepad.setFont(Font.font("Arial", 13));
settingsConnection.setFont(Font.font("Arial", 13));
gamepadSelect = new ComboBox();
connectButton = new Button("Connect");
connectButton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
if(socketThread != null) {
if(socketThread.isAlive()) {
connectButton.setText("Connect");
try {
lujSocket.terminate();
socketThread.join();
console.appendText("Disconnected. Socket thread " + socketThread.toString() + " terminated successfully.\n");
socketThread = null;
} catch (InterruptedException ex) {
Logger.getLogger(LujKomandor.class.getName()).log(Level.SEVERE, null, ex);
}
}
} else {
if(lujSocket.connect())
{
connectButton.setText("Disconnect");
socketThread = new Thread(lujSocket);
lujSocket.start();
socketThread.start();
console.appendText("Connected successfully. Socket thread " + socketThread.toString() + " started.\n");
} else
console.appendText("Cannot connect to Luj Szperacz.\n");
}
}
});
// CONTROL
VBox control = new VBox();
control.setSpacing(5);
Text controlTitle = new Text("Control");
controlTitle.setFont(Font.font("Arial", 18));
// STATUS
VBox status = new VBox();
status.setSpacing(5);
Text statusTitle = new Text("Status");
statusTitle.setFont(Font.font("Arial", 18));
Text statusPadXAxisCaption = new Text("Gamepad X Axis");
Text statusPadYAxisCaption = new Text("Gamepad Y Axis");
Text statusPiTempCaption = new Text("RPi CPU Temperature");
Text statusOutsideTempCaption = new Text("Outside Temperature");
Text statusBatteryVoltageCaption = new Text("Battery Voltage");
statusPadXAxisCaption.setFont(Font.font("Arial", 14));
statusPadYAxisCaption.setFont(Font.font("Arial", 14));
statusPiTempCaption.setFont(Font.font("Arial", 14));
statusOutsideTempCaption.setFont(Font.font("Arial", 14));
statusBatteryVoltageCaption.setFont(Font.font("Arial", 14));
Text statusPadXAxis = new Text("Settings");
Text statusPadYAxis = new Text("Settings");
Text statusPiTemp = new Text("Gamepad settings");
Text statusOutsideTemp = new Text("Connection settings");
Text statusBatteryVoltage = new Text("Connection settings");
statusIndicators = new LinkedList<Text>();
statusIndicators.add(statusPadXAxis);
statusIndicators.add(statusPadYAxis);
statusIndicators.add(statusPiTemp);
statusIndicators.add(statusOutsideTemp);
statusIndicators.add(statusBatteryVoltage);
for(Text t : statusIndicators)
t.setFont(Font.font("Arial", 11));
SensorsUpdater sensorsUpdater = new SensorsUpdater(statusIndicators, lujSocket);
sensorsUpdaterThread = new Thread(sensorsUpdater);
sensorsUpdater.start();
sensorsUpdaterThread.start();
settings.getChildren().addAll(settingsTitle, settingsGamepad, gamepadSelect, settingsConnection, connectButton);
control.getChildren().addAll(controlTitle);
status.getChildren().addAll(statusTitle, statusPadXAxisCaption, statusPadXAxis, statusPadYAxisCaption, statusPadYAxis, statusPiTempCaption, statusPiTemp, statusOutsideTempCaption, statusOutsideTemp, statusBatteryVoltageCaption, statusBatteryVoltage);
hbox.getChildren().addAll(settings, control, status);
return hbox;
}
public static void main(String[] args) {
launch(args);
}
}