Update GUI and refactor to use SliderUpdater class

pull/35/head
James Ball 2020-11-23 22:56:50 +00:00
rodzic 976b67b889
commit 95bb73debd
5 zmienionych plików z 123 dodań i 45 usunięć

Wyświetl plik

@ -40,7 +40,7 @@ public class FrameProducer implements Runnable {
}
}
public void setFocalLength(float focalLength) {
public void setFocalLength(Double focalLength) {
objParser.setFocalLength(focalLength);
}
@ -60,4 +60,13 @@ public class FrameProducer implements Runnable {
"Provided file extension in file " + filePath + " not supported.");
}
}
public void setRotateSpeed(Double rotateSpeed) {
}
public void setTranslationSpeed(Double translationSpeed) {
}
public void setScale(Double scale) {
}
}

Wyświetl plik

@ -120,7 +120,7 @@ public class Camera {
return lines;
}
public void setFocalLength(float focalLength) {
public void setFocalLength(double focalLength) {
this.focalLength = focalLength;
}
}

Wyświetl plik

@ -6,26 +6,21 @@ import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathEvaluationResult;
import org.xml.sax.SAXException;
import parser.FileParser;
import parser.ObjParser;
import parser.TextParser;
import parser.svg.SvgParser;
import shapes.Shape;
import shapes.Vector2;
public class Controller implements Initializable {
@ -35,29 +30,64 @@ public class Controller implements Initializable {
private final FileChooser fileChooser = new FileChooser();
private final BlockingQueue<List<Shape>> frameQueue = new ArrayBlockingQueue<>(BUFFER_SIZE);
private final AudioPlayer player = new AudioPlayer(SAMPLE_RATE, frameQueue);
/* Default parser. */
private final FrameProducer producer = new FrameProducer(frameQueue);
private Stage stage;
@FXML
private Button btnBrowse;
private Button chooseFileButton;
@FXML
private Text sliderOutput;
public Label fileLabel;
@FXML
private Slider slider;
public TextField translationXTextField;
@FXML
public TextField translationYTextField;
@FXML
public TextField weightTextField;
@FXML
public Slider rotateSpeedSlider;
@FXML
public Label rotateSpeedLabel;
@FXML
public Slider translationSpeedSlider;
@FXML
public Label translationSpeedLabel;
@FXML
public Slider scaleSlider;
@FXML
public Label scaleLabel;
@FXML
private Slider focalLengthSlider;
@FXML
private Label focalLengthLabel;
public Controller() throws IOException, ParserConfigurationException, SAXException {
}
private Map<Slider, SliderUpdater<Double>> initializeSliderMap() {
return Map.of(
rotateSpeedSlider,
new SliderUpdater<>(rotateSpeedLabel::setText, producer::setRotateSpeed),
translationSpeedSlider,
new SliderUpdater<>(translationSpeedLabel::setText, producer::setTranslationSpeed),
scaleSlider,
new SliderUpdater<>(scaleLabel::setText, producer::setScale),
focalLengthSlider,
new SliderUpdater<>(focalLengthLabel::setText, producer::setFocalLength)
);
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
slider.valueProperty().addListener((source, oldValue, newValue) -> {
sliderOutput.setText(String.valueOf(slider.getValue()));
producer.setFocalLength((float) slider.getValue());
});
Map<Slider, SliderUpdater<Double>> sliders = initializeSliderMap();
btnBrowse.setOnAction(e -> {
for (Slider slider : sliders.keySet()) {
slider.valueProperty().addListener((source, oldValue, newValue) ->
sliders.get(slider).update(slider.getValue())
);
}
chooseFileButton.setOnAction(e -> {
File file = fileChooser.showOpenDialog(stage);
try {
producer.setParser(file.getAbsolutePath());
@ -67,7 +97,7 @@ public class Controller implements Initializable {
});
new Thread(producer).start();
new Thread(new AudioPlayer(SAMPLE_RATE, frameQueue)).start();
new Thread(player).start();
}
public void setStage(Stage stage) {

Wyświetl plik

@ -1,16 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.Controller">
<children>
<AnchorPane prefHeight="400.0" prefWidth="400.0">
<AnchorPane prefHeight="279.0" prefWidth="400.0">
<children>
<Slider fx:id="slider" blockIncrement="2.0" layoutX="45.0" layoutY="238.0" max="2.0" min="1.0E-5" prefHeight="14.0" prefWidth="313.0" />
<Text fx:id="sliderOutput" layoutX="196.0" layoutY="219.0" strokeType="OUTSIDE" strokeWidth="0.0" text="0" />
<Button fx:id="btnBrowse" layoutX="23.0" layoutY="22.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="119.0" text="Browse" />
<Button fx:id="chooseFileButton" layoutX="8.0" layoutY="15.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="119.0" text="Choose File" />
<SplitPane dividerPositions="0.4723618090452261" layoutX="8.0" layoutY="56.0" orientation="VERTICAL" prefHeight="371.0" prefWidth="386.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="0.0" prefWidth="385.0">
<children>
<Slider fx:id="rotateSpeedSlider" layoutX="116.0" layoutY="79.0" max="20.0" prefHeight="14.0" prefWidth="198.0" />
<Label layoutX="37.0" layoutY="77.0" text="Rotate speed" />
<Label fx:id="rotateSpeedLabel" layoutX="316.0" layoutY="77.0" text="0" />
<Slider fx:id="translationSpeedSlider" layoutX="116.0" layoutY="105.0" max="20.0" prefHeight="14.0" prefWidth="198.0" />
<Label layoutX="14.0" layoutY="103.0" text="Translation speed" />
<Label fx:id="translationSpeedLabel" layoutX="316.0" layoutY="103.0" text="0" />
<Slider fx:id="scaleSlider" layoutX="116.0" layoutY="130.0" max="10.0" prefHeight="14.0" prefWidth="198.0" value="1.0" />
<Label layoutX="80.0" layoutY="128.0" text="Scale" />
<Label fx:id="scaleLabel" layoutX="316.0" layoutY="128.0" text="0" />
<TextField fx:id="weightTextField" layoutX="118.0" layoutY="45.0" prefHeight="26.0" prefWidth="70.0" />
<Label layoutX="70.0" layoutY="50.0" text="Weight" />
<TextField fx:id="translationXTextField" layoutX="136.0" layoutY="13.0" prefHeight="26.0" prefWidth="70.0" />
<Label layoutX="51.0" layoutY="18.0" text="Translation" />
<Label layoutX="120.0" layoutY="18.0" text="x :" />
<Label layoutX="217.0" layoutY="18.0" text="y :" />
<TextField fx:id="translationYTextField" layoutX="234.0" layoutY="13.0" prefHeight="26.0" prefWidth="70.0" />
</children></AnchorPane>
<TitledPane animated="false" collapsible="false" text="3D .obj file settings">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Slider fx:id="focalLengthSlider" blockIncrement="2.0" layoutX="116.0" layoutY="15.0" max="2.0" min="1.0E-5" prefHeight="14.0" prefWidth="198.0" value="1.0" />
<Label layoutX="43.0" layoutY="14.0" text="Focal length" />
<Label fx:id="focalLengthLabel" layoutX="316.0" layoutY="14.0" text="0" />
</children></AnchorPane>
</content>
</TitledPane>
</items>
</SplitPane>
<Label fx:id="fileLabel" layoutX="134.0" layoutY="20.0" />
</children>
</AnchorPane>
</children>

Wyświetl plik

@ -64,7 +64,7 @@ public class ObjParser extends FileParser {
// If camera position arguments haven't been specified, automatically work out the position of
// the camera based on the size of the object in the camera's view.
public void setFocalLength(float focalLength) {
public void setFocalLength(double focalLength) {
camera.setFocalLength(focalLength);
if (isDefaultPosition) {
camera.findZPos(object);