Merge remote-tracking branch 'origin/AudioDisplay' into OtherGames

This commit is contained in:
Ticho Hidding
2025-10-27 13:56:07 +01:00
11 changed files with 146 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package org.toop.app.view.displays;
import javafx.application.Platform;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import org.toop.framework.audio.AudioEventListener;
@@ -38,12 +39,36 @@ public class SongDisplay extends VBox {
// TODO ADD BETTER CSS FOR THE SKIPBUTTON WHERE ITS AT A NICER POSITION
Button skipButton = new Button(">>");
Button pauseButton = new Button("");
Button previousButton = new Button("<<");
skipButton.getStyleClass().setAll("skip-button");
pauseButton.getStyleClass().setAll("pause-button");
previousButton.getStyleClass().setAll("previous-button");
skipButton.setOnAction( event -> {
GlobalEventBus.post(new AudioEvents.SkipMusic());
});
getChildren().addAll(songTitle, progressBar, progressText, skipButton);
pauseButton.setOnAction(event -> {
GlobalEventBus.post(new AudioEvents.PauseMusic());
if (pauseButton.getText().equals("")) {
pauseButton.setText("");
}
else if (pauseButton.getText().equals("")) {
pauseButton.setText("");
}
});
previousButton.setOnAction( event -> {
GlobalEventBus.post(new AudioEvents.PreviousMusic());
});
HBox control = new HBox(10, previousButton, pauseButton, skipButton);
control.setAlignment(Pos.CENTER);
control.getStyleClass().add("controls");
getChildren().addAll(songTitle, progressBar, progressText, control);
}
private void updateTheSong(AudioEvents.PlayingMusic event) {

View File

@@ -55,6 +55,28 @@
-fx-fill: white;
}
.pause-button {
-fx-background-color: transparent;
-fx-background-radius: 0;
-fx-cursor: hand;
-fx-text-fill: white;
}
.pause-button .text {
-fx-fill: white;
}
.previous-button {
-fx-background-color: transparent;
-fx-background-radius: 0;
-fx-cursor: hand;
-fx-text-fill: white;
}
.previous-button .text {
-fx-fill: white;
}
.button {
-fx-background-color: linear-gradient(to bottom, #1f5e20, #2e7d2e);
-fx-background-radius: 8;

View File

@@ -55,6 +55,28 @@
-fx-fill: white;
}
.pause-button {
-fx-background-color: transparent;
-fx-background-radius: 0;
-fx-cursor: hand;
-fx-text-fill: white;
}
.pause-button .text {
-fx-fill: white;
}
.previous-button {
-fx-background-color: transparent;
-fx-background-radius: 0;
-fx-cursor: hand;
-fx-text-fill: white;
}
.previous-button .text {
-fx-fill: white;
}
.button {
-fx-background-color: linear-gradient(to bottom, #1b7a1b, #2da32d);
-fx-background-radius: 8;

View File

@@ -56,6 +56,28 @@
-fx-fill: black;
}
.pause-button {
-fx-background-color: transparent;
-fx-background-radius: 0;
-fx-cursor: hand;
-fx-text-fill: black;
}
.pause-button .text {
-fx-fill: black;
}
.previous-button {
-fx-background-color: transparent;
-fx-background-radius: 0;
-fx-cursor: hand;
-fx-text-fill: black;
}
.previous-button .text {
-fx-fill: black;
}
.button {
-fx-background-color: linear-gradient(to bottom, #7ac27a, #90d090);
-fx-background-radius: 8;

View File

@@ -28,6 +28,8 @@ public class AudioEventListener<T extends AudioResource, K extends AudioResource
.listen(this::handleStopMusicManager)
.listen(this::handlePlaySound)
.listen(this::handleSkipSong)
.listen(this::handlePauseSong)
.listen(this::handlePreviousSong)
.listen(this::handleStopSound)
.listen(this::handleMusicStart)
.listen(this::handleVolumeChange)
@@ -50,6 +52,15 @@ public class AudioEventListener<T extends AudioResource, K extends AudioResource
this.musicManager.skip();
}
private void handlePauseSong(AudioEvents.PauseMusic event) {
this.musicManager.pause();
}
private void handlePreviousSong(AudioEvents.PreviousMusic event) {
this.musicManager.previous();
}
private void handleStopSound(AudioEvents.StopEffect event) {
this.soundEffectManager.stop(event.fileName());
}

View File

@@ -23,6 +23,7 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
private final List<T> resources;
private int playingIndex = 0;
private boolean playing = false;
private long pausedPosition = 0;
private ScheduledExecutorService scheduler;
@@ -156,4 +157,29 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
playing = false;
dispatcher.run(() -> backgroundMusic.forEach(T::stop));
}
public void pause() {
T current = backgroundMusic.get(playingIndex);
if (this.playing) {
current.pause();
playing = false;
}
else {
this.playing = true;
dispatcher.run(() -> {
current.play();
setTrackRunnable(current);
});
}
}
public void previous() {
if (backgroundMusic.isEmpty()) return;
if (playingIndex == 0) return;
stop();
scheduler.shutdownNow();
playingIndex = playingIndex - 1;
playing = true;
playCurrentTrack();
}
}

View File

@@ -24,6 +24,10 @@ public class AudioEvents extends EventsBase {
/** Skips the song to the last second of the song resulting in a skip effect */
public record SkipMusic() implements GenericEvent {}
public record PreviousMusic() implements GenericEvent {}
public record PauseMusic() implements GenericEvent {}
/** Change volume, choose type with {@link VolumeControl}. */
public record ChangeVolume(double newVolume, VolumeControl controlType) implements GenericEvent {}

View File

@@ -6,4 +6,6 @@ public interface MusicManager<T extends AudioResource> extends AudioManager<T> {
void play();
void stop();
void skip();
void previous();
void pause();
}

View File

@@ -78,6 +78,10 @@ public class MusicAsset extends BaseResource implements LoadableResource, AudioR
getMediaPlayer().play();
}
public void pause() {
getMediaPlayer().pause();
}
@Override
public void stop() {
getMediaPlayer().stop();

View File

@@ -142,6 +142,12 @@ public class SoundEffectAsset extends BaseResource implements LoadableResource,
if (this.clip.isRunning()) this.clip.stop();
}
// had to be implemented for mediaplayer.pause() to work so:
// TODO: get this removed, somehow OR get a clip.pause which I have no idea why you would ever use
public void pause() {
this.clip.stop();
}
@Override
public long duration() {
if (clip != null) {

View File

@@ -9,4 +9,5 @@ public interface AudioResource {
long currentPosition();
void setOnEnd(Runnable run);
void setOnError(Runnable run);
void pause();
}