fix: cargo fmt

This commit is contained in:
Nova
2023-02-07 18:04:20 -05:00
parent 33d12787c9
commit 4620e0ca09
4 changed files with 109 additions and 103 deletions

View File

@@ -2,7 +2,7 @@ use super::scenegraph::Scenegraph;
use crate::{ use crate::{
core::{registry::OwnedRegistry, task}, core::{registry::OwnedRegistry, task},
nodes::{ nodes::{
data, drawable, fields, hmd, input, items, audio, audio, data, drawable, fields, hmd, input, items,
root::Root, root::Root,
spatial, spatial,
startup::{self, StartupSettings, STARTUP_SETTINGS}, startup::{self, StartupSettings, STARTUP_SETTINGS},

View File

@@ -5,7 +5,7 @@ mod objects;
mod wayland; mod wayland;
use crate::core::destroy_queue; use crate::core::destroy_queue;
use crate::nodes::{drawable, hmd, input, audio}; use crate::nodes::{audio, drawable, hmd, input};
use crate::objects::input::mouse_pointer::MousePointer; use crate::objects::input::mouse_pointer::MousePointer;
use crate::objects::input::sk_controller::SkController; use crate::objects::input::sk_controller::SkController;
use crate::objects::input::sk_hand::SkHand; use crate::objects::input::sk_hand::SkHand;

View File

@@ -1,11 +1,11 @@
use super::Node; use super::Node;
use crate::core::client::Client; use crate::core::client::Client;
use crate::core::destroy_queue; use crate::core::destroy_queue;
use crate::core::resource::ResourceID;
use crate::core::registry::Registry; use crate::core::registry::Registry;
use crate::nodes::spatial::{Spatial, find_spatial_parent, parse_transform}; use crate::core::resource::ResourceID;
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
use color_eyre::eyre::{ensure, eyre, Result}; use color_eyre::eyre::{ensure, eyre, Result};
use glam::Vec4Swizzles; use glam::{vec3a, Vec4Swizzles};
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use parking_lot::Mutex; use parking_lot::Mutex;
use send_wrapper::SendWrapper; use send_wrapper::SendWrapper;
@@ -13,125 +13,131 @@ use serde::Deserialize;
use stardust_xr::schemas::flex::deserialize; use stardust_xr::schemas::flex::deserialize;
use stardust_xr::values::Transform; use stardust_xr::values::Transform;
use std::ops::DerefMut; use std::ops::DerefMut;
use std::{sync::Arc, path::PathBuf, ffi::OsStr, fmt::Error}; use std::{ffi::OsStr, fmt::Error, path::PathBuf, sync::Arc};
use stereokit::sound::Sound as SKSound; use stereokit::sound::Sound as SKSound;
use stereokit::sound::SoundInstance; use stereokit::sound::SoundInstance;
static SOUND_REGISTRY: Registry<Sound> = Registry::new(); static SOUND_REGISTRY: Registry<Sound> = Registry::new();
pub struct Sound { pub struct Sound {
space: Arc<Spatial>, space: Arc<Spatial>,
resource_id: ResourceID, resource_id: ResourceID,
pending_audio_path: OnceCell<PathBuf>, pending_audio_path: OnceCell<PathBuf>,
instance: Mutex<Option<SoundInstance>>, instance: Mutex<Option<SoundInstance>>,
volume: f32, volume: f32,
sk_sound: OnceCell<SendWrapper<SKSound>>, sk_sound: OnceCell<SendWrapper<SKSound>>,
} }
impl Sound { impl Sound {
pub fn add_to(node: &Arc<Node>, resource_id: ResourceID) -> Result<Arc<Sound>> { pub fn add_to(node: &Arc<Node>, resource_id: ResourceID) -> Result<Arc<Sound>> {
ensure!( ensure!(
node.spatial.get().is_some(), node.spatial.get().is_some(),
"Internal: Node does not have a spatial attached!" "Internal: Node does not have a spatial attached!"
); );
let sound = Sound { let sound = Sound {
space: node.spatial.get().unwrap().clone(), space: node.spatial.get().unwrap().clone(),
resource_id, resource_id,
volume: 1.0, volume: 1.0,
instance: Mutex::new(None), instance: Mutex::new(None),
pending_audio_path: OnceCell::new(), pending_audio_path: OnceCell::new(),
sk_sound: OnceCell::new(), sk_sound: OnceCell::new(),
}; };
let sound_arc = SOUND_REGISTRY.add(sound); let sound_arc = SOUND_REGISTRY.add(sound);
node.add_local_signal("play", Sound::play_flex); node.add_local_signal("play", Sound::play_flex);
node.add_local_signal("stop", Sound::stop_flex); node.add_local_signal("stop", Sound::stop_flex);
let _ = sound_arc.pending_audio_path.set( let _ = sound_arc.pending_audio_path.set(
sound_arc sound_arc
.resource_id .resource_id
.get_file( .get_file(
&node &node
.get_client() .get_client()
.ok_or_else(|| eyre!("Client not found"))? .ok_or_else(|| eyre!("Client not found"))?
.base_resource_prefixes .base_resource_prefixes
.lock() .lock()
.clone(), .clone(),
&[OsStr::new("wav"), OsStr::new("mp3")] &[OsStr::new("wav"), OsStr::new("mp3")],
) )
.ok_or_else(|| eyre!("Resource not found"))?, .ok_or_else(|| eyre!("Resource not found"))?,
); );
let _ = node.sound.set(sound_arc.clone()); let _ = node.sound.set(sound_arc.clone());
Ok(sound_arc) Ok(sound_arc)
} }
fn update(&self) { fn update(&self) {
if let Some(instance) = self.instance.lock().deref_mut() { if let Some(instance) = self.instance.lock().deref_mut() {
instance.set_position(self.space.global_transform().w_axis.xyz()) instance.set_position(self.space.global_transform().w_axis.xyz())
} }
} }
fn play_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> { fn play_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
let sound =node.sound.get().unwrap(); let sound = node.sound.get().unwrap();
let sk_sound = sound let sk_sound = sound
.sk_sound .sk_sound
.get_or_try_init(|| -> color_eyre::eyre::Result<SendWrapper<SKSound>> { .get_or_try_init(|| -> color_eyre::eyre::Result<SendWrapper<SKSound>> {
let pending_audio_path = sound.pending_audio_path.get().ok_or(Error)?; let pending_audio_path = sound.pending_audio_path.get().ok_or(Error)?;
let sound = SKSound::from_file(pending_audio_path.as_path()).ok_or(Error)?; let sound = SKSound::from_file(pending_audio_path.as_path()).ok_or(Error)?;
Ok(SendWrapper::new(sound)) Ok(SendWrapper::new(sound))
}) })
.ok(); .ok();
if let Some(sk_sound) = sk_sound { if let Some(sk_sound) = sk_sound {
sound.instance.lock().replace(sk_sound.play_sound(sound.space.global_transform().to_scale_rotation_translation().2, sound.volume)); let position = sound
} .space
.global_transform()
.transform_point3a(vec3a(0.0, 0.0, 0.0));
sound
.instance
.lock()
.replace(sk_sound.play_sound(position, sound.volume));
}
Ok(()) Ok(())
} }
pub fn stop_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> { pub fn stop_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
let sound = node.sound.get().unwrap(); let sound = node.sound.get().unwrap();
if let Some(instance) = sound.instance.lock().take() { if let Some(instance) = sound.instance.lock().take() {
instance.stop(); instance.stop();
} }
Ok(()) Ok(())
} }
} }
pub fn update() { pub fn update() {
for sound in SOUND_REGISTRY.get_valid_contents() { for sound in SOUND_REGISTRY.get_valid_contents() {
sound.update() sound.update()
} }
} }
pub fn create_interface(client: &Arc<Client>) -> Result<()> { pub fn create_interface(client: &Arc<Client>) -> Result<()> {
let node = Node::create(client, "", "audio", false); let node = Node::create(client, "", "audio", false);
node.add_local_signal("create_sound", create_flex); node.add_local_signal("create_sound", create_flex);
node.add_to_scenegraph().map(|_| ()) node.add_to_scenegraph().map(|_| ())
} }
pub fn create_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> { pub fn create_flex(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
#[derive(Deserialize)] #[derive(Deserialize)]
struct CreateSoundInfo<'a> { struct CreateSoundInfo<'a> {
name: &'a str, name: &'a str,
parent_path: &'a str, parent_path: &'a str,
transform: Transform, transform: Transform,
resource: ResourceID, resource: ResourceID,
} }
let info: CreateSoundInfo = deserialize(data)?; let info: CreateSoundInfo = deserialize(data)?;
let node = Node::create(&calling_client, "/audio/sound", info.name, true); let node = Node::create(&calling_client, "/audio/sound", info.name, true);
let parent = find_spatial_parent(&calling_client, info.parent_path)?; let parent = find_spatial_parent(&calling_client, info.parent_path)?;
let transform = parse_transform(info.transform, true, true, true); let transform = parse_transform(info.transform, true, true, true);
let node = node.add_to_scenegraph()?; let node = node.add_to_scenegraph()?;
Spatial::add_to(&node, Some(parent), transform, false)?; Spatial::add_to(&node, Some(parent), transform, false)?;
Sound::add_to(&node, info.resource)?; Sound::add_to(&node, info.resource)?;
Ok(()) Ok(())
} }
impl Drop for Sound { impl Drop for Sound {
fn drop(&mut self) { fn drop(&mut self) {
if let Some(instance) = self.instance.lock().take() { if let Some(instance) = self.instance.lock().take() {
destroy_queue::add(instance); destroy_queue::add(instance);
} }
SOUND_REGISTRY.remove(self); SOUND_REGISTRY.remove(self);
} }
} }

View File

@@ -1,4 +1,5 @@
pub mod alias; pub mod alias;
pub mod audio;
pub mod data; pub mod data;
pub mod drawable; pub mod drawable;
pub mod fields; pub mod fields;
@@ -8,7 +9,6 @@ pub mod items;
pub mod root; pub mod root;
pub mod spatial; pub mod spatial;
pub mod startup; pub mod startup;
pub mod audio;
use color_eyre::eyre::{eyre, Result}; use color_eyre::eyre::{eyre, Result};
use nanoid::nanoid; use nanoid::nanoid;
@@ -33,13 +33,13 @@ use crate::core::registry::Registry;
use self::alias::Alias; use self::alias::Alias;
use self::data::{PulseReceiver, PulseSender}; use self::data::{PulseReceiver, PulseSender};
use self::audio::Sound;
use self::drawable::lines::Lines; use self::drawable::lines::Lines;
use self::drawable::model::Model; use self::drawable::model::Model;
use self::drawable::text::Text; use self::drawable::text::Text;
use self::fields::Field; use self::fields::Field;
use self::input::{InputHandler, InputMethod}; use self::input::{InputHandler, InputMethod};
use self::items::{Item, ItemAcceptor, ItemUI}; use self::items::{Item, ItemAcceptor, ItemUI};
use self::audio::Sound;
use self::spatial::zone::Zone; use self::spatial::zone::Zone;
use self::spatial::Spatial; use self::spatial::Spatial;
use self::startup::StartupSettings; use self::startup::StartupSettings;