Add lua sliders that can be controlled when a lua file is opened and used in scripts

pull/82/head
James Ball 2022-06-09 23:23:26 +01:00 zatwierdzone przez James H Ball
rodzic 71a0980ce9
commit a4a6f4bb53
6 zmienionych plików z 209 dodań i 7 usunięć

Wyświetl plik

@ -0,0 +1,113 @@
package sh.ball.gui.controller;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Slider;
import javafx.scene.shape.SVGPath;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
public class LuaController implements Initializable, SubController {
private MainController mainController;
@FXML
private Slider luaASlider;
@FXML
private SVGPath luaAMidi;
@FXML
private CheckBox luaAMic;
@FXML
private Slider luaBSlider;
@FXML
private SVGPath luaBMidi;
@FXML
private CheckBox luaBMic;
@FXML
private Slider luaCSlider;
@FXML
private SVGPath luaCMidi;
@FXML
private CheckBox luaCMic;
@FXML
private Slider luaDSlider;
@FXML
private SVGPath luaDMidi;
@FXML
private CheckBox luaDMic;
@FXML
private Slider luaESlider;
@FXML
private SVGPath luaEMidi;
@FXML
private CheckBox luaEMic;
@FXML
private Button resetStepCounterButton;
public void setMainController(MainController mainController) {
this.mainController = mainController;
}
@Override
public Map<SVGPath, Slider> getMidiButtonMap() {
return Map.of(
luaAMidi, luaASlider,
luaBMidi, luaBSlider,
luaCMidi, luaCSlider,
luaDMidi, luaDSlider,
luaEMidi, luaESlider
);
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
sliders().forEach(slider ->
slider.valueProperty().addListener(e -> updateLuaVariable(slider))
);
resetStepCounterButton.setOnAction(e -> mainController.resetLuaStep());
}
private void updateLuaVariable(Slider slider) {
mainController.setLuaVariable("slider_" + slider.getId().toLowerCase().charAt(3), slider.getValue());
}
public void updateLuaVariables() {
sliders().forEach(this::updateLuaVariable);
}
@Override
public List<CheckBox> micCheckBoxes() {
return List.of(luaAMic, luaBMic, luaCMic, luaDMic, luaEMic);
}
@Override
public List<Slider> sliders() {
return List.of(luaASlider, luaBSlider, luaCSlider, luaDSlider, luaESlider);
}
@Override
public List<String> labels() {
return List.of("luaA", "luaB", "luaC", "luaD", "luaE");
}
@Override
public Element save(Document document) {
return document.createElement("null");
}
@Override
public void load(Element root) {
}
@Override
public void slidersUpdated() {}
}

Wyświetl plik

@ -53,13 +53,10 @@ import sh.ball.engine.ObjectServer;
import sh.ball.engine.ObjectSet;
import sh.ball.gui.Gui;
import sh.ball.oscilloscope.ByteWebSocketServer;
import sh.ball.parser.lua.LuaSampleSource;
import sh.ball.parser.lua.LuaParser;
import sh.ball.parser.obj.ObjFrameSettings;
import sh.ball.parser.obj.ObjParser;
import sh.ball.parser.ParserFactory;
import sh.ball.parser.svg.SvgParser;
import sh.ball.parser.txt.TextParser;
import sh.ball.shapes.Shape;
import sh.ball.shapes.Vector2;
@ -125,12 +122,16 @@ public class MainController implements Initializable, FrequencyListener, MidiLis
@FXML
private ObjController objController;
@FXML
private LuaController luaController;
@FXML
private ImageController imageController;
@FXML
private GeneralController generalController;
@FXML
private TitledPane objTitledPane;
@FXML
private TitledPane luaTitledPane;
@FXML
private MenuItem openProjectMenuItem;
@FXML
private MenuItem saveProjectMenuItem;
@ -208,7 +209,7 @@ public class MainController implements Initializable, FrequencyListener, MidiLis
}
private List<SubController> subControllers() {
return List.of(effectsController, objController, imageController, generalController);
return List.of(effectsController, objController, imageController, generalController, luaController);
}
private void updateSliderUnits(Slider slider) {
@ -337,6 +338,7 @@ public class MainController implements Initializable, FrequencyListener, MidiLis
objController.setAudioProducer(producer);
generalController.setMainController(this);
luaController.setMainController(this);
this.midiButtonMap = initializeMidiButtonMap();
@ -504,6 +506,7 @@ public class MainController implements Initializable, FrequencyListener, MidiLis
}
editor.setCallback(this::updateFileData);
luaController.updateLuaVariables();
objectServer = new ObjectServer(this::enableObjectServerRendering, this::disableObjectServerRendering);
new Thread(objectServer).start();
@ -514,6 +517,10 @@ public class MainController implements Initializable, FrequencyListener, MidiLis
new Thread(() -> sendAudioDataToWebSocket(webSocketServer)).start();
}
public void setLuaVariable(String variableName, Object value) {
luaParser.setVariable(variableName, value);
}
public void setSoftwareOscilloscopeAction(Runnable openBrowser) {
softwareOscilloscopeMenuItem.setOnAction((e) -> openBrowser.run());
}
@ -650,8 +657,23 @@ public class MainController implements Initializable, FrequencyListener, MidiLis
generalController.setFrameSourceName(frameSourcePaths.get(currentFrameSource));
generalController.updateFrameLabels();
// enable the .obj file settings iff the new frameSource is for a 3D object.
objTitledPane.setDisable(!ObjParser.isObjFile(frameSourcePaths.get(index)));
String fileName = frameSourcePaths.get(index);
Platform.runLater(() -> {
if (ObjParser.isObjFile(fileName)) {
objTitledPane.setVisible(true);
objTitledPane.setDisable(false);
luaTitledPane.setVisible(false);
} else if (LuaParser.isLuaFile(fileName)) {
objTitledPane.setVisible(false);
luaTitledPane.setVisible(true);
} else {
objTitledPane.setVisible(true);
objTitledPane.setDisable(true);
luaTitledPane.setVisible(false);
}
});
}
void updateFileData(byte[] file, String name) {
@ -1274,6 +1296,10 @@ public class MainController implements Initializable, FrequencyListener, MidiLis
stage.setTitle(title);
}
public void resetLuaStep() {
luaParser.resetStep();
}
private record PrintableSlider(Slider slider) {
@Override
public String toString() {

Wyświetl plik

@ -38,4 +38,12 @@ public class LuaParser extends FileParser<FrameSource<Vector2>> {
public static boolean isLuaFile(String path) {
return path.matches(".*\\.lua");
}
public void setVariable(String variableName, Object value) {
sampleSource.setVariable(variableName, value);
}
public void resetStep() {
sampleSource.resetStep();
}
}

Wyświetl plik

@ -7,6 +7,10 @@ import sh.ball.shapes.Vector2;
import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.SimpleBindings;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class LuaSampleSource implements FrameSource<Vector2> {
@ -17,6 +21,7 @@ public class LuaSampleSource implements FrameSource<Vector2> {
private String textScript;
private boolean active = true;
private long step = 1;
private Set<String> programDefinedVariables = new HashSet<>();
public void setScript(String textScript, CompiledScript script) {
if (lastWorkingScript == null) {
@ -50,7 +55,13 @@ public class LuaSampleSource implements FrameSource<Vector2> {
if (updatedFile) {
// reset variables
step = 1;
bindings = new SimpleBindings();
List<String> variablesToRemove = new ArrayList<>();
bindings.keySet().forEach(name -> {
if (!programDefinedVariables.contains(name)) {
variablesToRemove.add(name);
}
});
variablesToRemove.forEach(name -> bindings.remove(name));
}
return new Vector2(result.get(1).checkdouble(), result.get(2).checkdouble());
@ -81,4 +92,13 @@ public class LuaSampleSource implements FrameSource<Vector2> {
public Object getFrameSettings() {
return null;
}
public void setVariable(String variableName, Object value) {
programDefinedVariables.add(variableName);
bindings.put(variableName, value);
}
public void resetStep() {
step = 1;
}
}

Wyświetl plik

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.SVGPath?>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="313.0" prefWidth="454.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sh.ball.gui.controller.LuaController">
<Slider fx:id="luaASlider" blockIncrement="0.01" layoutX="106.0" layoutY="16.0" majorTickUnit="0.1" max="1.0" prefHeight="42.0" prefWidth="257.0" showTickLabels="true" showTickMarks="true" value="1.0" />
<Label layoutX="54.0" layoutY="16.0" text="Slider a" />
<SVGPath fx:id="luaAMidi" content="M20.15 8.26H22V15.74H20.15M13 8.26H18.43C19 8.26 19.3 8.74 19.3 9.3V14.81C19.3 15.5 19 15.74 18.38 15.74H13V11H14.87V13.91H17.5V9.95H13M10.32 8.26H12.14V15.74H10.32M2 8.26H8.55C9.1 8.26 9.41 8.74 9.41 9.3V15.74H7.59V10.15H6.5V15.74H4.87V10.15H3.83V15.74H2Z" fill="WHITE" layoutX="370.0" layoutY="13.0" pickOnBounds="true" />
<SVGPath fx:id="luaBMidi" content="M20.15 8.26H22V15.74H20.15M13 8.26H18.43C19 8.26 19.3 8.74 19.3 9.3V14.81C19.3 15.5 19 15.74 18.38 15.74H13V11H14.87V13.91H17.5V9.95H13M10.32 8.26H12.14V15.74H10.32M2 8.26H8.55C9.1 8.26 9.41 8.74 9.41 9.3V15.74H7.59V10.15H6.5V15.74H4.87V10.15H3.83V15.74H2Z" fill="WHITE" layoutX="370.0" layoutY="62.0" pickOnBounds="true" />
<CheckBox fx:id="luaBMic" layoutX="407.0" layoutY="65.0" mnemonicParsing="false" text="Mic" />
<Button fx:id="resetStepCounterButton" layoutX="175.0" layoutY="262.0" mnemonicParsing="false" text="Reset Step Counter" />
<SVGPath fx:id="luaCMidi" content="M20.15 8.26H22V15.74H20.15M13 8.26H18.43C19 8.26 19.3 8.74 19.3 9.3V14.81C19.3 15.5 19 15.74 18.38 15.74H13V11H14.87V13.91H17.5V9.95H13M10.32 8.26H12.14V15.74H10.32M2 8.26H8.55C9.1 8.26 9.41 8.74 9.41 9.3V15.74H7.59V10.15H6.5V15.74H4.87V10.15H3.83V15.74H2Z" fill="WHITE" layoutX="370.0" layoutY="111.0" pickOnBounds="true" />
<CheckBox fx:id="luaCMic" layoutX="407.0" layoutY="114.0" mnemonicParsing="false" text="Mic" />
<SVGPath fx:id="luaDMidi" content="M20.15 8.26H22V15.74H20.15M13 8.26H18.43C19 8.26 19.3 8.74 19.3 9.3V14.81C19.3 15.5 19 15.74 18.38 15.74H13V11H14.87V13.91H17.5V9.95H13M10.32 8.26H12.14V15.74H10.32M2 8.26H8.55C9.1 8.26 9.41 8.74 9.41 9.3V15.74H7.59V10.15H6.5V15.74H4.87V10.15H3.83V15.74H2Z" fill="WHITE" layoutX="370.0" layoutY="159.0" pickOnBounds="true" />
<CheckBox fx:id="luaDMic" layoutX="407.0" layoutY="162.0" mnemonicParsing="false" text="Mic" />
<SVGPath fx:id="luaEMidi" content="M20.15 8.26H22V15.74H20.15M13 8.26H18.43C19 8.26 19.3 8.74 19.3 9.3V14.81C19.3 15.5 19 15.74 18.38 15.74H13V11H14.87V13.91H17.5V9.95H13M10.32 8.26H12.14V15.74H10.32M2 8.26H8.55C9.1 8.26 9.41 8.74 9.41 9.3V15.74H7.59V10.15H6.5V15.74H4.87V10.15H3.83V15.74H2Z" fill="WHITE" layoutX="370.0" layoutY="206.0" pickOnBounds="true" />
<CheckBox fx:id="luaEMic" layoutX="407.0" layoutY="209.0" mnemonicParsing="false" text="Mic" />
<Label layoutX="54.0" layoutY="65.0" text="Slider b" />
<Label layoutX="54.0" layoutY="114.0" text="Slider c" />
<Label layoutX="53.0" layoutY="162.0" text="Slider d" />
<Label layoutX="54.0" layoutY="209.0" text="Slider e" />
<CheckBox fx:id="luaAMic" layoutX="407.0" layoutY="16.0" mnemonicParsing="false" text="Mic" />
<Slider fx:id="luaBSlider" blockIncrement="0.01" layoutX="106.0" layoutY="65.0" majorTickUnit="0.1" max="1.0" prefHeight="42.0" prefWidth="257.0" showTickLabels="true" showTickMarks="true" value="1.0" />
<Slider fx:id="luaCSlider" blockIncrement="0.01" layoutX="106.0" layoutY="114.0" majorTickUnit="0.1" max="1.0" prefHeight="42.0" prefWidth="257.0" showTickLabels="true" showTickMarks="true" value="1.0" />
<Slider fx:id="luaDSlider" blockIncrement="0.01" layoutX="106.0" layoutY="162.0" majorTickUnit="0.1" max="1.0" prefHeight="42.0" prefWidth="257.0" showTickLabels="true" showTickMarks="true" value="1.0" />
<Slider fx:id="luaESlider" blockIncrement="0.01" layoutX="106.0" layoutY="209.0" majorTickUnit="0.1" max="1.0" prefHeight="42.0" prefWidth="257.0" showTickLabels="true" showTickMarks="true" value="1.0" />
</AnchorPane>

Wyświetl plik

@ -210,4 +210,7 @@
<TitledPane fx:id="objTitledPane" animated="false" collapsible="false" layoutX="7.0" layoutY="411.0" prefHeight="334.0" prefWidth="464.0" text="3D .obj file settings">
<fx:include fx:id="obj" source="obj.fxml" />
</TitledPane>
<TitledPane fx:id="luaTitledPane" animated="false" collapsible="false" layoutX="7.0" layoutY="411.0" prefHeight="334.0" prefWidth="464.0" text=".lua file settings" visible="false">
<fx:include fx:id="lua" source="lua.fxml" />
</TitledPane>
</AnchorPane>