mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
Add a way to toggle AudioSink
(#6321)
# Objective Currently toggling an `AudioSink` (for example from a game menu) requires writing ```rs if sink.is_paused() { sink.play(); } else { sink.pause(); } ``` It would be nicer if we could reduce this down to a single line ```rs sink.toggle(); ``` ## Solution Add an `AudioSink::toggle` method which does exactly that. --- ## Changelog - Added `AudioSink::toggle` which can be used to toggle state of a sink.
This commit is contained in:
parent
13da481bea
commit
599ca782e3
2 changed files with 13 additions and 6 deletions
|
@ -178,9 +178,20 @@ impl AudioSink {
|
|||
self.sink.as_ref().unwrap().pause();
|
||||
}
|
||||
|
||||
/// Toggles the playback of this sink.
|
||||
///
|
||||
/// Will pause if playing, and will be resumed if paused.
|
||||
pub fn toggle(&self) {
|
||||
if self.is_paused() {
|
||||
self.play();
|
||||
} else {
|
||||
self.pause();
|
||||
}
|
||||
}
|
||||
|
||||
/// Is this sink paused?
|
||||
///
|
||||
/// Sinks can be paused and resumed using [`pause`](Self::pause) and [`play`](Self::play).
|
||||
/// Sinks can be paused and resumed using [`pause`](Self::pause), [`play`](Self::play), and [`toggle`](Self::toggle).
|
||||
pub fn is_paused(&self) -> bool {
|
||||
self.sink.as_ref().unwrap().is_paused()
|
||||
}
|
||||
|
|
|
@ -43,11 +43,7 @@ fn pause(
|
|||
) {
|
||||
if keyboard_input.just_pressed(KeyCode::Space) {
|
||||
if let Some(sink) = audio_sinks.get(&music_controller.0) {
|
||||
if sink.is_paused() {
|
||||
sink.play();
|
||||
} else {
|
||||
sink.pause();
|
||||
}
|
||||
sink.toggle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue