49 lines
1 KiB
Rust
49 lines
1 KiB
Rust
//! `Audible` is a property to play a sound
|
|
|
|
use crate::prelude::*;
|
|
use anyhow::Result;
|
|
use assetpath::AssetPath;
|
|
|
|
#[cfg(feature = "audio")]
|
|
use audio::*;
|
|
|
|
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());
|
|
}
|
|
|
|
let sound = gui_handler.context().sound_handler().load_sound(
|
|
path,
|
|
"gui",
|
|
SoundInterpretation::Generic,
|
|
)?;
|
|
|
|
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()
|
|
.sound_handler()
|
|
.remove_sound(&self.sound)
|
|
.unwrap();
|
|
}
|
|
}
|