fix: Clear generator when source is cleared, and improve handling for changing source types.

This commit is contained in:
Nolan Darilek 2024-12-02 11:34:28 -06:00
parent 2364f638db
commit 4c02a98eb4

View File

@ -403,6 +403,7 @@ fn update_source_properties(
Option<&AngularPan>, Option<&AngularPan>,
Option<&ScalarPan>, Option<&ScalarPan>,
Option<&GlobalTransform>, Option<&GlobalTransform>,
Option<&mut Sound>,
)>, )>,
) { ) {
for ( for (
@ -416,15 +417,18 @@ fn update_source_properties(
angular_pan, angular_pan,
scalar_pan, scalar_pan,
transform, transform,
sound,
) in &mut query ) in &mut query
{ {
let Source { gain, .. } = *source; let Source { gain, .. } = *source;
assert!(gain >= 0.); assert!(gain >= 0.);
if let Some(handle) = source.handle.as_mut() { let Some(handle) = source.handle.as_mut() else {
continue;
};
handle.gain().set(gain).expect("Failed to set gain"); handle.gain().set(gain).expect("Failed to set gain");
let mut clear_source = false; let mut clear_source = false;
if let Some(transform) = transform {
if let Some(source) = handle.cast_to::<syz::Source3D>().expect("Failed to cast") { if let Some(source) = handle.cast_to::<syz::Source3D>().expect("Failed to cast") {
if let Some(transform) = transform {
let translation = transform.translation(); let translation = transform.translation();
source source
.position() .position()
@ -462,6 +466,7 @@ fn update_source_properties(
.map(|v| **v) .map(|v| **v)
.unwrap_or_else(|| context.default_rolloff().get().unwrap()); .unwrap_or_else(|| context.default_rolloff().get().unwrap());
assert!(rolloff >= 0.); assert!(rolloff >= 0.);
assert!(rolloff >= 0.);
source source
.rolloff() .rolloff()
.set(rolloff) .set(rolloff)
@ -474,23 +479,23 @@ fn update_source_properties(
.closeness_boost() .closeness_boost()
.set(closeness_boost) .set(closeness_boost)
.expect("Failed to set closeness_boost"); .expect("Failed to set closeness_boost");
let closeness_boost_distance = let closeness_boost_distance = closeness_boost_distance
closeness_boost_distance.map(|v| **v).unwrap_or_else(|| { .map(|v| **v)
context.default_closeness_boost_distance().get().unwrap() .unwrap_or_else(|| context.default_closeness_boost_distance().get().unwrap());
});
assert!(closeness_boost_distance >= 0.); assert!(closeness_boost_distance >= 0.);
source source
.closeness_boost_distance() .closeness_boost_distance()
.set(closeness_boost_distance) .set(closeness_boost_distance)
.expect("Failed to set closeness_boost_distance"); .expect("Failed to set closeness_boost_distance");
} else { } else {
println!("Clearing 3D source because no transform");
clear_source = true; clear_source = true;
} }
} else if let Some(angular_pan) = angular_pan { } else if let Some(source) = handle
if let Some(source) = handle
.cast_to::<syz::AngularPannedSource>() .cast_to::<syz::AngularPannedSource>()
.expect("Failed to cast") .expect("Failed to cast")
{ {
if let Some(angular_pan) = angular_pan {
assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.); assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.);
source source
.azimuth() .azimuth()
@ -504,11 +509,11 @@ fn update_source_properties(
} else { } else {
clear_source = true; clear_source = true;
} }
} else if let Some(scalar_pan) = scalar_pan { } else if let Some(source) = handle
if let Some(source) = handle
.cast_to::<syz::ScalarPannedSource>() .cast_to::<syz::ScalarPannedSource>()
.expect("Failed to cast") .expect("Failed to cast")
{ {
if let Some(scalar_pan) = scalar_pan {
assert!(**scalar_pan >= -1. && **scalar_pan <= 1.); assert!(**scalar_pan >= -1. && **scalar_pan <= 1.);
source source
.panning_scalar() .panning_scalar()
@ -517,9 +522,21 @@ fn update_source_properties(
} else { } else {
clear_source = true; clear_source = true;
} }
} else if handle
.cast_to::<syz::DirectSource>()
.expect("Failed to cast")
.is_some()
{
if transform.is_some() || angular_pan.is_some() || scalar_pan.is_some() {
clear_source = true;
}
} else if source.handle.is_some() {
clear_source = true;
} }
if clear_source { if clear_source {
source.handle = None; source.handle = None;
if let Some(mut sound) = sound {
sound.generator = None;
} }
} }
} }