Remove coupling with FileParser in FrameProducer and add conversion betweem java.awt.Shape and Shape

pull/35/head
James Ball 2021-05-03 21:31:10 +01:00
rodzic 8136104d1b
commit 2e01ba0948
7 zmienionych plików z 82 dodań i 38 usunięć

Wyświetl plik

@ -11,6 +11,7 @@ module oscirender {
requires java.data.front; requires java.data.front;
requires org.jgrapht.core; requires org.jgrapht.core;
requires org.unbescape.html; requires org.unbescape.html;
requires java.desktop;
opens sh.ball.gui; opens sh.ball.gui;
} }

Wyświetl plik

@ -1,15 +1,8 @@
package sh.ball.audio; package sh.ball.audio;
import org.xml.sax.SAXException;
import sh.ball.FrameSet; import sh.ball.FrameSet;
import sh.ball.Renderer; import sh.ball.Renderer;
import java.io.IOException;
import sh.ball.parser.FileParser;
import javax.xml.parsers.ParserConfigurationException;
public class FrameProducer<T> implements Runnable { public class FrameProducer<T> implements Runnable {
private final Renderer<T> renderer; private final Renderer<T> renderer;
@ -17,9 +10,9 @@ public class FrameProducer<T> implements Runnable {
private boolean running; private boolean running;
public FrameProducer(Renderer<T> renderer, FileParser<FrameSet<T>> parser) throws IOException, SAXException, ParserConfigurationException { public FrameProducer(Renderer<T> renderer, FrameSet<T> frames) {
this.renderer = renderer; this.renderer = renderer;
this.frames = parser.parse(); this.frames = frames;
} }
@Override @Override

Wyświetl plik

@ -4,6 +4,7 @@ import sh.ball.MovableRenderer;
import sh.ball.audio.AudioPlayer; import sh.ball.audio.AudioPlayer;
import sh.ball.audio.FrameProducer; import sh.ball.audio.FrameProducer;
import java.awt.geom.RoundRectangle2D;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -29,6 +30,7 @@ import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import sh.ball.engine.Vector3; import sh.ball.engine.Vector3;
import sh.ball.shapes.ShapeFrameSet;
import sh.ball.parser.obj.ObjFrameSettings; import sh.ball.parser.obj.ObjFrameSettings;
import sh.ball.parser.obj.ObjParser; import sh.ball.parser.obj.ObjParser;
import sh.ball.parser.ParserFactory; import sh.ball.parser.ParserFactory;
@ -45,7 +47,7 @@ public class Controller implements Initializable {
private FrameProducer<List<Shape>> producer = new FrameProducer<>( private FrameProducer<List<Shape>> producer = new FrameProducer<>(
renderer, renderer,
new ObjParser(DEFAULT_OBJ) new ObjParser(DEFAULT_OBJ).parse()
); );
private Stage stage; private Stage stage;
@ -87,7 +89,7 @@ public class Controller implements Initializable {
@FXML @FXML
private TextField cameraZTextField; private TextField cameraZTextField;
public Controller() throws ParserConfigurationException, SAXException, IOException { public Controller() throws IOException {
} }
private Map<Slider, SliderUpdater<Double>> initializeSliderMap() { private Map<Slider, SliderUpdater<Double>> initializeSliderMap() {
@ -164,7 +166,7 @@ public class Controller implements Initializable {
String path = file.getAbsolutePath(); String path = file.getAbsolutePath();
producer = new FrameProducer<>( producer = new FrameProducer<>(
renderer, renderer,
ParserFactory.getParser(path) ParserFactory.getParser(path).parse()
); );
executor.submit(producer); executor.submit(producer);

Wyświetl plik

@ -25,7 +25,7 @@ import org.w3c.dom.Node;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import sh.ball.FrameSet; import sh.ball.FrameSet;
import sh.ball.parser.FileParser; import sh.ball.parser.FileParser;
import sh.ball.parser.ShapeFrameSet; import sh.ball.shapes.ShapeFrameSet;
import sh.ball.shapes.Shape; import sh.ball.shapes.Shape;
import sh.ball.shapes.Vector2; import sh.ball.shapes.Vector2;

Wyświetl plik

@ -12,7 +12,7 @@ import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import sh.ball.FrameSet; import sh.ball.FrameSet;
import sh.ball.parser.FileParser; import sh.ball.parser.FileParser;
import sh.ball.parser.ShapeFrameSet; import sh.ball.shapes.ShapeFrameSet;
import sh.ball.parser.svg.SvgParser; import sh.ball.parser.svg.SvgParser;
import sh.ball.shapes.Shape; import sh.ball.shapes.Shape;
import sh.ball.shapes.Vector2; import sh.ball.shapes.Vector2;

Wyświetl plik

@ -1,5 +1,7 @@
package sh.ball.shapes; package sh.ball.shapes;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -32,6 +34,52 @@ public abstract class Shape {
/* SHAPE HELPER FUNCTIONS */ /* SHAPE HELPER FUNCTIONS */
public static List<Shape> convert(java.awt.Shape shape) {
List<Shape> shapes = new ArrayList<>();
Vector2 moveToPoint = new Vector2();
Vector2 currentPoint = new Vector2();
float[] coords = new float[6];
PathIterator pathIterator = shape.getPathIterator(new AffineTransform());
while (!pathIterator.isDone()) {
switch (pathIterator.currentSegment(coords)) {
case PathIterator.SEG_MOVETO -> {
currentPoint = new Vector2(coords[0], coords[1]);
moveToPoint = currentPoint;
}
case PathIterator.SEG_LINETO -> {
shapes.add(new Line(
currentPoint,
new Vector2(coords[0], coords[1])
));
currentPoint = new Vector2(coords[0], coords[1]);
}
case PathIterator.SEG_QUADTO -> {
shapes.add(new QuadraticBezierCurve(
currentPoint,
new Vector2(coords[0], coords[1]),
new Vector2(coords[2], coords[3])
));
currentPoint = new Vector2(coords[2], coords[3]);
}
case PathIterator.SEG_CUBICTO -> {
shapes.add(new CubicBezierCurve(
currentPoint,
new Vector2(coords[0], coords[1]),
new Vector2(coords[2], coords[3]),
new Vector2(coords[4], coords[5])
));
currentPoint = new Vector2(coords[4], coords[5]);
}
case PathIterator.SEG_CLOSE ->
shapes.add(new Line(currentPoint, moveToPoint));
}
pathIterator.next();
}
return shapes;
}
// Normalises sh.ball.shapes between the coords -1 and 1 for proper scaling on an oscilloscope. May not // Normalises sh.ball.shapes between the coords -1 and 1 for proper scaling on an oscilloscope. May not
// work perfectly with curves that heavily deviate from their start and end points. // work perfectly with curves that heavily deviate from their start and end points.
public static List<Shape> normalize(List<Shape> shapes) { public static List<Shape> normalize(List<Shape> shapes) {

Wyświetl plik

@ -1,4 +1,4 @@
package sh.ball.parser; package sh.ball.shapes;
import sh.ball.FrameSet; import sh.ball.FrameSet;
import sh.ball.shapes.Shape; import sh.ball.shapes.Shape;