mirror of
https://github.com/aNNiMON/HotaruFX.git
synced 2024-09-19 14:14:21 +03:00
Move evaluating to separate class
This commit is contained in:
parent
3adc39f288
commit
1f5ded0c40
@ -0,0 +1,8 @@
|
||||
package com.annimon.hotarufx.exceptions;
|
||||
|
||||
public class RendererException extends RuntimeException {
|
||||
|
||||
public RendererException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
117
app/src/main/java/com/annimon/hotarufx/ui/Renderer.java
Normal file
117
app/src/main/java/com/annimon/hotarufx/ui/Renderer.java
Normal file
@ -0,0 +1,117 @@
|
||||
package com.annimon.hotarufx.ui;
|
||||
|
||||
import com.annimon.hotarufx.bundles.Bundle;
|
||||
import com.annimon.hotarufx.bundles.BundleLoader;
|
||||
import com.annimon.hotarufx.exceptions.RendererException;
|
||||
import com.annimon.hotarufx.lexer.HotaruLexer;
|
||||
import com.annimon.hotarufx.lib.Context;
|
||||
import com.annimon.hotarufx.parser.HotaruParser;
|
||||
import com.annimon.hotarufx.parser.visitors.InterpreterVisitor;
|
||||
import com.annimon.hotarufx.visual.Composition;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import javafx.stage.Modality;
|
||||
import javafx.stage.Stage;
|
||||
import lombok.val;
|
||||
|
||||
public class Renderer {
|
||||
|
||||
public static Renderer init() {
|
||||
return new Renderer();
|
||||
}
|
||||
|
||||
private Renderer() {
|
||||
}
|
||||
|
||||
public WithInput input(String input) {
|
||||
return new WithInput(input);
|
||||
}
|
||||
|
||||
|
||||
public class WithInput {
|
||||
|
||||
private final String input;
|
||||
|
||||
private WithInput(String input) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
public WithContext context(Context context) {
|
||||
return new WithContext(this, context);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class WithContext {
|
||||
|
||||
private final WithInput source;
|
||||
private final Context context;
|
||||
|
||||
private WithContext(WithInput source, Context context) {
|
||||
this.source = source;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public Evaluated evaluateWithRuntimeBundle() {
|
||||
return evaluateWithBundles(BundleLoader.runtimeBundles());
|
||||
}
|
||||
|
||||
public Evaluated evaluateWithBundles(List<Class<? extends Bundle>> bundles) {
|
||||
BundleLoader.load(context, bundles);
|
||||
return evaluate();
|
||||
}
|
||||
|
||||
private Evaluated evaluate() {
|
||||
val parser = new HotaruParser(HotaruLexer.tokenize(source.input));
|
||||
val program = parser.parse();
|
||||
if (parser.getParseErrors().hasErrors()) {
|
||||
throw new RendererException(parser.getParseErrors().toString());
|
||||
}
|
||||
program.accept(new InterpreterVisitor(), context);
|
||||
return new Evaluated(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class Evaluated {
|
||||
|
||||
private final WithContext source;
|
||||
|
||||
private Evaluated(WithContext source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public WithStage prepareStage(Stage primaryStage) {
|
||||
checkCompositionExists();
|
||||
val stage = new Stage();
|
||||
stage.initOwner(primaryStage);
|
||||
stage.initModality(Modality.WINDOW_MODAL);
|
||||
val composition = source.context.composition();
|
||||
stage.setScene(composition.produceAnimationScene());
|
||||
return new WithStage(composition, stage);
|
||||
}
|
||||
|
||||
private void checkCompositionExists() {
|
||||
if (source.context.composition() == null) {
|
||||
throw new RendererException("There is no composition.\n" +
|
||||
"Make sure you call composition method.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class WithStage {
|
||||
|
||||
private final Composition composition;
|
||||
private final Stage stage;
|
||||
|
||||
private WithStage(Composition composition, Stage stage) {
|
||||
this.composition = composition;
|
||||
this.stage = stage;
|
||||
}
|
||||
|
||||
public WithStage peek(BiConsumer<Stage, Composition> consumer) {
|
||||
consumer.accept(stage, composition);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,16 +1,14 @@
|
||||
package com.annimon.hotarufx.ui.controller;
|
||||
|
||||
import com.annimon.hotarufx.Main;
|
||||
import com.annimon.hotarufx.bundles.BundleLoader;
|
||||
import com.annimon.hotarufx.exceptions.Exceptions;
|
||||
import com.annimon.hotarufx.exceptions.RendererException;
|
||||
import com.annimon.hotarufx.io.DocumentListener;
|
||||
import com.annimon.hotarufx.io.DocumentManager;
|
||||
import com.annimon.hotarufx.io.FileManager;
|
||||
import com.annimon.hotarufx.io.IOStream;
|
||||
import com.annimon.hotarufx.lexer.HotaruLexer;
|
||||
import com.annimon.hotarufx.lib.Context;
|
||||
import com.annimon.hotarufx.parser.HotaruParser;
|
||||
import com.annimon.hotarufx.parser.visitors.InterpreterVisitor;
|
||||
import com.annimon.hotarufx.ui.Renderer;
|
||||
import com.annimon.hotarufx.ui.SyntaxHighlighter;
|
||||
import com.annimon.hotarufx.ui.control.LibraryItem;
|
||||
import java.io.IOException;
|
||||
@ -129,35 +127,13 @@ public class EditorController implements Initializable, DocumentListener {
|
||||
if (input.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
val context = new Context();
|
||||
BundleLoader.load(context, BundleLoader.runtimeBundles());
|
||||
|
||||
val parser = new HotaruParser(HotaruLexer.tokenize(input));
|
||||
val program = parser.parse();
|
||||
if (parser.getParseErrors().hasErrors()) {
|
||||
log.setText(parser.getParseErrors().toString());
|
||||
logPane.setExpanded(true);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
program.accept(new InterpreterVisitor(), context);
|
||||
} catch (RuntimeException e) {
|
||||
logError(Exceptions.stackTraceToString(e));
|
||||
logPane.setExpanded(true);
|
||||
return;
|
||||
}
|
||||
|
||||
val stage = new Stage();
|
||||
val composition = context.composition();
|
||||
if (composition == null) {
|
||||
logError("There is no composition.\nMake sure you call composition method.");
|
||||
logPane.setExpanded(true);
|
||||
return;
|
||||
}
|
||||
stage.initOwner(primaryStage);
|
||||
stage.initModality(Modality.WINDOW_MODAL);
|
||||
stage.setScene(composition.produceAnimationScene());
|
||||
Renderer.init()
|
||||
.input(input)
|
||||
.context(new Context())
|
||||
.evaluateWithRuntimeBundle()
|
||||
.prepareStage(primaryStage)
|
||||
.peek((stage, composition) -> {
|
||||
val timeline = composition.getTimeline();
|
||||
timeline.getFxTimeline().currentTimeProperty().addListener((o, oldValue, d) -> {
|
||||
val min = (int) d.toMinutes();
|
||||
@ -168,8 +144,17 @@ public class EditorController implements Initializable, DocumentListener {
|
||||
val allFrame = (int) (d.toMillis() * timeline.getFrameRate() / 1000d);
|
||||
stage.setTitle(String.format("%02d:%02d.%02d %d", min, sec, frame, allFrame));
|
||||
});
|
||||
|
||||
stage.setOnShown(e -> timeline.getFxTimeline().play());
|
||||
stage.show();
|
||||
});
|
||||
} catch (RendererException re) {
|
||||
logError(re.getMessage());
|
||||
logPane.setExpanded(true);
|
||||
} catch (RuntimeException e) {
|
||||
logError(Exceptions.stackTraceToString(e));
|
||||
logPane.setExpanded(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user