Click recipe book buttons at forUI volume and test the mixer

Recipe book widgets play SimpleSoundInstance.forUI(UI_BUTTON_CLICK, 1.0F),
whose volume is 0.25. Unit tests cover the mixer distance gain and the
stopped/looping source states.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-10 22:13:40 +02:00
parent 1357e2f01b
commit f77a22177a
3 changed files with 52 additions and 6 deletions
+44
View File
@@ -552,3 +552,47 @@ impl Sounds for Audio {
self.bound().update_category_volume(source, gain);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn channel(samples: Vec<f32>, linear: Option<f32>, at: [f32; 3], looping: bool) -> Channel {
Channel {
samples: Some((Arc::new(samples), 48_000)),
position: 0.0,
state: State::Playing,
gain: 1.0,
pitch: 1.0,
linear,
looping,
at,
relative: false,
}
}
#[test]
fn linear_attenuation_reaches_zero_at_the_max_distance() {
assert_eq!(distance_gain(None, 100.0), 1.0);
assert_eq!(distance_gain(Some(16.0), 0.0), 1.0);
assert_eq!(distance_gain(Some(16.0), 8.0), 0.5);
assert_eq!(distance_gain(Some(16.0), 16.0), 0.0);
assert_eq!(distance_gain(Some(16.0), 40.0), 0.0);
}
#[test]
fn sources_play_attenuated_then_stop_or_loop() {
let mut m = Mixer::default();
m.channels
.insert(1, channel(vec![1.0; 4], Some(16.0), [8.0, 0.0, 0.0], false));
m.channels
.insert(2, channel(vec![1.0; 4], None, [0.0; 3], true));
let mut data = [0.0f32; 16];
mix(&mut m, &mut data, 2, 48_000);
assert_eq!(m.channels[&1].state, State::Stopped);
assert_eq!(m.channels[&2].state, State::Playing);
// Mono sum of the two at the first frame: the far one is half as loud.
let first = data[0] + data[1];
assert!(first > 1.4 && first < 3.1, "{first}");
}
}
@@ -490,7 +490,7 @@ impl Game<'_> {
if !open {
self.recipe_ui.overlay = None;
}
self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0);
self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0);
return true;
}
if !self.book_open(book_type) || self.spectator() {
@@ -518,12 +518,12 @@ impl Game<'_> {
if self.recipe_ui.page + 1 < pages && inside(cursor, xo + 93.0, yo + 137.0, 12.0, 17.0)
{
self.recipe_ui.page += 1;
self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0);
self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0);
return true;
}
if self.recipe_ui.page > 0 && inside(cursor, xo + 38.0, yo + 137.0, 12.0, 17.0) {
self.recipe_ui.page -= 1;
self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0);
self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0);
return true;
}
}
@@ -570,7 +570,7 @@ impl Game<'_> {
let filtering = !self.book_filtering(book_type);
self.level.recipe_book.filtering[book_type.index()] = filtering;
self.recipe_ui.page = 0;
self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0);
self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0);
return true;
}
// Tabs.
@@ -581,7 +581,7 @@ impl Game<'_> {
self.recipe_ui.tab = Some(tab);
self.recipe_ui.page = 0;
}
self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0);
self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0);
return true;
}
}
+3 -1
View File
@@ -20,7 +20,9 @@ pub enum Sampled {
Product(Vec<Sampled>),
}
impl Sampled {
/// `SampledFloat.sample`: constants draw nothing.
/// `SampledFloat.sample`: constants draw nothing (sounds.json only has
/// constants, but the random stays part of the call as in vanilla).
#[allow(clippy::only_used_in_recursion)]
pub fn sample(&self, random: &mut impl RandomSource) -> f32 {
match self {
Self::Constant(v) => *v,