diff --git a/pom.xml b/pom.xml index 39ae278..14891b9 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ UTF-8 17 - 17.0.0.1 + 17.0.1 sh.ball.gui.Launcher @@ -148,6 +148,11 @@ javafx-controls ${javafx.version} + + org.openjfx + javafx-web + ${javafx.version} + org.unbescape diff --git a/src/main/java/sh/ball/gui/Gui.java b/src/main/java/sh/ball/gui/Gui.java index a808b17..7967357 100644 --- a/src/main/java/sh/ball/gui/Gui.java +++ b/src/main/java/sh/ball/gui/Gui.java @@ -5,16 +5,21 @@ import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseEvent; +import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; import javafx.stage.Stage; import sh.ball.audio.ShapeAudioPlayer; import sh.ball.audio.engine.AudioDevice; import sh.ball.audio.engine.ConglomerateAudioEngine; import sh.ball.audio.midi.MidiCommunicator; import sh.ball.engine.Vector3; +import sh.ball.gui.components.CodeEditor; import sh.ball.gui.controller.MainController; import sh.ball.shapes.Vector2; @@ -97,6 +102,19 @@ public class Gui extends Application { Platform.exit(); System.exit(0); }); + + CodeEditor editor = new CodeEditor("theta = theta + math.sqrt(step) / 1000000000\n" + + "return {0.3 * math.tan(theta * step), 0.3 * math.tan(theta * step + math.pi / 2), theta}"); + + // display the scene. + Scene editorScene = new Scene(editor); + Stage editorStage = new Stage(); + + editor.prefHeightProperty().bind(stage.heightProperty()); + editor.prefWidthProperty().bind(stage.widthProperty()); + + editorStage.setScene(editorScene); + editorStage.show(); } public static void main(String[] args) { diff --git a/src/main/java/sh/ball/gui/components/CodeEditor.java b/src/main/java/sh/ball/gui/components/CodeEditor.java new file mode 100644 index 0000000..0fa15f3 --- /dev/null +++ b/src/main/java/sh/ball/gui/components/CodeEditor.java @@ -0,0 +1,55 @@ +// FROM https://gist.github.com/jewelsea/1463485 BY USER https://gist.github.com/jewelsea + +package sh.ball.gui.components; + +import javafx.scene.layout.StackPane; +import javafx.scene.web.WebView; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; + +/** + * A syntax highlighting code editor for JavaFX created by wrapping a + * CodeMirror code editor in a WebView. + * + * See http://codemirror.net for more information on using the codemirror editor. + */ +public class CodeEditor extends StackPane { + /** a webview used to encapsulate the CodeMirror JavaScript. */ + final WebView webview = new WebView(); + + /** a snapshot of the code to be edited kept for easy initilization and reversion of editable code. */ + private String editingCode; + + /** applies the editing template to the editing code to create the html+javascript source for a code editor. */ + private String applyEditingTemplate() throws URISyntaxException, IOException { + String template = Files.readString(Path.of(getClass().getResource("/html/code_editor.html").toURI())); + return template.replace("${code}", editingCode); + } + + /** sets the current code in the editor and creates an editing snapshot of the code which can be reverted to. */ + public void setCode(String newCode) throws URISyntaxException, IOException { + this.editingCode = newCode; + webview.getEngine().loadContent(applyEditingTemplate()); + } + + /** returns the current code in the editor and updates an editing snapshot of the code which can be reverted to. */ + public String getCodeAndSnapshot() { + this.editingCode = (String) webview.getEngine().executeScript("editor.getValue();"); + return editingCode; + } + + /** + * Create a new code editor. + * @param editingCode the initial code to be edited in the code editor. + */ + public CodeEditor(String editingCode) throws URISyntaxException, IOException { + this.editingCode = editingCode; + + webview.getEngine().loadContent(applyEditingTemplate()); + + this.getChildren().add(webview); + } +} \ No newline at end of file diff --git a/src/main/resources/html/code_editor.html b/src/main/resources/html/code_editor.html new file mode 100644 index 0000000..eacafbb --- /dev/null +++ b/src/main/resources/html/code_editor.html @@ -0,0 +1,12454 @@ + + + + + + + + + + + + + + + + + lua code editor + + + + + + \ No newline at end of file