Implement basic code editor

pull/82/head
James Ball 2022-06-06 21:35:31 +01:00 zatwierdzone przez James H Ball
rodzic c6c036e7a0
commit 2779a03eb2
4 zmienionych plików z 12533 dodań i 1 usunięć

Wyświetl plik

@ -13,7 +13,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
<javafx.version>17.0.0.1</javafx.version>
<javafx.version>17.0.1</javafx.version>
<appMainClass>sh.ball.gui.Launcher</appMainClass>
</properties>
@ -148,6 +148,11 @@
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>${javafx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.unbescape/unbescape -->
<dependency>
<groupId>org.unbescape</groupId>

Wyświetl plik

@ -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) {

Wyświetl plik

@ -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);
}
}