diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 709160886..e16c1043e 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3665,6 +3665,9 @@ impl Game<'_> { } if let Some(title) = &mut self.title { title.poll_backup(); + if let Some(sound) = title.update_toast(self.settings.notification_display_time) { + self.audio.play(sound, None, 1.0, 1.0); + } } if let Some(title) = &self.title { if horizon_client::ui_theme::horizon() { diff --git a/crates/horizon-client/src/game/advancements.rs b/crates/horizon-client/src/game/advancements.rs index 4c5c0fbbe..3a5711e41 100644 --- a/crates/horizon-client/src/game/advancements.rs +++ b/crates/horizon-client/src/game/advancements.rs @@ -7,6 +7,7 @@ use horizon_client::{ hud::HudBatch, items::ItemStack, text::{self, Style, Text}, + toasts::Slide, }; use serde_json::Value; use std::{ @@ -68,51 +69,6 @@ impl Toast { (fully_for as f64 / (5000.0 * multiplier / n).max(1.0) % n) as usize } } -/// `ToastManager.ToastInstance`: slides a toast in and out over 600 ms, -/// eased by the square, and counts how long it has been fully shown. -#[derive(Default)] -struct Slide { - /// When it first updated; the times below are milliseconds since. - origin: Option, - begun: bool, - /// `animationStartTime`. - start: i64, - /// `becameFullyVisibleAt`. - fully_at: i64, - show: bool, - /// `visiblePortion`: 0 off screen, 1 fully in. - portion: f32, - /// `fullyVisibleFor`. - fully_for: i64, -} -impl Slide { - const MS: i64 = 600; - /// `ToastInstance.update` at `now`; `wants_show` is the toast's own - /// update. The visibility it switched to (the sound to play), and whether - /// it has finished sliding out. - fn update(&mut self, now: i64, wants_show: impl FnOnce(i64) -> bool) -> (Option, bool) { - let was = self.show; - if !self.begun { - self.begun = true; - self.start = now; - self.show = true; - } - if self.show && now - self.start <= Self::MS { - self.fully_at = now; - } - self.fully_for = now - self.fully_at; - let progress = ((now - self.start) as f32 / Self::MS as f32).clamp(0.0, 1.0); - let progress = progress * progress; - self.portion = if self.show { progress } else { 1.0 - progress }; - let wanted = wants_show(self.fully_for); - if wanted != self.show { - self.start = now - ((1.0 - self.portion) * Self::MS as f32) as i64; - self.show = wanted; - } - let finished = !self.show && now - self.start > Self::MS; - ((self.show != was).then_some(self.show), finished) - } -} #[derive(Default)] pub(super) struct AdvState { pub(super) tree: Arc, diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index b12758f5a..2df795b88 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -167,6 +167,7 @@ pub mod text; pub mod tick_chunk_membership; pub mod title; pub mod title_edition; +pub mod toasts; pub mod ui; pub mod ui_theme; pub mod vanillagen; diff --git a/crates/horizon-client/src/title.rs b/crates/horizon-client/src/title.rs index 39adf5e74..31a51939b 100644 --- a/crates/horizon-client/src/title.rs +++ b/crates/horizon-client/src/title.rs @@ -136,7 +136,8 @@ pub struct Title { message_lines: Cell, backup: Option>>, backup_shown: Cell, - toast: Option<(std::time::Instant, BackupToast)>, + /// EditWorldScreen's `SystemToast` (`WORLD_BACKUP`) and its slide. + toast: Option<(crate::toasts::Slide, BackupToast)>, icons: RefCell>)>>, splash: Option, /// The mode last drawn and when it appeared (Horizon's screen fades). @@ -424,9 +425,28 @@ impl Title { Ok(bytes) => BackupToast::Created { folder, bytes }, Err(error) => BackupToast::Failed(error), }; - self.toast = Some((std::time::Instant::now(), toast)); + self.toast = Some((crate::toasts::Slide::default(), toast)); } } + /// `ToastManager.update` for the system toast: slides it in, keeps it + /// for 5 s times the notification display time, slides it out. The + /// sound of a new visibility (`Toast.Visibility`), if any. + pub fn update_toast(&mut self, multiplier: f32) -> Option<&'static str> { + let (slide, _) = self.toast.as_mut()?; + // SystemToast.update: shown until displayTime (5000 ms) has passed. + let time = 5000.0 * f64::from(multiplier); + let (sound, finished) = slide.update_now(|fully| (fully as f64) < time); + if finished { + self.toast = None; + } + sound.map(|show| { + if show { + "minecraft:ui.toast.in" + } else { + "minecraft:ui.toast.out" + } + }) + } fn start_backup(&mut self, world: WorldEntry) { let (send, receive) = mpsc::channel(); let saves = self.saves.clone(); @@ -1123,25 +1143,18 @@ impl Title { if let Mode::Multiplayer(screen) = &self.mode { screen.draw_tooltip(atlas, batch, cursor); } - if let Some((at, toast)) = &self.toast - && at.elapsed().as_secs() < 5 - { + if let Some((slide, toast)) = &self.toast { let (title, message) = toast.text(); let width = HudBatch::text_width(atlas, &title) .max(HudBatch::text_width(atlas, &message)) .ceil() .max(160.0) + 30.0; - toast_background(atlas, batch, w - width, width); - batch.text( - atlas, - &title, - w - width + 18.0, - 7.0, - rgba(255, 255, 0, 255), - false, - ); - batch.text(atlas, &message, w - width + 18.0, 19.0, u32::MAX, false); + // Toast.xPos: in from the right edge by the visible portion. + let x = w - width * slide.portion; + toast_background(atlas, batch, x, width); + batch.text(atlas, &title, x + 18.0, 7.0, rgba(255, 255, 0, 255), false); + batch.text(atlas, &message, x + 18.0, 19.0, u32::MAX, false); } } /// The action and whether to play `AbstractWidget.playButtonClickSound`. diff --git a/crates/horizon-client/src/title/tests.rs b/crates/horizon-client/src/title/tests.rs index fdcd98a68..c994885d8 100644 --- a/crates/horizon-client/src/title/tests.rs +++ b/crates/horizon-client/src/title/tests.rs @@ -373,6 +373,38 @@ fn stopped_backup_worker_returns_to_the_list_with_an_error_toast() { ); } +#[test] +fn backup_toast_slides_in_and_out_with_its_sounds() { + let mut f = Fixture::new(); + f.world("World", "World"); + f.select(); + f.click("Edit"); + f.click("Make Backup"); + f.title.poll_backup(); + f.finish_backup(); + assert_eq!(f.title.update_toast(1.0), Some("minecraft:ui.toast.in")); + assert_eq!(f.title.update_toast(1.0), None); + let age = |f: &mut Fixture, ms: u64| { + let (slide, _) = f.title.toast.as_mut().unwrap(); + let origin = slide.origin.as_mut().unwrap(); + *origin -= std::time::Duration::from_millis(ms); + }; + // Half way in: (300 / 600)² of its width shows. + age(&mut f, 300); + assert_eq!(f.title.update_toast(1.0), None); + let portion = f.title.toast.as_ref().unwrap().0.portion; + assert!((portion - 0.25).abs() < 0.02, "{portion}"); + // Shown in full for the display time, then out. + age(&mut f, 300); + f.title.update_toast(1.0); + age(&mut f, 5100); + assert_eq!(f.title.update_toast(1.0), Some("minecraft:ui.toast.out")); + assert!(f.title.toast.is_some()); + age(&mut f, 700); + assert_eq!(f.title.update_toast(1.0), None); + assert!(f.title.toast.is_none()); +} + #[test] fn failed_operations_show_error_and_leave_world_intact() { let mut f = Fixture::new(); diff --git a/crates/horizon-client/src/toasts.rs b/crates/horizon-client/src/toasts.rs new file mode 100644 index 000000000..26807100a --- /dev/null +++ b/crates/horizon-client/src/toasts.rs @@ -0,0 +1,58 @@ +//! `ToastManager`'s slide, shared by the game's toasts and the title +//! screens' system toasts. +use std::time::Instant; + +/// `ToastManager.ToastInstance`: slides a toast in and out over 600 ms, +/// eased by the square, and counts how long it has been fully shown. +#[derive(Default)] +pub struct Slide { + /// When it first updated; the times below are milliseconds since. + pub origin: Option, + begun: bool, + /// `animationStartTime`. + start: i64, + /// `becameFullyVisibleAt`. + fully_at: i64, + show: bool, + /// `visiblePortion`: 0 off screen, 1 fully in. + pub portion: f32, + /// `fullyVisibleFor`. + pub fully_for: i64, +} +impl Slide { + pub const MS: i64 = 600; + /// `ToastInstance.update` at `now`; `wants_show` is the toast's own + /// update. The visibility it switched to (the sound to play), and whether + /// it has finished sliding out. + pub fn update( + &mut self, + now: i64, + wants_show: impl FnOnce(i64) -> bool, + ) -> (Option, bool) { + let was = self.show; + if !self.begun { + self.begun = true; + self.start = now; + self.show = true; + } + if self.show && now - self.start <= Self::MS { + self.fully_at = now; + } + self.fully_for = now - self.fully_at; + let progress = ((now - self.start) as f32 / Self::MS as f32).clamp(0.0, 1.0); + let progress = progress * progress; + self.portion = if self.show { progress } else { 1.0 - progress }; + let wanted = wants_show(self.fully_for); + if wanted != self.show { + self.start = now - ((1.0 - self.portion) * Self::MS as f32) as i64; + self.show = wanted; + } + let finished = !self.show && now - self.start > Self::MS; + ((self.show != was).then_some(self.show), finished) + } + /// `update` at the time since the first one. + pub fn update_now(&mut self, wants_show: impl FnOnce(i64) -> bool) -> (Option, bool) { + let origin = *self.origin.get_or_insert_with(Instant::now); + self.update(origin.elapsed().as_millis() as i64, wants_show) + } +}