47 lines
1,010 B
Rust
47 lines
1,010 B
Rust
|
//! `Audible` is a property to play a sound
|
||
|
|
||
|
use crate::prelude::*;
|
||
|
use anyhow::Result;
|
||
|
use assetpath::AssetPath;
|
||
|
|
||
|
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()
|
||
|
.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()
|
||
|
.remove_sound(&self.sound)
|
||
|
.unwrap();
|
||
|
}
|
||
|
}
|