Merge pull request #182 from rdbtCVS/t3code/fix-classic-ui-menu-sounds
Fix classic menu click sounds to match vanilla
This commit is contained in:
@@ -617,6 +617,18 @@ impl Audio {
|
|||||||
let seed = self.random.next_long();
|
let seed = self.random.next_long();
|
||||||
self.play_seeded(event, at, volume, pitch, seed);
|
self.play_seeded(event, at, volume, pitch, seed);
|
||||||
}
|
}
|
||||||
|
/// `AbstractWidget.playButtonClickSound`: an unseeded, relative UI sound.
|
||||||
|
pub fn play_button_click(&mut self) {
|
||||||
|
self.play_instance(
|
||||||
|
SoundInstance::for_ui(
|
||||||
|
"minecraft:ui.button.click",
|
||||||
|
1.0,
|
||||||
|
0.25,
|
||||||
|
SoundEngine::unseeded(),
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
/// `play` with the seed a sound packet carries.
|
/// `play` with the seed a sound packet carries.
|
||||||
pub fn play_seeded(
|
pub fn play_seeded(
|
||||||
&mut self,
|
&mut self,
|
||||||
@@ -749,6 +761,38 @@ impl Sounds for Audio {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn button_click_uses_vanilla_ui_parameters_without_advancing_world_random() {
|
||||||
|
let mut audio = Audio::silent(Path::new("/nonexistent/horizon-test-assets"));
|
||||||
|
audio.engine.registry.add_file(
|
||||||
|
"minecraft",
|
||||||
|
br#"{"ui.button.click":{"sounds":["gui/button"]}}"#,
|
||||||
|
&|_, _| true,
|
||||||
|
);
|
||||||
|
// Exercise the mixer without opening an output device. The missing
|
||||||
|
// sample is harmless: this test checks the source the engine creates.
|
||||||
|
audio.engine.loaded = true;
|
||||||
|
audio.backend.live = true;
|
||||||
|
let mut random = audio.random.clone();
|
||||||
|
audio.play_button_click();
|
||||||
|
let instance = audio.engine.instance(1).expect("click reached the engine");
|
||||||
|
assert_eq!(instance.identifier, "minecraft:ui.button.click");
|
||||||
|
assert_eq!(instance.source, crate::sound_engine::UI);
|
||||||
|
assert_eq!(instance.volume, 0.25);
|
||||||
|
assert_eq!(instance.pitch, 1.0);
|
||||||
|
assert_eq!(instance.attenuation, Attenuation::None);
|
||||||
|
assert_eq!(instance.position, [0.0; 3]);
|
||||||
|
assert!(instance.relative);
|
||||||
|
assert!(!instance.looping);
|
||||||
|
assert_eq!(audio.random.next_long(), random.next_long());
|
||||||
|
audio.pause();
|
||||||
|
assert_eq!(
|
||||||
|
audio.backend.mixer.lock().unwrap().channels[&1].state,
|
||||||
|
State::Initial
|
||||||
|
);
|
||||||
|
audio.stop_all();
|
||||||
|
}
|
||||||
|
|
||||||
fn channel(samples: Vec<f32>, linear: Option<f32>, at: [f32; 3], looping: bool) -> Channel {
|
fn channel(samples: Vec<f32>, linear: Option<f32>, at: [f32; 3], looping: bool) -> Channel {
|
||||||
Channel {
|
Channel {
|
||||||
samples: Some((Arc::new(samples), 48_000)),
|
samples: Some((Arc::new(samples), 48_000)),
|
||||||
|
|||||||
@@ -8415,13 +8415,19 @@ impl ApplicationHandler for Game<'_> {
|
|||||||
.unwrap_or([1, 1]);
|
.unwrap_or([1, 1]);
|
||||||
let mut b = HudBatch::new(size);
|
let mut b = HudBatch::new(size);
|
||||||
let atlas = &self.res.atlas;
|
let atlas = &self.res.atlas;
|
||||||
let action = self.title.as_mut().map_or(Action::None, |t| {
|
let (action, sound) = self.title.as_mut().map_or((Action::None, false), |t| {
|
||||||
if horizon_client::ui_theme::horizon() {
|
if horizon_client::ui_theme::horizon() {
|
||||||
t.click_horizon(atlas, &mut b, self.cursor.0, self.cursor.1)
|
(
|
||||||
|
t.click_horizon(atlas, &mut b, self.cursor.0, self.cursor.1),
|
||||||
|
false,
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
t.click(self.cursor.0, self.cursor.1, (b.width, b.height))
|
t.click(self.cursor.0, self.cursor.1, (b.width, b.height))
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if sound {
|
||||||
|
self.audio.play_button_click();
|
||||||
|
}
|
||||||
match action {
|
match action {
|
||||||
Action::Quit => event_loop.exit(),
|
Action::Quit => event_loop.exit(),
|
||||||
Action::Options => self.open_options(true),
|
Action::Options => self.open_options(true),
|
||||||
@@ -8500,11 +8506,17 @@ impl ApplicationHandler for Game<'_> {
|
|||||||
Screen::Creative if button == MouseButton::Left => {
|
Screen::Creative if button == MouseButton::Left => {
|
||||||
self.creative_screen.release();
|
self.creative_screen.release();
|
||||||
}
|
}
|
||||||
Screen::Paused if pressed && self.pause_menu => self.pause_click()?,
|
Screen::Paused
|
||||||
|
if pressed && button == MouseButton::Left && self.pause_menu =>
|
||||||
|
{
|
||||||
|
self.pause_click()?
|
||||||
|
}
|
||||||
Screen::WorldOptions if pressed && button == MouseButton::Left => {
|
Screen::WorldOptions if pressed && button == MouseButton::Left => {
|
||||||
self.world_options_click()
|
self.world_options_click()
|
||||||
}
|
}
|
||||||
Screen::Dead if pressed => self.death_click()?,
|
Screen::Dead if pressed && button == MouseButton::Left => {
|
||||||
|
self.death_click()?
|
||||||
|
}
|
||||||
Screen::Advancements if button == MouseButton::Left => {
|
Screen::Advancements if button == MouseButton::Left => {
|
||||||
let size = self
|
let size = self
|
||||||
.renderer
|
.renderer
|
||||||
|
|||||||
@@ -282,6 +282,9 @@ impl Game<'_> {
|
|||||||
o.active(*button) && ui::hovered(self.cursor, *x, *y, *bw, 20.0)
|
o.active(*button) && ui::hovered(self.cursor, *x, *y, *bw, 20.0)
|
||||||
})
|
})
|
||||||
.map(|b| b.0);
|
.map(|b| b.0);
|
||||||
|
if hit.is_some() {
|
||||||
|
self.audio.play_button_click();
|
||||||
|
}
|
||||||
match hit {
|
match hit {
|
||||||
Some(Button::DefaultMode) => o.default_mode = (o.default_mode + 1) % 4,
|
Some(Button::DefaultMode) => o.default_mode = (o.default_mode + 1) % 4,
|
||||||
Some(Button::PersonalMode) => o.personal_mode = (o.personal_mode + 1) % 4,
|
Some(Button::PersonalMode) => o.personal_mode = (o.personal_mode + 1) % 4,
|
||||||
|
|||||||
@@ -44,11 +44,16 @@ impl Game<'_> {
|
|||||||
}
|
}
|
||||||
pub(super) fn options_mouse(&mut self, pressed: bool, right: bool) -> Result<()> {
|
pub(super) fn options_mouse(&mut self, pressed: bool, right: bool) -> Result<()> {
|
||||||
if !pressed {
|
if !pressed {
|
||||||
self.ui.options.release();
|
if right {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let events = self.ui.options.release();
|
||||||
self.dragging = false;
|
self.dragging = false;
|
||||||
return Ok(());
|
return self.handle_option_events(events);
|
||||||
|
}
|
||||||
|
if !right {
|
||||||
|
self.dragging = true;
|
||||||
}
|
}
|
||||||
self.dragging = true;
|
|
||||||
let size = self.gui_size();
|
let size = self.gui_size();
|
||||||
let mut ui = std::mem::take(&mut self.ui.options);
|
let mut ui = std::mem::take(&mut self.ui.options);
|
||||||
let events = ui.click(
|
let events = ui.click(
|
||||||
@@ -115,6 +120,7 @@ impl Game<'_> {
|
|||||||
pub(super) fn handle_option_events(&mut self, events: Vec<Event>) -> Result<()> {
|
pub(super) fn handle_option_events(&mut self, events: Vec<Event>) -> Result<()> {
|
||||||
for e in events {
|
for e in events {
|
||||||
match e {
|
match e {
|
||||||
|
Event::ButtonClick => self.audio.play_button_click(),
|
||||||
Event::Changed => {
|
Event::Changed => {
|
||||||
self.settings.save(&Self::options_path());
|
self.settings.save(&Self::options_path());
|
||||||
self.apply_settings();
|
self.apply_settings();
|
||||||
|
|||||||
@@ -151,6 +151,9 @@ impl Game<'_> {
|
|||||||
.find(|b| b.5 && ui::hovered(self.cursor, b.0, b.1, b.2, 20.0))
|
.find(|b| b.5 && ui::hovered(self.cursor, b.0, b.1, b.2, 20.0))
|
||||||
.map(|b| b.4)
|
.map(|b| b.4)
|
||||||
};
|
};
|
||||||
|
if hit.is_some() && !horizon_client::ui_theme::horizon() {
|
||||||
|
self.audio.play_button_click();
|
||||||
|
}
|
||||||
match hit {
|
match hit {
|
||||||
Some(0) => {
|
Some(0) => {
|
||||||
self.screen = Screen::Playing;
|
self.screen = Screen::Playing;
|
||||||
@@ -255,6 +258,9 @@ impl Game<'_> {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.find(|d| ui::hovered(self.cursor, d.0, d.1, 200.0, 20.0))
|
.find(|d| ui::hovered(self.cursor, d.0, d.1, 200.0, 20.0))
|
||||||
.map(|d| d.3);
|
.map(|d| d.3);
|
||||||
|
if hit.is_some() {
|
||||||
|
self.audio.play_button_click();
|
||||||
|
}
|
||||||
match hit {
|
match hit {
|
||||||
Some(0) if self.remote.is_some() => {
|
Some(0) if self.remote.is_some() => {
|
||||||
self.ui.death_text = None;
|
self.ui.death_text = None;
|
||||||
|
|||||||
@@ -145,6 +145,7 @@ impl Game<'_> {
|
|||||||
let (x, y) = (b.width / 2.0 - 100.0, b.height / 4.0 + 144.0);
|
let (x, y) = (b.width / 2.0 - 100.0, b.height / 4.0 + 144.0);
|
||||||
let (mx, my) = self.cursor;
|
let (mx, my) = self.cursor;
|
||||||
if mx >= x && mx < x + 200.0 && my >= y && my < y + 20.0 {
|
if mx >= x && mx < x + 200.0 && my >= y && my < y + 20.0 {
|
||||||
|
self.audio.play_button_click();
|
||||||
self.close_sign_editor();
|
self.close_sign_editor();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -456,6 +456,7 @@ impl Item {
|
|||||||
/// What the options screens must do after an interaction here.
|
/// What the options screens must do after an interaction here.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum Action {
|
pub enum Action {
|
||||||
|
ButtonClick,
|
||||||
Changed,
|
Changed,
|
||||||
OpenAdvanced,
|
OpenAdvanced,
|
||||||
OpenSimple,
|
OpenSimple,
|
||||||
@@ -671,6 +672,9 @@ impl GraphicsUi {
|
|||||||
}
|
}
|
||||||
return Vec::new();
|
return Vec::new();
|
||||||
}
|
}
|
||||||
|
if right {
|
||||||
|
return Vec::new();
|
||||||
|
}
|
||||||
for wd in widgets {
|
for wd in widgets {
|
||||||
if !ui::hovered((x, y), wd.x, wd.y, wd.w, 20.0) {
|
if !ui::hovered((x, y), wd.x, wd.y, wd.w, 20.0) {
|
||||||
continue;
|
continue;
|
||||||
@@ -679,13 +683,14 @@ impl GraphicsUi {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
return match wd.target {
|
return match wd.target {
|
||||||
Target::Button(action) => vec![action],
|
Target::Button(action) => vec![Action::ButtonClick, action],
|
||||||
Target::Item(item) => {
|
Target::Item(item) => {
|
||||||
if item.slider(s).is_some() {
|
if item.slider(s).is_some() {
|
||||||
self.dragging = Some(item);
|
self.dragging = Some(item);
|
||||||
item.set_slider(s, ui::slider_value(wd.x, wd.w, x));
|
item.set_slider(s, ui::slider_value(wd.x, wd.w, x));
|
||||||
} else {
|
} else {
|
||||||
item.cycle(s, right);
|
item.cycle(s, right);
|
||||||
|
return vec![Action::ButtonClick, Action::Changed];
|
||||||
}
|
}
|
||||||
vec![Action::Changed]
|
vec![Action::Changed]
|
||||||
}
|
}
|
||||||
@@ -693,8 +698,8 @@ impl GraphicsUi {
|
|||||||
}
|
}
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
pub fn release(&mut self) {
|
pub fn release(&mut self) -> bool {
|
||||||
self.dragging = None;
|
self.dragging.take().is_some()
|
||||||
}
|
}
|
||||||
pub fn scroll(&mut self, level: Level, h: f32, amount: f32) {
|
pub fn scroll(&mut self, level: Level, h: f32, amount: f32) {
|
||||||
let content = Self::content_height(level);
|
let content = Self::content_height(level);
|
||||||
@@ -708,6 +713,49 @@ impl GraphicsUi {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn graphics_buttons_sound_on_press_and_sliders_on_release() {
|
||||||
|
let mut s = Settings::default();
|
||||||
|
let mut ui = GraphicsUi::default();
|
||||||
|
let size = (480.0, 320.0);
|
||||||
|
let (widgets, _, _) = ui.layout(Level::Simple, size.0, size.1);
|
||||||
|
let button = widgets
|
||||||
|
.iter()
|
||||||
|
.find(|w| matches!(w.target, Target::Button(_)))
|
||||||
|
.unwrap();
|
||||||
|
let point = (button.x + 1.0, button.y + 1.0);
|
||||||
|
assert!(
|
||||||
|
ui.click(Level::Simple, &mut s, point, size, false, true)
|
||||||
|
.is_empty()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
ui.click(Level::Simple, &mut s, point, size, false, false)[0],
|
||||||
|
Action::ButtonClick
|
||||||
|
);
|
||||||
|
assert!(!ui.release());
|
||||||
|
let slider = widgets
|
||||||
|
.iter()
|
||||||
|
.find(|w| matches!(w.target, Target::Item(i) if i.slider(&s).is_some()))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
ui.click(
|
||||||
|
Level::Simple,
|
||||||
|
&mut s,
|
||||||
|
(slider.x + 1.0, slider.y + 1.0),
|
||||||
|
size,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
),
|
||||||
|
vec![Action::Changed]
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
ui.click(Level::Simple, &mut s, (-100.0, -100.0), size, true, false),
|
||||||
|
vec![Action::Changed]
|
||||||
|
);
|
||||||
|
assert!(ui.release());
|
||||||
|
assert!(!ui.release());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn scrolling_stops_at_both_limits_before_layout() {
|
fn scrolling_stops_at_both_limits_before_layout() {
|
||||||
for level in [Level::Simple, Level::Advanced] {
|
for level in [Level::Simple, Level::Advanced] {
|
||||||
|
|||||||
@@ -665,6 +665,8 @@ struct Widget {
|
|||||||
/// What the game must do after an interaction.
|
/// What the game must do after an interaction.
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
|
/// An accepted button press or the release of a slider.
|
||||||
|
ButtonClick,
|
||||||
/// Close the options screens (back to the pause menu or title).
|
/// Close the options screens (back to the pause menu or title).
|
||||||
Close,
|
Close,
|
||||||
/// Settings changed: apply them.
|
/// Settings changed: apply them.
|
||||||
@@ -1618,6 +1620,7 @@ impl OptionsUi {
|
|||||||
let mut events = Vec::new();
|
let mut events = Vec::new();
|
||||||
for action in self.graphics.click(level, s, (x, y), size, drag, right) {
|
for action in self.graphics.click(level, s, (x, y), size, drag, right) {
|
||||||
match action {
|
match action {
|
||||||
|
Action::ButtonClick => events.push(Event::ButtonClick),
|
||||||
Action::Changed => events.push(Event::Changed),
|
Action::Changed => events.push(Event::Changed),
|
||||||
Action::OpenAdvanced => self.push(Page::GraphicsAdvanced),
|
Action::OpenAdvanced => self.push(Page::GraphicsAdvanced),
|
||||||
Action::OpenSimple => {
|
Action::OpenSimple => {
|
||||||
@@ -1642,6 +1645,9 @@ impl OptionsUi {
|
|||||||
}
|
}
|
||||||
let m = crate::screen_draw::Measure(&atlas.font);
|
let m = crate::screen_draw::Measure(&atlas.font);
|
||||||
let p = self.packs(atlas, size);
|
let p = self.packs(atlas, size);
|
||||||
|
if p.button_at(&m, x as f64, y as f64) {
|
||||||
|
events.push(Event::ButtonClick);
|
||||||
|
}
|
||||||
let before = p.search_focused();
|
let before = p.search_focused();
|
||||||
let action = p.click(&m, x as f64, y as f64);
|
let action = p.click(&m, x as f64, y as f64);
|
||||||
if !before && p.search_focused() {
|
if !before && p.search_focused() {
|
||||||
@@ -1682,6 +1688,11 @@ impl OptionsUi {
|
|||||||
events.push(Event::Changed);
|
events.push(Event::Changed);
|
||||||
return events;
|
return events;
|
||||||
}
|
}
|
||||||
|
// AbstractWidget accepts only the primary button. Rebinding above
|
||||||
|
// accepts other mouse buttons, but is not a widget activation.
|
||||||
|
if right {
|
||||||
|
return events;
|
||||||
|
}
|
||||||
for wd in widgets {
|
for wd in widgets {
|
||||||
if !ui::hovered((x, y), wd.x, wd.y, wd.w, wd.h) {
|
if !ui::hovered((x, y), wd.x, wd.y, wd.w, wd.h) {
|
||||||
continue;
|
continue;
|
||||||
@@ -1692,43 +1703,46 @@ impl OptionsUi {
|
|||||||
match wd.kind {
|
match wd.kind {
|
||||||
WKind::Button {
|
WKind::Button {
|
||||||
active: true, act, ..
|
active: true, act, ..
|
||||||
} => match act {
|
} => {
|
||||||
Act::Push(p) => self.push(p),
|
events.push(Event::ButtonClick);
|
||||||
Act::Done => events.extend(self.back(s)),
|
match act {
|
||||||
Act::Cycle(opt) => {
|
Act::Push(p) => self.push(p),
|
||||||
opt.cycle(s, right);
|
Act::Done => events.extend(self.back(s)),
|
||||||
if opt == Opt::Preset {
|
Act::Cycle(opt) => {
|
||||||
apply_preset(s);
|
opt.cycle(s, right);
|
||||||
|
if opt == Opt::Preset {
|
||||||
|
apply_preset(s);
|
||||||
|
}
|
||||||
|
events.push(Event::Changed);
|
||||||
}
|
}
|
||||||
events.push(Event::Changed);
|
Act::Key(name) => self.selected_key = Some(name),
|
||||||
}
|
Act::ResetKey(name) => {
|
||||||
Act::Key(name) => self.selected_key = Some(name),
|
s.keys.set(name, KeyBinds::default_of(name));
|
||||||
Act::ResetKey(name) => {
|
events.push(Event::Changed);
|
||||||
s.keys.set(name, KeyBinds::default_of(name));
|
|
||||||
events.push(Event::Changed);
|
|
||||||
}
|
|
||||||
Act::ResetKeys => {
|
|
||||||
s.keys.reset_all();
|
|
||||||
events.push(Event::Changed);
|
|
||||||
}
|
|
||||||
Act::PadKey(action) => self.selected_pad_action = Some(action),
|
|
||||||
Act::ResetPadKey(action) => {
|
|
||||||
s.controller
|
|
||||||
.bind(action, gamepad::PadSettings::default_of(action));
|
|
||||||
events.push(Event::Changed);
|
|
||||||
}
|
|
||||||
Act::ResetPadKeys => {
|
|
||||||
s.controller.reset_bindings();
|
|
||||||
events.push(Event::Changed);
|
|
||||||
}
|
|
||||||
Act::HorizonMenus => {
|
|
||||||
self.horizon_menus();
|
|
||||||
if let Ok(dir) = std::env::current_dir() {
|
|
||||||
crate::ui_theme::save_preferences(&dir);
|
|
||||||
}
|
}
|
||||||
|
Act::ResetKeys => {
|
||||||
|
s.keys.reset_all();
|
||||||
|
events.push(Event::Changed);
|
||||||
|
}
|
||||||
|
Act::PadKey(action) => self.selected_pad_action = Some(action),
|
||||||
|
Act::ResetPadKey(action) => {
|
||||||
|
s.controller
|
||||||
|
.bind(action, gamepad::PadSettings::default_of(action));
|
||||||
|
events.push(Event::Changed);
|
||||||
|
}
|
||||||
|
Act::ResetPadKeys => {
|
||||||
|
s.controller.reset_bindings();
|
||||||
|
events.push(Event::Changed);
|
||||||
|
}
|
||||||
|
Act::HorizonMenus => {
|
||||||
|
self.horizon_menus();
|
||||||
|
if let Ok(dir) = std::env::current_dir() {
|
||||||
|
crate::ui_theme::save_preferences(&dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Act::None => {}
|
||||||
}
|
}
|
||||||
Act::None => {}
|
}
|
||||||
},
|
|
||||||
WKind::Slider { opt, .. } => {
|
WKind::Slider { opt, .. } => {
|
||||||
self.dragging = Some(opt);
|
self.dragging = Some(opt);
|
||||||
opt.set_slider(s, ui::slider_value(wd.x, wd.w, x));
|
opt.set_slider(s, ui::slider_value(wd.x, wd.w, x));
|
||||||
@@ -1763,10 +1777,17 @@ impl OptionsUi {
|
|||||||
self.section_shown = None;
|
self.section_shown = None;
|
||||||
self.hscroll = 0.0;
|
self.hscroll = 0.0;
|
||||||
}
|
}
|
||||||
pub fn release(&mut self) {
|
pub fn release(&mut self) -> Vec<Event> {
|
||||||
self.dragging = None;
|
// AbstractSliderButton suppresses the press sound and plays once on
|
||||||
|
// release, even if the pointer has left the widget or its value stayed.
|
||||||
|
let slider = self.dragging.take().is_some();
|
||||||
self.hdragging = None;
|
self.hdragging = None;
|
||||||
self.graphics.release();
|
let graphics_slider = self.graphics.release();
|
||||||
|
if slider || graphics_slider {
|
||||||
|
vec![Event::ButtonClick]
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn scroll(&mut self, s: &Settings, size: (f32, f32), amount: f32) {
|
pub fn scroll(&mut self, s: &Settings, size: (f32, f32), amount: f32) {
|
||||||
let (_, top, bottom, content) = self.layout(s, size.0, size.1);
|
let (_, top, bottom, content) = self.layout(s, size.0, size.1);
|
||||||
@@ -1960,6 +1981,141 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::ui_theme::{MenuStyle, set_style, style};
|
use crate::ui_theme::{MenuStyle, set_style, style};
|
||||||
|
|
||||||
|
fn atlas() -> HudAtlas {
|
||||||
|
HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn classic_buttons_sound_but_disabled_and_nonprimary_clicks_are_silent() {
|
||||||
|
let atlas = atlas();
|
||||||
|
let mut s = Settings::default();
|
||||||
|
let mut ui = OptionsUi::default();
|
||||||
|
ui.show(Page::Mouse);
|
||||||
|
let size = (480.0, 320.0);
|
||||||
|
let (widgets, _, _, _) = ui.layout(&s, size.0, size.1);
|
||||||
|
let button = widgets
|
||||||
|
.iter()
|
||||||
|
.find(|wd| {
|
||||||
|
matches!(
|
||||||
|
wd.kind,
|
||||||
|
WKind::Button {
|
||||||
|
active: true,
|
||||||
|
act: Act::Cycle(_),
|
||||||
|
..
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let (x, y) = (button.x + 1.0, button.y + 1.0);
|
||||||
|
assert!(ui.click(&mut s, &atlas, x, y, size, false, true).is_empty());
|
||||||
|
assert_eq!(
|
||||||
|
ui.click(&mut s, &atlas, x, y, size, false, false),
|
||||||
|
vec![Event::ButtonClick, Event::Changed]
|
||||||
|
);
|
||||||
|
assert!(ui.release().is_empty());
|
||||||
|
assert!(
|
||||||
|
ui.click(&mut s, &atlas, 0.0, 0.0, size, false, false)
|
||||||
|
.is_empty()
|
||||||
|
);
|
||||||
|
ui.show(Page::KeyBinds);
|
||||||
|
let (widgets, _, _, _) = ui.layout(&s, size.0, size.1);
|
||||||
|
let disabled = widgets
|
||||||
|
.iter()
|
||||||
|
.find(|wd| matches!(wd.kind, WKind::Button { active: false, .. }))
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
ui.click(
|
||||||
|
&mut s,
|
||||||
|
&atlas,
|
||||||
|
disabled.x + 1.0,
|
||||||
|
disabled.y + 1.0,
|
||||||
|
size,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
.is_empty()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn classic_sliders_sound_once_on_release_including_unchanged_values() {
|
||||||
|
let atlas = atlas();
|
||||||
|
let size = (480.0, 320.0);
|
||||||
|
let mut s = Settings::default();
|
||||||
|
let mut ui = OptionsUi::default();
|
||||||
|
ui.show(Page::Sound);
|
||||||
|
let (widgets, _, _, _) = ui.layout(&s, size.0, size.1);
|
||||||
|
let slider = widgets
|
||||||
|
.iter()
|
||||||
|
.find(|wd| {
|
||||||
|
matches!(
|
||||||
|
wd.kind,
|
||||||
|
WKind::Slider {
|
||||||
|
opt: Opt::Volume(0),
|
||||||
|
..
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let (x, y) = (slider.x + slider.w - 1.0, slider.y + 1.0);
|
||||||
|
assert_eq!(
|
||||||
|
ui.click(&mut s, &atlas, x, y, size, false, false),
|
||||||
|
vec![Event::Changed]
|
||||||
|
);
|
||||||
|
assert_eq!(s.master_volume, 1.0);
|
||||||
|
assert_eq!(ui.release(), vec![Event::ButtonClick]);
|
||||||
|
assert!(ui.release().is_empty());
|
||||||
|
assert_eq!(
|
||||||
|
ui.click(&mut s, &atlas, x, y, size, false, false),
|
||||||
|
vec![Event::Changed]
|
||||||
|
);
|
||||||
|
for cx in [-100.0, 250.0, 1000.0] {
|
||||||
|
assert_eq!(
|
||||||
|
ui.click(&mut s, &atlas, cx, -100.0, size, true, false),
|
||||||
|
vec![Event::Changed]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert_eq!(ui.release(), vec![Event::ButtonClick]);
|
||||||
|
assert!(ui.click(&mut s, &atlas, x, y, size, true, false).is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn classic_language_rows_and_text_fields_are_silent() {
|
||||||
|
let atlas = atlas();
|
||||||
|
let mut s = Settings::default();
|
||||||
|
let mut ui = OptionsUi {
|
||||||
|
languages: languages(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
ui.show(Page::Language);
|
||||||
|
let size = (480.0, 320.0);
|
||||||
|
let (widgets, top, bottom, _) = ui.layout(&s, size.0, size.1);
|
||||||
|
let row = widgets
|
||||||
|
.iter()
|
||||||
|
.find(|wd| {
|
||||||
|
matches!(wd.kind, WKind::LanguageRow { .. }) && wd.y >= top && wd.y + wd.h <= bottom
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
ui.click(&mut s, &atlas, row.x + 1.0, row.y + 1.0, size, false, false)
|
||||||
|
.is_empty()
|
||||||
|
);
|
||||||
|
assert!(ui.release().is_empty());
|
||||||
|
let search = ui.language_search.clone();
|
||||||
|
assert!(
|
||||||
|
ui.click(
|
||||||
|
&mut s,
|
||||||
|
&atlas,
|
||||||
|
search.x + 1.0,
|
||||||
|
search.y + 1.0,
|
||||||
|
size,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
.is_empty()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
fn languages() -> Vec<LanguageInfo> {
|
fn languages() -> Vec<LanguageInfo> {
|
||||||
(0..30)
|
(0..30)
|
||||||
.map(|i| LanguageInfo {
|
.map(|i| LanguageInfo {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ pub enum Compat {
|
|||||||
TooNew,
|
TooNew,
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Compat {
|
impl Compat {
|
||||||
fn key(self) -> &'static str {
|
fn key(self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
@@ -457,6 +458,18 @@ impl PackScreen {
|
|||||||
self.info(id).is_none_or(|p| !p.fixed || !p.required)
|
self.info(id).is_none_or(|p| !p.fixed || !p.required)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether a left click activates a footer or confirmation button.
|
||||||
|
/// Pack entries and the search field do not play widget click sounds.
|
||||||
|
pub fn button_at(&self, m: &Measure, x: f64, y: f64) -> bool {
|
||||||
|
let buttons = if let Some(c) = &self.confirm {
|
||||||
|
self.confirm_layout(m, c).2
|
||||||
|
} else {
|
||||||
|
self.footer()
|
||||||
|
};
|
||||||
|
buttons.into_iter().any(|(bx, by)| {
|
||||||
|
x >= bx as f64 && y >= by as f64 && x < (bx + 150) as f64 && y < (by + 20) as f64
|
||||||
|
})
|
||||||
|
}
|
||||||
/// A left click (`Screen.mouseClicked`).
|
/// A left click (`Screen.mouseClicked`).
|
||||||
pub fn click(&mut self, m: &Measure, x: f64, y: f64) -> Action {
|
pub fn click(&mut self, m: &Measure, x: f64, y: f64) -> Action {
|
||||||
if self.confirm.is_some() {
|
if self.confirm.is_some() {
|
||||||
@@ -928,3 +941,58 @@ impl PackScreen {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod click_sound_tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn only_footer_and_confirmation_buttons_request_a_click_sound() {
|
||||||
|
let atlas =
|
||||||
|
crate::hud::HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets"))
|
||||||
|
.unwrap();
|
||||||
|
let m = Measure(&atlas.font);
|
||||||
|
let mut screen = PackScreen::new(&m, 480, 320, false, Vec::new(), &[]);
|
||||||
|
assert!(!screen.button_at(&m, 0.0, 0.0));
|
||||||
|
assert!(!screen.button_at(
|
||||||
|
&m,
|
||||||
|
screen.search.x as f64 + 1.0,
|
||||||
|
screen.search.y as f64 + 1.0
|
||||||
|
));
|
||||||
|
let buttons = screen.footer();
|
||||||
|
for (i, (x, y)) in buttons.into_iter().enumerate() {
|
||||||
|
assert!(screen.button_at(&m, x as f64 + 1.0, y as f64 + 1.0));
|
||||||
|
assert!(!screen.button_at(&m, (x + 150) as f64, (y + 20) as f64));
|
||||||
|
assert_eq!(
|
||||||
|
screen.click(&m, x as f64 + 1.0, y as f64 + 1.0),
|
||||||
|
if i == 0 {
|
||||||
|
Action::OpenFolder
|
||||||
|
} else {
|
||||||
|
Action::Done
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
screen.confirm = Some(Confirm {
|
||||||
|
id: "incompatible".into(),
|
||||||
|
title: Text::literal("Confirm"),
|
||||||
|
message: Text::literal("Use this pack?"),
|
||||||
|
});
|
||||||
|
let buttons = screen
|
||||||
|
.confirm_layout(&m, screen.confirm.as_ref().unwrap())
|
||||||
|
.2;
|
||||||
|
for (x, y) in buttons {
|
||||||
|
assert!(screen.button_at(&m, x as f64 + 1.0, y as f64 + 1.0));
|
||||||
|
}
|
||||||
|
assert!(!screen.button_at(
|
||||||
|
&m,
|
||||||
|
screen.footer()[0].0 as f64 + 1.0,
|
||||||
|
screen.footer()[0].1 as f64 + 1.0
|
||||||
|
));
|
||||||
|
let (x, y) = buttons[1];
|
||||||
|
assert_eq!(
|
||||||
|
screen.click(&m, x as f64 + 1.0, y as f64 + 1.0),
|
||||||
|
Action::None
|
||||||
|
);
|
||||||
|
assert!(!screen.confirming());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -447,13 +447,21 @@ impl Title {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn click(&mut self, x: f32, y: f32, size: (f32, f32)) -> Action {
|
/// The action and whether to play `AbstractWidget.playButtonClickSound`.
|
||||||
|
/// Navigation buttons can return `Action::None` while still sounding.
|
||||||
|
pub fn click(&mut self, x: f32, y: f32, size: (f32, f32)) -> (Action, bool) {
|
||||||
let (w, h) = size;
|
let (w, h) = size;
|
||||||
let hit = self
|
let hit = self
|
||||||
.buttons(w, h)
|
.buttons(w, h)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find(|b| b.enabled && x >= b.x && x < b.x + b.w && y >= b.y && y < b.y + 20.0)
|
.find(|b| b.enabled && x >= b.x && x < b.x + b.w && y >= b.y && y < b.y + 20.0)
|
||||||
.map(|b| b.id);
|
.map(|b| b.id);
|
||||||
|
let action = self.click_action(x, y, size, hit);
|
||||||
|
// World list selection is silent; launching its selected entry sounds.
|
||||||
|
let sound = hit.is_some() || matches!(action, Action::Play { .. });
|
||||||
|
(action, sound)
|
||||||
|
}
|
||||||
|
fn click_action(&mut self, x: f32, y: f32, (w, h): (f32, f32), hit: Option<u8>) -> Action {
|
||||||
match (&mut self.mode, hit) {
|
match (&mut self.mode, hit) {
|
||||||
(Mode::Main, Some(1)) => {
|
(Mode::Main, Some(1)) => {
|
||||||
self.mode = Mode::Select {
|
self.mode = Mode::Select {
|
||||||
@@ -635,3 +643,62 @@ fn date_string(secs: u64) -> String {
|
|||||||
let y = if mo <= 2 { y + 1 } else { y };
|
let y = if mo <= 2 { y + 1 } else { y };
|
||||||
format!("{y}-{mo:02}-{d:02} {h:02}:{m:02}")
|
format!("{y}-{mo:02}-{d:02} {h:02}:{m:02}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn click_button(title: &mut Title, id: u8) -> (Action, bool) {
|
||||||
|
let b = title
|
||||||
|
.buttons(480.0, 320.0)
|
||||||
|
.into_iter()
|
||||||
|
.find(|b| b.id == id)
|
||||||
|
.unwrap();
|
||||||
|
title.click(b.x + 1.0, b.y + 1.0, (480.0, 320.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn classic_navigation_buttons_sound_even_without_a_game_action() {
|
||||||
|
let mut title = Title::new(PathBuf::from("/nonexistent/horizon-test-saves"));
|
||||||
|
assert_eq!(title.click(0.0, 0.0, (480.0, 320.0)), (Action::None, false));
|
||||||
|
assert_eq!(click_button(&mut title, 0), (Action::None, false)); // Disabled Realms.
|
||||||
|
assert_eq!(click_button(&mut title, 1), (Action::None, true)); // Singleplayer.
|
||||||
|
assert_eq!(click_button(&mut title, 10), (Action::None, false)); // No selected world.
|
||||||
|
assert_eq!(click_button(&mut title, 11), (Action::None, true)); // Create screen.
|
||||||
|
assert_eq!(
|
||||||
|
title.click(240.0, 55.0, (480.0, 320.0)),
|
||||||
|
(Action::None, false)
|
||||||
|
);
|
||||||
|
assert_eq!(click_button(&mut title, 20), (Action::None, true)); // Game mode.
|
||||||
|
assert_eq!(click_button(&mut title, 22), (Action::None, true)); // Cancel.
|
||||||
|
assert_eq!(click_button(&mut title, 12), (Action::None, true)); // Back.
|
||||||
|
assert_eq!(click_button(&mut title, 3), (Action::Options, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn classic_world_selection_is_silent_until_the_entry_launches() {
|
||||||
|
let mut title = Title::new(PathBuf::from("/nonexistent/horizon-test-saves"));
|
||||||
|
title.mode = Mode::Select {
|
||||||
|
worlds: vec![WorldEntry {
|
||||||
|
folder: "world".into(),
|
||||||
|
name: "World".into(),
|
||||||
|
creative: false,
|
||||||
|
modified: std::time::SystemTime::UNIX_EPOCH,
|
||||||
|
}],
|
||||||
|
selected: None,
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
title.click(240.0, 40.0, (480.0, 320.0)),
|
||||||
|
(Action::None, false)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
title.click(240.0, 40.0, (480.0, 320.0)),
|
||||||
|
(
|
||||||
|
Action::Play {
|
||||||
|
folder: "world".into()
|
||||||
|
},
|
||||||
|
true
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user