Address title menu review: language, icon opacity, and splash behavior
This commit is contained in:
@@ -108,7 +108,7 @@ impl Title {
|
||||
/// live backdrop is visible (0 shows the plain cold background).
|
||||
pub fn draw_horizon(&self, ui: &mut Ui, backdrop: f32) {
|
||||
if classic(&self.mode) {
|
||||
self.draw(ui.atlas, ui.batch, ui.cursor);
|
||||
self.draw(ui.atlas, ui.batch, ui.cursor, false);
|
||||
return;
|
||||
}
|
||||
let reveal = self.opening_phase(backdrop >= 1.0);
|
||||
|
||||
@@ -3428,7 +3428,12 @@ impl Game<'_> {
|
||||
title.draw_horizon(&mut ui, fade);
|
||||
ui.finish();
|
||||
} else {
|
||||
title.draw(&self.res.atlas, &mut batch, self.cursor);
|
||||
title.draw(
|
||||
&self.res.atlas,
|
||||
&mut batch,
|
||||
self.cursor,
|
||||
self.settings.file.bool("hideSplashTexts"),
|
||||
);
|
||||
}
|
||||
return batch;
|
||||
}
|
||||
@@ -8691,6 +8696,9 @@ impl ApplicationHandler for Game<'_> {
|
||||
Action::Options => self.open_options(true),
|
||||
Action::Accessibility => self
|
||||
.open_options_at(true, horizon_client::options_ui::Page::Accessibility),
|
||||
Action::Language => {
|
||||
self.open_options_at(true, horizon_client::options_ui::Page::Language)
|
||||
}
|
||||
Action::Play { folder } => self.open_world(&folder, None, None, false)?,
|
||||
Action::Create {
|
||||
folder,
|
||||
|
||||
@@ -813,9 +813,8 @@ impl OptionsUi {
|
||||
}
|
||||
/// Open a page whose Done/Escape returns directly to the calling screen.
|
||||
pub fn open_at(&mut self, in_world: bool, page: Page) {
|
||||
self.stack = vec![page];
|
||||
self.scroll = 0.0;
|
||||
self.scrollbar_drag_y = None;
|
||||
self.stack.clear();
|
||||
self.push(page);
|
||||
self.in_world = in_world;
|
||||
self.selected_key = None;
|
||||
}
|
||||
@@ -2461,17 +2460,33 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn title_accessibility_done_returns_directly_to_the_title() {
|
||||
fn title_shortcuts_initialize_pages_and_return_directly_to_the_title() {
|
||||
let mut ui = OptionsUi::default();
|
||||
let mut s = Settings::default();
|
||||
ui.open_at(false, Page::Accessibility);
|
||||
assert_eq!(ui.page(), Page::Accessibility);
|
||||
assert!(!ui.in_world);
|
||||
assert!(ui.back(&mut s).contains(&Event::Close));
|
||||
ui.open(false);
|
||||
ui.show(Page::Accessibility);
|
||||
assert!(!ui.back(&mut s).contains(&Event::Close));
|
||||
assert_eq!(ui.page(), Page::Main);
|
||||
for page in [Page::Accessibility, Page::Language] {
|
||||
ui.language_choice = Some("de_de".into());
|
||||
ui.language_search.set_value("old query");
|
||||
ui.open_at(false, page);
|
||||
assert_eq!(ui.page(), page);
|
||||
assert!(!ui.in_world);
|
||||
if page == Page::Language {
|
||||
assert_eq!(ui.language_choice, None);
|
||||
assert!(ui.language_search.value.is_empty());
|
||||
assert!(ui.center_language);
|
||||
}
|
||||
assert!(ui.back(&mut s).contains(&Event::Close));
|
||||
assert_eq!(s.language, "en_us");
|
||||
ui.open(false);
|
||||
ui.show(page);
|
||||
assert!(!ui.back(&mut s).contains(&Event::Close));
|
||||
assert_eq!(ui.page(), Page::Main);
|
||||
}
|
||||
ui.open_at(false, Page::Language);
|
||||
ui.language_choice = Some("de_de".into());
|
||||
let events = ui.back(&mut s);
|
||||
assert!(events.contains(&Event::Language("de_de".into())));
|
||||
assert!(events.contains(&Event::Close));
|
||||
assert_eq!(s.language, "de_de");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+114
-109
@@ -104,6 +104,7 @@ pub enum Action {
|
||||
Quit,
|
||||
Options,
|
||||
Accessibility,
|
||||
Language,
|
||||
Play {
|
||||
folder: String,
|
||||
},
|
||||
@@ -130,7 +131,7 @@ pub struct Title {
|
||||
backup_shown: Cell<bool>,
|
||||
toast: Option<(std::time::Instant, BackupToast)>,
|
||||
icons: RefCell<std::collections::HashMap<PathBuf, (std::time::SystemTime, Option<Vec<u8>>)>>,
|
||||
splash: String,
|
||||
splash: Option<String>,
|
||||
/// The mode last drawn and when it appeared (Horizon's screen fades).
|
||||
shown: std::cell::Cell<(std::mem::Discriminant<Mode>, std::time::Instant)>,
|
||||
/// The launcher-style opening (Horizon style, first title of the session).
|
||||
@@ -192,20 +193,33 @@ pub fn list_worlds(saves: &Path) -> Vec<WorldEntry> {
|
||||
worlds.sort_by_key(|w| std::cmp::Reverse(w.modified));
|
||||
worlds
|
||||
}
|
||||
fn choose_splash(text: &str, index: usize) -> String {
|
||||
fn choose_splash(text: &str, index: usize, date: (u32, u32)) -> Option<String> {
|
||||
// SplashManager checks SpecialDates before the resource list (local date).
|
||||
let special = match date {
|
||||
(12, 24) => Some("Merry X-mas!"),
|
||||
(1, 1) => Some("Happy new year!"),
|
||||
(10, 31) => Some("OOoooOOOoooo! Spooky!"),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(special) = special {
|
||||
return Some(special.to_owned());
|
||||
}
|
||||
let splashes: Vec<_> = text
|
||||
.lines()
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.filter(|s| {
|
||||
!s.is_empty() && horizon_core::random::java_string_hash(s.encode_utf16()) != 125780783
|
||||
})
|
||||
.collect();
|
||||
splashes
|
||||
.get(index % splashes.len().max(1))
|
||||
.copied()
|
||||
.unwrap_or("Made in Rust!")
|
||||
.to_owned()
|
||||
.get(index.checked_rem(splashes.len())?)
|
||||
.map(|s| (*s).to_owned())
|
||||
}
|
||||
|
||||
impl Title {
|
||||
pub fn new(saves: PathBuf, resources: &Path) -> Self {
|
||||
use chrono::Datelike;
|
||||
let today = chrono::Local::now();
|
||||
let mode = Mode::Main;
|
||||
let now = std::time::Instant::now();
|
||||
Self {
|
||||
@@ -229,6 +243,7 @@ impl Title {
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.subsec_nanos() as usize,
|
||||
(today.month(), today.day()),
|
||||
),
|
||||
opening: std::cell::Cell::new(None),
|
||||
recent: std::cell::RefCell::new(None),
|
||||
@@ -497,7 +512,7 @@ impl Title {
|
||||
];
|
||||
for (i, (label, enabled, id, icon, size)) in [
|
||||
("Friends", false, 5, "friends/friends", 15.0),
|
||||
("Language", false, 6, "icon/language", 15.0),
|
||||
("Language", true, 6, "icon/language", 15.0),
|
||||
(
|
||||
"Accessibility Settings",
|
||||
true,
|
||||
@@ -691,7 +706,13 @@ impl Title {
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
pub fn draw(&self, atlas: &HudAtlas, batch: &mut HudBatch, cursor: (f32, f32)) {
|
||||
pub fn draw(
|
||||
&self,
|
||||
atlas: &HudAtlas,
|
||||
batch: &mut HudBatch,
|
||||
cursor: (f32, f32),
|
||||
hide_splash: bool,
|
||||
) {
|
||||
let (w, h) = (batch.width.ceil(), batch.height.ceil());
|
||||
match &self.mode {
|
||||
Mode::Main => {
|
||||
@@ -711,30 +732,32 @@ impl Title {
|
||||
128.0,
|
||||
16.0,
|
||||
);
|
||||
let tw = HudBatch::text_width(atlas, &self.splash);
|
||||
let start = batch.vertices.len();
|
||||
batch.text(
|
||||
atlas,
|
||||
&self.splash,
|
||||
-(tw / 2.0).floor(),
|
||||
-8.0,
|
||||
rgba(255, 255, 0, 255),
|
||||
true,
|
||||
);
|
||||
let scale = (1.8
|
||||
- (self.started.elapsed().as_secs_f32() * std::f32::consts::TAU)
|
||||
.sin()
|
||||
.abs()
|
||||
* 0.1)
|
||||
* 100.0
|
||||
/ (tw + 32.0);
|
||||
let (sin, cos) = (-20.0f32).to_radians().sin_cos();
|
||||
for v in &mut batch.vertices[start..] {
|
||||
let [x, y] = v.pos;
|
||||
v.pos = [
|
||||
w / 2.0 + 123.0 + scale * (x * cos - y * sin),
|
||||
69.0 + scale * (x * sin + y * cos),
|
||||
];
|
||||
if !hide_splash && let Some(splash) = self.splash.as_deref() {
|
||||
let tw = HudBatch::text_width(atlas, splash);
|
||||
let start = batch.vertices.len();
|
||||
batch.text(
|
||||
atlas,
|
||||
splash,
|
||||
-(tw / 2.0).floor(),
|
||||
-8.0,
|
||||
rgba(255, 255, 0, 255),
|
||||
true,
|
||||
);
|
||||
let scale = (1.8
|
||||
- (self.started.elapsed().as_secs_f32() * std::f32::consts::TAU)
|
||||
.sin()
|
||||
.abs()
|
||||
* 0.1)
|
||||
* 100.0
|
||||
/ (tw + 32.0);
|
||||
let (sin, cos) = (-20.0f32).to_radians().sin_cos();
|
||||
for v in &mut batch.vertices[start..] {
|
||||
let [x, y] = v.pos;
|
||||
v.pos = [
|
||||
w / 2.0 + 123.0 + scale * (x * cos - y * sin),
|
||||
69.0 + scale * (x * sin + y * cos),
|
||||
];
|
||||
}
|
||||
}
|
||||
let version = "Horizon 26.3 (Rust)";
|
||||
batch.text(atlas, version, 2.0, h - 10.0, u32::MAX, true);
|
||||
@@ -1077,11 +1100,7 @@ impl Title {
|
||||
b.y + 10.0 - (size / 2.0).floor(),
|
||||
size,
|
||||
size,
|
||||
if b.enabled {
|
||||
u32::MAX
|
||||
} else {
|
||||
rgba(255, 255, 255, 128)
|
||||
},
|
||||
u32::MAX,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
@@ -1153,6 +1172,7 @@ impl Title {
|
||||
}
|
||||
(Mode::Main, Some(3)) => Action::Options,
|
||||
(Mode::Main, Some(7)) => Action::Accessibility,
|
||||
(Mode::Main, Some(6)) => Action::Language,
|
||||
(Mode::Select { worlds, selected }, Some(10)) => {
|
||||
match selected.and_then(|i| worlds.get(i)) {
|
||||
Some(world) => Action::Play {
|
||||
@@ -1518,38 +1538,8 @@ fn date_string(secs: u64) -> String {
|
||||
mod classic_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"),
|
||||
Path::new("/nonexistent/horizon-test-resources"),
|
||||
);
|
||||
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_main_menu_icon_row_matches_reference_and_routes_accessibility() {
|
||||
fn classic_main_menu_icons_route_language_and_accessibility() {
|
||||
let mut title = Title::new(
|
||||
PathBuf::new(),
|
||||
Path::new("/nonexistent/horizon-test-resources"),
|
||||
@@ -1560,7 +1550,7 @@ mod classic_tests {
|
||||
let y = (h / 4.0).floor() + 48.0;
|
||||
for (id, x, enabled, icon) in [
|
||||
(5, cx - 34.0, false, "friends/friends"),
|
||||
(6, cx - 10.0, false, "icon/language"),
|
||||
(6, cx - 10.0, true, "icon/language"),
|
||||
(7, cx + 14.0, true, "icon/accessibility"),
|
||||
] {
|
||||
let b = buttons.iter().find(|b| b.id == id).unwrap();
|
||||
@@ -1570,10 +1560,10 @@ mod classic_tests {
|
||||
);
|
||||
assert_eq!(
|
||||
title.click(x + 10.0, y + 82.0, (w, h)),
|
||||
if enabled {
|
||||
(Action::Accessibility, true)
|
||||
} else {
|
||||
(Action::None, false)
|
||||
match id {
|
||||
6 => (Action::Language, true),
|
||||
7 => (Action::Accessibility, true),
|
||||
_ => (Action::None, false),
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -1593,10 +1583,35 @@ mod classic_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn splash_selection_uses_resource_lines_and_survives_missing_resources() {
|
||||
assert_eq!(choose_splash("\n First! \r\n\r\nSecond!\n", 0), "First!");
|
||||
assert_eq!(choose_splash("First!\nSecond!", 3), "Second!");
|
||||
assert_eq!(choose_splash(" \r\n", 42), "Made in Rust!");
|
||||
fn splash_selection_filters_the_forbidden_hash_and_has_no_fallback() {
|
||||
let hidden = "This message will never appear on the splash screen, isn't that weird?";
|
||||
assert_eq!(
|
||||
horizon_core::random::java_string_hash(hidden.encode_utf16()),
|
||||
125780783
|
||||
);
|
||||
let text = format!("\n First! \r\n{hidden}\nSecond!\n");
|
||||
for index in 0..6 {
|
||||
assert_eq!(
|
||||
choose_splash(&text, index, (10, 15)).as_deref(),
|
||||
Some(if index % 2 == 0 { "First!" } else { "Second!" })
|
||||
);
|
||||
}
|
||||
for text in ["", " \r\n", hidden] {
|
||||
assert_eq!(choose_splash(text, 42, (10, 15)), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn special_date_splashes_override_resources_even_when_the_list_is_empty() {
|
||||
for (date, expected) in [
|
||||
((12, 24), "Merry X-mas!"),
|
||||
((1, 1), "Happy new year!"),
|
||||
((10, 31), "OOoooOOOoooo! Spooky!"),
|
||||
] {
|
||||
for text in ["", "Ordinary splash!"] {
|
||||
assert_eq!(choose_splash(text, 0, date).as_deref(), Some(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1630,9 +1645,10 @@ mod classic_tests {
|
||||
},
|
||||
);
|
||||
}
|
||||
let title = Title::new(PathBuf::new(), root);
|
||||
let mut title = Title::new(PathBuf::new(), root);
|
||||
title.splash = Some("Visible splash!".into());
|
||||
let mut batch = HudBatch::new([1920, 1080]);
|
||||
title.draw(&atlas, &mut batch, (0.0, 0.0));
|
||||
title.draw(&atlas, &mut batch, (0.0, 0.0), false);
|
||||
let cx = (batch.width / 2.0).floor();
|
||||
assert_eq!(batch.vertices[0].pos, [cx - 128.0, 30.0]);
|
||||
assert_eq!(batch.vertices[2].pos, [cx + 128.0, 74.0]);
|
||||
@@ -1652,41 +1668,30 @@ mod classic_tests {
|
||||
let y = (batch.height / 4.0).floor() + 48.0 + 72.0 + 3.0;
|
||||
assert_eq!(quad[0].pos, [x, y]);
|
||||
assert_eq!(quad[2].pos, [x + 15.0, y + 15.0]);
|
||||
assert!(quad.iter().all(|v| v.color == u32::MAX));
|
||||
}
|
||||
assert!(
|
||||
batch.vertices[12..]
|
||||
.iter()
|
||||
.any(|v| v.color == rgba(255, 255, 0, 255))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classic_world_selection_is_silent_until_the_entry_launches() {
|
||||
let mut title = Title::new(
|
||||
PathBuf::from("/nonexistent/horizon-test-saves"),
|
||||
Path::new("/nonexistent/horizon-test-resources"),
|
||||
);
|
||||
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)),
|
||||
let settings = crate::settings::Settings::parse("hideSplashTexts:true\n");
|
||||
for (hide_splash, splash) in [
|
||||
(
|
||||
Action::Play {
|
||||
folder: "world".into()
|
||||
},
|
||||
true
|
||||
)
|
||||
);
|
||||
settings.file.bool("hideSplashTexts"),
|
||||
Some("Hidden splash!".into()),
|
||||
),
|
||||
(false, None),
|
||||
] {
|
||||
title.splash = splash;
|
||||
let mut hidden = HudBatch::new([1920, 1080]);
|
||||
title.draw(&atlas, &mut hidden, (0.0, 0.0), hide_splash);
|
||||
assert!(
|
||||
!hidden
|
||||
.vertices
|
||||
.iter()
|
||||
.any(|v| v.color == rgba(255, 255, 0, 255))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ impl Fixture {
|
||||
));
|
||||
fs::create_dir_all(root.join("saves")).unwrap();
|
||||
Self {
|
||||
title: Title::new(root.join("saves")),
|
||||
title: Title::new(root.join("saves"), &root),
|
||||
root,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user