mirror of
https://github.com/horizon-rust/horizon.git
synced 2026-11-09 17:19:15 -10:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92dab6cafc | ||
|
|
e11233f2dc |
No files matched your search
@@ -171,7 +171,7 @@ impl OptionsUi {
|
||||
added::Row::Text { text, .. } => {
|
||||
out.extend(wrap(&text, 64).into_iter().map(Row::Note))
|
||||
}
|
||||
added::Row::Space(_) => continue,
|
||||
added::Row::Space(_) | added::Row::Field { .. } => continue,
|
||||
added::Row::Button {
|
||||
label,
|
||||
active,
|
||||
|
||||
@@ -7,6 +7,7 @@ use crate::lan::discovery::Scanner;
|
||||
use crate::text::{DARK_GRAY, GRAY, Text, format_translation};
|
||||
use crate::title::confirm_top;
|
||||
use horizon_protocol::servers::{self, ServerEntry};
|
||||
pub use page::Form;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
path::{Path, PathBuf},
|
||||
@@ -138,6 +139,8 @@ pub enum Screen {
|
||||
relay: String,
|
||||
focus: u8,
|
||||
},
|
||||
/// A page a crate registered at the title screen's Friends icon, as a form.
|
||||
Page(Form),
|
||||
Disconnected {
|
||||
reason: String,
|
||||
/// Auto-reconnect's next attempt, until it is cancelled.
|
||||
@@ -146,6 +149,21 @@ pub enum Screen {
|
||||
lines: std::cell::Cell<usize>,
|
||||
},
|
||||
}
|
||||
mod page;
|
||||
|
||||
/// What the title screen's Friends icon opens: the page a crate registered there
|
||||
/// (`Place::Title`), else Join a Friend when an invite code can be joined. None: it is inactive.
|
||||
pub fn title_entry() -> Option<Screen> {
|
||||
match page::title_page() {
|
||||
Some(page) => Some(Screen::Page(Form::open(page))),
|
||||
None => crate::net::joins_friends().then(|| Screen::friend(true)),
|
||||
}
|
||||
}
|
||||
/// Whether the title screen's Friends icon opens something ([`title_entry`]).
|
||||
pub fn has_title_entry() -> bool {
|
||||
page::title_page().is_some() || crate::net::joins_friends()
|
||||
}
|
||||
|
||||
/// What a click asks the title screen to do.
|
||||
pub enum Outcome {
|
||||
None,
|
||||
@@ -176,6 +194,7 @@ impl Screen {
|
||||
Self::Direct { .. } => "direct_connect",
|
||||
Self::ConfirmDelete { .. } => "server_delete",
|
||||
Self::Friend { .. } => "friends_join",
|
||||
Self::Page(form) => form.id(),
|
||||
Self::Disconnected { .. } => "disconnected",
|
||||
}
|
||||
}
|
||||
@@ -744,6 +763,7 @@ impl Screen {
|
||||
53,
|
||||
),
|
||||
],
|
||||
Self::Page(form) => form.buttons(w, h),
|
||||
Self::Friend { code, .. } => vec![
|
||||
b(
|
||||
half(w) - 100.0,
|
||||
@@ -864,6 +884,7 @@ impl Screen {
|
||||
]),
|
||||
)
|
||||
}
|
||||
Self::Page(form) => form.narration(),
|
||||
Self::Friend { .. } => ScreenInfo::new(
|
||||
"multiplayer/friend",
|
||||
get("horizon.multiplayer.join_friend", "Join a Friend"),
|
||||
@@ -1318,6 +1339,7 @@ impl Screen {
|
||||
centered(batch, &line.plain(), top + 17.0 + i as f32 * 9.0, u32::MAX);
|
||||
}
|
||||
}
|
||||
Self::Page(form) => form.draw(atlas, batch, caret, focused_at),
|
||||
Self::Friend {
|
||||
code, relay, focus, ..
|
||||
} => {
|
||||
@@ -1383,6 +1405,7 @@ impl Screen {
|
||||
/// selected server or LAN game, as a click on it does.
|
||||
pub fn confirm(&self) -> Outcome {
|
||||
match self {
|
||||
Self::Page(form) => form.confirm(),
|
||||
Self::Direct { address } if servers::parse_address(address).is_some() => {
|
||||
Outcome::Connect {
|
||||
address: direct_address(address),
|
||||
@@ -1431,6 +1454,9 @@ impl Screen {
|
||||
saves: &Path,
|
||||
) -> Outcome {
|
||||
let (w, h) = size;
|
||||
if let Self::Page(form) = self {
|
||||
return form.click(id, x, y);
|
||||
}
|
||||
match (&mut *self, id) {
|
||||
(
|
||||
Self::List {
|
||||
@@ -1823,6 +1849,7 @@ impl Screen {
|
||||
Self::Friend { focus: 0, .. } => 64.0,
|
||||
Self::Friend { focus: 1, .. } => 105.0,
|
||||
Self::Friend { .. } => return None,
|
||||
Self::Page(form) => return form.focused_box(),
|
||||
_ => return None,
|
||||
};
|
||||
Some([(w / 2.0).floor() - 100.0, y, 200.0, 20.0])
|
||||
@@ -1851,6 +1878,13 @@ impl Screen {
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
/// Whether a press off the buttons may land on one of its text fields.
|
||||
pub fn has_fields(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::Edit { .. } | Self::Friend { .. } | Self::Page(_)
|
||||
)
|
||||
}
|
||||
/// Join a friend, with the relay the player set before.
|
||||
pub fn friend(from_title: bool) -> Self {
|
||||
Self::Friend {
|
||||
@@ -1910,6 +1944,9 @@ impl Screen {
|
||||
}
|
||||
/// The focused text field, its id and maximum length (copy and paste).
|
||||
pub fn focused_text(&mut self) -> Option<(crate::clipboard::FieldId, &mut String, usize)> {
|
||||
if let Self::Page(form) = self {
|
||||
return form.focused_text();
|
||||
}
|
||||
let id = match self {
|
||||
Self::Edit { focus: 0, .. } => SERVER_NAME,
|
||||
Self::Edit { .. } => SERVER_ADDRESS,
|
||||
@@ -1934,6 +1971,9 @@ impl Screen {
|
||||
/// round to the first field. `button` is the focused button, if any; a field
|
||||
/// has the focus (`focus` below 2) only while no button does.
|
||||
pub fn tab_dir(&mut self, reverse: bool, buttons: &[u8], button: &mut Option<u8>) {
|
||||
if let Self::Page(form) = self {
|
||||
return form.tab_dir(reverse, buttons, button);
|
||||
}
|
||||
let (Self::Edit { focus, .. } | Self::Friend { focus, .. }) = self else {
|
||||
return;
|
||||
};
|
||||
@@ -1960,6 +2000,7 @@ impl Screen {
|
||||
pub fn escape(&mut self, saves: &Path) -> bool {
|
||||
match self {
|
||||
Self::List { .. }
|
||||
| Self::Page(_)
|
||||
| Self::Friend {
|
||||
from_title: true, ..
|
||||
} => true,
|
||||
|
||||
@@ -0,0 +1,467 @@
|
||||
//! A page a crate registers at `horizon_hooks::pages::Place::Title`, which the title screen's
|
||||
//! Friends icon opens: a form drawn as Join a Friend and `ManageServerScreen` are. The title at
|
||||
//! the top, then the rows from y 53 (a field's grey label, its 200-wide `EditBox` 11 below, the
|
||||
//! next 41 lower; text 10 a line), and the page's buttons in a column under them from a quarter
|
||||
//! of the height + 114, 24 apart, the one that leaves it last.
|
||||
//!
|
||||
//! The engine keeps each field's text while the form is open (the title screen's caret edits
|
||||
//! it, as every field of its own) and tells the page each change before anything else reaches
|
||||
//! it ([`Act::Edit`]), then Enter in a field ([`Act::Submit`]) and the presses. A page joins a
|
||||
//! server with `horizon_hooks::pages::connect`.
|
||||
use super::{ButtonSpec, Outcome, centered_x, half, quarter};
|
||||
use crate::hud::{HudAtlas, HudBatch, rgba};
|
||||
use horizon_hooks::pages::{self, Act, Page, Place, Row};
|
||||
use std::cell::RefCell;
|
||||
use std::time::Instant;
|
||||
|
||||
/// The first button's id; button `k` of the page is `FIRST + k`.
|
||||
const FIRST: u8 = 100;
|
||||
/// The text a line of the form wraps at.
|
||||
const WIDTH: f32 = 300.0;
|
||||
|
||||
/// The page the title screen's Friends icon opens: the first registered there.
|
||||
pub(super) fn title_page() -> Option<&'static dyn Page> {
|
||||
pages::engine::all()
|
||||
.iter()
|
||||
.copied()
|
||||
.find(|page| page.place() == Place::Title)
|
||||
}
|
||||
|
||||
/// An open form: the page, and what the engine keeps while it is open.
|
||||
#[derive(Clone)]
|
||||
pub struct Form {
|
||||
page: &'static dyn Page,
|
||||
/// Each field's text, in the order the rows have them.
|
||||
texts: Vec<String>,
|
||||
/// The text the page last heard of each field.
|
||||
sent: RefCell<Vec<String>>,
|
||||
/// The focused field; `texts.len()` (or more) while a button has the focus.
|
||||
pub(super) focus: usize,
|
||||
/// Each field's box where the last draw put it.
|
||||
boxes: RefCell<Vec<[f32; 4]>>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Form {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Form")
|
||||
.field("page", &self.page.id())
|
||||
.field("texts", &self.texts)
|
||||
.field("focus", &self.focus)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// A button of the form: its row (None: the one that leaves), label key, and whether it acts.
|
||||
struct Button {
|
||||
row: Option<usize>,
|
||||
label: &'static str,
|
||||
active: bool,
|
||||
half: bool,
|
||||
}
|
||||
|
||||
impl Form {
|
||||
/// The form of `page`, its fields holding the text its rows start with, the first focused.
|
||||
pub fn open(page: &'static dyn Page) -> Self {
|
||||
let mut rows = Vec::new();
|
||||
page.rows(&mut rows);
|
||||
let texts: Vec<String> = rows
|
||||
.iter()
|
||||
.filter_map(|row| match row {
|
||||
Row::Field { text, .. } => Some(text.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
Self {
|
||||
page,
|
||||
sent: RefCell::new(texts.clone()),
|
||||
boxes: RefCell::new(vec![[0.0; 4]; texts.len()]),
|
||||
texts,
|
||||
focus: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &'static str {
|
||||
self.page.id()
|
||||
}
|
||||
|
||||
/// The page's rows, after it heard every change to its fields.
|
||||
fn rows(&self) -> Vec<Row> {
|
||||
let mut rows = Vec::new();
|
||||
self.page.rows(&mut rows);
|
||||
let fields = field_rows(&rows);
|
||||
let mut sent = self.sent.borrow_mut();
|
||||
let mut told = false;
|
||||
for ((text, sent), row) in self.texts.iter().zip(sent.iter_mut()).zip(fields) {
|
||||
if text != sent {
|
||||
sent.clone_from(text);
|
||||
self.page.act(row, Act::Edit(text.clone()));
|
||||
told = true;
|
||||
}
|
||||
}
|
||||
if told {
|
||||
rows.clear();
|
||||
self.page.rows(&mut rows);
|
||||
}
|
||||
rows
|
||||
}
|
||||
|
||||
fn buttons_of(rows: &[Row], back: &'static str) -> Vec<Button> {
|
||||
let mut buttons: Vec<Button> = rows
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(r, row)| match row {
|
||||
Row::Button {
|
||||
label,
|
||||
active,
|
||||
half,
|
||||
..
|
||||
} => Some(Button {
|
||||
row: Some(r),
|
||||
label,
|
||||
active: *active,
|
||||
half: *half,
|
||||
}),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
buttons.push(Button {
|
||||
row: None,
|
||||
label: back,
|
||||
active: true,
|
||||
half: false,
|
||||
});
|
||||
buttons
|
||||
}
|
||||
|
||||
/// The id of the button that leaves: Cancel's or Done's, for the controller.
|
||||
fn back_id(&self) -> u8 {
|
||||
if self.page.back() == "gui.cancel" {
|
||||
57
|
||||
} else {
|
||||
50
|
||||
}
|
||||
}
|
||||
|
||||
pub fn buttons(&self, w: f32, h: f32) -> Vec<ButtonSpec> {
|
||||
let rows = self.rows();
|
||||
let cx = half(w);
|
||||
let mut y = quarter(h) + 114.0;
|
||||
let mut beside = false;
|
||||
let mut out = Vec::new();
|
||||
for (k, b) in Self::buttons_of(&rows, self.page.back())
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let id = match b.row {
|
||||
Some(_) => FIRST + k as u8,
|
||||
None => self.back_id(),
|
||||
};
|
||||
let label = crate::lang::get(b.label);
|
||||
let (x, bw) = match (b.half, beside) {
|
||||
(true, true) => {
|
||||
y -= 24.0;
|
||||
beside = false;
|
||||
(cx + 2.0, 98.0)
|
||||
}
|
||||
(true, false) => {
|
||||
beside = true;
|
||||
(cx - 100.0, 98.0)
|
||||
}
|
||||
(false, _) => {
|
||||
beside = false;
|
||||
(cx - 100.0, 200.0)
|
||||
}
|
||||
};
|
||||
out.push((x, y, bw, label, b.active, id));
|
||||
y += 24.0;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub fn draw(
|
||||
&self,
|
||||
atlas: &HudAtlas,
|
||||
batch: &mut HudBatch,
|
||||
caret: &mut crate::ui::Caret,
|
||||
focused_at: Instant,
|
||||
) {
|
||||
let w = batch.width;
|
||||
let rows = self.rows();
|
||||
let title = crate::lang::get(self.page.title());
|
||||
let tw = HudBatch::text_width(atlas, &title);
|
||||
batch.text(atlas, &title, centered_x(w, tw), 20.0, u32::MAX, true);
|
||||
let mut y = 53.0;
|
||||
let mut field = 0;
|
||||
let mut boxes = self.boxes.borrow_mut();
|
||||
for (r, row) in rows.iter().enumerate() {
|
||||
let (text, color) = match row {
|
||||
Row::Field { label, max, .. } => {
|
||||
let Some(value) = self.texts.get(field) else {
|
||||
break;
|
||||
};
|
||||
super::Screen::field(
|
||||
atlas,
|
||||
batch,
|
||||
caret,
|
||||
focused_at,
|
||||
&crate::lang::get(label),
|
||||
value,
|
||||
("page.field", r),
|
||||
*max,
|
||||
(y, y + 11.0),
|
||||
self.focus == field,
|
||||
);
|
||||
boxes[field] = [half(w) - 100.0, y + 11.0, 200.0, 20.0];
|
||||
field += 1;
|
||||
y += 41.0;
|
||||
continue;
|
||||
}
|
||||
Row::Space(px) => {
|
||||
y += f32::from(*px);
|
||||
continue;
|
||||
}
|
||||
Row::Heading(key) => (crate::lang::get(key), u32::MAX),
|
||||
Row::Note(text) => (text.clone(), rgba(160, 160, 160, 255)),
|
||||
Row::Text { text, quiet, .. } => {
|
||||
let grey = rgba(160, 160, 160, 255);
|
||||
(text.clone(), if *quiet { grey } else { u32::MAX })
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
for line in super::wrap(&text, WIDTH, atlas) {
|
||||
let lw = HudBatch::text_width(atlas, &line);
|
||||
batch.text(atlas, &line, centered_x(w, lw), y, color, true);
|
||||
y += 10.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn narration(&self) -> crate::widgets::ScreenInfo {
|
||||
crate::widgets::ScreenInfo::new(self.page.id(), crate::lang::get(self.page.title()))
|
||||
}
|
||||
|
||||
/// A press on button `id`, or on a field (`id` None).
|
||||
pub fn click(&mut self, id: Option<u8>, x: f32, y: f32) -> Outcome {
|
||||
match id {
|
||||
Some(id) if id == self.back_id() => Outcome::Back,
|
||||
Some(id) if id >= FIRST => {
|
||||
let rows = self.rows();
|
||||
let buttons = Self::buttons_of(&rows, self.page.back());
|
||||
match buttons.get(usize::from(id - FIRST)) {
|
||||
Some(Button {
|
||||
row: Some(r),
|
||||
active: true,
|
||||
..
|
||||
}) => self.act(*r, Act::Press),
|
||||
_ => Outcome::None,
|
||||
}
|
||||
}
|
||||
Some(_) => Outcome::None,
|
||||
None => {
|
||||
let hit = self
|
||||
.boxes
|
||||
.borrow()
|
||||
.iter()
|
||||
.position(|&[bx, by, bw, bh]| crate::ui::hovered((x, y), bx, by, bw, bh));
|
||||
if let Some(field) = hit {
|
||||
self.focus = field;
|
||||
}
|
||||
Outcome::None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Enter: the focused field submits.
|
||||
pub fn confirm(&self) -> Outcome {
|
||||
let rows = self.rows();
|
||||
match field_rows(&rows).nth(self.focus) {
|
||||
Some(r) => self.act(r, Act::Submit),
|
||||
None => Outcome::None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The page acts; a server it asked to join is joined.
|
||||
fn act(&self, row: usize, act: Act) -> Outcome {
|
||||
self.page.act(row, act);
|
||||
match pages::engine::take_connect() {
|
||||
Some(address) => Outcome::Connect {
|
||||
address,
|
||||
lan: false,
|
||||
},
|
||||
None => Outcome::None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn focused_box(&self) -> Option<[f32; 4]> {
|
||||
self.boxes.borrow().get(self.focus).copied()
|
||||
}
|
||||
|
||||
/// The focused field, its id and maximum length.
|
||||
pub fn focused_text(&mut self) -> Option<(crate::clipboard::FieldId, &mut String, usize)> {
|
||||
let mut rows = Vec::new();
|
||||
self.page.rows(&mut rows);
|
||||
let (r, max) = rows
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(r, row)| match row {
|
||||
Row::Field { max, .. } => Some((r, *max)),
|
||||
_ => None,
|
||||
})
|
||||
.nth(self.focus)?;
|
||||
let text = self.texts.get_mut(self.focus)?;
|
||||
Some((("page.field", r), text, max))
|
||||
}
|
||||
|
||||
/// Tab through the fields, then the enabled `buttons`, then round to the first field.
|
||||
pub fn tab_dir(&mut self, reverse: bool, buttons: &[u8], button: &mut Option<u8>) {
|
||||
let fields = self.texts.len();
|
||||
let stops = fields + buttons.len();
|
||||
if stops == 0 {
|
||||
return;
|
||||
}
|
||||
let at = match *button {
|
||||
Some(id) => buttons.iter().position(|&b| b == id).map(|i| fields + i),
|
||||
None => Some(self.focus.min(fields.saturating_sub(1))),
|
||||
}
|
||||
.unwrap_or(0);
|
||||
let next = if reverse {
|
||||
(at + stops - 1) % stops
|
||||
} else {
|
||||
(at + 1) % stops
|
||||
};
|
||||
if next < fields {
|
||||
self.focus = next;
|
||||
*button = None;
|
||||
} else {
|
||||
self.focus = fields;
|
||||
*button = Some(buttons[next - fields]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The rows that are fields, in order.
|
||||
fn field_rows(rows: &[Row]) -> impl Iterator<Item = usize> + '_ {
|
||||
rows.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, row)| matches!(row, Row::Field { .. }))
|
||||
.map(|(r, _)| r)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::Mutex;
|
||||
|
||||
/// A join form: a code and a relay, Join while the code has 4 characters or more.
|
||||
struct Join {
|
||||
heard: Mutex<Vec<(usize, Act)>>,
|
||||
code: Mutex<String>,
|
||||
}
|
||||
|
||||
impl Page for Join {
|
||||
fn id(&self) -> &'static str {
|
||||
"test.join"
|
||||
}
|
||||
fn title(&self) -> &'static str {
|
||||
"horizon.multiplayer.join_friend"
|
||||
}
|
||||
fn place(&self) -> Place {
|
||||
Place::Title
|
||||
}
|
||||
fn rows(&self, rows: &mut Vec<Row>) {
|
||||
let code = self.code.lock().unwrap().clone();
|
||||
rows.push(Row::field("horizon.multiplayer.invite_code", code.clone()).max(8));
|
||||
rows.push(Row::field(
|
||||
"horizon.multiplayer.relay",
|
||||
"relay.example:25599",
|
||||
));
|
||||
rows.push(Row::button("horizon.multiplayer.join").active(code.len() >= 4));
|
||||
}
|
||||
fn act(&self, row: usize, act: Act) {
|
||||
if let (0, Act::Edit(text)) = (row, &act) {
|
||||
text.clone_into(&mut self.code.lock().unwrap());
|
||||
}
|
||||
if matches!((row, &act), (2, Act::Press) | (0, Act::Submit)) {
|
||||
pages::connect(format!("friend:{}", self.code.lock().unwrap()));
|
||||
}
|
||||
self.heard.lock().unwrap().push((row, act));
|
||||
}
|
||||
fn back(&self) -> &'static str {
|
||||
"gui.cancel"
|
||||
}
|
||||
}
|
||||
|
||||
fn join() -> &'static Join {
|
||||
Box::leak(Box::new(Join {
|
||||
heard: Mutex::new(Vec::new()),
|
||||
code: Mutex::new(String::new()),
|
||||
}))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_form_lays_out_as_join_a_friend() {
|
||||
let form = Form::open(join());
|
||||
let buttons = form.buttons(427.0, 240.0);
|
||||
// Join at a quarter of the height + 114, Cancel 24 below, both 200 wide and centred.
|
||||
assert_eq!(
|
||||
buttons
|
||||
.iter()
|
||||
.map(|(x, y, w, _, on, id)| (*x, *y, *w, *on, *id))
|
||||
.collect::<Vec<_>>(),
|
||||
[
|
||||
(113.0, 174.0, 200.0, false, FIRST),
|
||||
(113.0, 198.0, 200.0, true, 57)
|
||||
]
|
||||
);
|
||||
assert_eq!(form.texts, ["", "relay.example:25599"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typing_reaches_the_page_before_a_press_and_enter_submits() {
|
||||
let page = join();
|
||||
let mut form = Form::open(page);
|
||||
let (id, text, max) = form.focused_text().unwrap();
|
||||
assert_eq!((id, max), (("page.field", 0), 8));
|
||||
text.push_str("AB");
|
||||
// Join is greyed: the page heard "AB" before the buttons were laid out.
|
||||
assert!(!form.buttons(427.0, 240.0)[0].4);
|
||||
form.focused_text().unwrap().1.push_str("CD");
|
||||
assert!(form.buttons(427.0, 240.0)[0].4);
|
||||
match form.click(Some(FIRST), 0.0, 0.0) {
|
||||
Outcome::Connect { address, lan } => {
|
||||
assert_eq!((address.as_str(), lan), ("friend:ABCD", false))
|
||||
}
|
||||
_ => panic!("Join did not connect"),
|
||||
}
|
||||
match form.confirm() {
|
||||
Outcome::Connect { address, .. } => assert_eq!(address, "friend:ABCD"),
|
||||
_ => panic!("Enter did not submit"),
|
||||
}
|
||||
let heard = page.heard.lock().unwrap();
|
||||
assert_eq!(
|
||||
*heard,
|
||||
[
|
||||
(0, Act::Edit("AB".into())),
|
||||
(0, Act::Edit("ABCD".into())),
|
||||
(2, Act::Press),
|
||||
(0, Act::Submit)
|
||||
]
|
||||
);
|
||||
assert!(matches!(form.click(Some(57), 0.0, 0.0), Outcome::Back));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tab_goes_through_the_fields_then_the_buttons() {
|
||||
let mut form = Form::open(join());
|
||||
let mut button = None;
|
||||
form.tab_dir(false, &[57], &mut button);
|
||||
assert_eq!((form.focus, button), (1, None));
|
||||
form.tab_dir(false, &[57], &mut button);
|
||||
assert_eq!((form.focus, button), (2, Some(57)));
|
||||
form.tab_dir(false, &[57], &mut button);
|
||||
assert_eq!((form.focus, button), (0, None));
|
||||
form.tab_dir(true, &[57], &mut button);
|
||||
assert_eq!(button, Some(57));
|
||||
assert!(form.focused_text().is_none());
|
||||
}
|
||||
}
|
||||
@@ -523,6 +523,22 @@ pub fn is_invite(code: &str, full: bool) -> bool {
|
||||
horizon_hooks::engine::tunnel().is_some_and(|r| r.is_code(code, full))
|
||||
}
|
||||
|
||||
/// Whether an invite code can be joined: a resolver is registered (horizon-official registers
|
||||
/// horizon-friends'). The title screen's Friends icon, which opens Join a Friend, is inactive
|
||||
/// without one, as the pause menu's is without a page to share from.
|
||||
pub fn joins_friends() -> bool {
|
||||
#[cfg(test)]
|
||||
if JOINS_FRIENDS_IN_TEST.get() {
|
||||
return true;
|
||||
}
|
||||
horizon_hooks::engine::tunnel().is_some()
|
||||
}
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// A test's stand-in for a registered resolver: the registry is the process's and sealed.
|
||||
pub(crate) static JOINS_FRIENDS_IN_TEST: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
|
||||
}
|
||||
|
||||
/// An address as players and logs see it: a friend's invite code stays
|
||||
/// out of it (its secret is in there).
|
||||
pub fn shown_address(address: &str) -> String {
|
||||
|
||||
@@ -1940,8 +1940,8 @@ impl Title {
|
||||
)
|
||||
.does(crate::widgets::Action::Exit),
|
||||
];
|
||||
// CommonButtons' icon buttons; Horizon's Join a Friend (an
|
||||
// invite code) sits behind vanilla's Friends button.
|
||||
// CommonButtons' icon buttons: vanilla's Friends button opens a crate's page or
|
||||
// Join a Friend, inactive with neither (`multiplayer::title_entry`).
|
||||
for (i, (key, plain, id, icon)) in [
|
||||
("gui.friends.open", "Friends", 5, "friends/friends"),
|
||||
("options.language", "Language...", 6, "icon/language"),
|
||||
@@ -1960,7 +1960,7 @@ impl Title {
|
||||
y + 72.0,
|
||||
20.0,
|
||||
&lang::get_or(key, plain),
|
||||
true,
|
||||
id != 5 || crate::multiplayer::has_title_entry(),
|
||||
id,
|
||||
);
|
||||
button.icon = Some((icon, 15.0));
|
||||
@@ -3658,12 +3658,7 @@ Would you like to enable the Narrator or visit the Accessibility Settings?",
|
||||
.map(|b| b.id);
|
||||
if hit.is_some() {
|
||||
self.focused_button = hit;
|
||||
} else if matches!(
|
||||
self.mode,
|
||||
Mode::Multiplayer(
|
||||
crate::multiplayer::Screen::Edit { .. } | crate::multiplayer::Screen::Friend { .. }
|
||||
)
|
||||
) {
|
||||
} else if matches!(&self.mode, Mode::Multiplayer(screen) if screen.has_fields()) {
|
||||
// A press on a text field takes the focus from the button.
|
||||
self.focused_button = None;
|
||||
}
|
||||
@@ -3782,7 +3777,7 @@ Would you like to enable the Narrator or visit the Accessibility Settings?",
|
||||
crate::multiplayer::Outcome::None => Action::None,
|
||||
},
|
||||
(Mode::Main, Some(5)) => {
|
||||
self.mode = Mode::Multiplayer(crate::multiplayer::Screen::friend(true));
|
||||
self.mode = crate::multiplayer::title_entry().map_or(Mode::Main, Mode::Multiplayer);
|
||||
Action::None
|
||||
}
|
||||
(Mode::Onboarding(_), Some(onboarding::SOUNDS)) => Action::Sounds,
|
||||
@@ -5306,6 +5301,8 @@ mod classic_tests {
|
||||
|
||||
#[test]
|
||||
fn classic_main_menu_icons_route_language_and_accessibility() {
|
||||
// Join a Friend needs a resolver (title/tests.rs tests the icon without one).
|
||||
crate::net::JOINS_FRIENDS_IN_TEST.set(true);
|
||||
let mut title = Title::new(
|
||||
PathBuf::new(),
|
||||
Path::new("/nonexistent/horizon-test-resources"),
|
||||
|
||||
@@ -100,13 +100,61 @@ impl Drop for Fixture {
|
||||
}
|
||||
}
|
||||
|
||||
/// Join a Friend needs a resolver; these tests stand one in (`net::JOINS_FRIENDS_IN_TEST`).
|
||||
fn with_resolver() -> Fixture {
|
||||
crate::net::JOINS_FRIENDS_IN_TEST.set(true);
|
||||
Fixture::new()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn without_a_resolver_the_friends_icon_is_inactive_and_opens_nothing() {
|
||||
crate::net::JOINS_FRIENDS_IN_TEST.set(false);
|
||||
let mut f = Fixture::new();
|
||||
let icons: Vec<_> = f
|
||||
.title
|
||||
.buttons(480.0, 262.0)
|
||||
.into_iter()
|
||||
.filter(|b| b.icon.is_some())
|
||||
.map(|b| (b.label.clone(), b.enabled, b.x))
|
||||
.collect();
|
||||
// Same place and look as with one, only greyed; Language and Accessibility stay active.
|
||||
let friends = icons.iter().find(|(label, ..)| label == "Friends").unwrap();
|
||||
assert!(!friends.1);
|
||||
assert!(
|
||||
icons
|
||||
.iter()
|
||||
.filter(|(label, ..)| label != "Friends")
|
||||
.all(|(_, on, _)| *on)
|
||||
);
|
||||
assert_eq!(f.click("Friends"), Action::None);
|
||||
assert!(matches!(f.title.mode, Mode::Main));
|
||||
crate::net::JOINS_FRIENDS_IN_TEST.set(true);
|
||||
let with: Vec<_> = f
|
||||
.title
|
||||
.buttons(480.0, 262.0)
|
||||
.into_iter()
|
||||
.filter(|b| b.icon.is_some())
|
||||
.map(|b| (b.label.clone(), b.x, b.y))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
with.iter()
|
||||
.map(|(l, x, _)| (l.clone(), *x))
|
||||
.collect::<Vec<_>>(),
|
||||
icons
|
||||
.iter()
|
||||
.map(|(l, _, x)| (l.clone(), *x))
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
crate::net::JOINS_FRIENDS_IN_TEST.set(false);
|
||||
}
|
||||
|
||||
mod multiplayer_parent_navigation {
|
||||
use super::*;
|
||||
use crate::multiplayer::{Outcome, Screen};
|
||||
|
||||
#[test]
|
||||
fn friend_cancel_returns_to_title() {
|
||||
let mut f = Fixture::new();
|
||||
let mut f = with_resolver();
|
||||
f.click("Friends");
|
||||
assert!(matches!(
|
||||
f.title.mode,
|
||||
@@ -118,7 +166,7 @@ mod multiplayer_parent_navigation {
|
||||
|
||||
#[test]
|
||||
fn friend_escape_returns_to_title() {
|
||||
let mut f = Fixture::new();
|
||||
let mut f = with_resolver();
|
||||
f.click("Friends");
|
||||
assert!(matches!(
|
||||
f.title.mode,
|
||||
|
||||
@@ -19,6 +19,11 @@ pub enum Place {
|
||||
/// start a group of their own after the page's. A [`Row::Heading`] starts another group. A
|
||||
/// [`Row::Key`] shows its key there, without changing it: give a page of its own to a key.
|
||||
Extras,
|
||||
/// The title screen's Friends icon slot (vanilla's `CommonButtons.friends`, beside Language
|
||||
/// and Accessibility): with a page registered here the icon opens the first one as a form of
|
||||
/// its own, as Join a Friend is drawn ([`Row::Field`]). It shows heading, note, text, space,
|
||||
/// field and button rows; the others belong in the options shell.
|
||||
Title,
|
||||
}
|
||||
|
||||
/// One page. Its values live in the crate (statics, often its settings [`crate::settings::Store`]);
|
||||
@@ -40,6 +45,11 @@ pub trait Page: Sync {
|
||||
fn tooltip(&self, _row: usize) -> Option<Label> {
|
||||
None
|
||||
}
|
||||
/// The label of the button the engine adds to leave a page drawn as its own screen: a
|
||||
/// translation key, `gui.done` by default (`gui.cancel` for a form that does something).
|
||||
fn back(&self) -> &'static str {
|
||||
"gui.done"
|
||||
}
|
||||
}
|
||||
|
||||
/// A row of a page. Labels are translation keys.
|
||||
@@ -85,6 +95,16 @@ pub enum Row {
|
||||
/// A key binding, as Key Binds shows one: `key` is its key name (`key.keyboard.z`,
|
||||
/// `key.mouse.left`). A press waits for the next key or mouse button ([`Act::Bind`]).
|
||||
Key { label: &'static str, key: String },
|
||||
/// A text field ([`Row::field`]) on a page drawn as its own screen, as vanilla's `EditBox`:
|
||||
/// its label above it, focus by click or Tab, typing, the caret keys, selection, copy and
|
||||
/// paste, at most `max` characters. `text` is its value when the page opens; the engine then
|
||||
/// keeps it, and sends each change ([`Act::Edit`]) and Enter ([`Act::Submit`]).
|
||||
#[non_exhaustive]
|
||||
Field {
|
||||
label: &'static str,
|
||||
text: String,
|
||||
max: usize,
|
||||
},
|
||||
}
|
||||
|
||||
impl Row {
|
||||
@@ -107,6 +127,23 @@ impl Row {
|
||||
}
|
||||
}
|
||||
|
||||
/// A text field starting with `text`, at most 32 characters (`EditBox`'s default).
|
||||
pub fn field(label: &'static str, text: impl Into<String>) -> Self {
|
||||
Self::Field {
|
||||
label,
|
||||
text: text.into(),
|
||||
max: 32,
|
||||
}
|
||||
}
|
||||
|
||||
/// A text field taking at most `chars` characters.
|
||||
pub fn max(mut self, chars: usize) -> Self {
|
||||
if let Self::Field { max, .. } = &mut self {
|
||||
*max = chars;
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// A button greyed out (`false`): drawn, never pressed.
|
||||
pub fn active(mut self, on: bool) -> Self {
|
||||
if let Self::Button { active, .. } = &mut self {
|
||||
@@ -167,6 +204,19 @@ pub enum Act {
|
||||
Slide(f32),
|
||||
/// A key row got its new key: its key name (`key.keyboard.z`).
|
||||
Bind(String),
|
||||
/// A field's text changed: all of it.
|
||||
Edit(String),
|
||||
/// Enter in a field.
|
||||
Submit,
|
||||
}
|
||||
|
||||
/// The server a page asked to join ([`connect`]).
|
||||
static CONNECT: std::sync::Mutex<Option<String>> = std::sync::Mutex::new(None);
|
||||
|
||||
/// From a [`Place::Title`] page's [`Page::act`]: join the server at `address` as Direct
|
||||
/// Connection does (the engine's addresses, `friend:<code>` among them), once the act returns.
|
||||
pub fn connect(address: impl Into<String>) {
|
||||
*CONNECT.lock().unwrap_or_else(|e| e.into_inner()) = Some(address.into());
|
||||
}
|
||||
|
||||
static PAGES: Registry<dyn Page> = Registry::new();
|
||||
@@ -185,4 +235,9 @@ pub mod engine {
|
||||
pub fn all() -> &'static [&'static dyn Page] {
|
||||
PAGES.all()
|
||||
}
|
||||
|
||||
/// The server a page asked to join since the last call ([`connect`]).
|
||||
pub fn take_connect() -> Option<String> {
|
||||
CONNECT.lock().unwrap_or_else(|e| e.into_inner()).take()
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,7 @@ pub mod settings {
|
||||
|
||||
/// Settings pages a crate adds to the options screens (docs/layers.md, "Platform registries").
|
||||
pub mod pages {
|
||||
pub use horizon_hooks::pages::{Act, Label, Page, Place, Row, register};
|
||||
pub use horizon_hooks::pages::{Act, Label, Page, Place, Row, connect, register};
|
||||
}
|
||||
|
||||
/// Drawing on the HUD (docs/layers.md, "Platform registries").
|
||||
|
||||
+16
-2
@@ -363,7 +363,7 @@ pub trait Store: Sync {
|
||||
A page lives in the options screen's shell, and its entry sits on one of the game's menus.
|
||||
|
||||
```rust
|
||||
pub enum Place { HorizonSettings, Title, Pause }
|
||||
pub enum Place { HorizonSettings, Pause, Extras, Title }
|
||||
pub trait Page: Sync {
|
||||
fn id(&self) -> &'static str; // stable, for horizon-ctl and the page stack
|
||||
fn title(&self) -> &'static str; // translation key
|
||||
@@ -371,6 +371,7 @@ pub trait Page: Sync {
|
||||
fn rows(&self, rows: &mut Vec<Row>); // asked when the page is drawn, not every frame elsewhere
|
||||
fn act(&self, row: usize, act: Act); // a click, a reverse click, a slider's value, a key
|
||||
fn tooltip(&self, row: usize) -> Option<Label> { None } // hover text, may follow the value
|
||||
fn back(&self) -> &'static str { "gui.done" } // the leave button of a page drawn as a screen
|
||||
}
|
||||
pub enum Row {
|
||||
Heading(&'static str), // translation key
|
||||
@@ -382,9 +383,11 @@ pub enum Row {
|
||||
Key { label: &'static str, key: String }, // a binding: `key.keyboard.z`
|
||||
Text { text: String, quiet: bool, groups: bool }, // Row::text(..): white or grey, wrapped
|
||||
Space(u8), // GUI pixels, on a page drawn as a screen
|
||||
Field { label: &'static str, text: String, max: usize }, // Row::field(..).max(n): an EditBox, on a page drawn as a screen
|
||||
}
|
||||
pub enum Label { Key(&'static str), Value(String) }
|
||||
pub enum Act { Press, PressBack, Slide(f32), Bind(String) }
|
||||
pub enum Act { Press, PressBack, Slide(f32), Bind(String), Edit(String), Submit }
|
||||
pub fn connect(address: impl Into<String>); // from a Title page's act: join that server
|
||||
```
|
||||
|
||||
- `HorizonSettings`: a row of the Horizon Settings page, after the engine's own pages. `Title` and `Pause`: a
|
||||
@@ -395,6 +398,17 @@ pub enum Act { Press, PressBack, Slide(f32), Bind(String) }
|
||||
button, `.half()` puts two side by side, `.footer()` sets one beside Done on a page drawn as its own screen,
|
||||
`.quiet()` greys text, `.groups()` breaks it only between `-`-separated groups (an invite code). In the options
|
||||
shell, text is notes and space is nothing.
|
||||
- `Title` is the title screen's Friends icon slot (vanilla's `CommonButtons.friends`), not a new row: the icon
|
||||
keeps its place and look at every GUI scale, and opens the first page registered there as a form drawn as Join a
|
||||
Friend is (`multiplayer/page.rs`): the title, the rows from y 53 (a field's grey label, its 200-wide box 11
|
||||
below, 41 a field), the buttons in a column from a quarter of the height + 114, the back button last
|
||||
(`Page::back`, `gui.cancel` for a form). It shows heading, note, text, space, field and button rows. With no page
|
||||
there the icon opens the engine's Join a Friend while an invite-code resolver is registered, else it is
|
||||
inactive.
|
||||
- `Field` is vanilla's `EditBox` on a page drawn as its own screen: focus by click or Tab, typing, the caret keys,
|
||||
selection, copy and paste, at most `max` characters (32 by default). The engine keeps its text while the page is
|
||||
open and sends each change (`Act::Edit`, before any other act or redraw reaches the page) and Enter
|
||||
(`Act::Submit`). The options shell has no field.
|
||||
- Rows are rebuilt when the page is drawn or acted on, like the engine's own pages: no cost while no page is
|
||||
open.
|
||||
- The engine keeps native mods' pages (`Page::NativeMod`) as they are. Both kinds sit in one module that
|
||||
|
||||
@@ -53,7 +53,8 @@ router that opened the port, or with a forwarded port, that's it: no server took
|
||||
(`horizon_hooks::tunnel`, `horizon_platform::hooks::tunnel` for the Horizon layer): the resolver a hook offers
|
||||
reads codes and opens the tunnel, which the join (`net.rs`) then reaches on a loopback port like any server.
|
||||
horizon-friends' `game` side offers it, and the official executable (`horizon-official`) registers it;
|
||||
horizon-client's own executable has none, so there a `friend:` address is refused. Hosting needs no hook: the
|
||||
horizon-client's own executable has none, so there a `friend:` address is refused and the title screen's
|
||||
Friends icon, which opens Join a Friend, is inactive (`net::joins_friends`). Hosting needs no hook: the
|
||||
host end connects to the world's LAN port over loopback, so tunnelled guests are LAN guests. It still lives in
|
||||
horizon-client (`game/friends.rs`, `game/friends/screen.rs`, the `friends` feature, which comes with
|
||||
`horizon-extras`) until the pause menu and `horizon-ctl` offer entries to crates above the engine.
|
||||
|
||||
Reference in new issue
Block a user