From 655f2ee90a29ddcbae91014b63f2f2bc5afaad70 Mon Sep 17 00:00:00 2001 From: James Ball Date: Wed, 6 Jul 2022 15:58:03 +0100 Subject: [PATCH] Add placeholder when no previous projects --- .../java/sh/ball/gui/ExceptionBiConsumer.java | 4 +- src/main/java/sh/ball/gui/Gui.java | 7 +- .../gui/controller/EffectsController.java | 4 + .../ball/gui/controller/MainController.java | 4 + .../controller/ProjectSelectController.java | 288 +++++++++--------- src/main/resources/css/main.css | 6 + src/main/resources/fxml/effects.fxml | 2 +- src/main/resources/fxml/main.fxml | 1 - src/main/resources/fxml/projectSelect.fxml | 23 +- src/main/resources/html/changelog.html | 3 +- 10 files changed, 190 insertions(+), 152 deletions(-) diff --git a/src/main/java/sh/ball/gui/ExceptionBiConsumer.java b/src/main/java/sh/ball/gui/ExceptionBiConsumer.java index 572fa9c..e9b249a 100644 --- a/src/main/java/sh/ball/gui/ExceptionBiConsumer.java +++ b/src/main/java/sh/ball/gui/ExceptionBiConsumer.java @@ -1,6 +1,6 @@ package sh.ball.gui; @FunctionalInterface -public interface ExceptionConsumer { - void accept(T value) throws Exception; +public interface ExceptionBiConsumer { + void accept(T value1, S value2) throws Exception; } diff --git a/src/main/java/sh/ball/gui/Gui.java b/src/main/java/sh/ball/gui/Gui.java index f1958ef..07b2530 100644 --- a/src/main/java/sh/ball/gui/Gui.java +++ b/src/main/java/sh/ball/gui/Gui.java @@ -114,7 +114,7 @@ public class Gui extends Application { root = loader.load(); MainController controller = loader.getController(); - projectSelectController.setApplicationLauncher(path -> launchMainApplication(controller, path)); + projectSelectController.setApplicationLauncher((path, muted) -> launchMainApplication(controller, path, muted)); controller.setAddRecentFile(projectSelectController::addRecentFile); @@ -172,9 +172,12 @@ public class Gui extends Application { editor.setCallback(controller::updateFileData); } - public void launchMainApplication(MainController controller, String projectPath) throws Exception { + public void launchMainApplication(MainController controller, String projectPath, Boolean muted) throws Exception { scene.setRoot(root); controller.openProject(projectPath); + if (muted) { + controller.setVolume(0); + } } public static void launchCodeEditor(String code, String fileName) { diff --git a/src/main/java/sh/ball/gui/controller/EffectsController.java b/src/main/java/sh/ball/gui/controller/EffectsController.java index 8f8ff12..ff44a12 100644 --- a/src/main/java/sh/ball/gui/controller/EffectsController.java +++ b/src/main/java/sh/ball/gui/controller/EffectsController.java @@ -321,4 +321,8 @@ public class EffectsController implements Initializable, SubController { public void disableMouseTranslate() { translateCheckBox.setSelected(false); } + + public void setVolume(double volumeValue) { + volume.controller.slider.setValue(volumeValue); + } } diff --git a/src/main/java/sh/ball/gui/controller/MainController.java b/src/main/java/sh/ball/gui/controller/MainController.java index 7d28a8f..4abf258 100644 --- a/src/main/java/sh/ball/gui/controller/MainController.java +++ b/src/main/java/sh/ball/gui/controller/MainController.java @@ -1433,6 +1433,10 @@ public class MainController implements Initializable, FrequencyListener, MidiLis this.addRecentFile = addRecentFile; } + public void setVolume(double volume) { + effectsController.setVolume(volume); + } + private record PrintableSlider(Slider slider) { @Override public String toString() { diff --git a/src/main/java/sh/ball/gui/controller/ProjectSelectController.java b/src/main/java/sh/ball/gui/controller/ProjectSelectController.java index 69b7e2d..16fca38 100644 --- a/src/main/java/sh/ball/gui/controller/ProjectSelectController.java +++ b/src/main/java/sh/ball/gui/controller/ProjectSelectController.java @@ -1,142 +1,146 @@ -package sh.ball.gui.controller; - -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; -import javafx.concurrent.Worker; -import javafx.fxml.FXML; -import javafx.fxml.Initializable; -import javafx.scene.control.Button; -import javafx.scene.control.CheckBox; -import javafx.scene.control.ListView; -import javafx.scene.web.WebView; -import netscape.javascript.JSObject; -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.w3c.dom.events.EventTarget; -import org.w3c.dom.html.HTMLAnchorElement; -import sh.ball.gui.ExceptionConsumer; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.ResourceBundle; -import java.util.function.Consumer; -import java.util.logging.Level; -import java.util.prefs.Preferences; - -import static sh.ball.gui.Gui.logger; - -public class ProjectSelectController implements Initializable { - - private static final int MAX_ITEMS = 20; - private static final String RECENT_FILE = "RECENT_FILE_"; - - private final Preferences userPreferences = Preferences.userNodeForPackage(getClass()); - private final ObservableList recentFiles = FXCollections.observableArrayList(); - private ExceptionConsumer launchMainApplication; - private Consumer openBrowser; - - @FXML - private ListView recentFilesListView; - @FXML - private Button newProjectButton; - @FXML - private CheckBox startMutedCheckBox; - @FXML - private WebView changelogWebView; - - @Override - public void initialize(URL url, ResourceBundle resourceBundle) { - for (int i = 0; i < MAX_ITEMS; i++) { - String path = userPreferences.get(RECENT_FILE + i, null); - if (path != null) { - recentFiles.add(path); - } else { - break; - } - } - - recentFilesListView.setItems(recentFiles); - recentFilesListView.setOnMouseClicked(e -> { - try { - launchMainApplication.accept(recentFilesListView.getSelectionModel().getSelectedItem()); - } catch (Exception ex) { - logger.log(Level.SEVERE, ex.getMessage(), ex); - } - }); - - newProjectButton.setOnAction(e -> { - try { - launchMainApplication.accept(null); - } catch (Exception ex) { - logger.log(Level.SEVERE, ex.getMessage(), ex); - } - }); - - try { - String changelogHtml = new String(getClass().getResourceAsStream("/html/changelog.html").readAllBytes(), StandardCharsets.UTF_8); - changelogWebView.getEngine().loadContent(changelogHtml); - InputStream changelogInputStream = getClass().getResourceAsStream("/CHANGELOG.md"); - String changelog = new String(changelogInputStream.readAllBytes(), StandardCharsets.UTF_8); - changelogWebView.getEngine().getLoadWorker().stateProperty().addListener((e, old, state) -> { - if (state == Worker.State.SUCCEEDED) { - JSObject window = (JSObject) changelogWebView.getEngine().executeScript("window"); - window.setMember("changelog", changelog); - changelogWebView.getEngine().executeScript( - "document.getElementById('content').innerHTML = marked.parse(changelog);" - ); - Document document = changelogWebView.getEngine().getDocument(); - NodeList nodeList = document.getElementsByTagName("a"); - for (int i = 0; i < nodeList.getLength(); i++) { - Node node= nodeList.item(i); - EventTarget eventTarget = (EventTarget) node; - eventTarget.addEventListener("click", evt -> { - EventTarget target = evt.getCurrentTarget(); - HTMLAnchorElement anchorElement = (HTMLAnchorElement) target; - String href = anchorElement.getHref(); - openBrowser.accept(href); - evt.preventDefault(); - }, false); - } - } - }); - } catch (IOException e) { - logger.log(Level.SEVERE, e.getMessage(), e); - } - } - - public void addRecentFile(String path) { - int index = recentFiles.indexOf(path); - if (index == -1) { - userPreferences.get(RECENT_FILE + recentFiles.size(), path); - recentFiles.add(0, path); - if (recentFiles.size() > MAX_ITEMS) { - recentFiles.remove(recentFiles.size() - 1); - } - } else { - recentFiles.remove(index); - recentFiles.add(0, path); - } - resetRecentFiles(); - } - - private void resetRecentFiles() { - for (int i = 0; i < MAX_ITEMS; i++) { - userPreferences.remove(RECENT_FILE + i); - } - - for (int i = 0; i < recentFiles.size(); i++) { - userPreferences.put(RECENT_FILE + i, recentFiles.get(i)); - } - } - - public void setApplicationLauncher(ExceptionConsumer launchMainApplication) { - this.launchMainApplication = launchMainApplication; - } - - public void setOpenBrowser(Consumer openBrowser) { - this.openBrowser = openBrowser; - } -} +package sh.ball.gui.controller; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.concurrent.Worker; +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.control.Button; +import javafx.scene.control.CheckBox; +import javafx.scene.control.ListView; +import javafx.scene.web.WebView; +import netscape.javascript.JSObject; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.events.EventTarget; +import org.w3c.dom.html.HTMLAnchorElement; +import sh.ball.gui.ExceptionBiConsumer; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ResourceBundle; +import java.util.function.Consumer; +import java.util.logging.Level; +import java.util.prefs.Preferences; + +import static sh.ball.gui.Gui.logger; + +public class ProjectSelectController implements Initializable { + + private static final int MAX_ITEMS = 20; + private static final String RECENT_FILE = "RECENT_FILE_"; + private static final String START_MUTED = "START_MUTED"; + + private final Preferences userPreferences = Preferences.userNodeForPackage(getClass()); + private final ObservableList recentFiles = FXCollections.observableArrayList(); + private ExceptionBiConsumer launchMainApplication; + private Consumer openBrowser; + + @FXML + private ListView recentFilesListView; + @FXML + private Button newProjectButton; + @FXML + private CheckBox startMutedCheckBox; + @FXML + private WebView changelogWebView; + + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + for (int i = 0; i < MAX_ITEMS; i++) { + String path = userPreferences.get(RECENT_FILE + i, null); + if (path != null) { + recentFiles.add(path); + } else { + break; + } + } + + recentFilesListView.setItems(recentFiles); + recentFilesListView.setOnMouseClicked(e -> { + try { + launchMainApplication.accept(recentFilesListView.getSelectionModel().getSelectedItem(), startMutedCheckBox.isSelected()); + } catch (Exception ex) { + logger.log(Level.SEVERE, ex.getMessage(), ex); + } + }); + + newProjectButton.setOnAction(e -> { + try { + launchMainApplication.accept(null, startMutedCheckBox.isSelected()); + } catch (Exception ex) { + logger.log(Level.SEVERE, ex.getMessage(), ex); + } + }); + + startMutedCheckBox.setSelected(userPreferences.getBoolean(START_MUTED, false)); + startMutedCheckBox.selectedProperty().addListener((e, old, startMuted) -> userPreferences.putBoolean(START_MUTED, startMuted)); + + try { + String changelogHtml = new String(getClass().getResourceAsStream("/html/changelog.html").readAllBytes(), StandardCharsets.UTF_8); + changelogWebView.getEngine().loadContent(changelogHtml); + InputStream changelogInputStream = getClass().getResourceAsStream("/CHANGELOG.md"); + String changelog = new String(changelogInputStream.readAllBytes(), StandardCharsets.UTF_8); + changelogWebView.getEngine().getLoadWorker().stateProperty().addListener((e, old, state) -> { + if (state == Worker.State.SUCCEEDED) { + JSObject window = (JSObject) changelogWebView.getEngine().executeScript("window"); + window.setMember("changelog", changelog); + changelogWebView.getEngine().executeScript( + "document.getElementById('content').innerHTML = marked.parse(changelog);" + ); + Document document = changelogWebView.getEngine().getDocument(); + NodeList nodeList = document.getElementsByTagName("a"); + for (int i = 0; i < nodeList.getLength(); i++) { + Node node= nodeList.item(i); + EventTarget eventTarget = (EventTarget) node; + eventTarget.addEventListener("click", evt -> { + EventTarget target = evt.getCurrentTarget(); + HTMLAnchorElement anchorElement = (HTMLAnchorElement) target; + String href = anchorElement.getHref(); + openBrowser.accept(href); + evt.preventDefault(); + }, false); + } + } + }); + } catch (IOException e) { + logger.log(Level.SEVERE, e.getMessage(), e); + } + } + + public void addRecentFile(String path) { + int index = recentFiles.indexOf(path); + if (index == -1) { + userPreferences.get(RECENT_FILE + recentFiles.size(), path); + recentFiles.add(0, path); + if (recentFiles.size() > MAX_ITEMS) { + recentFiles.remove(recentFiles.size() - 1); + } + } else { + recentFiles.remove(index); + recentFiles.add(0, path); + } + resetRecentFiles(); + } + + private void resetRecentFiles() { + for (int i = 0; i < MAX_ITEMS; i++) { + userPreferences.remove(RECENT_FILE + i); + } + + for (int i = 0; i < recentFiles.size(); i++) { + userPreferences.put(RECENT_FILE + i, recentFiles.get(i)); + } + } + + public void setApplicationLauncher(ExceptionBiConsumer launchMainApplication) { + this.launchMainApplication = launchMainApplication; + } + + public void setOpenBrowser(Consumer openBrowser) { + this.openBrowser = openBrowser; + } +} diff --git a/src/main/resources/css/main.css b/src/main/resources/css/main.css index 5ce5837..bb73b6c 100644 --- a/src/main/resources/css/main.css +++ b/src/main/resources/css/main.css @@ -385,4 +385,10 @@ -fx-border-width: 1px; -fx-border-radius: 0; -fx-border-color: white; +} + +.white-border { + -fx-border-width: 1px; + -fx-border-radius: 0; + -fx-border-color: white; } \ No newline at end of file diff --git a/src/main/resources/fxml/effects.fxml b/src/main/resources/fxml/effects.fxml index dea76eb..eaa3830 100644 --- a/src/main/resources/fxml/effects.fxml +++ b/src/main/resources/fxml/effects.fxml @@ -63,7 +63,7 @@ - +