From 72a7f56085412df9ae978c0911f04bfdef6e75fd Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Sun, 5 Jan 2020 23:01:17 +0100 Subject: [PATCH] added loading file and status --- src/main/java/brainfuck/gui/Controller.java | 59 ++++++++++++++++--- .../brainfuck/interpreter/BfInterpreter.java | 10 +++- src/main/java/brainfuck/interpreter/Main.java | 6 ++ src/main/resources/css/stylesheet.css | 9 +-- src/main/resources/fxml/layout.fxml | 33 ++++++++--- 5 files changed, 96 insertions(+), 21 deletions(-) diff --git a/src/main/java/brainfuck/gui/Controller.java b/src/main/java/brainfuck/gui/Controller.java index abbcc6c..6a3ad58 100644 --- a/src/main/java/brainfuck/gui/Controller.java +++ b/src/main/java/brainfuck/gui/Controller.java @@ -1,14 +1,17 @@ package brainfuck.gui; import brainfuck.interpreter.BfInterpreter; +import brainfuck.interpreter.Main; import javafx.fxml.FXML; -import javafx.scene.control.Alert; -import javafx.scene.control.Button; -import javafx.scene.control.Menu; -import javafx.scene.control.MenuBar; -import javafx.scene.control.MenuItem; -import javafx.scene.control.TextArea; +import javafx.scene.control.*; import javafx.scene.control.Alert.AlertType; +import javafx.scene.layout.AnchorPane; +import javafx.stage.FileChooser; +import org.apache.commons.io.IOUtils; + +import java.io.File; +import java.io.FileInputStream; +import java.nio.charset.StandardCharsets; public class Controller { @FXML @@ -29,22 +32,64 @@ public class Controller { @FXML private Button loadButton; + @FXML + private AnchorPane rootPane; + + @FXML + private Label statusText; + public void initialize() { + changeStatus("Idle", "#9a9c9a"); saveMenu.setOnAction(e -> { System.out.println("menu clicked"); }); interpretButton.setOnAction(e -> { + changeStatus("Interpreting...", "#28fff5"); try { interpretFromText(); } catch (Exception e1) { e1.printStackTrace(); } + changeStatus("Done!", "#00ff12"); + }); + + loadButton.setOnAction(e -> { + FileChooser fileChooser = new FileChooser(); + fileChooser.setSelectedExtensionFilter(new FileChooser.ExtensionFilter("Brainfuck file", ".bf", ".b")); + fileChooser.setTitle("Choose file to import"); + + changeStatus("Interpreting...", "#28fff5"); + try { + File selected = fileChooser.showOpenDialog(rootPane.getScene().getWindow()); + + if (selected == null) { + changeStatus("Idle", "#9a9c9a"); + return; + } + + FileInputStream fileInputStream = new FileInputStream(selected); + String input = IOUtils.toString(fileInputStream, StandardCharsets.UTF_8); + + textinput.setText(input); + BfInterpreter interpreter = new BfInterpreter(""); + interpreter.setCodeFromFile(selected); + String interpreted = interpreter.interpret(); + textoutput.setText(interpreted); + } catch (Exception ex) { + ex.printStackTrace(); + } + changeStatus("Done!", "#00ff12"); }); // interpretButton.setSkin(new ScaleSkin(interpretButton)); } + private void changeStatus(String text, String color) { + statusText.setStyle("-fx-text-fill: " + color); + statusText.setText(text); + } + private void interpretFromText() throws Exception { String inputCode = textinput.getText(); if (inputCode == null || inputCode.isEmpty()) { @@ -56,7 +101,7 @@ public class Controller { textoutput.setText(interpreted); } - + } } diff --git a/src/main/java/brainfuck/interpreter/BfInterpreter.java b/src/main/java/brainfuck/interpreter/BfInterpreter.java index b757ccb..0c31746 100644 --- a/src/main/java/brainfuck/interpreter/BfInterpreter.java +++ b/src/main/java/brainfuck/interpreter/BfInterpreter.java @@ -1,7 +1,10 @@ package brainfuck.interpreter; +import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.Scanner; import org.apache.commons.io.IOUtils; @@ -36,11 +39,16 @@ public class BfInterpreter { String res = ""; FileInputStream fis = new FileInputStream(fileName); - code = IOUtils.toString(fis, "UTF-8"); + code = IOUtils.toString(fis, StandardCharsets.UTF_8); // System.out.println("code is: " + code); } + public void setCodeFromFile(File file) throws IOException { + FileInputStream fileInputStream = new FileInputStream(file); + code = IOUtils.toString(fileInputStream, StandardCharsets.UTF_8); + } + public String interpret() { int loop = 0; StringBuilder result = new StringBuilder(); diff --git a/src/main/java/brainfuck/interpreter/Main.java b/src/main/java/brainfuck/interpreter/Main.java index 0937c26..2c26d77 100644 --- a/src/main/java/brainfuck/interpreter/Main.java +++ b/src/main/java/brainfuck/interpreter/Main.java @@ -14,6 +14,7 @@ import javafx.stage.Stage; * Main */ public class Main extends Application { + private Stage stage; public static void main(String[] args) { String excl = ">,[[----------[ >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<] ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],< ]> ]>>>++>+>>[ <<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>] >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]] >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>> ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<, ]"; @@ -24,6 +25,7 @@ public class Main extends Application { @Override public void start(Stage stage) throws Exception { + this.stage = stage; Parent pane = FXMLLoader.load(getClass().getResource("/fxml/layout.fxml")); Scene scene = new Scene(pane); scene.getStylesheets().add("/css/menuBar.css"); @@ -33,4 +35,8 @@ public class Main extends Application { stage.show(); } + + public Stage getStage() { + return this.stage; + } } \ No newline at end of file diff --git a/src/main/resources/css/stylesheet.css b/src/main/resources/css/stylesheet.css index 01dbfad..c73bcf6 100644 --- a/src/main/resources/css/stylesheet.css +++ b/src/main/resources/css/stylesheet.css @@ -13,13 +13,14 @@ .textbox { -fx-text-fill: rgb(33, 189, 33); } + .textbox .content { -fx-background-color: rgb(43, 43, 43); -fx-text-fill: rgb(33, 189, 33); } -.dialog-pane>.content { +.dialog-pane > .content { -fx-text-fill: -fx-button-color; -fx-font-weight: bold; -fx-font-size: 14px; @@ -28,16 +29,16 @@ } -.dialog-pane>.header-panel { +.dialog-pane > .header-panel { -fx-text-fill: -fx-button-color; -fx-font-size: 14px; -fx-padding: 0.833em 0 0 0.833em; /* 10px 0px 0px 10px */ } -.dialog-pane>.header-panel>.label { +.dialog-pane > .header-panel > .label { -fx-text-fill: -fx-button-color; -fx-font-size: 14px; -fx-padding: 0.833em 0 0 0.833em; /* 10px 0px 0px 10px */ -} \ No newline at end of file +} diff --git a/src/main/resources/fxml/layout.fxml b/src/main/resources/fxml/layout.fxml index 785bc19..263c511 100644 --- a/src/main/resources/fxml/layout.fxml +++ b/src/main/resources/fxml/layout.fxml @@ -1,15 +1,20 @@ - - - - + + + + + + + + + + - - + @@ -25,12 +30,22 @@ - -