AlmasB / FXGL-MobileApp

An example game that uses FXGL and runs on a mobile device
MIT License
9 stars 2 forks source link

GluonSample #5

Open AlmasB opened 4 years ago

AlmasB commented 4 years ago
private Optional<Audio> audio = Optional.empty();

private Parent createContent() {
    URL URL_MUSIC = getClass().getResource("TODO");
    URL URL_SOUND = getClass().getResource("TODO");

    var btnLoadMusic = newButton("Load Music (mp3)", () -> audio = AudioService.create().flatMap(s -> s.loadMusic(URL_MUSIC)));
    var btnLoadSound = newButton("Load Sound (wav)", () -> audio = AudioService.create().flatMap(s -> s.loadSound(URL_SOUND)));

    var btnPlay = newButton("Play", () -> audio.ifPresent(a -> a.play()));
    var btnPause = newButton("Pause", () -> audio.ifPresent(a -> a.pause()));
    var btnStop = newButton("Stop", () -> audio.ifPresent(a -> a.stop()));
    var btnDispose = newButton("Dispose", () -> audio.ifPresent(a -> a.dispose()));

    var cbLooping = new CheckBox("isLooping");
    cbLooping.selectedProperty().addListener((o, oldValue, newValue) -> {
        audio.ifPresent(a -> a.setLooping(newValue));
    });

    var sliderVolume = new Slider(0.0, 1.0, 1.0);
    sliderVolume.valueProperty().addListener((o, oldValue, newValue) -> {
        audio.ifPresent(a -> a.setVolume(newValue.doubleValue()));
    });

    return new VBox(15,
            btnLoadSound,
            btnLoadMusic,
            cbLooping,
            new HBox(new Text("Volume: "), sliderVolume),
            btnPlay,
            btnPause,
            btnStop,
            btnDispose
    );
}

private Button newButton(String name, Runnable action) {
    var btn = new Button(name);
    btn.setPrefSize(200, 30);
    btn.setOnAction(e -> action.run());
    return btn;
}