From 4464d97d6dc695ba7a93c3d45cf41894d3c2bc62 Mon Sep 17 00:00:00 2001 From: James Ball Date: Tue, 20 Sep 2022 21:58:17 +0100 Subject: [PATCH] Save font family and font style with project --- .../ball/gui/controller/MainController.java | 42 ++++++++++++++++--- .../sh/ball/parser/lua/LuaSampleSource.java | 3 ++ .../java/sh/ball/parser/txt/FontStyle.java | 21 ++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/main/java/sh/ball/gui/controller/MainController.java b/src/main/java/sh/ball/gui/controller/MainController.java index 23f02ed0..8c59463d 100644 --- a/src/main/java/sh/ball/gui/controller/MainController.java +++ b/src/main/java/sh/ball/gui/controller/MainController.java @@ -30,6 +30,7 @@ import java.util.concurrent.*; import java.util.function.Consumer; import java.util.function.UnaryOperator; import java.util.logging.Level; +import java.util.stream.Collectors; import javafx.fxml.FXML; import javafx.fxml.Initializable; @@ -462,12 +463,12 @@ public class MainController implements Initializable, FrequencyListener, MidiLis } else { fontFamilyListView.getSelectionModel().select(installedFonts[0]); } - fontFamilyListView.getSelectionModel().selectedItemProperty().addListener((e, old, family) -> updateFileData(openFiles.get(currentFrameSource), frameSourcePaths.get(currentFrameSource))); + fontFamilyListView.getSelectionModel().selectedItemProperty().addListener((e, old, family) -> updateTextFiles()); fontStyleComboBox.setItems(FXCollections.observableList(Arrays.stream(FontStyle.values()).toList())); fontStyleComboBox.setValue(FontStyle.PLAIN); - fontStyleComboBox.valueProperty().addListener((e, old, family) -> updateFileData(openFiles.get(currentFrameSource), frameSourcePaths.get(currentFrameSource))); + fontStyleComboBox.valueProperty().addListener((e, old, family) -> updateTextFiles()); sliderMinTextField.focusedProperty().addListener((e, old, focused) -> { String text = sliderMinTextField.getText(); @@ -529,6 +530,15 @@ public class MainController implements Initializable, FrequencyListener, MidiLis brightnessSlider.valueProperty().addListener((e, old, brightness) -> audioPlayer.setBrightness(brightness.doubleValue())); } + private void updateTextFiles() { + List textFrameSources = frameSourcePaths.stream() + .filter(path -> path.endsWith(".txt")) + .map(path -> frameSourcePaths.indexOf(path)) + .toList(); + + textFrameSources.forEach(i -> updateFileData(openFiles.get(i), frameSourcePaths.get(i), false)); + } + public void setLuaVariable(String variableName, Object value) { sampleParsers.forEach(sampleParser -> { if (sampleParser instanceof LuaParser luaParser) { @@ -775,7 +785,7 @@ public class MainController implements Initializable, FrequencyListener, MidiLis Platform.runLater(() -> generalController.showMultiFileTooltip(frameSources.size() > 1)); } - public void updateFileData(byte[] file, String name) { + public void updateFileData(byte[] file, String name, boolean updateUnsavedFiles) { int index = frameSourcePaths.indexOf(name); if (index == -1) { throw new RuntimeException("Can't find open file with name: " + name); @@ -798,8 +808,10 @@ public class MainController implements Initializable, FrequencyListener, MidiLis sampleParsers.set(index, null); } - unsavedFileNames.add(name); - setUnsavedFileWarning(); + if (updateUnsavedFiles) { + unsavedFileNames.add(name); + setUnsavedFileWarning(); + } } catch (Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); } @@ -1192,6 +1204,14 @@ public class MainController implements Initializable, FrequencyListener, MidiLis frameSource.appendChild(document.createTextNode(String.valueOf(currentFrameSource))); root.appendChild(frameSource); + Element fontFamily = document.createElement("fontFamily"); + fontFamily.appendChild(document.createTextNode(fontFamilyListView.getSelectionModel().getSelectedItem())); + root.appendChild(fontFamily); + + Element fontStyle = document.createElement("fontStyle"); + fontStyle.appendChild(document.createTextNode(String.valueOf(fontStyleComboBox.getValue().style))); + root.appendChild(fontStyle); + TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(document); @@ -1369,6 +1389,16 @@ public class MainController implements Initializable, FrequencyListener, MidiLis updateFiles(files, fileNames, 0); } + Element fontFamily = (Element) root.getElementsByTagName("fontFamily").item(0); + if (fontFamily != null) { + fontFamilyListView.getSelectionModel().select(fontFamily.getTextContent()); + } + + Element fontStyle = (Element) root.getElementsByTagName("fontStyle").item(0); + if (fontStyle != null) { + fontStyleComboBox.setValue(FontStyle.getStyle(Integer.parseInt(fontStyle.getTextContent()))); + } + luaController.updateLuaVariables(); openProjectPath = projectFileName; @@ -1443,7 +1473,7 @@ public class MainController implements Initializable, FrequencyListener, MidiLis } else if (SvgParser.isSvgFile(name)) { mimeType = "text/html"; } - launchCodeEditor(code, frameSourcePaths.get(currentFrameSource), mimeType, this::updateFileData); + launchCodeEditor(code, frameSourcePaths.get(currentFrameSource), mimeType, (data, fileName) -> updateFileData(data, fileName, true)); } private void updateTitle(String message, String projectName) { diff --git a/src/main/java/sh/ball/parser/lua/LuaSampleSource.java b/src/main/java/sh/ball/parser/lua/LuaSampleSource.java index c060d912..67ef8da4 100644 --- a/src/main/java/sh/ball/parser/lua/LuaSampleSource.java +++ b/src/main/java/sh/ball/parser/lua/LuaSampleSource.java @@ -32,6 +32,9 @@ public class LuaSampleSource implements FrameSource { @Override public Vector2 next() { LuaValue result = executor.execute(); + if (result.isnil()) { + return new Vector2(); + } return new Vector2(result.get(1).checkdouble(), result.get(2).checkdouble()); } diff --git a/src/main/java/sh/ball/parser/txt/FontStyle.java b/src/main/java/sh/ball/parser/txt/FontStyle.java index 1dd1de59..74283c75 100644 --- a/src/main/java/sh/ball/parser/txt/FontStyle.java +++ b/src/main/java/sh/ball/parser/txt/FontStyle.java @@ -1,6 +1,9 @@ package sh.ball.parser.txt; import java.awt.*; +import java.util.logging.Level; + +import static sh.ball.gui.Gui.logger; public enum FontStyle { PLAIN(Font.PLAIN), @@ -13,6 +16,24 @@ public enum FontStyle { this.style = style; } + public static FontStyle getStyle(int style) { + switch (style) { + case Font.PLAIN -> { + return PLAIN; + } + case Font.BOLD -> { + return BOLD; + } + case Font.ITALIC -> { + return ITALIC; + } + default -> { + logger.log(Level.WARNING, "Unknown font style: " + style); + return PLAIN; + } + } + } + @Override public String toString() { return name().substring(0, 1).toUpperCase() + name().substring(1).toLowerCase();