ui/src/guihandler/gui/audible.rs

50 lines
1 KiB
Rust
Raw Normal View History

2023-01-16 09:53:52 +00:00
//! `Audible` is a property to play a sound
use crate::prelude::*;
use anyhow::Result;
use assetpath::AssetPath;
2023-01-16 12:27:54 +00:00
#[cfg(feature = "audio")]
use audio::*;
2023-01-16 09:53:52 +00:00
use std::sync::Arc;
/// `Audible` gives the ability to play a sound
pub struct Audible {
gui_handler: Arc<GuiHandler>,
sound: Arc<Sound>,
}
impl Audible {
pub fn new(gui_handler: Arc<GuiHandler>, mut path: AssetPath) -> Result<Arc<Self>> {
if !path.has_prefix() {
path.set_prefix(&gui_handler.resource_base_path().full_path());
}
2023-01-16 12:27:54 +00:00
let sound = gui_handler.context().sound_handler().load_sound(
path,
"gui",
SoundInterpretation::Generic,
)?;
2023-01-16 09:53:52 +00:00
Ok(Arc::new(Audible { gui_handler, sound }))
}
pub fn play(&self) -> Result<()> {
self.sound.play(Some(false))?;
Ok(())
}
}
impl Drop for Audible {
fn drop(&mut self) {
self.gui_handler
.context()
2023-01-16 12:27:54 +00:00
.sound_handler()
2023-01-16 09:53:52 +00:00
.remove_sound(&self.sound)
.unwrap();
}
}