diff --git a/crates/horizon-client/src/front.rs b/crates/horizon-client/src/front.rs index 7b1771000..5b0a2c5ab 100644 --- a/crates/horizon-client/src/front.rs +++ b/crates/horizon-client/src/front.rs @@ -318,6 +318,7 @@ impl Title { x: f32, y: f32, ) -> Action { + self.remember_direct_address(); if self.skip_opening() { return Action::None; } @@ -472,7 +473,7 @@ impl Title { _ => None, }; if let Some(id) = id { - match screen.click(Some(id), 0.0, 0.0, (0.0, 0.0), &saves) { + match self.multiplayer_click(Some(id), 0.0, 0.0, (0.0, 0.0)) { Outcome::Back => self.mode = Mode::Main, Outcome::Connect(address) => return Action::Connect { address }, Outcome::None => {} @@ -1432,6 +1433,28 @@ mod tests { } } + #[test] + fn horizon_direct_connection_remembers_the_address_after_leaving() { + let atlas = + HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap(); + let saves = + std::env::temp_dir().join(format!("horizon-front-direct-{}", std::process::id())); + let mut title = Title::new(saves.clone(), &saves); + title.last_server = "localhost:25566".into(); + title.mode = Mode::Multiplayer(Screen::list(&saves)); + press(&mut title, &atlas, DIRECT); + assert_eq!(title.focused_field().unwrap().1, "localhost:25566"); + *title.focused_field().unwrap().1 = "example.org:25567".into(); + press(&mut title, &atlas, BACK); + press(&mut title, &atlas, DIRECT); + assert_eq!(title.focused_field().unwrap().1, "example.org:25567"); + *title.focused_field().unwrap().1 = "localhost:25568".into(); + press(&mut title, &atlas, NAV); + press(&mut title, &atlas, NAV + 2); + press(&mut title, &atlas, DIRECT); + assert_eq!(title.focused_field().unwrap().1, "localhost:25568"); + } + #[test] fn horizon_world_delete_confirms_and_keeps_the_selection_in_place() { let atlas = diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index b640c789d..e552eb118 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -6637,6 +6637,9 @@ impl Game<'_> { std::env::current_dir()?.join("saves"), &self.options.resources, )); + if let Some(title) = &mut self.title { + title.last_server = self.settings.file.string("lastServer"); + } self.capture(false); Ok(()) } @@ -9304,6 +9307,7 @@ impl ApplicationHandler for Game<'_> { clicked } }); + self.save_title_options(); if sound { self.audio.play_button_click(); } @@ -9542,6 +9546,7 @@ impl ApplicationHandler for Game<'_> { if let Some(t) = self.title.as_mut() { t.escape(); } + self.save_title_options(); } _ => self.title_edit(event.text.as_deref(), false), } @@ -10325,10 +10330,11 @@ pub fn run() -> Result<()> { game.open_world(&folder, None, Some(seed), creative, None)?; } None => { - let title = Title::new( + let mut title = Title::new( std::env::current_dir()?.join("saves"), &game.options.resources, ); + title.last_server = game.settings.file.string("lastServer"); // The launcher's opening, once per session and only in Horizon's style. if horizon_client::ui_theme::horizon() && game.options.frames.is_none() { title diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs index 25eb6eb96..dcc7bb23b 100644 --- a/crates/horizon-client/src/game/options.rs +++ b/crates/horizon-client/src/game/options.rs @@ -17,6 +17,17 @@ pub(super) const CONTROL_HELP: &[&str] = &[ ]; impl Game<'_> { + pub(super) fn save_title_options(&mut self) { + if let Some(title) = &self.title + && title.last_server != self.settings.file.string("lastServer") + { + self.settings.file.set( + "lastServer", + horizon_client::options_file::V::Str(title.last_server.clone()), + ); + self.settings.save(&Self::options_path()); + } + } /// `options-get` and `options-set`; None for other commands. pub(super) fn options_control(&mut self, words: &[&str], line: &str) -> Result> { match words[0] { diff --git a/crates/horizon-client/src/game/pad.rs b/crates/horizon-client/src/game/pad.rs index cae85bc6c..62d481b11 100644 --- a/crates/horizon-client/src/game/pad.rs +++ b/crates/horizon-client/src/game/pad.rs @@ -354,6 +354,7 @@ impl Game<'_> { } if let Some(title) = self.title.as_mut() { title.escape(); + self.save_title_options(); return Ok(()); } if self.sign_edit.is_some() { diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index a7b23bf32..0db8cca30 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -343,6 +343,7 @@ impl Game<'_> { } let saves = std::env::current_dir()?.join("saves"); let mut title = horizon_client::title::Title::new(saves.clone(), &self.options.resources); + title.last_server = self.settings.file.string("lastServer"); if single_player && reason.is_none() { self.title = Some(title); self.capture(false); diff --git a/crates/horizon-client/src/title.rs b/crates/horizon-client/src/title.rs index a28f401ea..172fb821a 100644 --- a/crates/horizon-client/src/title.rs +++ b/crates/horizon-client/src/title.rs @@ -125,6 +125,8 @@ pub enum Action { } pub struct Title { pub mode: Mode, + /// Direct Connection's address, synchronized with `options.txt`'s `lastServer`. + pub last_server: String, pub saves: PathBuf, pub started: std::time::Instant, search: String, @@ -232,6 +234,7 @@ impl Title { Self { shown: std::cell::Cell::new((std::mem::discriminant(&mode), now)), mode, + last_server: String::new(), saves, started: now, search: String::new(), @@ -1162,6 +1165,7 @@ impl Title { /// 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) { + self.remember_direct_address(); let (w, h) = (size.0.ceil(), size.1.ceil()); // MenuTabBar: a new tab clears the focus and clicks. if let Mode::Create { focus, options, .. } = &mut self.mode @@ -1195,16 +1199,14 @@ impl Title { self.mode = Mode::Multiplayer(crate::multiplayer::Screen::list(&self.saves)); Action::None } - (Mode::Multiplayer(screen), hit) => { - match screen.click(hit, x, y, (w, h), &self.saves) { - crate::multiplayer::Outcome::Back => { - self.mode = Mode::Main; - Action::None - } - crate::multiplayer::Outcome::Connect(address) => Action::Connect { address }, - crate::multiplayer::Outcome::None => Action::None, + (Mode::Multiplayer(_), hit) => match self.multiplayer_click(hit, x, y, (w, h)) { + crate::multiplayer::Outcome::Back => { + self.mode = Mode::Main; + Action::None } - } + crate::multiplayer::Outcome::Connect(address) => Action::Connect { address }, + crate::multiplayer::Outcome::None => Action::None, + }, (Mode::Main, Some(3)) => Action::Options, (Mode::Main, Some(7)) => Action::Accessibility, (Mode::Main, Some(6)) => Action::Language, @@ -1512,7 +1514,31 @@ impl Title { _ => None, } } + pub(crate) fn remember_direct_address(&mut self) { + if let Mode::Multiplayer(crate::multiplayer::Screen::Direct { address }) = &self.mode { + self.last_server.clone_from(address); + } + } + pub(crate) fn multiplayer_click( + &mut self, + hit: Option, + x: f32, + y: f32, + size: (f32, f32), + ) -> crate::multiplayer::Outcome { + let Mode::Multiplayer(screen) = &mut self.mode else { + return crate::multiplayer::Outcome::None; + }; + let outcome = screen.click(hit, x, y, size, &self.saves); + if hit == Some(41) + && let crate::multiplayer::Screen::Direct { address } = screen + { + address.clone_from(&self.last_server); + } + outcome + } pub fn escape(&mut self) { + self.remember_direct_address(); if let Mode::Multiplayer(screen) = &mut self.mode && !screen.escape(&self.saves) { diff --git a/crates/horizon-client/src/title/tests.rs b/crates/horizon-client/src/title/tests.rs index 4f759f960..572be2ef2 100644 --- a/crates/horizon-client/src/title/tests.rs +++ b/crates/horizon-client/src/title/tests.rs @@ -82,6 +82,41 @@ impl Drop for Fixture { } } +#[test] +fn direct_connection_remembers_the_address_on_cancel_escape_and_join() { + let mut f = Fixture::new(); + f.title.last_server = "localhost:25566".into(); + f.click("Multiplayer"); + f.click("Direct Connection"); + assert_eq!(f.title.focused_field().unwrap().1, "localhost:25566"); + + *f.title.focused_field().unwrap().1 = "example.org:25567".into(); + f.click("Cancel"); + f.click("Direct Connection"); + assert_eq!(f.title.focused_field().unwrap().1, "example.org:25567"); + + *f.title.focused_field().unwrap().1 = "[::1]:25568".into(); + f.title.escape(); + f.title.escape(); + f.click("Multiplayer"); + f.click("Direct Connection"); + assert_eq!(f.title.focused_field().unwrap().1, "[::1]:25568"); + + *f.title.focused_field().unwrap().1 = "localhost:25569".into(); + assert_eq!( + f.click("Join Server"), + Action::Connect { + address: "localhost:25569".into(), + } + ); + assert_eq!(f.title.last_server, "localhost:25569"); + + f.title.focused_field().unwrap().1.clear(); + f.title.escape(); + f.click("Direct Connection"); + assert!(f.title.focused_field().unwrap().1.is_empty()); +} + #[test] fn empty_singleplayer_opens_creation_and_cancel_returns_to_title() { let mut f = Fixture::new();