use super::asset::{ProbeBatchAsset, with_serialized_object_mut};
use super::configuration::SimulationConfiguration;
use super::simulation::Simulation;
use crate::context::Context;
use crate::error::SteamAudioError;
use crate::serialized_object::SerializedObject;
use bevy::prelude::{
Add, Changed, Component, On, Query, Reflect, ReflectComponent, Remove, ResMut,
};
use std::ops::{Deref, DerefMut};
#[cfg(doc)]
use crate::probe::{ProbeArray as RawProbeArray, ProbeBatch as RawProbeBatch};
#[derive(Component, Reflect, Clone, Debug, PartialEq, Eq)]
#[reflect(Component, opaque)]
pub struct ProbeArray(pub crate::probe::ProbeArray);
impl ProbeArray {
pub fn try_new(context: &Context) -> Result<Self, SteamAudioError> {
crate::probe::ProbeArray::try_new(context).map(Self)
}
}
impl Deref for ProbeArray {
type Target = crate::probe::ProbeArray;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ProbeArray {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl From<crate::probe::ProbeArray> for ProbeArray {
fn from(probe_array: crate::probe::ProbeArray) -> Self {
Self(probe_array)
}
}
#[derive(Component, Reflect, Clone, Debug)]
#[reflect(Component, opaque)]
pub struct ProbeBatch(pub crate::probe::ProbeBatch);
impl ProbeBatch {
pub fn try_new(context: &Context) -> Result<Self, SteamAudioError> {
crate::probe::ProbeBatch::try_new(context).map(Self)
}
pub fn load(
context: &Context,
serialized_object: &mut SerializedObject,
) -> Result<Self, SteamAudioError> {
crate::probe::ProbeBatch::load(context, serialized_object).map(Self)
}
pub fn from_asset(context: &Context, asset: &ProbeBatchAsset) -> Result<Self, SteamAudioError> {
with_serialized_object_mut(context, asset.bytes().to_vec(), |serialized_object| {
Self::load(context, serialized_object)
})
}
}
impl Deref for ProbeBatch {
type Target = crate::probe::ProbeBatch;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ProbeBatch {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl From<crate::probe::ProbeBatch> for ProbeBatch {
fn from(probe_batch: crate::probe::ProbeBatch) -> Self {
Self(probe_batch)
}
}
pub(crate) fn on_probe_batch_added<C: SimulationConfiguration>(
event: On<Add, ProbeBatch>,
query: Query<&ProbeBatch>,
mut simulation: ResMut<Simulation<C>>,
) {
let probe_batch = query.get(event.entity).unwrap();
simulation.0.simulator.add_probe_batch(&probe_batch.0);
simulation.0.request_simulator_commit();
}
pub(crate) fn on_probe_batch_removed<C: SimulationConfiguration>(
event: On<Remove, ProbeBatch>,
query: Query<&ProbeBatch>,
mut simulation: ResMut<Simulation<C>>,
) {
let probe_batch = query.get(event.entity).unwrap();
simulation.0.simulator.remove_probe_batch(&probe_batch.0);
simulation.0.request_simulator_commit();
}
pub(crate) fn commit_probe_batches<C: SimulationConfiguration>(
probe_batches: Query<&ProbeBatch, Changed<ProbeBatch>>,
mut simulation: ResMut<Simulation<C>>,
) {
if probe_batches.is_empty() {
return;
}
for probe_batch in &probe_batches {
probe_batch.0.commit();
}
simulation.0.request_simulator_commit();
}