2020-12-04 20:30:46 +00:00
|
|
|
package sh.ball.gui;
|
2020-11-22 14:39:38 +00:00
|
|
|
|
2021-05-08 21:49:43 +00:00
|
|
|
import javafx.scene.control.*;
|
2021-05-15 17:35:53 +00:00
|
|
|
import sh.ball.audio.Renderer;
|
2021-05-15 17:53:46 +00:00
|
|
|
import sh.ball.audio.effect.Effect;
|
|
|
|
import sh.ball.audio.effect.EffectType;
|
|
|
|
import sh.ball.audio.effect.RotateEffect;
|
|
|
|
import sh.ball.audio.effect.ScaleEffect;
|
|
|
|
import sh.ball.audio.effect.EffectFactory;
|
2020-12-04 20:30:46 +00:00
|
|
|
import sh.ball.audio.FrameProducer;
|
2021-04-16 23:03:43 +00:00
|
|
|
|
2020-11-22 21:07:48 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2021-05-02 21:38:11 +00:00
|
|
|
import java.io.InputStream;
|
2020-11-22 18:03:20 +00:00
|
|
|
import java.net.URL;
|
2020-11-22 21:07:48 +00:00
|
|
|
import java.util.List;
|
2020-11-23 22:56:50 +00:00
|
|
|
import java.util.Map;
|
2020-11-22 18:03:20 +00:00
|
|
|
import java.util.ResourceBundle;
|
2021-04-16 23:03:43 +00:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
2020-11-24 20:49:30 +00:00
|
|
|
import javafx.beans.InvalidationListener;
|
2020-11-22 18:03:20 +00:00
|
|
|
import javafx.fxml.FXML;
|
|
|
|
import javafx.fxml.Initializable;
|
2020-11-22 21:07:48 +00:00
|
|
|
import javafx.stage.FileChooser;
|
|
|
|
import javafx.stage.Stage;
|
2021-04-16 23:03:43 +00:00
|
|
|
|
2020-11-22 21:07:48 +00:00
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
2021-04-16 23:03:43 +00:00
|
|
|
|
2020-11-22 21:07:48 +00:00
|
|
|
import org.xml.sax.SAXException;
|
2021-05-15 17:53:46 +00:00
|
|
|
import sh.ball.audio.effect.TranslateEffect;
|
2021-04-17 11:14:49 +00:00
|
|
|
import sh.ball.engine.Vector3;
|
|
|
|
import sh.ball.parser.obj.ObjFrameSettings;
|
2021-04-16 23:03:43 +00:00
|
|
|
import sh.ball.parser.obj.ObjParser;
|
|
|
|
import sh.ball.parser.ParserFactory;
|
2020-12-04 20:30:46 +00:00
|
|
|
import sh.ball.shapes.Shape;
|
|
|
|
import sh.ball.shapes.Vector2;
|
2020-11-22 18:03:20 +00:00
|
|
|
|
|
|
|
public class Controller implements Initializable {
|
|
|
|
|
2021-05-15 17:35:53 +00:00
|
|
|
private static final int SAMPLE_RATE = 192000;
|
2021-05-02 21:38:11 +00:00
|
|
|
private static final InputStream DEFAULT_OBJ = Controller.class.getResourceAsStream("/models/cube.obj");
|
2021-05-17 18:54:27 +00:00
|
|
|
private static final double DEFAULT_ROTATE_SPEED = 0.1;
|
2020-11-22 21:07:48 +00:00
|
|
|
|
|
|
|
private final FileChooser fileChooser = new FileChooser();
|
2021-05-15 17:35:53 +00:00
|
|
|
private final Renderer<List<Shape>> renderer;
|
2021-04-16 23:03:43 +00:00
|
|
|
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
|
|
|
2021-05-15 17:35:53 +00:00
|
|
|
private final RotateEffect rotateEffect = new RotateEffect(SAMPLE_RATE);
|
|
|
|
private final TranslateEffect translateEffect = new TranslateEffect(SAMPLE_RATE);
|
|
|
|
private final ScaleEffect scaleEffect = new ScaleEffect();
|
|
|
|
|
|
|
|
private FrameProducer<List<Shape>> producer;
|
2020-11-23 22:56:50 +00:00
|
|
|
|
2020-11-22 21:07:48 +00:00
|
|
|
private Stage stage;
|
|
|
|
|
|
|
|
@FXML
|
2020-11-23 22:56:50 +00:00
|
|
|
private Button chooseFileButton;
|
|
|
|
@FXML
|
2020-11-25 21:28:37 +00:00
|
|
|
private Label fileLabel;
|
2020-11-23 22:56:50 +00:00
|
|
|
@FXML
|
2020-11-25 21:28:37 +00:00
|
|
|
private TextField translationXTextField;
|
2020-11-23 22:56:50 +00:00
|
|
|
@FXML
|
2020-11-25 21:28:37 +00:00
|
|
|
private TextField translationYTextField;
|
2020-11-23 22:56:50 +00:00
|
|
|
@FXML
|
2020-11-25 21:28:37 +00:00
|
|
|
private Slider weightSlider;
|
2020-11-24 20:49:30 +00:00
|
|
|
@FXML
|
2020-11-25 21:28:37 +00:00
|
|
|
private Label weightLabel;
|
2020-11-23 22:56:50 +00:00
|
|
|
@FXML
|
2020-11-25 21:28:37 +00:00
|
|
|
private Slider rotateSpeedSlider;
|
2020-11-23 22:56:50 +00:00
|
|
|
@FXML
|
2020-11-25 21:28:37 +00:00
|
|
|
private Label rotateSpeedLabel;
|
2020-11-22 18:03:20 +00:00
|
|
|
@FXML
|
2020-11-25 21:28:37 +00:00
|
|
|
private Slider translationSpeedSlider;
|
2020-11-22 18:03:20 +00:00
|
|
|
@FXML
|
2020-11-25 21:28:37 +00:00
|
|
|
private Label translationSpeedLabel;
|
2020-11-23 22:56:50 +00:00
|
|
|
@FXML
|
2020-11-25 21:28:37 +00:00
|
|
|
private Slider scaleSlider;
|
2020-11-23 22:56:50 +00:00
|
|
|
@FXML
|
2020-11-25 21:28:37 +00:00
|
|
|
private Label scaleLabel;
|
2020-11-23 22:56:50 +00:00
|
|
|
@FXML
|
2020-11-25 21:28:37 +00:00
|
|
|
private TitledPane objTitledPane;
|
2020-11-25 21:03:06 +00:00
|
|
|
@FXML
|
2020-11-23 22:56:50 +00:00
|
|
|
private Slider focalLengthSlider;
|
|
|
|
@FXML
|
|
|
|
private Label focalLengthLabel;
|
2020-11-25 21:28:37 +00:00
|
|
|
@FXML
|
|
|
|
private TextField cameraXTextField;
|
|
|
|
@FXML
|
|
|
|
private TextField cameraYTextField;
|
|
|
|
@FXML
|
|
|
|
private TextField cameraZTextField;
|
2021-05-08 21:49:43 +00:00
|
|
|
@FXML
|
2021-05-17 18:54:27 +00:00
|
|
|
private Slider objectRotateSpeedSlider;
|
|
|
|
@FXML
|
|
|
|
private Label objectRotateSpeedLabel;
|
|
|
|
@FXML
|
|
|
|
private TextField rotateXTextField;
|
|
|
|
@FXML
|
|
|
|
private TextField rotateYTextField;
|
|
|
|
@FXML
|
|
|
|
private TextField rotateZTextField;
|
|
|
|
@FXML
|
2021-05-08 21:49:43 +00:00
|
|
|
private CheckBox vectorCancellingCheckBox;
|
2021-05-09 19:00:46 +00:00
|
|
|
@FXML
|
|
|
|
private Slider vectorCancellingSlider;
|
2021-05-09 19:58:40 +00:00
|
|
|
@FXML
|
|
|
|
private CheckBox bitCrushCheckBox;
|
|
|
|
@FXML
|
|
|
|
private Slider bitCrushSlider;
|
2020-11-22 18:03:20 +00:00
|
|
|
|
2021-05-15 17:35:53 +00:00
|
|
|
public Controller(Renderer<List<Shape>> renderer) throws IOException {
|
|
|
|
this.renderer = renderer;
|
|
|
|
this.producer = new FrameProducer<>(
|
|
|
|
renderer,
|
|
|
|
new ObjParser(DEFAULT_OBJ).parse()
|
|
|
|
);
|
2021-04-16 23:03:43 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 22:56:50 +00:00
|
|
|
private Map<Slider, SliderUpdater<Double>> initializeSliderMap() {
|
|
|
|
return Map.of(
|
2021-04-16 23:03:43 +00:00
|
|
|
weightSlider,
|
|
|
|
new SliderUpdater<>(weightLabel::setText, renderer::setQuality),
|
|
|
|
rotateSpeedSlider,
|
2021-05-15 17:35:53 +00:00
|
|
|
new SliderUpdater<>(rotateSpeedLabel::setText, rotateEffect::setSpeed),
|
2021-04-16 23:03:43 +00:00
|
|
|
translationSpeedSlider,
|
2021-05-15 17:35:53 +00:00
|
|
|
new SliderUpdater<>(translationSpeedLabel::setText, translateEffect::setSpeed),
|
2021-04-16 23:03:43 +00:00
|
|
|
scaleSlider,
|
2021-05-15 17:35:53 +00:00
|
|
|
new SliderUpdater<>(scaleLabel::setText, scaleEffect::setScale),
|
2021-04-17 11:14:49 +00:00
|
|
|
focalLengthSlider,
|
2021-05-17 18:54:27 +00:00
|
|
|
new SliderUpdater<>(focalLengthLabel::setText, this::setFocalLength),
|
|
|
|
objectRotateSpeedSlider,
|
|
|
|
new SliderUpdater<>(objectRotateSpeedLabel::setText, this::setObjectRotateSpeed)
|
2020-11-23 22:56:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-09 19:00:46 +00:00
|
|
|
private Map<EffectType, Slider> effectTypes;
|
|
|
|
|
|
|
|
private void initializeEffectTypes() {
|
|
|
|
effectTypes = Map.of(
|
|
|
|
EffectType.VECTOR_CANCELLING,
|
2021-05-09 19:58:40 +00:00
|
|
|
vectorCancellingSlider,
|
|
|
|
EffectType.BIT_CRUSH,
|
|
|
|
bitCrushSlider
|
2021-05-09 19:00:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-17 18:54:27 +00:00
|
|
|
// TODO: Refactor and clean up duplication
|
2020-11-22 18:03:20 +00:00
|
|
|
@Override
|
|
|
|
public void initialize(URL url, ResourceBundle resourceBundle) {
|
2020-11-23 22:56:50 +00:00
|
|
|
Map<Slider, SliderUpdater<Double>> sliders = initializeSliderMap();
|
2021-05-09 19:00:46 +00:00
|
|
|
initializeEffectTypes();
|
2020-11-23 22:56:50 +00:00
|
|
|
|
|
|
|
for (Slider slider : sliders.keySet()) {
|
|
|
|
slider.valueProperty().addListener((source, oldValue, newValue) ->
|
2021-04-16 23:03:43 +00:00
|
|
|
sliders.get(slider).update(slider.getValue())
|
2020-11-23 22:56:50 +00:00
|
|
|
);
|
|
|
|
}
|
2020-11-22 21:07:48 +00:00
|
|
|
|
2020-11-24 20:49:30 +00:00
|
|
|
InvalidationListener translationUpdate = observable ->
|
2021-05-15 17:35:53 +00:00
|
|
|
translateEffect.setTranslation(new Vector2(
|
2021-04-16 23:03:43 +00:00
|
|
|
tryParse(translationXTextField.getText()),
|
|
|
|
tryParse(translationYTextField.getText())
|
|
|
|
));
|
2020-11-24 20:49:30 +00:00
|
|
|
|
|
|
|
translationXTextField.textProperty().addListener(translationUpdate);
|
|
|
|
translationYTextField.textProperty().addListener(translationUpdate);
|
|
|
|
|
2021-04-17 11:14:49 +00:00
|
|
|
InvalidationListener cameraPosUpdate = observable ->
|
|
|
|
producer.setFrameSettings(new ObjFrameSettings(new Vector3(
|
|
|
|
tryParse(cameraXTextField.getText()),
|
|
|
|
tryParse(cameraYTextField.getText()),
|
|
|
|
tryParse(cameraZTextField.getText())
|
2021-05-09 19:58:40 +00:00
|
|
|
)));
|
|
|
|
|
2021-04-17 11:14:49 +00:00
|
|
|
cameraXTextField.textProperty().addListener(cameraPosUpdate);
|
|
|
|
cameraYTextField.textProperty().addListener(cameraPosUpdate);
|
|
|
|
cameraZTextField.textProperty().addListener(cameraPosUpdate);
|
2020-11-25 21:28:37 +00:00
|
|
|
|
2021-05-17 18:54:27 +00:00
|
|
|
InvalidationListener rotateUpdate = observable ->
|
|
|
|
producer.setFrameSettings(new ObjFrameSettings(new Vector3(
|
|
|
|
tryParse(rotateXTextField.getText()),
|
|
|
|
tryParse(rotateYTextField.getText()),
|
|
|
|
tryParse(rotateZTextField.getText())
|
|
|
|
), null));
|
|
|
|
|
|
|
|
rotateXTextField.textProperty().addListener(rotateUpdate);
|
|
|
|
rotateYTextField.textProperty().addListener(rotateUpdate);
|
|
|
|
rotateZTextField.textProperty().addListener(rotateUpdate);
|
|
|
|
|
2021-05-09 19:58:40 +00:00
|
|
|
InvalidationListener vectorCancellingListener = e ->
|
|
|
|
updateEffect(EffectType.VECTOR_CANCELLING, vectorCancellingCheckBox.isSelected(),
|
2021-05-15 17:53:46 +00:00
|
|
|
EffectFactory.vectorCancelling((int) vectorCancellingSlider.getValue()));
|
2021-05-09 19:58:40 +00:00
|
|
|
InvalidationListener bitCrushListener = e ->
|
|
|
|
updateEffect(EffectType.BIT_CRUSH, bitCrushCheckBox.isSelected(),
|
2021-05-15 17:53:46 +00:00
|
|
|
EffectFactory.bitCrush(bitCrushSlider.getValue()));
|
2021-05-09 19:58:40 +00:00
|
|
|
|
|
|
|
vectorCancellingSlider.valueProperty().addListener(vectorCancellingListener);
|
|
|
|
vectorCancellingCheckBox.selectedProperty().addListener(vectorCancellingListener);
|
|
|
|
|
|
|
|
bitCrushSlider.valueProperty().addListener(bitCrushListener);
|
|
|
|
bitCrushCheckBox.selectedProperty().addListener(bitCrushListener);
|
2021-05-08 21:49:43 +00:00
|
|
|
|
2020-11-23 22:56:50 +00:00
|
|
|
chooseFileButton.setOnAction(e -> {
|
2021-04-17 11:14:49 +00:00
|
|
|
File file = fileChooser.showOpenDialog(stage);
|
|
|
|
if (file != null) {
|
|
|
|
chooseFile(file);
|
2020-11-24 20:49:30 +00:00
|
|
|
}
|
2020-11-22 21:07:48 +00:00
|
|
|
});
|
|
|
|
|
2021-05-17 18:54:27 +00:00
|
|
|
setObjectRotateSpeed(DEFAULT_ROTATE_SPEED);
|
|
|
|
|
2021-05-15 17:35:53 +00:00
|
|
|
renderer.addEffect(EffectType.SCALE, scaleEffect);
|
|
|
|
renderer.addEffect(EffectType.ROTATE, rotateEffect);
|
|
|
|
renderer.addEffect(EffectType.TRANSLATE, translateEffect);
|
|
|
|
|
2021-04-16 23:03:43 +00:00
|
|
|
executor.submit(producer);
|
2021-04-16 17:56:17 +00:00
|
|
|
new Thread(renderer).start();
|
2020-11-22 21:07:48 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 11:14:49 +00:00
|
|
|
private void setFocalLength(double focalLength) {
|
2021-05-09 19:58:40 +00:00
|
|
|
Vector3 pos = (Vector3) producer.setFrameSettings(new ObjFrameSettings(focalLength));
|
|
|
|
cameraXTextField.setText(String.valueOf(pos.getX()));
|
|
|
|
cameraYTextField.setText(String.valueOf(pos.getY()));
|
|
|
|
cameraZTextField.setText(String.valueOf(pos.getZ()));
|
2021-04-17 11:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 18:54:27 +00:00
|
|
|
private void setObjectRotateSpeed(double rotateSpeed) {
|
|
|
|
producer.setFrameSettings(new ObjFrameSettings(null, (Math.exp(3 * rotateSpeed) - 1) / 50));
|
|
|
|
}
|
|
|
|
|
2020-11-24 20:49:30 +00:00
|
|
|
private double tryParse(String value) {
|
|
|
|
try {
|
|
|
|
return Double.parseDouble(value);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 21:49:43 +00:00
|
|
|
private void updateEffect(EffectType type, boolean checked, Effect effect) {
|
|
|
|
if (checked) {
|
|
|
|
renderer.addEffect(type, effect);
|
2021-05-09 19:00:46 +00:00
|
|
|
effectTypes.get(type).setDisable(false);
|
2021-05-08 21:49:43 +00:00
|
|
|
} else {
|
|
|
|
renderer.removeEffect(type);
|
2021-05-09 19:00:46 +00:00
|
|
|
effectTypes.get(type).setDisable(true);
|
2021-05-08 21:49:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 21:28:37 +00:00
|
|
|
private void chooseFile(File file) {
|
2020-11-25 21:03:06 +00:00
|
|
|
try {
|
2021-04-16 23:03:43 +00:00
|
|
|
producer.stop();
|
2020-11-25 21:28:37 +00:00
|
|
|
String path = file.getAbsolutePath();
|
2021-04-16 23:03:43 +00:00
|
|
|
producer = new FrameProducer<>(
|
|
|
|
renderer,
|
2021-05-03 20:31:10 +00:00
|
|
|
ParserFactory.getParser(path).parse()
|
2021-04-16 23:03:43 +00:00
|
|
|
);
|
|
|
|
executor.submit(producer);
|
|
|
|
|
2020-11-25 21:28:37 +00:00
|
|
|
if (file.exists() && !file.isDirectory()) {
|
|
|
|
fileLabel.setText(path);
|
|
|
|
objTitledPane.setDisable(!ObjParser.isObjFile(path));
|
|
|
|
} else {
|
|
|
|
objTitledPane.setDisable(true);
|
|
|
|
}
|
2020-11-25 21:03:06 +00:00
|
|
|
} catch (IOException | ParserConfigurationException | SAXException ioException) {
|
|
|
|
ioException.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 21:07:48 +00:00
|
|
|
public void setStage(Stage stage) {
|
|
|
|
this.stage = stage;
|
2020-11-22 18:03:20 +00:00
|
|
|
}
|
2020-11-22 14:39:38 +00:00
|
|
|
}
|