diff --git a/crates/horizon-client/src/confirm_screen.rs b/crates/horizon-client/src/confirm_screen.rs index c6fda8236..5e3f4d979 100644 --- a/crates/horizon-client/src/confirm_screen.rs +++ b/crates/horizon-client/src/confirm_screen.rs @@ -15,33 +15,11 @@ pub struct Confirm { pub buttons: [(f32, f32); 2], } impl Confirm { - pub fn new(font: &Font, (w, h): (f32, f32), title: &Text, message: &Text) -> Self { - let message: Vec = font - .split_lines(message, w - 50.0) - .into_iter() - .map(|(line, _)| line) - .take(15) - .collect(); - let tw = font.width_text(title); - let mw = message - .iter() - .map(|l| font.width_text(l)) - .fold(0.0, f32::max); - let mh = 9.0 * message.len() as f32; - let col = tw.max(mw).max(304.0); - let total = 9.0 + 8.0 + mh + 8.0 + 36.0; - let (lx, ly) = (((w - col) / 2.0).trunc(), ((h - total) / 2.0).trunc()); - let cell = |cw: f32| lx + ((col - cw) / 2.0).trunc(); - let mid = cell(mw) + (mw / 2.0).trunc(); - let mut lines = vec![(title.clone(), cell(tw), ly)]; - for (i, line) in message.into_iter().enumerate() { - let x = mid - (font.width_text(&line) / 2.0).trunc(); - lines.push((line, x, ly + 17.0 + 9.0 * i as f32)); - } - let (bx, by) = (cell(304.0), ly + 9.0 + 8.0 + mh + 8.0 + 16.0); + pub fn new(font: &Font, size: (f32, f32), title: &Text, message: &Text) -> Self { + let Choice { lines, buttons, .. } = Choice::new(font, size, title, message, 2, 150.0); Self { lines, - buttons: [(bx, by), (bx + 154.0, by)], + buttons: [buttons[0], buttons[1]], } } /// The answer a click at `cursor` gives: true for yes, false for no. @@ -84,3 +62,119 @@ impl Confirm { } } } + +/// `ConfirmScreen` with a row of `count` buttons `width` wide, 4 apart: `ConfirmLinkScreen`'s +/// yes, copy and no are three of 100. `Confirm` is its two of 150. +pub struct Choice { + /// Each line and where it is drawn. + pub lines: Vec<(Text, f32, f32)>, + /// The buttons' corners, left to right. + pub buttons: Vec<(f32, f32)>, + pub width: f32, +} +impl Choice { + pub fn new( + font: &Font, + (w, h): (f32, f32), + title: &Text, + message: &Text, + count: usize, + width: f32, + ) -> Self { + let message: Vec = font + .split_lines(message, w - 50.0) + .into_iter() + .map(|(line, _)| line) + .take(15) + .collect(); + let tw = font.width_text(title); + let mw = message + .iter() + .map(|l| font.width_text(l)) + .fold(0.0, f32::max); + let mh = 9.0 * message.len() as f32; + let row = width * count as f32 + 4.0 * count.saturating_sub(1) as f32; + let col = tw.max(mw).max(row); + let total = 9.0 + 8.0 + mh + 8.0 + 36.0; + let (lx, ly) = (((w - col) / 2.0).trunc(), ((h - total) / 2.0).trunc()); + let cell = |cw: f32| lx + ((col - cw) / 2.0).trunc(); + let mid = cell(mw) + (mw / 2.0).trunc(); + let mut lines = vec![(title.clone(), cell(tw), ly)]; + for (i, line) in message.into_iter().enumerate() { + let x = mid - (font.width_text(&line) / 2.0).trunc(); + lines.push((line, x, ly + 17.0 + 9.0 * i as f32)); + } + let (bx, by) = (cell(row), ly + 9.0 + 8.0 + mh + 8.0 + 16.0); + Self { + lines, + buttons: (0..count) + .map(|i| (bx + (width + 4.0) * i as f32, by)) + .collect(), + width, + } + } + /// The button a click at `cursor` hits, counted from the left. + pub fn pressed_at(&self, cursor: (f32, f32)) -> Option { + self.buttons + .iter() + .position(|&(x, y)| ui::hovered(cursor, x, y, self.width, 20.0)) + } + /// The lines and the buttons, over whatever is behind. + pub fn draw( + &self, + batch: &mut HudBatch, + atlas: &HudAtlas, + cursor: (f32, f32), + labels: &[Text], + ) { + for (line, x, y) in &self.lines { + batch.text_styled(atlas, line, *x, *y, u32::MAX, true); + } + for (&(bx, by), label) in self.buttons.iter().zip(labels) { + let hover = ui::hovered(cursor, bx, by, self.width, 20.0); + ui::button(batch, atlas, bx, by, self.width, 20.0, label, true, hover); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::hud::Rect; + use std::{collections::HashMap, path::Path}; + + fn font() -> Font { + // The missing-glyph metrics let these tests run without game assets. + let rect = Rect { + u0: 0.0, + v0: 0.0, + u1: 1.0, + v1: 1.0, + w: 1, + h: 1, + }; + Font::load( + Path::new("/nonexistent-horizon-confirm-test-resources"), + &HashMap::new(), + &[], + (1, 1), + (0, 0), + rect, + ) + } + + #[test] + fn three_buttons_sit_four_apart_in_the_middle() { + let (title, message) = (Text::literal("Open?"), Text::literal("https://x")); + let c = Choice::new(&font(), (480.0, 270.0), &title, &message, 3, 100.0); + let xs: Vec = c.buttons.iter().map(|b| b.0).collect(); + // 3 x 100 + 2 x 4 = 308 wide, centred on 480. + assert_eq!(xs, vec![86.0, 190.0, 294.0]); + assert_eq!(c.pressed_at((191.0, c.buttons[1].1 + 1.0)), Some(1)); + assert_eq!(c.pressed_at((188.0, c.buttons[1].1 + 1.0)), None); + // Confirm keeps its two 150-wide buttons where they were. + let two = Confirm::new(&font(), (480.0, 270.0), &title, &message); + assert_eq!(two.buttons[0].0, 88.0); + assert_eq!(two.buttons[1].0, 242.0); + } +} diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index 49ad2be44..23161de81 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -808,16 +808,19 @@ impl Game<'_> { &self, url: &str, size: (f32, f32), - ) -> horizon_client::confirm_screen::Confirm { + ) -> horizon_client::confirm_screen::Choice { let warning = Text::translate("chat.link.warning", &[]).with_color(0xFF_CCCC); let message = Text::literal(url) .append(Text::literal("\n")) .append(warning); - horizon_client::confirm_screen::Confirm::new( + // ConfirmLinkScreen.addButtons: yes, copy and no, 100 wide each. + horizon_client::confirm_screen::Choice::new( &self.gui.atlas.font, size, &Text::translate("chat.link.confirm", &[]), &message, + 3, + 100.0, ) } pub(super) fn draw_link_prompt(&self, batch: &mut HudBatch) { @@ -827,12 +830,13 @@ impl Game<'_> { let atlas = &self.gui.atlas; let (w, h) = (batch.width, batch.height); horizon_client::ui::menu_background(batch, atlas, 0.0, 0.0, w, h, true); - let (yes, no) = ( + let labels = [ Text::translate("gui.yes", &[]), + Text::translate("chat.copy", &[]), Text::translate("gui.no", &[]), - ); + ]; self.link_prompt_layout(url, (w, h)) - .draw(batch, atlas, self.cursor, [&yes, &no]); + .draw(batch, atlas, self.cursor, &labels); } /// The prompt's answer: the link opens on yes; either way the chat is back. pub(super) fn answer_link_prompt(&mut self, yes: bool) { @@ -843,15 +847,20 @@ impl Game<'_> { } } pub(super) fn link_prompt_click(&mut self) { - let Some(url) = &self.link_prompt else { + let Some(url) = self.link_prompt.clone() else { return; }; - let answer = self - .link_prompt_layout(url, self.gui_size()) - .answer_at(self.cursor); - if let Some(yes) = answer { + let pressed = self + .link_prompt_layout(&url, self.gui_size()) + .pressed_at(self.cursor); + if let Some(button) = pressed { self.audio.play_button_click(); - self.answer_link_prompt(yes); + if button == LINK_COPY { + // ConfirmLinkScreen.copyToClipboard, then the callback's no. + use horizon_client::clipboard::Clipboard; + self.clipboard.set(&url); + } + self.answer_link_prompt(button == LINK_YES); } } /// `Screen.clickCommandAction`: run a clicked command as the player @@ -1210,3 +1219,7 @@ pub fn screenshot_name(dir: &std::path::Path) -> String { fn owner_permission(allow_commands: bool) -> u8 { if allow_commands { 4 } else { 0 } } + +/// `ConfirmLinkScreen`'s buttons, left to right: yes, copy, no. +const LINK_YES: usize = 0; +const LINK_COPY: usize = 1;