diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 87875a93a..0719878eb 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -63,7 +63,6 @@ mod frames; mod friends; mod functions; mod game_mode; -mod game_rules_screen; mod gamemode_switcher; mod ghasts; mod golems; @@ -12195,7 +12194,8 @@ impl ApplicationHandler for Game<'_> { let (action, sound) = self.title.as_mut().map_or((Action::None, false), |t| { let clicked = t.click(self.cursor.0, self.cursor.1, (b.width, b.height)); t.press_field(atlas, self.cursor, (b.width, b.height), shift); - clicked + let rules = t.press_rules(atlas, self.cursor, (b.width, b.height)); + (clicked.0, clicked.1 || rules) }); self.save_title_options(); if sound { diff --git a/crates/horizon-client/src/game/lan_screen.rs b/crates/horizon-client/src/game/lan_screen.rs index 260cc76c3..055d1ea1f 100644 --- a/crates/horizon-client/src/game/lan_screen.rs +++ b/crates/horizon-client/src/game/lan_screen.rs @@ -5,14 +5,10 @@ //! forcing the game mode. Its difficulty lock asks first (PopupScreen), and //! Restrictions opens RestrictionsScreen. //! Parity reference: https://mcsrc.dev/2/26.3/net/minecraft/client/gui/screens/WorldOptionsScreen -use super::{ - Game, Screen, - game_rules_screen::{Exit, RulesScreen}, - integrated::ServerOptions, - lan::LanSettings, -}; +use super::{Game, Screen, integrated::ServerOptions, lan::LanSettings}; use horizon_client::{ font::Font, + game_rules_screen::{Exit, RulesScreen}, hud::{HudBatch, rgba}, text::{Text, named_color}, ui::{self, EditBox}, diff --git a/crates/horizon-client/src/game/startup_menu.rs b/crates/horizon-client/src/game/startup_menu.rs index ed5971324..fdeb726ae 100644 --- a/crates/horizon-client/src/game/startup_menu.rs +++ b/crates/horizon-client/src/game/startup_menu.rs @@ -546,6 +546,11 @@ impl Loading { (batch.width, batch.height), self.menu.modifiers.shift_key(), ); + title.press_rules( + &gui.atlas, + self.menu.cursor, + (batch.width, batch.height), + ); self.menu_action(event_loop, action); } } diff --git a/crates/horizon-client/src/game/world_loading.rs b/crates/horizon-client/src/game/world_loading.rs index 73f1820f2..454f9f2eb 100644 --- a/crates/horizon-client/src/game/world_loading.rs +++ b/crates/horizon-client/src/game/world_loading.rs @@ -94,6 +94,8 @@ impl WorldRequest { level.difficulty = horizon_client::title::DIFFICULTIES[o.difficulty()].into(); level.allow_commands = Some(o.commands(creative)); horizon_client::save::write_world_gen_settings(&save_root, level.seed, o.world_gen())?; + // WorldCreationUiState.getGameRules, written with the level. + level.game_rules = o.rules.by_name(); } // Worlds made without the Create New World screen (--world, the // control socket) take its default: commands on for Creative only. diff --git a/crates/horizon-client/src/game/game_rules_screen.rs b/crates/horizon-client/src/game_rules_screen.rs similarity index 91% rename from crates/horizon-client/src/game/game_rules_screen.rs rename to crates/horizon-client/src/game_rules_screen.rs index 0992f7689..7f7c307b6 100644 --- a/crates/horizon-client/src/game/game_rules_screen.rs +++ b/crates/horizon-client/src/game_rules_screen.rs @@ -1,8 +1,9 @@ -//! InWorldGameRulesScreen (AbstractGameRulesScreen), opened from World -//! Options: the rules by category under a search box, a switch or a number -//! box each, and Done sending the changed ones (ServerboundSetGameRulePacket). -//! Leaving with changes asks first (ConfirmScreen). -use horizon_client::{ +//! AbstractGameRulesScreen: the rules by category under a search box, a +//! switch or a number box each. InWorldGameRulesScreen, opened from World +//! Options, sends the changed ones on Done (ServerboundSetGameRulePacket) and +//! asks before leaving with changes (ConfirmScreen); WorldCreationGameRulesScreen, +//! opened from Create New World, hands them back and leaves without asking. +use crate::{ confirm_screen::Confirm, font::Font, gamerules::{self, GameRules, RULES, Rule, Value}, @@ -32,14 +33,14 @@ enum Row { /// What a click or Escape asks of World Options. #[derive(Debug, PartialEq)] -pub(super) enum Exit { +pub enum Exit { /// Done: the rules that changed. Apply(Vec<(&'static str, Value)>), /// Cancel, or discarding the changes. Discard, } -pub(super) struct RulesScreen { +pub struct RulesScreen { search: EditBox, values: GameRules, initial: GameRules, @@ -49,6 +50,8 @@ pub(super) struct RulesScreen { scroll: ui::Scroll, /// ConfirmScreen "Game Rule Changes" in its place. confirm: bool, + /// InWorldGameRulesScreen rather than WorldCreationGameRulesScreen. + in_world: bool, } fn name_key(r: &Rule) -> String { @@ -102,7 +105,15 @@ fn confirm(font: &Font, size: (f32, f32)) -> Confirm { } impl RulesScreen { - pub(super) fn new(rules: &GameRules) -> Self { + /// InWorldGameRulesScreen over the world's rules. + pub fn new(rules: &GameRules) -> Self { + Self::build(rules, true) + } + /// WorldCreationGameRulesScreen over the new world's rules. + pub fn for_creation(rules: &GameRules) -> Self { + Self::build(rules, false) + } + fn build(rules: &GameRules, in_world: bool) -> Self { // setInitialFocus: the search box. let search = EditBox::new(0.0, 0.0, SEARCH_WIDTH, 15.0); let numbers = RULES @@ -123,6 +134,7 @@ impl RulesScreen { numbers, scroll: ui::Scroll::default(), confirm: false, + in_world, } } @@ -197,9 +209,10 @@ impl RulesScreen { }) .collect() } - /// InWorldGameRulesScreen.onClose: pending changes ask first. + /// InWorldGameRulesScreen.onClose: pending changes ask first; + /// WorldCreationGameRulesScreen.onClose discards them. fn close(&mut self) -> Option { - if self.changes().is_empty() { + if !self.in_world || self.changes().is_empty() { Some(Exit::Discard) } else { self.confirm = true; @@ -207,7 +220,7 @@ impl RulesScreen { } } /// Escape: ConfirmScreen answers no; the rules close. - pub(super) fn escape(&mut self) -> Option { + pub fn escape(&mut self) -> Option { if self.confirm { self.confirm = false; None @@ -216,7 +229,7 @@ impl RulesScreen { } } /// The mouse wheel (AbstractSelectionList: half an entry a step). - pub(super) fn wheel(&mut self, delta: f32, h: f32) { + pub fn wheel(&mut self, delta: f32, h: f32) { if !self.confirm { let content = self.content(); self.scroll @@ -224,20 +237,20 @@ impl RulesScreen { } } /// A press on the scrollbar holds it (AbstractSelectionList.updateScrolling). - pub(super) fn press_scrollbar(&mut self, cursor: (f32, f32), (w, h): (f32, f32)) -> bool { + pub fn press_scrollbar(&mut self, cursor: (f32, f32), (w, h): (f32, f32)) -> bool { let content = self.content(); self.scroll .press(cursor, Self::bar(w), HEADER, h - FOOTER, content) } /// The pointer moved while the scrollbar is held. - pub(super) fn drag(&mut self, y: f32, h: f32) { + pub fn drag(&mut self, y: f32, h: f32) { if self.scroll.held() { let content = self.content(); self.scroll.drag(y, HEADER, h - FOOTER, content); } } /// The button came up: the scrollbar lets go. - pub(super) fn release(&mut self) { + pub fn release(&mut self) { self.scroll.release(); } fn focused(&mut self) -> Option<(Option<&'static str>, &mut EditBox)> { @@ -272,7 +285,7 @@ impl RulesScreen { } } /// Run an edit on the focused box, then its responder. - pub(super) fn edit(&mut self, f: impl FnOnce(&mut EditBox)) { + pub fn edit(&mut self, f: impl FnOnce(&mut EditBox)) { let Some((rule, b)) = self.focused() else { return; }; @@ -282,12 +295,12 @@ impl RulesScreen { self.edited(rule); } } - pub(super) fn typing(&self) -> bool { + pub fn typing(&self) -> bool { !self.confirm && (self.search.focused || self.numbers.iter().any(|(_, b)| b.focused)) } /// A left click; Some when the screen closes. - pub(super) fn click( + pub fn click( &mut self, atlas: &HudAtlas, cursor: (f32, f32), @@ -355,12 +368,7 @@ impl RulesScreen { } } /// Whether a click lands on a button (for its sound). - pub(super) fn clicks_button( - &self, - atlas: &HudAtlas, - cursor: (f32, f32), - size: (f32, f32), - ) -> bool { + pub fn clicks_button(&self, atlas: &HudAtlas, cursor: (f32, f32), size: (f32, f32)) -> bool { let (w, h) = size; let on = |(x, y): (f32, f32)| ui::hovered(cursor, x, y, 150.0, 20.0); if self.confirm { @@ -377,9 +385,9 @@ impl RulesScreen { }) } - pub(super) fn draw(&mut self, batch: &mut HudBatch, atlas: &HudAtlas, cursor: (f32, f32)) { + pub fn draw(&mut self, batch: &mut HudBatch, atlas: &HudAtlas, cursor: (f32, f32)) { let (w, h) = (batch.width, batch.height); - ui::menu_background(batch, atlas, 0.0, 0.0, w, h, true); + ui::menu_background(batch, atlas, 0.0, 0.0, w, h, self.in_world); if self.confirm { let labels = [ Text::translate("gui.yes", &[]), @@ -393,7 +401,7 @@ impl RulesScreen { self.scroll.clamp(HEADER, h - FOOTER, content); // RuleList: the in-world list background, its entries clipped to it, // then the separators and scrollbar. - ui::list_background(batch, atlas, HEADER, h - FOOTER, true); + ui::list_background(batch, atlas, HEADER, h - FOOTER, self.in_world); let first = batch.vertices.len(); let hovered = self.hovered(rows.len(), cursor, (w, h)); let (left, x) = (row_left(w), control_x(w)); @@ -450,7 +458,7 @@ impl RulesScreen { } } batch.clip_since(first, 0.0, HEADER, w, h - FOOTER); - ui::separators(batch, atlas, HEADER, h - FOOTER, true); + ui::separators(batch, atlas, HEADER, h - FOOTER, self.in_world); ui::scrollbar( batch, atlas, @@ -526,8 +534,8 @@ mod tests { #[test] fn box_colours_are_vanillas() { - assert_eq!(VALID, horizon_client::hud::rgba(224, 224, 224, 255)); - assert_eq!(INVALID, horizon_client::hud::rgba(255, 0, 0, 255)); + assert_eq!(VALID, crate::hud::rgba(224, 224, 224, 255)); + assert_eq!(INVALID, crate::hud::rgba(255, 0, 0, 255)); } #[test] diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index fa5ef1c4d..64d8bf60e 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -74,6 +74,7 @@ pub mod fog; pub mod font; pub mod friends_screen; pub mod game_mode_switcher; +pub mod game_rules_screen; pub mod gamepad; pub mod gfx; pub mod gizmos; diff --git a/crates/horizon-client/src/title.rs b/crates/horizon-client/src/title.rs index 87bd05fd0..9984cdcdf 100644 --- a/crates/horizon-client/src/title.rs +++ b/crates/horizon-client/src/title.rs @@ -1,6 +1,7 @@ //! Title, world selection and world creation screens (TitleScreen, //! SelectWorldScreen, CreateWorldScreen layouts), drawn as HUD quads. use crate::{ + game_rules_screen::{Exit, RulesScreen}, hud::{HudAtlas, HudBatch, rgba}, lang, save::Level, @@ -144,6 +145,8 @@ pub struct Title { splash: Option, /// The saves listed for Home, re-read every couple of seconds. recent: std::cell::RefCell)>>, + /// WorldCreationGameRulesScreen, over Create New World while it is open. + rules: RefCell>, } pub(crate) struct Button { x: f32, @@ -252,6 +255,7 @@ impl Title { (today.month(), today.day()), ), recent: std::cell::RefCell::new(None), + rules: RefCell::new(None), } } /// Vanilla bypasses the selector when there are no saves. @@ -360,6 +364,9 @@ impl Title { } /// The pointer moved with the button held (`mouseDragged`). pub fn drag(&mut self, cursor: (f32, f32), size: (f32, f32)) { + if let Some(rules) = self.rules.get_mut() { + return rules.drag(cursor.1, size.1.ceil()); + } if let Mode::Multiplayer(screen) = &mut self.mode { return screen.drag(cursor.1, size); } @@ -372,6 +379,9 @@ impl Title { } /// The button was released (`onRelease`). pub fn release(&mut self) { + if let Some(rules) = self.rules.get_mut() { + rules.release(); + } if let Mode::Multiplayer(screen) = &mut self.mode { screen.release(); } @@ -379,6 +389,9 @@ impl Title { } /// Mouse wheel over the classic world and server lists. pub fn scroll_worlds(&mut self, delta: f32, cursor: (f32, f32), size: (f32, f32)) { + if let Some(rules) = self.rules.get_mut() { + return rules.wheel(delta, size.1.ceil()); + } if let Mode::Multiplayer(screen) = &mut self.mode { screen.wheel(delta, cursor, size); return; @@ -566,6 +579,10 @@ impl Title { folder } fn buttons(&self, w: f32, h: f32) -> Vec